-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
service.go
313 lines (274 loc) · 12.3 KB
/
service.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
/*
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 service
import (
"context"
"fmt"
"github.com/google/go-cmp/cmp/cmpopts"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "knative.dev/serving/pkg/client/clientset/versioned"
ksvcreconciler "knative.dev/serving/pkg/client/injection/reconciler/serving/v1/service"
"knative.dev/pkg/controller"
"knative.dev/pkg/kmp"
"knative.dev/pkg/logging"
pkgreconciler "knative.dev/pkg/reconciler"
"knative.dev/serving/pkg/apis/serving"
v1 "knative.dev/serving/pkg/apis/serving/v1"
listers "knative.dev/serving/pkg/client/listers/serving/v1"
configresources "knative.dev/serving/pkg/reconciler/configuration/resources"
"knative.dev/serving/pkg/reconciler/service/resources"
resourcenames "knative.dev/serving/pkg/reconciler/service/resources/names"
)
// Reconciler implements controller.Reconciler for Service resources.
type Reconciler struct {
client clientset.Interface
// listers index properties about resources
configurationLister listers.ConfigurationLister
revisionLister listers.RevisionLister
routeLister listers.RouteLister
}
// Check that our Reconciler implements ksvcreconciler.Interface
var _ ksvcreconciler.Interface = (*Reconciler)(nil)
// ReconcileKind implements Interface.ReconcileKind.
func (c *Reconciler) ReconcileKind(ctx context.Context, service *v1.Service) pkgreconciler.Event {
ctx, cancel := context.WithTimeout(ctx, pkgreconciler.DefaultTimeout)
defer cancel()
logger := logging.FromContext(ctx)
config, err := c.config(ctx, service)
if err != nil {
return err
}
if config.Generation != config.Status.ObservedGeneration {
// The Configuration hasn't yet reconciled our latest changes to
// its desired state, so its conditions are outdated.
service.Status.MarkConfigurationNotReconciled()
// If BYO-Revision name is used we must serialize reconciling the Configuration
// and Route. Wait for observed generation to match before continuing.
if config.Spec.GetTemplate().Name != "" {
return nil
}
} else {
logger.Debugf("Configuration Conditions = %#v", config.Status.Conditions)
// Update our Status based on the state of our underlying Configuration.
service.Status.PropagateConfigurationStatus(&config.Status)
}
// When the Configuration names a Revision, check that the named Revision is owned
// by our Configuration and matches its generation before reprogramming the Route,
// otherwise a bad patch could lead to folks inadvertently routing traffic to a
// pre-existing Revision (possibly for another Configuration).
if err := CheckNameAvailability(config, c.revisionLister); err != nil &&
!apierrs.IsNotFound(err) {
service.Status.MarkRevisionNameTaken(config.Spec.GetTemplate().Name)
return nil
}
route, err := c.route(ctx, service)
if err != nil {
return err
}
// Update our Status based on the state of our underlying Route.
ss := &service.Status
if route.Generation != route.Status.ObservedGeneration {
// The Route hasn't yet reconciled our latest changes to
// its desired state, so its conditions are outdated.
ss.MarkRouteNotReconciled()
} else {
// Update our Status based on the state of our underlying Route.
ss.PropagateRouteStatus(&route.Status)
}
c.checkRoutesNotReady(config, logger, route, service)
return nil
}
func (c *Reconciler) config(ctx context.Context, service *v1.Service) (*v1.Configuration, error) {
recorder := controller.GetEventRecorder(ctx)
configName := resourcenames.Configuration(service)
config, err := c.configurationLister.Configurations(service.Namespace).Get(configName)
if apierrs.IsNotFound(err) {
config, err = c.createConfiguration(ctx, service)
if err != nil {
recorder.Eventf(service, corev1.EventTypeWarning, "CreationFailed", "Failed to create Configuration %q: %v", configName, err)
return nil, fmt.Errorf("failed to create Configuration: %w", err)
}
recorder.Eventf(service, corev1.EventTypeNormal, "Created", "Created Configuration %q", configName)
} else if err != nil {
return nil, fmt.Errorf("failed to get Configuration: %w", err)
} else if !metav1.IsControlledBy(config, service) {
// Surface an error in the service's status,and return an error.
service.Status.MarkConfigurationNotOwned(configName)
return nil, fmt.Errorf("service: %q does not own configuration: %q", service.Name, configName)
} else if config, err = c.reconcileConfiguration(ctx, service, config); err != nil {
return nil, fmt.Errorf("failed to reconcile Configuration: %w", err)
}
return config, nil
}
func (c *Reconciler) route(ctx context.Context, service *v1.Service) (*v1.Route, error) {
recorder := controller.GetEventRecorder(ctx)
routeName := resourcenames.Route(service)
route, err := c.routeLister.Routes(service.Namespace).Get(routeName)
if apierrs.IsNotFound(err) {
route, err = c.createRoute(ctx, service)
if err != nil {
recorder.Eventf(service, corev1.EventTypeWarning, "CreationFailed", "Failed to create Route %q: %v", routeName, err)
return nil, fmt.Errorf("failed to create Route: %w", err)
}
recorder.Eventf(service, corev1.EventTypeNormal, "Created", "Created Route %q", routeName)
} else if err != nil {
return nil, fmt.Errorf("failed to get Route: %w", err)
} else if !metav1.IsControlledBy(route, service) {
// Surface an error in the service's status, and return an error.
service.Status.MarkRouteNotOwned(routeName)
return nil, fmt.Errorf("service: %q does not own route: %q", service.Name, routeName)
} else if route, err = c.reconcileRoute(ctx, service, route); err != nil {
return nil, fmt.Errorf("failed to reconcile Route: %w", err)
}
return route, nil
}
func (c *Reconciler) checkRoutesNotReady(config *v1.Configuration, logger *zap.SugaredLogger, route *v1.Route, service *v1.Service) {
// `manual` is not reconciled.
rc := service.Status.GetCondition(v1.ServiceConditionRoutesReady)
if rc == nil || rc.Status != corev1.ConditionTrue {
return
}
if len(route.Spec.Traffic) != len(route.Status.Traffic) {
service.Status.MarkRouteNotYetReady()
return
}
want, got := route.Spec.DeepCopy().Traffic, route.Status.DeepCopy().Traffic
// Replace `configuration` target with its latest ready revision.
for idx := range want {
if want[idx].ConfigurationName == config.Name {
want[idx].RevisionName = config.Status.LatestReadyRevisionName
want[idx].ConfigurationName = ""
}
}
ignoreFields := cmpopts.IgnoreFields(v1.TrafficTarget{}, "URL", "LatestRevision")
if diff, err := kmp.SafeDiff(got, want, ignoreFields); err != nil || diff != "" {
logger.Errorf("Route %s is not yet what we want: %s", route.Name, diff)
service.Status.MarkRouteNotYetReady()
}
}
func (c *Reconciler) createConfiguration(ctx context.Context, service *v1.Service) (*v1.Configuration, error) {
return c.client.ServingV1().Configurations(service.Namespace).Create(
ctx, resources.MakeConfiguration(service), metav1.CreateOptions{})
}
func configSemanticEquals(ctx context.Context, desiredConfig, config *v1.Configuration) (bool, error) {
logger := logging.FromContext(ctx)
specDiff, err := kmp.SafeDiff(desiredConfig.Spec, config.Spec)
if err != nil {
logger.Warnw("Error diffing config spec", zap.Error(err))
return false, fmt.Errorf("failed to diff Configuration: %w", err)
} else if specDiff != "" {
logger.Info("Reconciling configuration diff (-desired, +observed):\n", specDiff)
}
return equality.Semantic.DeepEqual(desiredConfig.Spec, config.Spec) &&
equality.Semantic.DeepEqual(desiredConfig.Labels, config.Labels) &&
equality.Semantic.DeepEqual(desiredConfig.Annotations, config.Annotations) &&
specDiff == "", nil
}
func (c *Reconciler) reconcileConfiguration(ctx context.Context, service *v1.Service, config *v1.Configuration) (*v1.Configuration, error) {
existing := config.DeepCopy()
// In the case of an upgrade, there can be default values set that don't exist pre-upgrade.
// We are setting the up-to-date default values here so an update won't be triggered if the only
// diff is the new default values.
existing.SetDefaults(ctx)
desiredConfig := resources.MakeConfigurationFromExisting(service, existing)
equals, err := configSemanticEquals(ctx, desiredConfig, existing)
if err != nil {
return nil, err
}
if equals {
return config, nil
}
logger := logging.FromContext(ctx)
logger.Warnf("Service-delegated Configuration %q diff found. Clobbering.", existing.Name)
// Preserve the rest of the object (e.g. ObjectMeta except for labels).
existing.Spec = desiredConfig.Spec
existing.Labels = desiredConfig.Labels
existing.Annotations = desiredConfig.Annotations
return c.client.ServingV1().Configurations(service.Namespace).Update(ctx, existing, metav1.UpdateOptions{})
}
func (c *Reconciler) createRoute(ctx context.Context, service *v1.Service) (*v1.Route, error) {
return c.client.ServingV1().Routes(service.Namespace).Create(
ctx, resources.MakeRoute(service), metav1.CreateOptions{})
}
func routeSemanticEquals(ctx context.Context, desiredRoute, route *v1.Route) (bool, error) {
logger := logging.FromContext(ctx)
specDiff, err := kmp.SafeDiff(desiredRoute.Spec, route.Spec)
if err != nil {
logger.Errorw("Error diffing route spec", zap.Error(err))
return false, fmt.Errorf("failed to diff Route: %w", err)
} else if specDiff != "" {
logger.Info("Reconciling route diff (-desired, +observed):\n", specDiff)
}
return equality.Semantic.DeepEqual(desiredRoute.Spec, route.Spec) &&
equality.Semantic.DeepEqual(desiredRoute.Labels, route.Labels) &&
equality.Semantic.DeepEqual(desiredRoute.Annotations, route.Annotations) &&
specDiff == "", nil
}
func (c *Reconciler) reconcileRoute(ctx context.Context, service *v1.Service, route *v1.Route) (*v1.Route, error) {
existing := route.DeepCopy()
// In the case of an upgrade, there can be default values set that don't exist pre-upgrade.
// We are setting the up-to-date default values here so an update won't be triggered if the only
// diff is the new default values.
existing.SetDefaults(ctx)
desiredRoute := resources.MakeRoute(service)
equals, err := routeSemanticEquals(ctx, desiredRoute, existing)
if err != nil {
return nil, err
}
if equals {
return route, nil
}
// Preserve the rest of the object (e.g. ObjectMeta except for labels and annotations).
existing.Spec = desiredRoute.Spec
existing.Labels = desiredRoute.Labels
existing.Annotations = desiredRoute.Annotations
return c.client.ServingV1().Routes(service.Namespace).Update(ctx, existing, metav1.UpdateOptions{})
}
// CheckNameAvailability checks that if the named Revision specified by the Configuration
// is available (not found), exists (but matches), or exists with conflict (doesn't match).
//
// TODO(dprotaso) de-dupe once this controller is migrated to v1 apis
func CheckNameAvailability(config *v1.Configuration, lister listers.RevisionLister) error {
// If config.Spec.GetTemplate().Name is set, then we can directly look up
// the revision by name.
name := config.Spec.GetTemplate().Name
if name == "" {
return nil
}
errConflict := apierrs.NewAlreadyExists(v1.Resource("revisions"), name)
rev, err := lister.Revisions(config.Namespace).Get(name)
if err != nil {
return err
}
if !metav1.IsControlledBy(rev, config) {
// If the revision isn't controller by this configuration, then
// do not use it.
return errConflict
}
// Check the generation on this revision.
generationKey := serving.ConfigurationGenerationLabelKey
expectedValue := configresources.RevisionLabelValueForKey(generationKey, config)
if rev.Labels != nil && rev.Labels[generationKey] == expectedValue {
return nil
}
// We only require spec equality because the rest is immutable and the user may have
// annotated or labeled the Revision (beyond what the Configuration might have).
if !equality.Semantic.DeepEqual(config.Spec.GetTemplate().Spec, rev.Spec) {
return errConflict
}
return nil
}