forked from knative/serving
/
route.go
447 lines (385 loc) · 14.5 KB
/
route.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
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
http://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 route
import (
"context"
"sort"
"strings"
corev1 "k8s.io/api/core/v1"
kubelabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
corev1listers "k8s.io/client-go/listers/core/v1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/logging"
pkgreconciler "knative.dev/pkg/reconciler"
"knative.dev/pkg/system"
"knative.dev/pkg/tracker"
"knative.dev/serving/pkg/apis/networking"
netv1alpha1 "knative.dev/serving/pkg/apis/networking/v1alpha1"
v1 "knative.dev/serving/pkg/apis/serving/v1"
clientset "knative.dev/serving/pkg/client/clientset/versioned"
routereconciler "knative.dev/serving/pkg/client/injection/reconciler/serving/v1/route"
networkinglisters "knative.dev/serving/pkg/client/listers/networking/v1alpha1"
listers "knative.dev/serving/pkg/client/listers/serving/v1"
"knative.dev/serving/pkg/network"
kaccessor "knative.dev/serving/pkg/reconciler/accessor"
networkaccessor "knative.dev/serving/pkg/reconciler/accessor/networking"
"knative.dev/serving/pkg/reconciler/route/config"
"knative.dev/serving/pkg/reconciler/route/domains"
"knative.dev/serving/pkg/reconciler/route/resources"
"knative.dev/serving/pkg/reconciler/route/resources/labels"
resourcenames "knative.dev/serving/pkg/reconciler/route/resources/names"
"knative.dev/serving/pkg/reconciler/route/traffic"
"knative.dev/serving/pkg/reconciler/route/visibility"
)
// Reconciler implements controller.Reconciler for Route resources.
type Reconciler struct {
kubeclient kubernetes.Interface
client clientset.Interface
// Listers index properties about resources
configurationLister listers.ConfigurationLister
revisionLister listers.RevisionLister
serviceLister corev1listers.ServiceLister
ingressLister networkinglisters.IngressLister
certificateLister networkinglisters.CertificateLister
tracker tracker.Interface
clock system.Clock
}
// Check that our Reconciler implements routereconciler.Interface
var _ routereconciler.Interface = (*Reconciler)(nil)
func ingressClassForRoute(ctx context.Context, r *v1.Route) string {
if ingressClass := r.Annotations[networking.IngressClassAnnotationKey]; ingressClass != "" {
return ingressClass
}
return config.FromContext(ctx).Network.DefaultIngressClass
}
func certClass(ctx context.Context, r *v1.Route) string {
if class := r.Annotations[networking.CertificateClassAnnotationKey]; class != "" {
return class
}
return config.FromContext(ctx).Network.DefaultCertificateClass
}
func (c *Reconciler) getServices(route *v1.Route) ([]*corev1.Service, error) {
currentServices, err := c.serviceLister.Services(route.Namespace).List(resources.SelectorFromRoute(route))
if err != nil {
return nil, err
}
serviceCopy := make([]*corev1.Service, len(currentServices))
for i, svc := range currentServices {
serviceCopy[i] = svc.DeepCopy()
}
return serviceCopy, err
}
func (c *Reconciler) ReconcileKind(ctx context.Context, r *v1.Route) pkgreconciler.Event {
logger := logging.FromContext(ctx)
// We may be reading a version of the object that was stored at an older version
// and may not have had all of the assumed defaults specified. This won't result
// in this getting written back to the API Server, but lets downstream logic make
// assumptions about defaulting.
r.SetDefaults(ctx)
r.Status.InitializeConditions()
logger.Infof("Reconciling route: %#v", r)
// Configure traffic based on the RouteSpec.
traffic, err := c.configureTraffic(ctx, r)
if traffic == nil || err != nil {
// Traffic targets aren't ready, no need to configure child resources.
// Need to update ObservedGeneration, otherwise Route's Ready state won't
// be propagated to Service and the Service's RoutesReady will stay in
// 'Unknown'.
r.Status.ObservedGeneration = r.Generation
return err
}
logger.Info("Updating targeted revisions.")
// In all cases we will add annotations to the referred targets. This is so that when they become
// routable we can know (through a listener) and attempt traffic configuration again.
if err := c.reconcileTargetRevisions(ctx, traffic, r); err != nil {
return err
}
r.Status.Address = &duckv1.Addressable{
URL: &apis.URL{
Scheme: "http",
Host: resourcenames.K8sServiceFullname(r),
},
}
logger.Info("Creating placeholder k8s services")
services, err := c.reconcilePlaceholderServices(ctx, r, traffic.Targets)
if err != nil {
return err
}
tls, acmeChallenges, err := c.tls(ctx, r.Status.URL.Host, r, traffic)
if err != nil {
return err
}
// Reconcile ingress and its children resources.
ingress, err := c.reconcileIngressResources(ctx, r, traffic, tls, ingressClassForRoute(ctx, r), acmeChallenges...)
if err != nil {
return err
}
if ingress.GetObjectMeta().GetGeneration() != ingress.Status.ObservedGeneration || !ingress.Status.IsReady() {
r.Status.MarkIngressNotConfigured()
} else {
r.Status.PropagateIngressStatus(ingress.Status)
}
logger.Info("Updating placeholder k8s services with ingress information")
if err := c.updatePlaceholderServices(ctx, r, services, ingress); err != nil {
return err
}
r.Status.ObservedGeneration = r.Generation
logger.Info("Route successfully synced")
return nil
}
func (c *Reconciler) reconcileIngressResources(ctx context.Context, r *v1.Route, tc *traffic.Config, tls []netv1alpha1.IngressTLS,
ingressClass string, acmeChallenges ...netv1alpha1.HTTP01Challenge) (*netv1alpha1.Ingress, error) {
desired, err := resources.MakeIngress(ctx, r, tc, tls, ingressClass, acmeChallenges...)
if err != nil {
return nil, err
}
ingress, err := c.reconcileIngress(ctx, r, desired)
if err != nil {
return nil, err
}
return ingress, nil
}
func (c *Reconciler) tls(ctx context.Context, host string, r *v1.Route, traffic *traffic.Config) ([]netv1alpha1.IngressTLS, []netv1alpha1.HTTP01Challenge, error) {
tls := []netv1alpha1.IngressTLS{}
if !config.FromContext(ctx).Network.AutoTLS {
return tls, nil, nil
}
domainToTagMap, err := domains.GetAllDomainsAndTags(ctx, r, getTrafficNames(traffic.Targets), traffic.Visibility)
if err != nil {
return nil, nil, err
}
for domain := range domainToTagMap {
if domains.IsClusterLocal(domain) {
delete(domainToTagMap, domain)
}
}
routeDomain := config.FromContext(ctx).Domain.LookupDomainForLabels(r.Labels)
labelSelector := kubelabels.SelectorFromSet(kubelabels.Set{
networking.WildcardCertDomainLabelKey: routeDomain,
})
allWildcardCerts, err := c.certificateLister.Certificates(r.Namespace).List(labelSelector)
if err != nil {
return nil, nil, err
}
acmeChallenges := []netv1alpha1.HTTP01Challenge{}
desiredCerts := resources.MakeCertificates(r, domainToTagMap, certClass(ctx, r))
for _, desiredCert := range desiredCerts {
dnsNames := sets.NewString(desiredCert.Spec.DNSNames...)
// Look for a matching wildcard cert before provisioning a new one. This saves the
// the time required to provision a new cert and reduces the chances of hitting the
// Let's Encrypt API rate limits.
cert := findMatchingWildcardCert(ctx, desiredCert.Spec.DNSNames, allWildcardCerts)
if cert == nil {
cert, err = networkaccessor.ReconcileCertificate(ctx, r, desiredCert, c)
if err != nil {
if kaccessor.IsNotOwned(err) {
r.Status.MarkCertificateNotOwned(desiredCert.Name)
} else {
r.Status.MarkCertificateProvisionFailed(desiredCert.Name)
}
return nil, nil, err
}
dnsNames = sets.NewString(cert.Spec.DNSNames...)
}
// r.Status.URL is for the major domain, so only change if the cert is for
// the major domain
if dnsNames.Has(host) {
r.Status.URL.Scheme = "https"
}
// TODO: we should only mark https for the public visible targets when
// we are able to configure visibility per target.
setTargetsScheme(&r.Status, dnsNames.List(), "https")
if cert.Status.IsReady() {
r.Status.MarkCertificateReady(cert.Name)
tls = append(tls, resources.MakeIngressTLS(cert, dnsNames.List()))
} else {
acmeChallenges = append(acmeChallenges, cert.Status.HTTP01Challenges...)
r.Status.MarkCertificateNotReady(cert.Name)
// When httpProtocol is enabled, downward http scheme.
if config.FromContext(ctx).Network.HTTPProtocol == network.HTTPEnabled {
if dnsNames.Has(host) {
r.Status.URL = &apis.URL{
Scheme: "http",
Host: host,
}
}
setTargetsScheme(&r.Status, dnsNames.List(), "http")
}
}
}
sort.Slice(acmeChallenges, func(i, j int) bool {
return acmeChallenges[i].URL.String() < acmeChallenges[j].URL.String()
})
return tls, acmeChallenges, nil
}
// configureTraffic attempts to configure traffic based on the RouteSpec. If there are missing
// targets (e.g. Configurations without a Ready Revision, or Revision that isn't Ready or Inactive),
// no traffic will be configured.
//
// If traffic is configured we update the RouteStatus with AllTrafficAssigned = True. Otherwise we
// mark AllTrafficAssigned = False, with a message referring to one of the missing target.
func (c *Reconciler) configureTraffic(ctx context.Context, r *v1.Route) (*traffic.Config, error) {
logger := logging.FromContext(ctx)
t, trafficErr := traffic.BuildTrafficConfiguration(c.configurationLister, c.revisionLister, r)
if t == nil {
return nil, trafficErr
}
// Augment traffic configuration with visibility information. Do not overwrite trafficErr,
// since we will use it later.
visibility, err := visibility.NewResolver(c.serviceLister).GetVisibility(ctx, r)
if err != nil {
return nil, err
}
t.Visibility = visibility
// Update the Route URL.
if err := c.updateRouteStatusURL(ctx, r, t.Visibility); err != nil {
return nil, err
}
// Tell our trackers to reconcile Route whenever the things referred to by our
// traffic stanza change. We also track missing targets since there may be
// race conditions were routes are reconciled before their targets appear
// in the informer cache
for _, obj := range t.MissingTargets {
if err := c.tracker.Track(obj, r); err != nil {
return nil, err
}
}
for _, configuration := range t.Configurations {
if err := c.tracker.Track(objectRef(configuration), r); err != nil {
return nil, err
}
}
for _, revision := range t.Revisions {
if revision.Status.IsActivationRequired() {
logger.Infof("Revision %s/%s is inactive", revision.Namespace, revision.Name)
}
if err := c.tracker.Track(objectRef(revision), r); err != nil {
return nil, err
}
}
badTarget, isTargetError := trafficErr.(traffic.TargetError)
if trafficErr != nil && !isTargetError {
// An error that's not due to missing traffic target should
// make us fail fast.
r.Status.MarkUnknownTrafficError(trafficErr.Error())
return nil, trafficErr
}
if badTarget != nil && isTargetError {
logger.Infof("Marking bad traffic target: %v", badTarget)
badTarget.MarkBadTrafficTarget(&r.Status)
// Traffic targets aren't ready, no need to configure Route.
return nil, nil
}
logger.Info("All referred targets are routable, marking AllTrafficAssigned with traffic information.")
// Domain should already be present
r.Status.Traffic, err = t.GetRevisionTrafficTargets(ctx, r)
if err != nil {
return nil, err
}
r.Status.MarkTrafficAssigned()
return t, nil
}
func (c *Reconciler) updateRouteStatusURL(ctx context.Context, route *v1.Route, visibility map[string]netv1alpha1.IngressVisibility) error {
isClusterLocal := visibility[traffic.DefaultTarget] == netv1alpha1.IngressVisibilityClusterLocal
mainRouteMeta := route.ObjectMeta.DeepCopy()
labels.SetVisibility(mainRouteMeta, isClusterLocal)
host, err := domains.DomainNameFromTemplate(ctx, *mainRouteMeta, route.Name)
if err != nil {
return err
}
route.Status.URL = &apis.URL{
Scheme: "http",
Host: host,
}
return nil
}
// GetServingClient returns the client to access Knative serving resources.
func (c *Reconciler) GetServingClient() clientset.Interface {
return c.client
}
// GetCertificateLister returns the lister for Knative Certificate.
func (c *Reconciler) GetCertificateLister() networkinglisters.CertificateLister {
return c.certificateLister
}
/////////////////////////////////////////
// Misc helpers.
/////////////////////////////////////////
type accessor interface {
GetGroupVersionKind() schema.GroupVersionKind
GetNamespace() string
GetName() string
}
func objectRef(a accessor) corev1.ObjectReference {
gvk := a.GetGroupVersionKind()
apiVersion, kind := gvk.ToAPIVersionAndKind()
return corev1.ObjectReference{
APIVersion: apiVersion,
Kind: kind,
Namespace: a.GetNamespace(),
Name: a.GetName(),
}
}
func getTrafficNames(targets map[string]traffic.RevisionTargets) []string {
names := []string{}
for name := range targets {
names = append(names, name)
}
return names
}
// Sets the traffic URL scheme to scheme if the URL matches the dnsNames.
// dnsNames are DNS names under a certificate for a particular domain, and so only change
// the corresponding traffic under the route, rather than all traffic
func setTargetsScheme(rs *v1.RouteStatus, dnsNames []string, scheme string) {
for i := range rs.Traffic {
if rs.Traffic[i].URL == nil {
continue
}
for _, dnsName := range dnsNames {
if rs.Traffic[i].URL.Host == dnsName {
rs.Traffic[i].URL.Scheme = scheme
break
}
}
}
}
func findMatchingWildcardCert(ctx context.Context, domains []string, certs []*netv1alpha1.Certificate) *netv1alpha1.Certificate {
for _, cert := range certs {
if wildcardCertMatches(ctx, domains, cert) {
return cert
}
}
return nil
}
func wildcardCertMatches(ctx context.Context, domains []string, cert *netv1alpha1.Certificate) bool {
dnsNames := sets.NewString()
logger := logging.FromContext(ctx)
for _, dns := range cert.Spec.DNSNames {
dnsParts := strings.SplitAfterN(dns, ".", 2)
if len(dnsParts) < 2 {
logger.Infof("got non-FQDN DNSName %s in certificate %s", dns, cert.Name)
continue
}
dnsNames.Insert(dnsParts[1])
}
for _, domain := range domains {
domainParts := strings.SplitAfterN(domain, ".", 2)
if len(domainParts) < 2 || !dnsNames.Has(domainParts[1]) {
return false
}
}
return true
}