This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
topic.go
87 lines (75 loc) · 2.95 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
/*
Copyright 2020 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 pubsub
import (
"context"
"cloud.google.com/go/pubsub"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"knative.dev/eventing/pkg/logging"
)
const (
topicCreated = "TopicCreated"
topicDeleted = "TopicDeleted"
)
func (r *Reconciler) ReconcileTopic(ctx context.Context, id string, topicConfig *pubsub.TopicConfig, obj runtime.Object, updater StatusUpdater) (*pubsub.Topic, error) {
logger := logging.FromContext(ctx)
// Check if topic exists, and if not, create it.
topic := r.client.Topic(id)
exists, err := topic.Exists(ctx)
if err != nil {
logger.Error("Failed to verify Pub/Sub topic exists", zap.Error(err))
updater.MarkTopicUnknown("TopicVerificationFailed", "Failed to verify Pub/Sub topic exists: %w", err)
return nil, err
}
if exists {
updater.MarkTopicReady()
return topic, nil
}
// Create a new topic.
logger.Debug("Creating topic with cfg", zap.String("id", id), zap.Any("cfg", topicConfig))
topic, err = r.client.CreateTopicWithConfig(ctx, id, topicConfig)
if err != nil {
logger.Error("Failed to create Pub/Sub topic", zap.Error(err))
updater.MarkTopicFailed("TopicCreationFailed", "Topic creation failed: %w", err)
return nil, err
}
logger.Info("Created PubSub topic", zap.String("name", topic.ID()))
r.recorder.Eventf(obj, corev1.EventTypeNormal, topicCreated, "Created PubSub topic %q", topic.ID())
updater.MarkTopicReady()
return topic, nil
}
func (r *Reconciler) DeleteTopic(ctx context.Context, id string, obj runtime.Object, updater StatusUpdater) error {
logger := logging.FromContext(ctx)
logger.Debug("Deleting decoupling topic")
// Delete topic if it exists. Pull subscriptions continue pulling from the
// topic until deleted themselves.
topic := r.client.Topic(id)
exists, err := topic.Exists(ctx)
if err != nil {
logger.Error("Failed to verify Pub/Sub topic exists", zap.Error(err))
updater.MarkTopicUnknown("FinalizeTopicVerificationFailed", "failed to verify Pub/Sub topic exists: %w", err)
return err
}
if exists {
if err := topic.Delete(ctx); err != nil {
logger.Error("Failed to delete Pub/Sub topic", zap.Error(err))
updater.MarkTopicUnknown("FinalizeTopicDeletionFailed", "failed to delete Pub/Sub topic: %w", err)
return err
}
logger.Info("Deleted PubSub topic", zap.String("name", topic.ID()))
r.recorder.Eventf(obj, corev1.EventTypeNormal, topicDeleted, "Deleted PubSub topic %q", topic.ID())
}
return nil
}