/
containersource.go
324 lines (274 loc) · 11.6 KB
/
containersource.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
/*
Copyright 2019 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 containersource
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"time"
"knative.dev/pkg/resolver"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
appsv1listers "k8s.io/client-go/listers/apps/v1"
"k8s.io/client-go/tools/cache"
"go.uber.org/zap"
"knative.dev/pkg/controller"
status "knative.dev/eventing/pkg/apis/duck"
"knative.dev/eventing/pkg/apis/sources/v1alpha1"
listers "knative.dev/eventing/pkg/client/listers/sources/v1alpha1"
"knative.dev/eventing/pkg/logging"
"knative.dev/eventing/pkg/reconciler"
"knative.dev/eventing/pkg/reconciler/containersource/resources"
)
const (
// Name of the corev1.Events emitted from the reconciliation process
sourceReconciled = "ContainerSourceReconciled"
sourceReadinessChanged = "ContainerSourceReadinessChanged"
sourceUpdateStatusFailed = "ContainerSourceUpdateStatusFailed"
)
type Reconciler struct {
*reconciler.Base
// listers index properties about resources
containerSourceLister listers.ContainerSourceLister
deploymentLister appsv1listers.DeploymentLister
sinkResolver *resolver.URIResolver
}
// Check that our Reconciler implements controller.Reconciler
var _ controller.Reconciler = (*Reconciler)(nil)
// Reconcile compares the actual state with the desired, and attempts to
// converge the two. It then updates the Status block of the CronJobSource
// resource with the current status of the resource.
func (r *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 {
r.Logger.Errorf("invalid resource key: %s", key)
return nil
}
// Get the CronJobSource resource with this namespace/name
original, err := r.containerSourceLister.ContainerSources(namespace).Get(name)
if apierrors.IsNotFound(err) {
// The resource may no longer exist, in which case we stop processing.
logging.FromContext(ctx).Error("ContainerSource key in work queue no longer exists", zap.Any("key", key))
return nil
} else if err != nil {
return err
}
// Don't modify the informers copy
source := original.DeepCopy()
// Reconcile this copy of the ContainerSource and then write back any status
// updates regardless of whether the reconcile error out.
err = r.reconcile(ctx, source)
if err != nil {
logging.FromContext(ctx).Warn("Error reconciling ContainerSource", zap.Error(err))
} else {
logging.FromContext(ctx).Debug("ContainerSource reconciled")
r.Recorder.Eventf(source, corev1.EventTypeNormal, sourceReconciled, `ContainerSource reconciled: "%s/%s"`, source.Namespace, source.Name)
}
if _, updateStatusErr := r.updateStatus(ctx, source.DeepCopy()); updateStatusErr != nil {
logging.FromContext(ctx).Warn("Failed to update the ContainerSource", zap.Error(err))
r.Recorder.Eventf(source, corev1.EventTypeWarning, sourceUpdateStatusFailed, "Failed to update ContainerSource's status: %v", err)
return updateStatusErr
}
// Requeue if the resource is not ready:
return err
}
func (r *Reconciler) reconcile(ctx context.Context, source *v1alpha1.ContainerSource) error {
// No need to reconcile if the source has been marked for deletion.
if source.DeletionTimestamp != nil {
return nil
}
source.Status.ObservedGeneration = source.Generation
source.Status.InitializeConditions()
annotations := make(map[string]string)
// Then wire through any annotations / labels from the Source
if source.ObjectMeta.Annotations != nil {
for k, v := range source.ObjectMeta.Annotations {
annotations[k] = v
}
}
labels := make(map[string]string)
if source.ObjectMeta.Labels != nil {
for k, v := range source.ObjectMeta.Labels {
labels[k] = v
}
}
args := resources.ContainerArguments{
Source: source,
Name: source.Name,
Namespace: source.Namespace,
Template: source.Spec.Template,
Image: source.Spec.DeprecatedImage,
Args: source.Spec.DeprecatedArgs,
Env: source.Spec.DeprecatedEnv,
ServiceAccountName: source.Spec.DeprecatedServiceAccountName,
Annotations: annotations,
Labels: labels,
}
err := r.setSinkURIArg(ctx, source, &args)
if err != nil {
r.Recorder.Eventf(source, corev1.EventTypeWarning, "SetSinkURIFailed", "Failed to set Sink URI: %v", err)
return err
}
ra, err := r.reconcileReceiveAdapter(ctx, source, args)
if err != nil {
return fmt.Errorf("reconciling receive adapter: %v", err)
}
if status.DeploymentIsAvailable(&ra.Status, false) {
source.Status.MarkDeployed()
r.Recorder.Eventf(source, corev1.EventTypeNormal, "DeploymentReady", "Deployment %q has %d ready replicas", ra.Name, ra.Status.ReadyReplicas)
}
return nil
}
// setSinkURIArg attempts to get the sink URI from the sink reference and
// set it in the source status. On failure, the source's Sink condition is
// updated to reflect the error.
// If an error is returned from this function, the caller should also record
// an Event containing the error string.
func (r *Reconciler) setSinkURIArg(ctx context.Context, source *v1alpha1.ContainerSource, args *resources.ContainerArguments) error {
if uri, ok := sinkArg(source); ok {
source.Status.MarkSink(uri)
return nil
}
if source.Spec.Sink == nil {
source.Status.MarkNoSink("Missing", "Sink missing from spec")
return errors.New("sink missing from spec")
}
dest := source.Spec.Sink.DeepCopy()
if dest.Ref != nil {
// To call URIFromDestination(), dest.Ref must have a Namespace. If there is
// no Namespace defined in dest.Ref, we will use the Namespace of the source
// as the Namespace of dest.Ref.
if dest.Ref.Namespace == "" {
//TODO how does this work with deprecated fields
dest.Ref.Namespace = source.GetNamespace()
}
} else if dest.DeprecatedName != "" && dest.DeprecatedNamespace == "" {
// If Ref is nil and the deprecated ref is present, we need to check for
// DeprecatedNamespace. This can be removed when DeprecatedNamespace is
// removed.
dest.DeprecatedNamespace = source.GetNamespace()
}
sinkURI, err := r.sinkResolver.URIFromDestination(*dest, source)
if err != nil {
source.Status.MarkNoSink("NotFound", `Couldn't get Sink URI from "%s/%s"`, dest.Ref.Namespace, dest.Ref.Name)
return fmt.Errorf("getting sink URI: %v", err)
}
if source.Spec.Sink.DeprecatedAPIVersion != "" &&
source.Spec.Sink.DeprecatedKind != "" &&
source.Spec.Sink.DeprecatedName != "" {
source.Status.MarkSinkWarnRefDeprecated(sinkURI)
} else {
source.Status.MarkSink(sinkURI)
}
args.Sink = sinkURI
return nil
}
func sinkArg(source *v1alpha1.ContainerSource) (string, bool) {
var args []string
if source.Spec.Template != nil {
for _, c := range source.Spec.Template.Spec.Containers {
args = append(args, c.Args...)
}
}
args = append(args, source.Spec.DeprecatedArgs...)
for _, a := range args {
if strings.HasPrefix(a, "--sink=") {
return strings.Replace(a, "--sink=", "", -1), true
}
}
return "", false
}
func (r *Reconciler) reconcileReceiveAdapter(ctx context.Context, src *v1alpha1.ContainerSource, args resources.ContainerArguments) (*appsv1.Deployment, error) {
expected := resources.MakeDeployment(args)
ra, err := r.KubeClientSet.AppsV1().Deployments(src.Namespace).Get(expected.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
ra, err = r.KubeClientSet.AppsV1().Deployments(src.Namespace).Create(expected)
if err != nil {
r.markNotDeployedRecordEvent(src, corev1.EventTypeWarning, "DeploymentCreateFailed", "Could not create deployment: %v", err)
return nil, fmt.Errorf("creating new deployment: %v", err)
}
r.markDeployingAndRecordEvent(src, corev1.EventTypeNormal, "DeploymentCreated", "Created deployment %q", ra.Name)
return ra, nil
} else if err != nil {
r.markDeployingAndRecordEvent(src, corev1.EventTypeWarning, "DeploymentGetFailed", "Error getting deployment: %v", err)
return nil, fmt.Errorf("getting deployment: %v", err)
} else if !metav1.IsControlledBy(ra, src) {
r.markDeployingAndRecordEvent(src, corev1.EventTypeWarning, "DeploymentNotOwned", "Deployment %q is not owned by this ContainerSource", ra.Name)
return nil, fmt.Errorf("deployment %q is not owned by ContainerSource %q", ra.Name, src.Name)
} else if r.podSpecChanged(ra.Spec.Template.Spec, expected.Spec.Template.Spec) {
ra.Spec.Template.Spec = expected.Spec.Template.Spec
ra, err = r.KubeClientSet.AppsV1().Deployments(src.Namespace).Update(ra)
if err != nil {
return ra, fmt.Errorf("updating deployment: %v", err)
}
return ra, nil
} else {
logging.FromContext(ctx).Debug("Reusing existing receive adapter", zap.Any("receiveAdapter", ra))
}
return ra, nil
}
func (r *Reconciler) podSpecChanged(oldPodSpec corev1.PodSpec, newPodSpec corev1.PodSpec) bool {
// Since the Deployment spec has fields defaulted by the webhook, it won't
// be equal to expected. Use DeepDerivative to compare only the fields that
// are set in newPodSpec.
if !equality.Semantic.DeepDerivative(newPodSpec, oldPodSpec) {
return true
}
if len(oldPodSpec.Containers) != len(newPodSpec.Containers) {
return true
}
for i := range newPodSpec.Containers {
if !equality.Semantic.DeepEqual(newPodSpec.Containers[i].Env, oldPodSpec.Containers[i].Env) {
return true
}
}
return false
}
func (r *Reconciler) markDeployingAndRecordEvent(source *v1alpha1.ContainerSource, evType string, reason string, messageFmt string, args ...interface{}) {
r.Recorder.Eventf(source, evType, reason, messageFmt, args...)
source.Status.MarkDeploying(reason, messageFmt, args...)
}
func (r *Reconciler) markNotDeployedRecordEvent(source *v1alpha1.ContainerSource, evType string, reason string, messageFmt string, args ...interface{}) {
r.Recorder.Eventf(source, evType, reason, messageFmt, args...)
source.Status.MarkNotDeployed(reason, messageFmt, args...)
}
func (r *Reconciler) updateStatus(ctx context.Context, desired *v1alpha1.ContainerSource) (*v1alpha1.ContainerSource, error) {
source, err := r.containerSourceLister.ContainerSources(desired.Namespace).Get(desired.Name)
if err != nil {
return nil, err
}
// If there's nothing to update, just return.
if reflect.DeepEqual(source.Status, desired.Status) {
return source, nil
}
becomesReady := desired.Status.IsReady() && !source.Status.IsReady()
// Don't modify the informers copy.
existing := source.DeepCopy()
existing.Status = desired.Status
cj, err := r.EventingClientSet.SourcesV1alpha1().ContainerSources(desired.Namespace).UpdateStatus(existing)
if err == nil && becomesReady {
duration := time.Since(cj.ObjectMeta.CreationTimestamp.Time)
r.Logger.Infof("ContainerSource %q became ready after %v", source.Name, duration)
r.Recorder.Event(source, corev1.EventTypeNormal, sourceReadinessChanged, fmt.Sprintf("ContainerSource %q became ready", source.Name))
if reportErr := r.StatsReporter.ReportReady("ContainerSource", source.Namespace, source.Name, duration); reportErr != nil {
logging.FromContext(ctx).Sugar().Infof("failed to record ready for ContainerSource, %v", reportErr)
}
}
return cj, err
}