forked from knative/serving
/
cluster_ingress.go
164 lines (146 loc) · 5.59 KB
/
cluster_ingress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright 2018 The Knative Authors
Licensed 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
https://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 resources
import (
"sort"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"github.com/knative/pkg/system"
"github.com/knative/serving/pkg/activator"
"github.com/knative/serving/pkg/apis/networking"
"github.com/knative/serving/pkg/apis/networking/v1alpha1"
"github.com/knative/serving/pkg/apis/serving"
servingv1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/knative/serving/pkg/reconciler"
revisionresources "github.com/knative/serving/pkg/reconciler/v1alpha1/revision/resources"
"github.com/knative/serving/pkg/reconciler/v1alpha1/route/resources/names"
"github.com/knative/serving/pkg/reconciler/v1alpha1/route/traffic"
"github.com/knative/serving/pkg/utils"
)
func isClusterLocal(r *servingv1alpha1.Route) bool {
return strings.HasSuffix(r.Status.Domain, utils.GetClusterDomainName())
}
// MakeClusterIngress creates ClusterIngress to set up routing rules. Such ClusterIngress specifies
// which Hosts that it applies to, as well as the routing rules.
func MakeClusterIngress(r *servingv1alpha1.Route, tc *traffic.Config, ingressClass string) *v1alpha1.ClusterIngress {
ci := &v1alpha1.ClusterIngress{
ObjectMeta: metav1.ObjectMeta{
// As ClusterIngress resource is cluster-scoped,
// here we use GenerateName to avoid conflict.
Name: names.ClusterIngress(r),
Labels: map[string]string{
serving.RouteLabelKey: r.Name,
serving.RouteNamespaceLabelKey: r.Namespace,
},
Annotations: r.ObjectMeta.Annotations,
},
Spec: makeClusterIngressSpec(r, tc.Targets),
}
// Set the ingress class annotation.
if ci.ObjectMeta.Annotations == nil {
ci.ObjectMeta.Annotations = make(map[string]string)
}
ci.ObjectMeta.Annotations[networking.IngressClassAnnotationKey] = ingressClass
return ci
}
func makeClusterIngressSpec(r *servingv1alpha1.Route, targets map[string]traffic.RevisionTargets) v1alpha1.IngressSpec {
// Domain should have been specified in route status
// before calling this func.
names := make([]string, 0, len(targets))
for name := range targets {
names = append(names, name)
}
// Sort the names to give things a deterministic ordering.
sort.Strings(names)
// The routes are matching rule based on domain name to traffic split targets.
rules := make([]v1alpha1.ClusterIngressRule, 0, len(names))
for _, name := range names {
rules = append(rules, *makeClusterIngressRule(
routeDomains(name, r), r.Namespace, targets[name]))
}
spec := v1alpha1.IngressSpec{
Rules: rules,
Visibility: v1alpha1.IngressVisibilityExternalIP,
}
if isClusterLocal(r) {
spec.Visibility = v1alpha1.IngressVisibilityClusterLocal
}
return spec
}
func routeDomains(targetName string, r *servingv1alpha1.Route) []string {
domains := []string{traffic.SubrouteDomain(targetName, r.Status.Domain)}
if targetName == traffic.DefaultTarget {
// The default target is also referred to by its internal K8s
// generated domain name.
internalHost := names.K8sServiceFullname(r)
if internalHost != "" && domains[0] != internalHost {
domains = append(domains, internalHost)
}
}
return domains
}
func makeClusterIngressRule(domains []string, ns string, targets traffic.RevisionTargets) *v1alpha1.ClusterIngressRule {
active, inactive := targets.GroupTargets()
// Optimistically allocate |active| elements.
splits := make([]v1alpha1.ClusterIngressBackendSplit, 0, len(active))
for _, t := range active {
splits = append(splits, v1alpha1.ClusterIngressBackendSplit{
ClusterIngressBackend: v1alpha1.ClusterIngressBackend{
ServiceNamespace: ns,
ServiceName: reconciler.GetServingK8SServiceNameForObj(t.TrafficTarget.RevisionName),
ServicePort: intstr.FromInt(int(revisionresources.ServicePort)),
},
Percent: t.Percent,
})
}
path := &v1alpha1.HTTPClusterIngressPath{
Splits: splits,
// TODO(lichuqiang): #2201, plumbing to config timeout and retries.
}
return &v1alpha1.ClusterIngressRule{
Hosts: domains,
HTTP: &v1alpha1.HTTPClusterIngressRuleValue{
Paths: []v1alpha1.HTTPClusterIngressPath{
*addInactive(path, ns, inactive),
},
},
}
}
// addInactive constructs Splits for the inactive targets, and add into given IngressPath.
func addInactive(r *v1alpha1.HTTPClusterIngressPath, ns string, inactive traffic.RevisionTargets) *v1alpha1.HTTPClusterIngressPath {
if len(inactive) == 0 {
return r
}
totalInactivePercent := 0
maxInactiveTarget := &inactive[0]
for i, t := range inactive {
totalInactivePercent += t.Percent
if t.Percent >= maxInactiveTarget.Percent {
maxInactiveTarget = &inactive[i]
}
}
r.Splits = append(r.Splits, v1alpha1.ClusterIngressBackendSplit{
ClusterIngressBackend: v1alpha1.ClusterIngressBackend{
ServiceNamespace: system.Namespace(),
ServiceName: activator.K8sServiceName,
ServicePort: intstr.FromInt(int(activator.ServicePort(maxInactiveTarget.Protocol))),
},
Percent: totalInactivePercent,
})
r.AppendHeaders = map[string]string{
activator.RevisionHeaderName: maxInactiveTarget.RevisionName,
activator.RevisionHeaderNamespace: ns,
}
return r
}