forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
464 lines (400 loc) · 16.2 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
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 (
"bytes"
"context"
"encoding/json"
"fmt"
"text/template"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
corev1informers "k8s.io/client-go/informers/core/v1"
corev1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
"github.com/knative/pkg/configmap"
"github.com/knative/pkg/controller"
"github.com/knative/pkg/logging"
"github.com/knative/pkg/system"
"github.com/knative/pkg/tracker"
"github.com/knative/serving/pkg/apis/networking"
"github.com/knative/serving/pkg/apis/serving"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
networkinginformers "github.com/knative/serving/pkg/client/informers/externalversions/networking/v1alpha1"
servinginformers "github.com/knative/serving/pkg/client/informers/externalversions/serving/v1alpha1"
networkinglisters "github.com/knative/serving/pkg/client/listers/networking/v1alpha1"
listers "github.com/knative/serving/pkg/client/listers/serving/v1alpha1"
"github.com/knative/serving/pkg/network"
"github.com/knative/serving/pkg/reconciler"
"github.com/knative/serving/pkg/reconciler/v1alpha1/route/config"
"github.com/knative/serving/pkg/reconciler/v1alpha1/route/resources"
resourcenames "github.com/knative/serving/pkg/reconciler/v1alpha1/route/resources/names"
"github.com/knative/serving/pkg/reconciler/v1alpha1/route/traffic"
)
const (
controllerAgentName = "route-controller"
)
// routeFinalizer is the name that we put into the resource finalizer list, e.g.
// metadata:
// finalizers:
// - routes.serving.knative.dev
var (
routeResource = v1alpha1.Resource("routes")
routeFinalizer = routeResource.String()
)
type configStore interface {
ToContext(ctx context.Context) context.Context
WatchConfigs(w configmap.Watcher)
}
// DomainTemplateValues are the available properties people can choose from
// in their Route's "DomainTemplate" golang template sting.
// We could add more over time - e.g. RevisionName if we thought that
// might be of interest to people.
type DomainTemplateValues struct {
Name string
Namespace string
Domain string
}
// Reconciler implements controller.Reconciler for Route resources.
type Reconciler struct {
*reconciler.Base
// Listers index properties about resources
routeLister listers.RouteLister
configurationLister listers.ConfigurationLister
revisionLister listers.RevisionLister
serviceLister corev1listers.ServiceLister
clusterIngressLister networkinglisters.ClusterIngressLister
configStore configStore
tracker tracker.Interface
clock system.Clock
}
// Check that our Reconciler implements controller.Reconciler
var _ controller.Reconciler = (*Reconciler)(nil)
// NewController initializes the controller and is called by the generated code
// Registers eventhandlers to enqueue events
// config - client configuration for talking to the apiserver
// si - informer factory shared across all controllers for listening to events and indexing resource properties
// reconcileKey - function for mapping queue keys to resource names
func NewController(
opt reconciler.Options,
routeInformer servinginformers.RouteInformer,
configInformer servinginformers.ConfigurationInformer,
revisionInformer servinginformers.RevisionInformer,
serviceInformer corev1informers.ServiceInformer,
clusterIngressInformer networkinginformers.ClusterIngressInformer,
) *controller.Impl {
return NewControllerWithClock(opt, routeInformer, configInformer, revisionInformer,
serviceInformer, clusterIngressInformer, system.RealClock{})
}
func NewControllerWithClock(
opt reconciler.Options,
routeInformer servinginformers.RouteInformer,
configInformer servinginformers.ConfigurationInformer,
revisionInformer servinginformers.RevisionInformer,
serviceInformer corev1informers.ServiceInformer,
clusterIngressInformer networkinginformers.ClusterIngressInformer,
clock system.Clock,
) *controller.Impl {
// No need to lock domainConfigMutex yet since the informers that can modify
// domainConfig haven't started yet.
c := &Reconciler{
Base: reconciler.NewBase(opt, controllerAgentName),
routeLister: routeInformer.Lister(),
configurationLister: configInformer.Lister(),
revisionLister: revisionInformer.Lister(),
serviceLister: serviceInformer.Lister(),
clusterIngressLister: clusterIngressInformer.Lister(),
clock: clock,
}
impl := controller.NewImpl(c, c.Logger, "Routes", reconciler.MustNewStatsReporter("Routes", c.Logger))
c.Logger.Info("Setting up event handlers")
routeInformer.Informer().AddEventHandler(reconciler.Handler(impl.Enqueue))
serviceInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: controller.Filter(v1alpha1.SchemeGroupVersion.WithKind("Route")),
Handler: reconciler.Handler(impl.EnqueueControllerOf),
})
clusterIngressInformer.Informer().AddEventHandler(reconciler.Handler(
impl.EnqueueLabelOfNamespaceScopedResource(
serving.RouteNamespaceLabelKey, serving.RouteLabelKey)))
c.tracker = tracker.New(impl.EnqueueKey, opt.GetTrackerLease())
configInformer.Informer().AddEventHandler(reconciler.Handler(
// Call the tracker's OnChanged method, but we've seen the objects
// coming through this path missing TypeMeta, so ensure it is properly
// populated.
controller.EnsureTypeMeta(
c.tracker.OnChanged,
v1alpha1.SchemeGroupVersion.WithKind("Configuration"),
),
))
revisionInformer.Informer().AddEventHandler(reconciler.Handler(
// Call the tracker's OnChanged method, but we've seen the objects
// coming through this path missing TypeMeta, so ensure it is properly
// populated.
controller.EnsureTypeMeta(
c.tracker.OnChanged,
v1alpha1.SchemeGroupVersion.WithKind("Revision"),
),
))
c.Logger.Info("Setting up ConfigMap receivers")
configsToResync := []interface{}{
&network.Config{},
&config.Domain{},
}
resync := configmap.TypeFilter(configsToResync...)(func(string, interface{}) {
impl.GlobalResync(routeInformer.Informer())
})
c.configStore = config.NewStore(c.Logger.Named("config-store"), resync)
c.configStore.WatchConfigs(opt.ConfigMapWatcher)
return impl
}
/////////////////////////////////////////
// Event handlers
/////////////////////////////////////////
// Reconcile compares the actual state with the desired, and attempts to
// converge the two. It then updates the Status block of the Route resource
// with the current status of the resource.
func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
// Convert the namespace/name string into a distinct namespace and name
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
c.Logger.Errorf("invalid resource key: %s", key)
return nil
}
logger := logging.FromContext(ctx)
ctx = c.configStore.ToContext(ctx)
// Get the Route resource with this namespace/name.
original, err := c.routeLister.Routes(namespace).Get(name)
if apierrs.IsNotFound(err) {
// The resource may no longer exist, in which case we stop processing.
logger.Errorf("route %q in work queue no longer exists", key)
return nil
} else if err != nil {
return err
}
// Don't modify the informers copy.
route := original.DeepCopy()
// Reconcile this copy of the route and then write back any status
// updates regardless of whether the reconciliation errored out.
err = c.reconcile(ctx, route)
if equality.Semantic.DeepEqual(original.Status, route.Status) {
// If we didn't change anything then don't call updateStatus.
// This is important because the copy we loaded from the informer's
// cache may be stale and we don't want to overwrite a prior update
// to status with this stale state.
} else if _, err := c.updateStatus(route); err != nil {
logger.Warnw("Failed to update route status", zap.Error(err))
c.Recorder.Eventf(route, corev1.EventTypeWarning, "UpdateFailed",
"Failed to update status for Route %q: %v", route.Name, err)
return err
}
if err != nil {
c.Recorder.Event(route, corev1.EventTypeWarning, "InternalError", err.Error())
}
return err
}
func ingressClassForRoute(ctx context.Context, r *v1alpha1.Route) string {
if ingressClass := r.Annotations[networking.IngressClassAnnotationKey]; ingressClass != "" {
return ingressClass
}
return config.FromContext(ctx).Network.DefaultClusterIngressClass
}
func (c *Reconciler) reconcile(ctx context.Context, r *v1alpha1.Route) error {
logger := logging.FromContext(ctx)
if r.GetDeletionTimestamp() != nil {
// Check for a DeletionTimestamp. If present, elide the normal reconcile logic.
return c.reconcileDeletion(ctx, r)
}
// 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)
// Update the information that makes us Addressable. This is needed to configure traffic and
// make the cluster ingress.
var err error
r.Status.Domain, err = routeDomain(ctx, r)
if err != nil {
return err
}
// 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.
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.DeprecatedDomainInternal = resourcenames.K8sServiceFullname(r)
r.Status.Address = &duckv1alpha1.Addressable{
Hostname: resourcenames.K8sServiceFullname(r),
}
// Add the finalizer before creating the ClusterIngress so that we can be sure it gets cleaned up.
if err := c.ensureFinalizer(r); err != nil {
return err
}
logger.Info("Creating ClusterIngress.")
desired := resources.MakeClusterIngress(r, traffic, ingressClassForRoute(ctx, r))
clusterIngress, err := c.reconcileClusterIngress(ctx, r, desired)
if err != nil {
return err
}
r.Status.PropagateClusterIngressStatus(clusterIngress.Status)
logger.Info("Creating/Updating placeholder k8s services")
if err := c.reconcilePlaceholderService(ctx, r, clusterIngress); err != nil {
return err
}
r.Status.ObservedGeneration = r.Generation
logger.Info("Route successfully synced")
return nil
}
func (c *Reconciler) reconcileDeletion(ctx context.Context, r *v1alpha1.Route) error {
logger := logging.FromContext(ctx)
// If our Finalizer is first, delete the ClusterIngress for this Route
// and remove the finalizer.
if len(r.Finalizers) == 0 || r.Finalizers[0] != routeFinalizer {
return nil
}
// Delete the ClusterIngress resources for this Route.
logger.Info("Cleaning up ClusterIngress")
if err := c.deleteClusterIngressesForRoute(r); err != nil {
return err
}
// Update the Route to remove the Finalizer.
logger.Info("Removing Finalizer")
r.Finalizers = r.Finalizers[1:]
_, err := c.ServingClientSet.ServingV1alpha1().Routes(r.Namespace).Update(r)
return err
}
// 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 *v1alpha1.Route) (*traffic.Config, error) {
logger := logging.FromContext(ctx)
t, err := traffic.BuildTrafficConfiguration(c.configurationLister, c.revisionLister, r)
if t != nil {
// Tell our trackers to reconcile Route whenever the things referred to by our
// Traffic stanza change.
gvk := v1alpha1.SchemeGroupVersion.WithKind("Configuration")
for _, configuration := range t.Configurations {
if err := c.tracker.Track(objectRef(configuration, gvk), r); err != nil {
return nil, err
}
}
gvk = v1alpha1.SchemeGroupVersion.WithKind("Revision")
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, gvk), r); err != nil {
return nil, err
}
}
}
badTarget, isTargetError := err.(traffic.TargetError)
if err != nil && !isTargetError {
// An error that's not due to missing traffic target should
// make us fail fast.
r.Status.MarkUnknownTrafficError(err.Error())
return nil, err
}
if badTarget != nil && isTargetError {
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 = t.GetRevisionTrafficTargets(r.Status.Domain)
r.Status.MarkTrafficAssigned()
return t, nil
}
func (c *Reconciler) ensureFinalizer(route *v1alpha1.Route) error {
finalizers := sets.NewString(route.Finalizers...)
if finalizers.Has(routeFinalizer) {
return nil
}
mergePatch := map[string]interface{}{
"metadata": map[string]interface{}{
"finalizers": append(route.Finalizers, routeFinalizer),
"resourceVersion": route.ResourceVersion,
},
}
patch, err := json.Marshal(mergePatch)
if err != nil {
return err
}
_, err = c.ServingClientSet.ServingV1alpha1().Routes(route.Namespace).Patch(route.Name, types.MergePatchType, patch)
return err
}
/////////////////////////////////////////
// Misc helpers.
/////////////////////////////////////////
type accessor interface {
GroupVersionKind() schema.GroupVersionKind
GetNamespace() string
GetName() string
}
func objectRef(a accessor, gvk schema.GroupVersionKind) corev1.ObjectReference {
// We can't always rely on the TypeMeta being populated.
// See: https://github.com/knative/serving/issues/2372
// Also: https://github.com/kubernetes/apiextensions-apiserver/issues/29
// gvk := a.GroupVersionKind()
apiVersion, kind := gvk.ToAPIVersionAndKind()
return corev1.ObjectReference{
APIVersion: apiVersion,
Kind: kind,
Namespace: a.GetNamespace(),
Name: a.GetName(),
}
}
// routeDeomain will generate the Route's Domain(host) for the Service based on
// the "DomainTemplateKey" from the "config-network" configMap.
func routeDomain(ctx context.Context, route *v1alpha1.Route) (string, error) {
domainConfig := config.FromContext(ctx).Domain
domain := domainConfig.LookupDomainForLabels(route.ObjectMeta.Labels)
// These are the available properties they can choose from.
// We could add more over time - e.g. RevisionName if we thought that
// might be of interest to people.
data := DomainTemplateValues{
Name: route.Name,
Namespace: route.Namespace,
Domain: domain,
}
networkConfig := config.FromContext(ctx).Network
text := networkConfig.DomainTemplate
// It's ok if we keep using the same name
templ, err := template.New("knTemplate").Parse(text)
if err != nil {
return "", fmt.Errorf("Error parsing the DomainTemplate(%s): %s", text, err)
}
buf := bytes.Buffer{}
if err := templ.Execute(&buf, data); err != nil {
return "", fmt.Errorf("Error executing the DomainTemplate(%s): %s", text, err)
}
return buf.String(), nil
}