Skip to content

Commit

Permalink
Refactor handling of path Prefix and Exact
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Nov 10, 2020
1 parent 52726ab commit 3f153ad
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 71 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/imdario/mergo v0.3.10
github.com/json-iterator/go v1.1.10
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
github.com/mitchellh/copystructure v1.0.0
github.com/mitchellh/go-ps v1.0.0
github.com/mitchellh/hashstructure v1.0.0
github.com/mitchellh/mapstructure v1.3.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182aff
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
Expand All @@ -435,6 +437,8 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg=
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mmarkdown/mmark v2.0.40+incompatible h1:vMeUeDzBK3H+/mU0oMVfMuhSXJlIA+DE/DMPQNAj5C4=
github.com/mmarkdown/mmark v2.0.40+incompatible/go.mod h1:Uvmoz7tvsWpr7bMVxIpqZPyN3FbOtzDmnsJDFp7ltJs=
github.com/moby/sys/mountinfo v0.1.3/go.mod h1:w2t2Avltqx8vE7gX5l+QiBKxODu2TX0+Syr3h52Tw4o=
Expand Down
14 changes: 14 additions & 0 deletions internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,20 @@ func (n *NGINXController) getConfiguration(ingresses []*ingress.Ingress) (sets.S
hosts := sets.NewString()

for _, server := range servers {
// If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of
// proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, or grpc_pass, then the special processing is performed.
// In response to a request with URI equal to // this string, but without the trailing slash, a permanent redirect with the
// code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the
// URIand location could be defined like this:
//
// location /user/ {
// proxy_pass http://user.example.com;
// }
// location = /user {
// proxy_pass http://login.example.com;
// }
server.Locations = updateServerLocations(server.Locations)

if !hosts.Has(server.Hostname) {
hosts.Insert(server.Hostname)
}
Expand Down
54 changes: 26 additions & 28 deletions internal/ingress/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,6 @@ func TestCheckIngress(t *testing.T) {
})
}

var pathPrefix = networking.PathTypePrefix

func TestMergeAlternativeBackends(t *testing.T) {
testCases := map[string]struct {
ingress *ingress.Ingress
Expand All @@ -295,7 +293,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-canary",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -331,7 +329,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: "example-http-svc-80",
},
},
Expand All @@ -357,7 +355,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: "example-http-svc-80",
},
},
Expand All @@ -379,7 +377,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "foo-http-svc-canary",
ServicePort: intstr.IntOrString{
Expand All @@ -399,7 +397,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-canary",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -447,7 +445,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: "example-foo-http-svc-80",
},
},
Expand All @@ -457,7 +455,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: "example-http-svc-80",
},
},
Expand Down Expand Up @@ -495,7 +493,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: "example-http-svc-80",
},
},
Expand All @@ -517,7 +515,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-canary",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -582,7 +580,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: "example-http-svc-80",
},
},
Expand All @@ -608,7 +606,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: "example-http-svc-80",
},
},
Expand Down Expand Up @@ -650,7 +648,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: "upstream-default-backend",
},
},
Expand All @@ -663,7 +661,7 @@ func TestMergeAlternativeBackends(t *testing.T) {
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: "upstream-default-backend",
},
},
Expand Down Expand Up @@ -999,7 +997,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-canary",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1059,7 +1057,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1096,7 +1094,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-canary",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1165,7 +1163,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/a",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-1",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1202,7 +1200,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/a",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-2",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1239,7 +1237,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/b",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-2",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1276,7 +1274,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/b",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-1",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1313,7 +1311,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/c",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-1",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1350,7 +1348,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/c",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "http-svc-2",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1435,7 +1433,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/path1",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "path1-svc",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1475,7 +1473,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/path2",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "path2-svc",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1540,7 +1538,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/path1",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "path1-svc",
ServicePort: intstr.IntOrString{
Expand Down Expand Up @@ -1580,7 +1578,7 @@ func TestGetBackendServers(t *testing.T) {
Paths: []networking.HTTPIngressPath{
{
Path: "/path2",
PathType: &pathPrefix,
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
ServiceName: "path2-svc",
ServicePort: intstr.IntOrString{
Expand Down
Loading

0 comments on commit 3f153ad

Please sign in to comment.