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 82
/
broker.go
221 lines (184 loc) · 8.31 KB
/
broker.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
/*
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 broker implements the Broker controller and reconciler reconciling
// Brokers and Triggers.
package broker
import (
"context"
"fmt"
"github.com/google/knative-gcp/pkg/apis/configs/dataresidency"
"cloud.google.com/go/pubsub"
"github.com/google/knative-gcp/pkg/logging"
"go.uber.org/multierr"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
pkgreconciler "knative.dev/pkg/reconciler"
brokerv1beta1 "github.com/google/knative-gcp/pkg/apis/broker/v1beta1"
brokerreconciler "github.com/google/knative-gcp/pkg/client/injection/reconciler/broker/v1beta1/broker"
inteventslisters "github.com/google/knative-gcp/pkg/client/listers/intevents/v1alpha1"
metadataClient "github.com/google/knative-gcp/pkg/gclient/metadata"
"github.com/google/knative-gcp/pkg/reconciler"
"github.com/google/knative-gcp/pkg/reconciler/broker/resources"
reconcilerutilspubsub "github.com/google/knative-gcp/pkg/reconciler/utils/pubsub"
"github.com/google/knative-gcp/pkg/utils"
)
const (
// Name of the corev1.Events emitted from the Broker reconciliation process.
brokerReconciled = "BrokerReconciled"
brokerFinalized = "BrokerFinalized"
brokerCellCreated = "BrokerCellCreated"
)
type Reconciler struct {
*reconciler.Base
// listers index properties about resources
brokerCellLister inteventslisters.BrokerCellLister
projectID string
// pubsubClient is used as the Pubsub client when present.
pubsubClient *pubsub.Client
dataresidencyStore *dataresidency.Store
}
// Check that Reconciler implements Interface
var _ brokerreconciler.Interface = (*Reconciler)(nil)
var _ brokerreconciler.Finalizer = (*Reconciler)(nil)
var brokerGVK = brokerv1beta1.SchemeGroupVersion.WithKind("Broker")
func (r *Reconciler) ReconcileKind(ctx context.Context, b *brokerv1beta1.Broker) pkgreconciler.Event {
if err := r.reconcileBroker(ctx, b); err != nil {
logging.FromContext(ctx).Error("Problem reconciling broker", zap.Error(err))
return fmt.Errorf("failed to reconcile broker: %w", err)
//TODO instead of returning on error, update the data plane configmap with
// whatever info is available. or put this in a defer?
}
return pkgreconciler.NewEvent(corev1.EventTypeNormal, brokerReconciled, "Broker reconciled: \"%s/%s\"", b.Namespace, b.Name)
}
func (r *Reconciler) FinalizeKind(ctx context.Context, b *brokerv1beta1.Broker) pkgreconciler.Event {
logger := logging.FromContext(ctx)
logger.Debug("Finalizing Broker", zap.Any("broker", b))
if err := r.deleteDecouplingTopicAndSubscription(ctx, b); err != nil {
return fmt.Errorf("failed to delete Pub/Sub topic: %v", err)
}
return pkgreconciler.NewEvent(corev1.EventTypeNormal, brokerFinalized, "Broker finalized: \"%s/%s\"", b.Namespace, b.Name)
}
func (r *Reconciler) reconcileBroker(ctx context.Context, b *brokerv1beta1.Broker) error {
logger := logging.FromContext(ctx)
logger.Debug("Reconciling Broker", zap.Any("broker", b))
b.Status.InitializeConditions()
b.Status.ObservedGeneration = b.Generation
if err := r.ensureBrokerCellExists(ctx, b); err != nil {
return fmt.Errorf("brokercell reconcile failed: %v", err)
}
// Create decoupling topic and pullsub for this broker. Ingress will push
// to this topic and fanout will pull from the pull sub.
if err := r.reconcileDecouplingTopicAndSubscription(ctx, b); err != nil {
return fmt.Errorf("decoupling topic reconcile failed: %v", err)
}
return nil
}
func (r *Reconciler) reconcileDecouplingTopicAndSubscription(ctx context.Context, b *brokerv1beta1.Broker) error {
logger := logging.FromContext(ctx)
logger.Debug("Reconciling decoupling topic", zap.Any("broker", b))
// get ProjectID from metadata if projectID isn't set
projectID, err := utils.ProjectID(r.projectID, metadataClient.NewDefaultMetadataClient())
if err != nil {
logger.Error("Failed to find project id", zap.Error(err))
b.Status.MarkTopicUnknown("ProjectIdNotFound", "Failed to find project id: %v", err)
b.Status.MarkSubscriptionUnknown("ProjectIdNotFound", "Failed to find project id: %v", err)
return err
}
// Set the projectID in the status.
//TODO uncomment when eventing webhook allows this
//b.Status.ProjectID = projectID
client := r.pubsubClient
if client == nil {
var err error
client, err = pubsub.NewClient(ctx, projectID)
if err != nil {
logger.Error("Failed to create Pub/Sub client", zap.Error(err))
b.Status.MarkTopicUnknown("PubSubClientCreationFailed", "Failed to create Pub/Sub client: %v", err)
b.Status.MarkSubscriptionUnknown("PubSubClientCreationFailed", "Failed to create Pub/Sub client: %v", err)
return err
}
defer client.Close()
}
pubsubReconciler := reconcilerutilspubsub.NewReconciler(client, r.Recorder)
labels := map[string]string{
"resource": "brokers",
"broker_class": brokerv1beta1.BrokerClass,
"namespace": b.Namespace,
"name": b.Name,
//TODO add resource labels, but need to be sanitized: https://cloud.google.com/pubsub/docs/labels#requirements
}
// Check if topic exists, and if not, create it.
topicID := resources.GenerateDecouplingTopicName(b)
topicConfig := &pubsub.TopicConfig{Labels: labels}
if r.dataresidencyStore != nil {
if dataresidencyConfig := r.dataresidencyStore.Load(); dataresidencyConfig != nil {
if dataresidencyConfig.DataResidencyDefaults.ComputeAllowedPersistenceRegions(topicConfig) {
logging.FromContext(ctx).Debug("Updated Topic Config AllowedPersistenceRegions for Broker", zap.Any("topicConfig", *topicConfig))
}
}
}
topic, err := pubsubReconciler.ReconcileTopic(ctx, topicID, topicConfig, b, &b.Status)
if err != nil {
return err
}
// TODO(grantr): this isn't actually persisted due to webhook issues.
//TODO uncomment when eventing webhook allows this
//b.Status.TopicID = topic.ID()
// Check if PullSub exists, and if not, create it.
subID := resources.GenerateDecouplingSubscriptionName(b)
subConfig := pubsub.SubscriptionConfig{
Topic: topic,
Labels: labels,
//TODO(grantr): configure these settings?
// AckDeadline
// RetentionDuration
}
if _, err := pubsubReconciler.ReconcileSubscription(ctx, subID, subConfig, b, &b.Status); err != nil {
return err
}
// TODO(grantr): this isn't actually persisted due to webhook issues.
//TODO uncomment when eventing webhook allows this
//b.Status.SubscriptionID = sub.ID()
return nil
}
func (r *Reconciler) deleteDecouplingTopicAndSubscription(ctx context.Context, b *brokerv1beta1.Broker) error {
logger := logging.FromContext(ctx)
logger.Debug("Deleting decoupling topic")
// get ProjectID from metadata if projectID isn't set
projectID, err := utils.ProjectID(r.projectID, metadataClient.NewDefaultMetadataClient())
if err != nil {
logger.Error("Failed to find project id", zap.Error(err))
b.Status.MarkTopicUnknown("FinalizeTopicProjectIdNotFound", "Failed to find project id: %v", err)
b.Status.MarkSubscriptionUnknown("FinalizeSubscriptionProjectIdNotFound", "Failed to find project id: %v", err)
return err
}
client := r.pubsubClient
if client == nil {
client, err := pubsub.NewClient(ctx, projectID)
if err != nil {
logger.Error("Failed to create Pub/Sub client", zap.Error(err))
b.Status.MarkTopicUnknown("FinalizeTopicPubSubClientCreationFailed", "Failed to create Pub/Sub client: %v", err)
b.Status.MarkSubscriptionUnknown("FinalizeSubscriptionPubSubClientCreationFailed", "Failed to create Pub/Sub client: %v", err)
return err
}
defer client.Close()
}
pubsubReconciler := reconcilerutilspubsub.NewReconciler(client, r.Recorder)
// Delete topic if it exists. Pull subscriptions continue pulling from the
// topic until deleted themselves.
topicID := resources.GenerateDecouplingTopicName(b)
err = multierr.Append(nil, pubsubReconciler.DeleteTopic(ctx, topicID, b, &b.Status))
subID := resources.GenerateDecouplingSubscriptionName(b)
err = multierr.Append(err, pubsubReconciler.DeleteSubscription(ctx, subID, b, &b.Status))
return err
}