Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag header based routing #6036

Merged
merged 3 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/apis/networking/v1alpha1/ingress_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ type HTTPIngressPath struct {
// If RewriteHost is specified, Splits must not be.
RewriteHost string `json:"rewriteHost,omitempty"`

// Headers defines header matching rules which is a map from a header name
// to HeaderMatch which specify a matching condition.
// When a request matched with all the header matching rules,
// the request is routed by the corresponding ingress rule.
// If it is empty, the headers are not used for matching
// +optional
Headers map[string]HeaderMatch `json:"headers,omitempty"`

// Splits defines the referenced service endpoints to which the traffic
// will be forwarded to.
//
Expand Down Expand Up @@ -361,3 +369,9 @@ const (
func (t *Ingress) GetStatus() *duckv1.Status {
return &t.Status.Status
}

// HeaderMatch represents a matching value of Headers in HTTPIngressPath.
// Currently, only the exact matching is supported.
type HeaderMatch struct {
Exact string `json:"exact"`
}
23 changes: 23 additions & 0 deletions pkg/apis/networking/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pkg/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ const (
// AutoscalingUserAgent is the user-agent header value set in probe
// requests sent by autoscaling implementations.
AutoscalingUserAgent = "Knative-Autoscaling-Probe"

// TagHeaderName is the name of the header entry which has a tag name as value.
// The tag name specifies which route was expected to be chosen by Ingress.
TagHeaderName = "Knative-Serving-Tag"

// DefaultRouteHeaderName is the name of the header entry
// identifying whether a request is routed via the default route or not.
// It has one of the string value "true" or "false".
DefaultRouteHeaderName = "Knative-Serving-Default-Route"

// TagHeaderBasedRoutingKey is the name of the configuration entry
// that specifies enabling tag header based routing or not.
TagHeaderBasedRoutingKey = "tagHeaderBasedRouting"
)

// DomainTemplateValues are the available properties people can choose from
Expand Down Expand Up @@ -195,6 +208,9 @@ type Config struct {

// DefaultCertificateClass specifies the default Certificate class.
DefaultCertificateClass string

// TagHeaderBasedRouting specifies if TagHeaderBasedRouting is enabled or not.
TagHeaderBasedRouting bool
}

// HTTPProtocol indicates a type of HTTP endpoint behavior
Expand Down Expand Up @@ -270,6 +286,7 @@ func NewConfigFromMap(data map[string]string) (*Config, error) {
templateCache.Add(nc.TagTemplate, t)

nc.AutoTLS = strings.EqualFold(data[AutoTLSKey], "enabled")
nc.TagHeaderBasedRouting = strings.EqualFold(data[TagHeaderBasedRoutingKey], "enabled")

switch strings.ToLower(data[HTTPProtocolKey]) {
case "", string(HTTPEnabled):
Expand Down
69 changes: 60 additions & 9 deletions pkg/reconciler/route/resources/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
netv1alpha1 "knative.dev/serving/pkg/apis/networking/v1alpha1"
"knative.dev/serving/pkg/apis/serving"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
"knative.dev/serving/pkg/network"
"knative.dev/serving/pkg/reconciler/route/config"
"knative.dev/serving/pkg/reconciler/route/domains"
"knative.dev/serving/pkg/reconciler/route/resources/labels"
"knative.dev/serving/pkg/reconciler/route/resources/names"
Expand Down Expand Up @@ -99,6 +101,8 @@ func MakeIngressSpec(
rules := make([]netv1alpha1.IngressRule, 0, len(names))
challengeHosts := getChallengeHosts(acmeChallenges)

networkConfig := config.FromContext(ctx).Network

for _, name := range names {
visibilities := []netv1alpha1.IngressVisibility{netv1alpha1.IngressVisibilityClusterLocal}
// If this is a public target (or not being marked as cluster-local), we also make public rule.
Expand All @@ -111,6 +115,33 @@ func MakeIngressSpec(
return netv1alpha1.IngressSpec{}, err
}
rule := *makeIngressRule([]string{domain}, r.Namespace, visibility, targets[name])
if networkConfig.TagHeaderBasedRouting {
if rule.HTTP.Paths[0].AppendHeaders == nil {
rule.HTTP.Paths[0].AppendHeaders = make(map[string]string)
}

if name == traffic.DefaultTarget {
// To provide a information if a request is routed via the "default route" or not,
// the header "Knative-Serving-Default-Route: true" is appended here.
// If the header has "true" and there is a "Knative-Serving-Tag" header,
// then the request is having the undefined tag header, which will be observed in queue-proxy.
rule.HTTP.Paths[0].AppendHeaders[network.DefaultRouteHeaderName] = "true"
// Add ingress paths for a request with the tag header.
// If a request has one of the `names`(tag name) except the default path,
// the request will be routed via one of the ingress paths, corresponding to the tag name.
rule.HTTP.Paths = append(
makeTagBasedRoutingIngressPaths(r.Namespace, targets, names), rule.HTTP.Paths...)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful to add some comments about what this ingress path is used for.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved.

} else {
// If a request is routed by a tag-attached hostname instead of the tag header,
// the request may not have the tag header "Knative-Serving-Tag",
// even though the ingress path used in the case is also originated
// from the same Knative route with the ingress path for the tag based routing.
//
// To prevent such inconsistency,
// the tag header is appended with the tag corresponding to the tag-attached hostname
rule.HTTP.Paths[0].AppendHeaders[network.TagHeaderName] = name

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be also helpful to explain why we append header here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved.

}
}
// If this is a public rule, we need to configure ACME challenge paths.
if visibility == netv1alpha1.IngressVisibilityExternalIP {
rule.HTTP.Paths = append(
Expand Down Expand Up @@ -173,6 +204,32 @@ func makeACMEIngressPaths(challenges map[string]netv1alpha1.HTTP01Challenge, dom
}

func makeIngressRule(domains []string, ns string, visibility netv1alpha1.IngressVisibility, targets traffic.RevisionTargets) *netv1alpha1.IngressRule {
return &netv1alpha1.IngressRule{
Hosts: domains,
Visibility: visibility,
HTTP: &netv1alpha1.HTTPIngressRuleValue{
Paths: []netv1alpha1.HTTPIngressPath{
*makeBaseIngressPath(ns, targets),
},
},
}
}

func makeTagBasedRoutingIngressPaths(ns string, targets map[string]traffic.RevisionTargets, names []string) []netv1alpha1.HTTPIngressPath {
paths := make([]netv1alpha1.HTTPIngressPath, 0, len(names))

for _, name := range names {
if name != traffic.DefaultTarget {
path := makeBaseIngressPath(ns, targets[name])
path.Headers = map[string]netv1alpha1.HeaderMatch{network.TagHeaderName: {Exact: name}}
paths = append(paths, *path)
}
}

return paths
}

func makeBaseIngressPath(ns string, targets traffic.RevisionTargets) *netv1alpha1.HTTPIngressPath {
// Optimistically allocate |targets| elements.
splits := make([]netv1alpha1.IngressBackendSplit, 0, len(targets))
for _, t := range targets {
Expand All @@ -196,14 +253,8 @@ func makeIngressRule(domains []string, ns string, visibility netv1alpha1.Ingress
})
}

return &netv1alpha1.IngressRule{
Hosts: domains,
Visibility: visibility,
HTTP: &netv1alpha1.HTTPIngressRuleValue{
Paths: []netv1alpha1.HTTPIngressPath{{
Splits: splits,
// TODO(lichuqiang): #2201, plumbing to config timeout and retries.
}},
},
return &netv1alpha1.HTTPIngressPath{
Splits: splits,
// TODO(lichuqiang): #2201, plumbing to config timeout and retries.
}
}
172 changes: 172 additions & 0 deletions pkg/reconciler/route/resources/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,178 @@ func TestMakeIngressSpec_CorrectRuleVisibility(t *testing.T) {
}
}

func TestMakeIngressSpec_CorrectRulesWithTagBasedRouting(t *testing.T) {
targets := map[string]traffic.RevisionTargets{
traffic.DefaultTarget: {{
TrafficTarget: v1.TrafficTarget{
ConfigurationName: "config",
RevisionName: "v2",
Percent: ptr.Int64(100),
},
ServiceName: "gilberto",
Active: true,
}},
"v1": {{
TrafficTarget: v1.TrafficTarget{
ConfigurationName: "config",
RevisionName: "v1",
Percent: ptr.Int64(100),
},
ServiceName: "jobim",
Active: true,
}},
}

r := Route(ns, "test-route", WithURL)

expected := []netv1alpha1.IngressRule{{
Hosts: []string{
"test-route." + ns + ".svc.cluster.local",
},
HTTP: &netv1alpha1.HTTPIngressRuleValue{
Paths: []netv1alpha1.HTTPIngressPath{{
Headers: map[string]netv1alpha1.HeaderMatch{
network.TagHeaderName: {
Exact: "v1",
},
},
Splits: []netv1alpha1.IngressBackendSplit{{
IngressBackend: netv1alpha1.IngressBackend{
ServiceNamespace: ns,
ServiceName: "jobim",
ServicePort: intstr.FromInt(80),
},
Percent: 100,
AppendHeaders: map[string]string{
"Knative-Serving-Revision": "v1",
"Knative-Serving-Namespace": ns,
},
}},
}, {
AppendHeaders: map[string]string{
network.DefaultRouteHeaderName: "true",
},
Splits: []netv1alpha1.IngressBackendSplit{{
IngressBackend: netv1alpha1.IngressBackend{
ServiceNamespace: ns,
ServiceName: "gilberto",
ServicePort: intstr.FromInt(80),
},
Percent: 100,
AppendHeaders: map[string]string{
"Knative-Serving-Revision": "v2",
"Knative-Serving-Namespace": ns,
},
}},
}},
},
Visibility: netv1alpha1.IngressVisibilityClusterLocal,
}, {
Hosts: []string{
"test-route." + ns + ".example.com",
},
HTTP: &netv1alpha1.HTTPIngressRuleValue{
Paths: []netv1alpha1.HTTPIngressPath{{
Headers: map[string]netv1alpha1.HeaderMatch{
network.TagHeaderName: {
Exact: "v1",
},
},
Splits: []netv1alpha1.IngressBackendSplit{{
IngressBackend: netv1alpha1.IngressBackend{
ServiceNamespace: ns,
ServiceName: "jobim",
ServicePort: intstr.FromInt(80),
},
Percent: 100,
AppendHeaders: map[string]string{
"Knative-Serving-Revision": "v1",
"Knative-Serving-Namespace": ns,
},
}},
}, {
AppendHeaders: map[string]string{
network.DefaultRouteHeaderName: "true",
},
Splits: []netv1alpha1.IngressBackendSplit{{
IngressBackend: netv1alpha1.IngressBackend{
ServiceNamespace: ns,
ServiceName: "gilberto",
ServicePort: intstr.FromInt(80),
},
Percent: 100,
AppendHeaders: map[string]string{
"Knative-Serving-Revision": "v2",
"Knative-Serving-Namespace": ns,
},
}},
}},
},
Visibility: netv1alpha1.IngressVisibilityExternalIP,
}, {
Hosts: []string{
"v1-test-route." + ns + ".svc.cluster.local",
},
HTTP: &netv1alpha1.HTTPIngressRuleValue{
Paths: []netv1alpha1.HTTPIngressPath{{
AppendHeaders: map[string]string{
network.TagHeaderName: "v1",
},
Splits: []netv1alpha1.IngressBackendSplit{{
IngressBackend: netv1alpha1.IngressBackend{
ServiceNamespace: ns,
ServiceName: "jobim",
ServicePort: intstr.FromInt(80),
},
Percent: 100,
AppendHeaders: map[string]string{
"Knative-Serving-Revision": "v1",
"Knative-Serving-Namespace": ns,
},
}},
}},
},
Visibility: netv1alpha1.IngressVisibilityClusterLocal,
}, {
Hosts: []string{
"v1-test-route." + ns + ".example.com",
},
HTTP: &netv1alpha1.HTTPIngressRuleValue{
Paths: []netv1alpha1.HTTPIngressPath{{
AppendHeaders: map[string]string{
network.TagHeaderName: "v1",
},
Splits: []netv1alpha1.IngressBackendSplit{{
IngressBackend: netv1alpha1.IngressBackend{
ServiceNamespace: ns,
ServiceName: "jobim",
ServicePort: intstr.FromInt(80),
},
Percent: 100,
AppendHeaders: map[string]string{
"Knative-Serving-Revision": "v1",
"Knative-Serving-Namespace": ns,
},
}},
}},
},
Visibility: netv1alpha1.IngressVisibilityExternalIP,
}}

cfg := testConfig()
cfg.Network.TagHeaderBasedRouting = true
ctx := config.ToContext(context.Background(), cfg)

ci, err := MakeIngressSpec(ctx, r, nil, targets, nil /* visibility */)
if err != nil {
t.Errorf("Unexpected error %v", err)
}

if !cmp.Equal(expected, ci.Rules) {
t.Errorf("Unexpected rules (-want, +got): %s", cmp.Diff(expected, ci.Rules))
}
}

// One active target.
func TestMakeIngressRule_Vanilla(t *testing.T) {
targets := []traffic.RevisionTarget{{
Expand Down
Loading