Skip to content

Commit

Permalink
Support both host and host:<any-port> as :authority header (#3398) (#…
Browse files Browse the repository at this point in the history
…3410)

* Support both host and host:<any-port> as :authority header

* RegExp escape hostname when creating VirtualService
  • Loading branch information
tcnghia authored and mattmoor committed Mar 11, 2019
1 parent b3c82dc commit 4913b81
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package resources

import (
"fmt"
"regexp"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -145,7 +147,7 @@ func expandedHosts(hosts []string) []string {
func makeMatch(host string, pathRegExp string) v1alpha3.HTTPMatchRequest {
match := v1alpha3.HTTPMatchRequest{
Authority: &istiov1alpha1.StringMatch{
Exact: host,
Regex: hostRegExp(host),
},
}
// Empty pathRegExp is considered match all path. We only need to
Expand All @@ -158,6 +160,14 @@ func makeMatch(host string, pathRegExp string) v1alpha3.HTTPMatchRequest {
return match
}

// hostRegExp returns an ECMAScript regular expression to match either host or host:<any port>
func hostRegExp(host string) string {
// Should only match 1..65535, but for simplicity it matches 0-99999
portMatch := `(?::\d{1,5})?`

return fmt.Sprintf("^%s%s$", regexp.QuoteMeta(host), portMatch)
}

func getHosts(ci *v1alpha1.ClusterIngress) []string {
hosts := []string{}
for _, rule := range ci.Spec.Rules {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,16 @@ func TestMakeVirtualServiceSpec_CorrectRoutes(t *testing.T) {
expected := []v1alpha3.HTTPRoute{{
Match: []v1alpha3.HTTPMatchRequest{{
Uri: &istiov1alpha1.StringMatch{Regex: "^/pets/(.*?)?"},
Authority: &istiov1alpha1.StringMatch{Exact: "domain.com"},
Authority: &istiov1alpha1.StringMatch{Regex: `^domain\.com(?::\d{1,5})?$`},
}, {
Uri: &istiov1alpha1.StringMatch{Regex: "^/pets/(.*?)?"},
Authority: &istiov1alpha1.StringMatch{Exact: "test-route.test-ns"},
Authority: &istiov1alpha1.StringMatch{Regex: `^test-route\.test-ns(?::\d{1,5})?$`},
}, {
Uri: &istiov1alpha1.StringMatch{Regex: "^/pets/(.*?)?"},
Authority: &istiov1alpha1.StringMatch{Exact: "test-route.test-ns.svc"},
Authority: &istiov1alpha1.StringMatch{Regex: `^test-route\.test-ns\.svc(?::\d{1,5})?$`},
}, {
Uri: &istiov1alpha1.StringMatch{Regex: "^/pets/(.*?)?"},
Authority: &istiov1alpha1.StringMatch{Exact: "test-route.test-ns.svc.cluster.local"},
Authority: &istiov1alpha1.StringMatch{Regex: `^test-route\.test-ns\.svc\.cluster\.local(?::\d{1,5})?$`},
}},
Route: []v1alpha3.DestinationWeight{{
Destination: v1alpha3.Destination{
Expand All @@ -167,7 +167,7 @@ func TestMakeVirtualServiceSpec_CorrectRoutes(t *testing.T) {
}, {
Match: []v1alpha3.HTTPMatchRequest{{
Uri: &istiov1alpha1.StringMatch{Regex: "^/pets/(.*?)?"},
Authority: &istiov1alpha1.StringMatch{Exact: "v1.domain.com"},
Authority: &istiov1alpha1.StringMatch{Regex: `^v1\.domain\.com(?::\d{1,5})?$`},
}},
Route: []v1alpha3.DestinationWeight{{
Destination: v1alpha3.Destination{
Expand Down Expand Up @@ -212,9 +212,9 @@ func TestMakeVirtualServiceRoute_Vanilla(t *testing.T) {
route := makeVirtualServiceRoute(hosts, ingressPath)
expected := v1alpha3.HTTPRoute{
Match: []v1alpha3.HTTPMatchRequest{{
Authority: &istiov1alpha1.StringMatch{Exact: "a.com"},
Authority: &istiov1alpha1.StringMatch{Regex: `^a\.com(?::\d{1,5})?$`},
}, {
Authority: &istiov1alpha1.StringMatch{Exact: "b.org"},
Authority: &istiov1alpha1.StringMatch{Regex: `^b\.org(?::\d{1,5})?$`},
}},
Route: []v1alpha3.DestinationWeight{{
Destination: v1alpha3.Destination{
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestMakeVirtualServiceRoute_TwoTargets(t *testing.T) {
route := makeVirtualServiceRoute(hosts, ingressPath)
expected := v1alpha3.HTTPRoute{
Match: []v1alpha3.HTTPMatchRequest{{
Authority: &istiov1alpha1.StringMatch{Exact: "test.org"},
Authority: &istiov1alpha1.StringMatch{Regex: `^test\.org(?::\d{1,5})?$`},
}},
Route: []v1alpha3.DestinationWeight{{
Destination: v1alpha3.Destination{
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestGRPC(t *testing.T) {

conn, err := grpc.Dial(
*host+":80",
grpc.WithAuthority(domain),
grpc.WithAuthority(domain+":80"),
grpc.WithInsecure(),
)
if err != nil {
Expand Down

0 comments on commit 4913b81

Please sign in to comment.