-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for specifying port in external services (#1500)
Signed-off-by: Gallardot <tttick@163.com> Co-authored-by: Jintao Zhang <zhangjintao@apache.org>
- Loading branch information
1 parent
4208ca7
commit f162f71
Showing
11 changed files
with
305 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
pkg/providers/apisix/translation/apisix_upstream_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package translation | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
v2 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2" | ||
apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1" | ||
) | ||
|
||
func TestTranslateApisixUpstreamExternalNodesDomainType(t *testing.T) { | ||
tr := &translator{} | ||
defaultPort := 80 | ||
defaultWeight := 80 | ||
specifiedPort := 8080 | ||
testCases := map[*v2.ApisixUpstream][]apisixv1.UpstreamNode{ | ||
{ | ||
Spec: &v2.ApisixUpstreamSpec{ | ||
ExternalNodes: []v2.ApisixUpstreamExternalNode{{ | ||
Name: "domain.foobar.com", | ||
Type: v2.ExternalTypeDomain, | ||
Weight: &defaultWeight, | ||
Port: &defaultPort, | ||
}}, | ||
}, | ||
}: {{ | ||
Host: "domain.foobar.com", | ||
Port: defaultPort, | ||
Weight: defaultWeight, | ||
}}, | ||
{ | ||
Spec: &v2.ApisixUpstreamSpec{ | ||
ExternalNodes: []v2.ApisixUpstreamExternalNode{{ | ||
Name: "domain.foobar.com", | ||
Type: v2.ExternalTypeDomain, | ||
Weight: &defaultWeight, | ||
Port: &specifiedPort, | ||
}}, | ||
}, | ||
}: {{ | ||
Host: "domain.foobar.com", | ||
Port: specifiedPort, | ||
Weight: defaultWeight, | ||
}}, | ||
{ | ||
Spec: &v2.ApisixUpstreamSpec{ | ||
ExternalNodes: []v2.ApisixUpstreamExternalNode{{ | ||
Name: "domain.foobar.com", | ||
Type: v2.ExternalTypeDomain, | ||
Weight: &defaultWeight, | ||
}}, | ||
}, | ||
}: {{ | ||
Host: "domain.foobar.com", | ||
Port: defaultPort, | ||
Weight: defaultWeight, | ||
}}, | ||
} | ||
for k, v := range testCases { | ||
result, _ := tr.TranslateApisixUpstreamExternalNodes(k) | ||
assert.Equal(t, v, result) | ||
} | ||
} | ||
|
||
func TestTranslateApisixUpstreamExternalNodesDomainTypeError(t *testing.T) { | ||
tr := &translator{} | ||
testCases := []v2.ApisixUpstream{ | ||
{ | ||
Spec: &v2.ApisixUpstreamSpec{ | ||
ExternalNodes: []v2.ApisixUpstreamExternalNode{{ | ||
Name: "https://domain.foobar.com", | ||
Type: v2.ExternalTypeDomain, | ||
}}, | ||
}}, | ||
{ | ||
Spec: &v2.ApisixUpstreamSpec{ | ||
ExternalNodes: []v2.ApisixUpstreamExternalNode{{ | ||
Name: "grpc://domain.foobar.com", | ||
Type: v2.ExternalTypeDomain, | ||
}}, | ||
}}, | ||
{ | ||
Spec: &v2.ApisixUpstreamSpec{ | ||
ExternalNodes: []v2.ApisixUpstreamExternalNode{{ | ||
Name: "-domain.foobar.com", | ||
Type: v2.ExternalTypeDomain, | ||
}}, | ||
}}, | ||
{ | ||
Spec: &v2.ApisixUpstreamSpec{ | ||
ExternalNodes: []v2.ApisixUpstreamExternalNode{{ | ||
Name: "-123.foobar.com", | ||
Type: v2.ExternalTypeDomain, | ||
}}, | ||
}}, | ||
{ | ||
Spec: &v2.ApisixUpstreamSpec{ | ||
ExternalNodes: []v2.ApisixUpstreamExternalNode{{ | ||
Name: "-123.FOOBAR.com", | ||
Type: v2.ExternalTypeDomain, | ||
}}, | ||
}}, | ||
} | ||
|
||
for _, k := range testCases { | ||
_, err := tr.TranslateApisixUpstreamExternalNodes(&k) | ||
assert.Error(t, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package utils | ||
|
||
import "regexp" | ||
|
||
var hostDef = "^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$" | ||
var hostDefRegex = regexp.MustCompile(hostDef) | ||
|
||
// MatchHostDef checks that host matches host's shcema | ||
// ref to : https://github.com/apache/apisix/blob/c5fc10d9355a0c177a7532f01c77745ff0639a7f/apisix/schema_def.lua#L40 | ||
// ref to : https://github.com/kubernetes/kubernetes/blob/976a940f4a4e84fe814583848f97b9aafcdb083f/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go#L205 | ||
// They define regex differently, but k8s's dns is more accurate | ||
func MatchHostDef(host string) bool { | ||
return hostDefRegex.MatchString(host) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMatchHostDef(t *testing.T) { | ||
tcs := map[string]bool{ | ||
"163.com": true, | ||
"github.com": true, | ||
"GITHUB.com": false, | ||
"-github.COM": false, | ||
"https://github.com": false, | ||
"http://github.com": false, | ||
} | ||
|
||
for k, v := range tcs { | ||
ret := MatchHostDef(k) | ||
assert.Equal(t, ret, v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package utils | ||
|
||
import ( | ||
apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1" | ||
) | ||
|
||
var schemeToPortMaps = map[string]int{ | ||
apisixv1.SchemeHTTP: 80, | ||
apisixv1.SchemeHTTPS: 443, | ||
apisixv1.SchemeGRPC: 80, | ||
apisixv1.SchemeGRPCS: 443, | ||
} | ||
|
||
// SchemeToPort scheme converts to the default port | ||
// ref https://github.com/apache/apisix/blob/c5fc10d9355a0c177a7532f01c77745ff0639a7f/apisix/upstream.lua#L167-L172 | ||
func SchemeToPort(schema string) int { | ||
if val, ok := schemeToPortMaps[schema]; ok { | ||
return val | ||
} | ||
return 80 | ||
} |
Oops, something went wrong.