forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cluster_ingress.go
205 lines (181 loc) · 6.86 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
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 (
"context"
"sort"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"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/network"
"github.com/knative/serving/pkg/reconciler/route/domains"
"github.com/knative/serving/pkg/reconciler/route/resources/names"
"github.com/knative/serving/pkg/reconciler/route/traffic"
"github.com/knative/serving/pkg/resources"
)
// IsClusterLocal checks if a Route is publicly visible or only visible with cluster.
func IsClusterLocal(r *servingv1alpha1.Route) bool {
return r.Status.URL != nil && strings.HasSuffix(r.Status.URL.Host, network.GetClusterDomainName())
}
// MakeClusterIngressTLS creates ClusterIngressTLS to configure the ingress TLS.
func MakeClusterIngressTLS(cert *v1alpha1.Certificate, hostNames []string) v1alpha1.ClusterIngressTLS {
return v1alpha1.ClusterIngressTLS{
Hosts: hostNames,
SecretName: cert.Spec.SecretName,
SecretNamespace: cert.Namespace,
}
}
// 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(ctx context.Context, r *servingv1alpha1.Route, tc *traffic.Config, tls []v1alpha1.ClusterIngressTLS, ingressClass string) (*v1alpha1.ClusterIngress, error) {
spec, err := makeClusterIngressSpec(ctx, r, tls, tc.Targets)
if err != nil {
return nil, err
}
return &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: resources.UnionMaps(map[string]string{
networking.IngressClassAnnotationKey: ingressClass,
}, r.ObjectMeta.Annotations),
},
Spec: spec,
}, nil
}
func makeClusterIngressSpec(ctx context.Context, r *servingv1alpha1.Route, tls []v1alpha1.ClusterIngressTLS, targets map[string]traffic.RevisionTargets) (v1alpha1.IngressSpec, error) {
// 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 {
domains, err := routeDomains(ctx, name, r)
if err != nil {
return v1alpha1.IngressSpec{}, err
}
rules = append(rules, *makeClusterIngressRule(
domains, r.Namespace, targets[name]))
}
visibility := v1alpha1.IngressVisibilityExternalIP
if IsClusterLocal(r) {
visibility = v1alpha1.IngressVisibilityClusterLocal
}
return v1alpha1.IngressSpec{
Rules: rules,
Visibility: visibility,
TLS: tls,
}, nil
}
func routeDomains(ctx context.Context, targetName string, r *servingv1alpha1.Route) ([]string, error) {
fullName, err := domains.DomainNameFromTemplate(ctx, r, domains.SubdomainName(r, targetName))
if err != nil {
return nil, err
}
ruleDomains := []string{fullName}
// TODO(andrew-su): We are adding this for backwards compatibility. This should be removed when
// we feel the users had sufficient time to move away from the deprecated name.
if r.Status.URL != nil {
deprecatedFullName := traffic.DeprecatedTagDomain(targetName, r.Status.URL.Host)
if fullName != deprecatedFullName {
ruleDomains = append(ruleDomains, deprecatedFullName)
}
}
if targetName == traffic.DefaultTarget {
// The default target is also referred to by its internal K8s
// generated domain name.
internalHost := names.K8sServiceFullname(r)
if internalHost != "" && ruleDomains[0] != internalHost {
ruleDomains = append(ruleDomains, internalHost)
}
}
return ruleDomains, nil
}
func makeClusterIngressRule(domains []string, ns string, targets traffic.RevisionTargets) *v1alpha1.ClusterIngressRule {
// Optimistically allocate |targets| elements.
splits := make([]v1alpha1.ClusterIngressBackendSplit, 0, len(targets))
for _, t := range targets {
if t.Percent == 0 {
continue
}
splits = append(splits, v1alpha1.ClusterIngressBackendSplit{
ClusterIngressBackend: v1alpha1.ClusterIngressBackend{
ServiceNamespace: ns,
ServiceName: t.ServiceName,
// Port on the public service must match port on the activator.
// Otherwise, the serverless services can't guarantee seamless positive handoff.
ServicePort: intstr.FromInt(int(networking.ServicePort(t.Protocol))),
},
Percent: t.Percent,
// TODO(nghia): Append headers per-split.
// AppendHeaders: map[string]string{
// activator.RevisionHeaderName: t.TrafficTarget.RevisionName,
// activator.RevisionHeaderNamespace: ns,
// },
})
}
return &v1alpha1.ClusterIngressRule{
Hosts: domains,
HTTP: &v1alpha1.HTTPClusterIngressRuleValue{
Paths: []v1alpha1.HTTPClusterIngressPath{{
Splits: splits,
// TODO(lichuqiang): #2201, plumbing to config timeout and retries.
AppendHeaders: map[string]string{
activator.RevisionHeaderName: maxInactive(targets),
activator.RevisionHeaderNamespace: ns,
},
}},
},
}
}
// maxInactive constructs Splits for the inactive targets, and add into given IngressPath.
func maxInactive(targets traffic.RevisionTargets) string {
revisionName, inactiveRevisionName := "", ""
maxTargetPercent := 0 // There must be a non-zero target if things add to 100
maxInactiveTargetPercent := 0 // There must be a non-zero target if things add to 100
for _, t := range targets {
if t.Percent == 0 {
continue
}
if t.Percent >= maxTargetPercent {
revisionName = t.RevisionName
maxTargetPercent = t.Percent
}
if t.Active {
continue
}
if t.Percent >= maxInactiveTargetPercent {
inactiveRevisionName = t.RevisionName
maxInactiveTargetPercent = t.Percent
}
}
if inactiveRevisionName != "" {
return inactiveRevisionName
}
return revisionName
}