This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/
topic.go
492 lines (422 loc) · 16.1 KB
/
topic.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
Copyright 2019 Google LLC
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 topic
import (
"context"
"encoding/json"
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/cache"
"github.com/google/go-cmp/cmp"
"go.uber.org/zap"
"knative.dev/pkg/apis"
"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
servingv1beta1 "knative.dev/serving/pkg/apis/serving/v1beta1"
servingv1alpha1listers "knative.dev/serving/pkg/client/listers/serving/v1alpha1"
servingv1beta1listers "knative.dev/serving/pkg/client/listers/serving/v1beta1"
"github.com/google/knative-gcp/pkg/apis/pubsub/v1alpha1"
listers "github.com/google/knative-gcp/pkg/client/listers/pubsub/v1alpha1"
"github.com/google/knative-gcp/pkg/reconciler/pubsub"
"github.com/google/knative-gcp/pkg/reconciler/topic/resources"
)
const (
// ReconcilerName is the name of the reconciler
ReconcilerName = "Topics"
finalizerName = controllerAgentName
)
// Reconciler implements controller.Reconciler for Topic resources.
type Reconciler struct {
*pubsub.PubSubBase
// listers index properties about resources
topicLister listers.TopicLister
servingVersion string
serviceV1alpha1Lister servingv1alpha1listers.ServiceLister
serviceV1beta1Lister servingv1beta1listers.ServiceLister
publisherImage string
}
// 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 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 Topic resource with this namespace/name
original, err := c.topicLister.Topics(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
Topic := original.DeepCopy()
// Reconcile this copy of the Topic and then write back any status
// updates regardless of whether the reconciliation errored out.
var reconcileErr = c.reconcile(ctx, Topic)
// If no error is returned, mark the observed generation.
if reconcileErr == nil {
Topic.Status.ObservedGeneration = Topic.Generation
}
if equality.Semantic.DeepEqual(original.Finalizers, Topic.Finalizers) {
// If we didn't change finalizers then don't call updateFinalizers.
} else if _, updated, fErr := c.updateFinalizers(ctx, Topic); fErr != nil {
logger.Warnw("Failed to update Topic finalizers", zap.Error(fErr))
c.Recorder.Eventf(Topic, corev1.EventTypeWarning, "UpdateFailed",
"Failed to update finalizers for Topic %q: %v", Topic.Name, fErr)
return fErr
} else if updated {
// There was a difference and updateFinalizers said it updated and did not return an error.
c.Recorder.Eventf(Topic, corev1.EventTypeNormal, "Updated", "Updated Topic %q finalizers", Topic.GetName())
}
if equality.Semantic.DeepEqual(original.Status, Topic.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 _, uErr := c.updateStatus(ctx, Topic); uErr != nil {
logger.Warnw("Failed to update Topic status", zap.Error(uErr))
c.Recorder.Eventf(Topic, corev1.EventTypeWarning, "UpdateFailed",
"Failed to update status for Topic %q: %v", Topic.Name, uErr)
return uErr
} else if reconcileErr == nil {
// There was a difference and updateStatus did not return an error.
c.Recorder.Eventf(Topic, corev1.EventTypeNormal, "Updated", "Updated Topic %q", Topic.GetName())
}
if reconcileErr != nil {
c.Recorder.Event(Topic, corev1.EventTypeWarning, "InternalError", reconcileErr.Error())
}
return reconcileErr
}
func (c *Reconciler) reconcile(ctx context.Context, topic *v1alpha1.Topic) error {
logger := logging.FromContext(ctx)
topic.Status.InitializeConditions()
if topic.GetDeletionTimestamp() != nil {
logger.Debug("Topic Deleting.", zap.Any("propagationPolicy", topic.Spec.PropagationPolicy))
if topic.Spec.PropagationPolicy == v1alpha1.TopicPolicyCreateDelete {
// Ensure the Topic is deleted.
state, err := c.EnsureTopicDeleted(ctx, topic, *topic.Spec.Secret, topic.Spec.Project, topic.Status.TopicID)
switch state {
case pubsub.OpsJobGetFailed:
logger.Error("Failed to get Topic ops job.", zap.Any("state", state), zap.Error(err))
return err
case pubsub.OpsJobCreated:
// If we created a job to delete a topic, update the status.
topic.Status.MarkTopicOperating(
"Deleting",
"Created Job to delete topic %q.",
topic.Status.TopicID)
return nil
case pubsub.OpsJobCompleteSuccessful:
topic.Status.MarkNoTopic("Deleted", "Successfully deleted topic %q.", topic.Status.TopicID)
topic.Status.TopicID = ""
removeFinalizer(topic)
case pubsub.OpsJobCreateFailed, pubsub.OpsJobCompleteFailed:
logger.Error("Failed to delete topic.", zap.Any("state", state), zap.Error(err))
msg := "unknown"
if err != nil {
msg = err.Error()
}
topic.Status.MarkNoTopic(
"DeleteFailed",
"Failed to delete topic: %q.",
msg)
return err
}
} else {
removeFinalizer(topic)
}
return nil
}
// Set the topic being used.
topic.Status.TopicID = topic.Spec.Topic
switch topic.Spec.PropagationPolicy {
case v1alpha1.TopicPolicyCreateDelete, v1alpha1.TopicPolicyCreateNoDelete:
state, err := c.EnsureTopicCreated(ctx, topic, *topic.Spec.Secret, topic.Spec.Project, topic.Status.TopicID)
// Check state.
switch state {
case pubsub.OpsJobGetFailed:
logger.Error("Failed to get topic ops job.",
zap.Any("propagationPolicy", topic.Spec.PropagationPolicy),
zap.Any("state", state),
zap.Error(err))
return err
case pubsub.OpsJobCreated:
// If we created a job to make a topic, then add the finalizer and update the status.
addFinalizer(topic)
topic.Status.MarkTopicOperating("Creating",
"Created Job to create topic %q.",
topic.Status.TopicID)
return nil
case pubsub.OpsJobCompleteSuccessful:
topic.Status.MarkTopicReady()
case pubsub.OpsJobCreateFailed, pubsub.OpsJobCompleteFailed:
logger.Error("Failed to create topic.",
zap.Any("propagationPolicy", topic.Spec.PropagationPolicy),
zap.Any("state", state),
zap.Error(err))
msg := "unknown"
if err != nil {
msg = err.Error()
}
topic.Status.MarkNoTopic(
"CreateFailed",
"Failed to create Topic: %q",
msg)
return err
}
case v1alpha1.TopicPolicyNoCreateNoDelete:
state, err := c.EnsureTopicExists(ctx, topic, *topic.Spec.Secret, topic.Spec.Project, topic.Status.TopicID)
// Check state.
switch state {
case pubsub.OpsJobGetFailed:
logger.Error("Failed to get topic ops job.",
zap.Any("propagationPolicy", topic.Spec.PropagationPolicy),
zap.Any("state", state),
zap.Error(err))
return err
case pubsub.OpsJobCreated:
// If we created a job to verify a topic, then update the status.
topic.Status.MarkTopicOperating("Verifying",
"Created Job to verify topic %q.",
topic.Status.TopicID)
return nil
case pubsub.OpsJobCompleteSuccessful:
topic.Status.MarkTopicReady()
case pubsub.OpsJobCreateFailed, pubsub.OpsJobCompleteFailed:
logger.Error("Failed to verify topic.",
zap.Any("propagationPolicy", topic.Spec.PropagationPolicy),
zap.Any("state", state),
zap.Error(err))
msg := "unknown"
if err != nil {
msg = err.Error()
}
topic.Status.MarkNoTopic(
"VerifyFailed",
"Failed to verify Topic: %q",
msg)
return err
}
default:
logger.Error("Unknown propagation policy.", zap.Any("propagationPolicy", topic.Spec.PropagationPolicy))
return nil
}
switch c.servingVersion {
case "", "v1alpha1":
if err := c.createOrUpdatePublisherV1alpha1(ctx, topic); err != nil {
logger.Error("Unable to create the publisher@v1alpha1", zap.Error(err))
return err
}
case "v1beta1":
if err := c.createOrUpdatePublisherV1beta1(ctx, topic); err != nil {
logger.Error("Unable to create the publisher@v1beta1", zap.Error(err))
return err
}
default:
logger.Error("unknown serving version selected: %q", c.servingVersion)
}
return nil
}
func (c *Reconciler) updateStatus(ctx context.Context, desired *v1alpha1.Topic) (*v1alpha1.Topic, error) {
topic, err := c.topicLister.Topics(desired.Namespace).Get(desired.Name)
if err != nil {
return nil, err
}
// If there's nothing to update, just return.
if equality.Semantic.DeepEqual(topic.Status, desired.Status) {
return topic, nil
}
becomesReady := desired.Status.IsReady() && !topic.Status.IsReady()
// Don't modify the informers copy.
existing := topic.DeepCopy()
existing.Status = desired.Status
ch, err := c.RunClientSet.PubsubV1alpha1().Topics(desired.Namespace).UpdateStatus(existing)
if err == nil && becomesReady {
duration := time.Since(ch.ObjectMeta.CreationTimestamp.Time)
c.Logger.Infof("Topic %q became ready after %v", topic.Name, duration)
if err := c.StatsReporter.ReportReady("Topic", topic.Namespace, topic.Name, duration); err != nil {
logging.FromContext(ctx).Infof("failed to record ready for Topic, %v", err)
}
}
return ch, err
}
// updateFinalizers is a generic method for future compatibility with a
// reconciler SDK.
func (c *Reconciler) updateFinalizers(ctx context.Context, desired *v1alpha1.Topic) (*v1alpha1.Topic, bool, error) {
source, err := c.topicLister.Topics(desired.Namespace).Get(desired.Name)
if err != nil {
return nil, false, err
}
// Don't modify the informers copy.
existing := source.DeepCopy()
var finalizers []string
// If there's nothing to update, just return.
existingFinalizers := sets.NewString(existing.Finalizers...)
desiredFinalizers := sets.NewString(desired.Finalizers...)
if desiredFinalizers.Has(finalizerName) {
if existingFinalizers.Has(finalizerName) {
// Nothing to do.
return desired, false, nil
}
// Add the finalizer.
finalizers = append(existing.Finalizers, finalizerName)
} else {
if !existingFinalizers.Has(finalizerName) {
// Nothing to do.
return desired, false, nil
}
// Remove the finalizer.
existingFinalizers.Delete(finalizerName)
finalizers = existingFinalizers.List()
}
mergePatch := map[string]interface{}{
"metadata": map[string]interface{}{
"finalizers": finalizers,
"resourceVersion": existing.ResourceVersion,
},
}
patch, err := json.Marshal(mergePatch)
if err != nil {
return desired, false, err
}
update, err := c.RunClientSet.PubsubV1alpha1().Topics(existing.Namespace).Patch(existing.Name, types.MergePatchType, patch)
return update, true, err
}
func addFinalizer(s *v1alpha1.Topic) {
finalizers := sets.NewString(s.Finalizers...)
finalizers.Insert(finalizerName)
s.Finalizers = finalizers.List()
}
func removeFinalizer(s *v1alpha1.Topic) {
finalizers := sets.NewString(s.Finalizers...)
finalizers.Delete(finalizerName)
s.Finalizers = finalizers.List()
}
func (r *Reconciler) createOrUpdatePublisherV1alpha1(ctx context.Context, topic *v1alpha1.Topic) error {
name := resources.GeneratePublisherName(topic)
existing, err := r.ServingClientSet.ServingV1alpha1().Services(topic.Namespace).Get(name, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
logging.FromContext(ctx).Error("Unable to get an existing publisher", zap.Error(err))
return err
}
existing = nil
} else if !metav1.IsControlledBy(existing, topic) {
p, _ := json.Marshal(existing)
logging.FromContext(ctx).Error("Got a preowned publisher", zap.Any("publisher", string(p)))
return fmt.Errorf("Topic: %s does not own Service: %s", topic.Name, name)
}
desired := resources.MakePublisherV1alpha1(&resources.PublisherArgs{
Image: r.publisherImage,
Topic: topic,
Labels: resources.GetLabels(controllerAgentName, topic.Name),
})
svc := existing
if existing == nil {
svc, err = r.ServingClientSet.ServingV1alpha1().Services(topic.Namespace).Create(desired)
if err != nil {
return err
}
logging.FromContext(ctx).Desugar().Info("Publisher created.", zap.Error(err), zap.Any("publisher", svc))
} else if diff := cmp.Diff(desired.Spec, existing.Spec); diff != "" {
existing.Spec = desired.Spec
svc, err = r.ServingClientSet.ServingV1alpha1().Services(topic.Namespace).Update(existing)
if err != nil {
return err
}
logging.FromContext(ctx).Desugar().Info("Publisher updated.",
zap.Error(err), zap.Any("publisher", svc), zap.String("diff", diff))
} else {
logging.FromContext(ctx).Desugar().Info("Reusing existing publisher", zap.Any("publisher", existing))
}
// Update the topic.
topic.Status.PropagatePublisherStatus(svc.Status.GetCondition(apis.ConditionReady))
if svc.Status.IsReady() {
topic.Status.SetAddress(svc.Status.Address.URL)
}
return nil
}
func (r *Reconciler) createOrUpdatePublisherV1beta1(ctx context.Context, topic *v1alpha1.Topic) error {
name := resources.GeneratePublisherName(topic)
existing, err := r.ServingClientSet.ServingV1beta1().Services(topic.Namespace).Get(name, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
logging.FromContext(ctx).Error("Unable to get an existing publisher", zap.Error(err))
return err
}
existing = nil
} else if !metav1.IsControlledBy(existing, topic) {
return fmt.Errorf("Topic: %s does not own Service: %s", topic.Name, name)
}
desired := resources.MakePublisherV1beta1(&resources.PublisherArgs{
Image: r.publisherImage,
Topic: topic,
Labels: resources.GetLabels(controllerAgentName, topic.Name),
})
svc := existing
if existing == nil {
svc, err = r.ServingClientSet.ServingV1beta1().Services(topic.Namespace).Create(desired)
if err != nil {
return err
}
logging.FromContext(ctx).Desugar().Info("Publisher created.", zap.Error(err), zap.Any("publisher", svc))
} else if diff := cmp.Diff(desired.Spec, existing.Spec); diff != "" {
existing.Spec = desired.Spec
svc, err = r.ServingClientSet.ServingV1beta1().Services(topic.Namespace).Update(existing)
if err != nil {
return err
}
logging.FromContext(ctx).Desugar().Info("Publisher updated.",
zap.Error(err), zap.Any("publisher", svc), zap.String("diff", diff))
} else {
logging.FromContext(ctx).Desugar().Info("Reusing existing publisher", zap.Any("publisher", existing))
}
// Update the topic.
topic.Status.PropagatePublisherStatus(svc.Status.GetCondition(apis.ConditionReady))
if svc.Status.IsReady() {
topic.Status.SetAddress(svc.Status.Address.URL)
}
return nil
}
func (r *Reconciler) getPublisher(ctx context.Context, topic *v1alpha1.Topic) (*servingv1beta1.Service, error) {
pl, err := r.ServingClientSet.ServingV1beta1().Services(topic.Namespace).List(metav1.ListOptions{
LabelSelector: resources.GetLabelSelector(controllerAgentName, topic.Name).String(),
})
if err != nil {
logging.FromContext(ctx).Desugar().Error("Unable to list services: %v", zap.Error(err))
return nil, err
}
for _, pub := range pl.Items {
if metav1.IsControlledBy(&pub, topic) {
return &pub, nil
}
}
return nil, apierrors.NewNotFound(schema.GroupResource{}, "")
}