Skip to content

Commit

Permalink
internal/contour: bind https listener to ingress_https routes
Browse files Browse the repository at this point in the history
Fixes projectcontour#104

RDS now publishes routes to http and https caches. LDS now binds the
http listener to the RDS https cache, ingress_http, by name, and vice
versa for the https listener.

This permits RDS to compute different routes for http and https
listeners, enabling http to https redirects.

Signed-off-by: Dave Cheney <dave@cheney.net>
  • Loading branch information
davecheney committed Dec 19, 2017
1 parent 95c683f commit 58c3493
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
8 changes: 4 additions & 4 deletions internal/contour/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (lc *ListenerCache) recomputeListener(ingresses map[metadata]*v1beta1.Ingre
if len(ingresses) > 0 {
l.FilterChains = []*v2.FilterChain{{
Filters: []*v2.Filter{
httpfilter(ENVOY_HTTP_LISTENER, ENVOY_HTTP_LISTENER),
httpfilter(ENVOY_HTTP_LISTENER),
},
}}
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func (lc *ListenerCache) recomputeTLSListener(ingresses map[metadata]*v1beta1.In
},
TlsContext: tlscontext(i.Namespace, tls.SecretName),
Filters: []*v2.Filter{
httpfilter(ENVOY_HTTPS_LISTENER, ENVOY_HTTP_LISTENER), // stat_prefix, route_name
httpfilter(ENVOY_HTTPS_LISTENER),
},
}
l.FilterChains = append(l.FilterChains, fc)
Expand Down Expand Up @@ -145,13 +145,13 @@ func tlscontext(namespace, name string) *v2.DownstreamTlsContext {
}
}

func httpfilter(statprefix, routename string) *v2.Filter {
func httpfilter(routename string) *v2.Filter {
return &v2.Filter{
Name: httpFilter,
Config: &types.Struct{
Fields: map[string]*types.Value{
"codec_type": sv("auto"),
"stat_prefix": sv(statprefix), // TODO(dfc) should this come from pod.Name?
"stat_prefix": sv(routename),
"rds": st(map[string]*types.Value{
"route_config_name": sv(routename),
"config_source": st(map[string]*types.Value{
Expand Down
2 changes: 2 additions & 0 deletions internal/contour/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (t *Translator) addIngress(i *v1beta1.Ingress) {
}},
}
t.VirtualHostCache.HTTP.Add(&v)
t.VirtualHostCache.HTTPS.Add(&v)
return
}

Expand All @@ -306,6 +307,7 @@ func (t *Translator) removeIngress(i *v1beta1.Ingress) {

if i.Spec.Backend != nil {
t.VirtualHostCache.HTTP.Remove("*")
t.VirtualHostCache.HTTPS.Remove("*")
return
}

Expand Down
16 changes: 13 additions & 3 deletions internal/contour/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,12 @@ func TestTranslatorAddIngress(t *testing.T) {
tr.addIngress(tc.ing)
got := tr.VirtualHostCache.HTTP.Values()
if !reflect.DeepEqual(tc.want, got) {
t.Fatalf("addIngress(%v):\n got: %v\nwant: %v", tc.ing, got, tc.want)
t.Fatalf("addIngress(%v):\n (ingress_http) got: %v\nwant: %v", tc.ing, got, tc.want)
}

got = tr.VirtualHostCache.HTTPS.Values()
if !reflect.DeepEqual(tc.want, got) {
t.Fatalf("addIngress(%v):\n (ingress_https) got: %v\nwant: %v", tc.ing, got, tc.want)
}
})
}
Expand Down Expand Up @@ -855,9 +860,14 @@ func TestTranslatorRemoveIngress(t *testing.T) {
tr := NewTranslator(stdlog.New(ioutil.Discard, ioutil.Discard, NOFLAGS))
tc.setup(tr)
tr.removeIngress(tc.ing)
got := tr.VirtualHostCache.HTTP.Values()
got := tr.VirtualHostCache.HTTPS.Values()
if !reflect.DeepEqual(tc.want, got) {
t.Fatalf("removeIngress(%v) (ingress_http): got: %v, want: %v", tc.ing, got, tc.want)
}

got = tr.VirtualHostCache.HTTPS.Values()
if !reflect.DeepEqual(tc.want, got) {
t.Fatalf("removeIngress(%v): got: %v, want: %v", tc.ing, got, tc.want)
t.Fatalf("removeIngress(%v) (ingress_https): got: %v, want: %v", tc.ing, got, tc.want)
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions internal/contour/virtualhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (v *VirtualHostCache) recomputevhost(vhost string, ingresses []*v1beta1.Ing
// there are no ingresses registered with this vhost any more
// remove the VirtualHost from the grpc cache.
v.HTTP.Remove(hashname(60, vhost))
v.HTTPS.Remove(hashname(60, vhost))
default:
// otherwise there is at least one ingress object associated with
// this vhost, so regernate the cache record and add/overwrite the
Expand All @@ -63,6 +64,7 @@ func (v *VirtualHostCache) recomputevhost(vhost string, ingresses []*v1beta1.Ing
}
sort.Stable(sort.Reverse(longestRouteFirst(vv.Routes)))
v.HTTP.Add(&vv)
v.HTTPS.Add(&vv)
}
}

Expand Down

0 comments on commit 58c3493

Please sign in to comment.