This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
forked from knative/serving
/
service.go
280 lines (240 loc) · 10.4 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
/*
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"
"reflect"
"github.com/google/go-cmp/cmp"
"github.com/knative/pkg/controller"
"github.com/knative/pkg/logging"
resourcenames "github.com/knative/serving/pkg/reconciler/v1alpha1/service/resources/names"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/cache"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
servinginformers "github.com/knative/serving/pkg/client/informers/externalversions/serving/v1alpha1"
listers "github.com/knative/serving/pkg/client/listers/serving/v1alpha1"
"github.com/knative/serving/pkg/reconciler"
"github.com/knative/serving/pkg/reconciler/v1alpha1/service/resources"
)
const controllerAgentName = "service-controller"
// Reconciler implements controller.Reconciler for Service resources.
type Reconciler struct {
*reconciler.Base
// listers index properties about resources
serviceLister listers.ServiceLister
configurationLister listers.ConfigurationLister
routeLister listers.RouteLister
}
// 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
func NewController(
opt reconciler.Options,
serviceInformer servinginformers.ServiceInformer,
configurationInformer servinginformers.ConfigurationInformer,
routeInformer servinginformers.RouteInformer,
) *controller.Impl {
c := &Reconciler{
Base: reconciler.NewBase(opt, controllerAgentName),
serviceLister: serviceInformer.Lister(),
configurationLister: configurationInformer.Lister(),
routeLister: routeInformer.Lister(),
}
impl := controller.NewImpl(c, c.Logger, "Services", reconciler.MustNewStatsReporter("Services", c.Logger))
c.Logger.Info("Setting up event handlers")
serviceInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: impl.Enqueue,
UpdateFunc: controller.PassNew(impl.Enqueue),
DeleteFunc: impl.Enqueue,
})
configurationInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: controller.Filter(v1alpha1.SchemeGroupVersion.WithKind("Service")),
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: impl.EnqueueControllerOf,
UpdateFunc: controller.PassNew(impl.EnqueueControllerOf),
},
})
routeInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: controller.Filter(v1alpha1.SchemeGroupVersion.WithKind("Service")),
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: impl.EnqueueControllerOf,
UpdateFunc: controller.PassNew(impl.EnqueueControllerOf),
},
})
return impl
}
// Reconcile compares the actual state with the desired, and attempts to
// converge the two. It then updates the Status block of the Service 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)
// Get the Service resource with this namespace/name
original, err := c.serviceLister.Services(namespace).Get(name)
if apierrs.IsNotFound(err) {
// The resource may no longer exist, in which case we stop processing.
logger.Errorf("service %q in work queue no longer exists", key)
return nil
} else if err != nil {
return err
}
// Don't modify the informers copy
service := original.DeepCopy()
if service.Spec.Manual != nil {
// We do not know the status when in manual mode. The Route can be
// updated with Configurations not known to the Service which would
// make attempts to display status potentially incorrect
service.Status.SetManualStatus()
} else {
// Reconcile this copy of the service and then write back any status
// updates regardless of whether the reconciliation errored out.
err = c.reconcile(ctx, service)
}
if equality.Semantic.DeepEqual(original.Status, service.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(service); err != nil {
logger.Warn("Failed to update service status", zap.Error(err))
return err
}
return err
}
func (c *Reconciler) reconcile(ctx context.Context, service *v1alpha1.Service) error {
logger := logging.FromContext(ctx)
service.Status.InitializeConditions()
configName := resourcenames.Configuration(service)
config, err := c.configurationLister.Configurations(service.Namespace).Get(configName)
if errors.IsNotFound(err) {
config, err = c.createConfiguration(service)
if err != nil {
logger.Errorf("Failed to create Configuration %q: %v", configName, err)
c.Recorder.Eventf(service, corev1.EventTypeWarning, "CreationFailed", "Failed to create Configuration %q: %v", configName, err)
return err
}
} else if err != nil {
logger.Errorf("Failed to reconcile Service: %q failed to Get Configuration: %q; %v", service.Name, configName, zap.Error(err))
return err
} else if config, err = c.reconcileConfiguration(ctx, service, config); err != nil {
logger.Errorf("Failed to reconcile Service: %q failed to reconcile Configuration: %q; %v", service.Name, configName, zap.Error(err))
return err
}
// Update our Status based on the state of our underlying Configuration.
service.Status.PropagateConfigurationStatus(config.Status)
routeName := resourcenames.Route(service)
route, err := c.routeLister.Routes(service.Namespace).Get(routeName)
if errors.IsNotFound(err) {
route, err = c.createRoute(service)
if err != nil {
logger.Errorf("Failed to create Route %q: %v", routeName, err)
c.Recorder.Eventf(service, corev1.EventTypeWarning, "CreationFailed", "Failed to create Route %q: %v", routeName, err)
return err
}
} else if err != nil {
logger.Errorf("Failed to reconcile Service: %q failed to Get Route: %q", service.Name, routeName)
return err
} else if route, err = c.reconcileRoute(ctx, service, route); err != nil {
logger.Errorf("Failed to reconcile Service: %q failed to reconcile Route: %q", service.Name, routeName)
return err
}
// Update our Status based on the state of our underlying Route.
service.Status.PropagateRouteStatus(route.Status)
// Update the Status of the Service with the latest generation that
// we just reconciled against so we don't keep generating Revisions.
// TODO(#642): Remove this.
service.Status.ObservedGeneration = service.Spec.Generation
return nil
}
func (c *Reconciler) updateStatus(desired *v1alpha1.Service) (*v1alpha1.Service, error) {
service, err := c.serviceLister.Services(desired.Namespace).Get(desired.Name)
if err != nil {
return nil, err
}
// Check if there is anything to update.
if !reflect.DeepEqual(service.Status, desired.Status) {
// Don't modify the informers copy
existing := service.DeepCopy()
existing.Status = desired.Status
serviceClient := c.ServingClientSet.ServingV1alpha1().Services(desired.Namespace)
// TODO: for CRD there's no updatestatus, so use normal update.
return serviceClient.Update(existing)
}
return service, nil
}
func (c *Reconciler) createConfiguration(service *v1alpha1.Service) (*v1alpha1.Configuration, error) {
cfg, err := resources.MakeConfiguration(service)
if err != nil {
return nil, err
}
return c.ServingClientSet.ServingV1alpha1().Configurations(service.Namespace).Create(cfg)
}
func (c *Reconciler) reconcileConfiguration(ctx context.Context, service *v1alpha1.Service, config *v1alpha1.Configuration) (*v1alpha1.Configuration, error) {
logger := logging.FromContext(ctx)
desiredConfig, err := resources.MakeConfiguration(service)
if err != nil {
return nil, err
}
// TODO(#642): Remove this (needed to avoid continuous updates)
desiredConfig.Spec.Generation = config.Spec.Generation
if equality.Semantic.DeepEqual(desiredConfig.Spec, config.Spec) {
// No differences to reconcile.
return config, nil
}
logger.Infof("Reconciling configuration diff (-desired, +observed): %v", cmp.Diff(desiredConfig.Spec, config.Spec))
// Don't modify the informers copy
existing := config.DeepCopy()
// Preserve the rest of the object (e.g. ObjectMeta)
existing.Spec = desiredConfig.Spec
return c.ServingClientSet.ServingV1alpha1().Configurations(service.Namespace).Update(existing)
}
func (c *Reconciler) createRoute(service *v1alpha1.Service) (*v1alpha1.Route, error) {
route, err := resources.MakeRoute(service)
if err != nil {
// This should be unreachable as configuration creation
// happens first in reconcile()
return nil, err
}
return c.ServingClientSet.ServingV1alpha1().Routes(service.Namespace).Create(route)
}
func (c *Reconciler) reconcileRoute(ctx context.Context, service *v1alpha1.Service, route *v1alpha1.Route) (*v1alpha1.Route, error) {
logger := logging.FromContext(ctx)
desiredRoute, err := resources.MakeRoute(service)
if err != nil {
// This should be unreachable as configuration creation
// happens first in reconcile()
return nil, err
}
// TODO(#642): Remove this (needed to avoid continuous updates)
desiredRoute.Spec.Generation = route.Spec.Generation
if equality.Semantic.DeepEqual(desiredRoute.Spec, route.Spec) {
// No differences to reconcile.
return route, nil
}
logger.Infof("Reconciling route diff (-desired, +observed): %v", cmp.Diff(desiredRoute.Spec, route.Spec))
// Don't modify the informers copy
existing := route.DeepCopy()
// Preserve the rest of the object (e.g. ObjectMeta)
existing.Spec = desiredRoute.Spec
return c.ServingClientSet.ServingV1alpha1().Routes(service.Namespace).Update(existing)
}