forked from knative/eventing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconcile.go
285 lines (244 loc) · 9.25 KB
/
reconcile.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
/*
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 channel
import (
"context"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
eventingv1alpha1 "github.com/knative/eventing/pkg/apis/eventing/v1alpha1"
util "github.com/knative/eventing/pkg/provisioners"
ccpcontroller "github.com/knative/eventing/pkg/provisioners/inmemory/clusterchannelprovisioner"
"github.com/knative/eventing/pkg/reconciler/names"
"github.com/knative/eventing/pkg/sidecar/configmap"
"github.com/knative/eventing/pkg/sidecar/fanout"
"github.com/knative/eventing/pkg/sidecar/multichannelfanout"
)
const (
finalizerName = controllerAgentName
// Name of the corev1.Events emitted from the reconciliation process
channelReconciled = "ChannelReconciled"
channelUpdateStatusFailed = "ChannelUpdateStatusFailed"
channelConfigSyncFailed = "ChannelConfigSyncFailed"
k8sServiceCreateFailed = "K8sServiceCreateFailed"
virtualServiceCreateFailed = "VirtualServiceCreateFailed"
// TODO after in-memory-channel is retired, asyncProvisionerName should be removed
defaultProvisionerName = "in-memory-channel"
)
type reconciler struct {
client client.Client
recorder record.EventRecorder
logger *zap.Logger
configMapKey client.ObjectKey
}
// Verify the struct implements reconcile.Reconciler
var _ reconcile.Reconciler = &reconciler{}
func (r *reconciler) InjectClient(c client.Client) error {
r.client = c
return nil
}
func (r *reconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
// TODO: use this to store the logger and set a deadline
ctx := context.TODO()
logger := r.logger.With(zap.Any("request", request))
c := &eventingv1alpha1.Channel{}
err := r.client.Get(ctx, request.NamespacedName, c)
// The Channel may have been deleted since it was added to the workqueue. If so, there is
// nothing to be done.
if errors.IsNotFound(err) {
logger.Info("Could not find Channel", zap.Error(err))
return reconcile.Result{}, nil
}
// Any other error should be retried in another reconciliation.
if err != nil {
logger.Error("Unable to Get Channel", zap.Error(err))
return reconcile.Result{}, err
}
// Does this Controller control this Channel?
if !r.shouldReconcile(c) {
logger.Info("Not reconciling Channel, it is not controlled by this Controller", zap.Any("ref", c.Spec))
return reconcile.Result{}, nil
}
logger.Info("Reconciling Channel")
// Modify a copy, not the original.
c = c.DeepCopy()
err = r.reconcile(ctx, c)
if err != nil {
logger.Info("Error reconciling Channel", zap.Error(err))
// Note that we do not return the error here, because we want to update the Status
// regardless of the error.
} else {
logger.Info("Channel reconciled")
r.recorder.Eventf(c, corev1.EventTypeNormal, channelReconciled, "Channel reconciled: %q", c.Name)
}
if updateStatusErr := util.UpdateChannel(ctx, r.client, c); updateStatusErr != nil {
logger.Info("Error updating Channel Status", zap.Error(updateStatusErr))
r.recorder.Eventf(c, corev1.EventTypeWarning, channelUpdateStatusFailed, "Failed to update Channel's status: %v", err)
return reconcile.Result{}, updateStatusErr
}
return reconcile.Result{}, err
}
// shouldReconcile determines if this Controller should control (and therefore reconcile) a given
// ClusterChannelProvisioner. This Controller only handles in-memory channels.
func (r *reconciler) shouldReconcile(c *eventingv1alpha1.Channel) bool {
if c.Spec.Provisioner != nil {
return ccpcontroller.IsControlled(c.Spec.Provisioner)
}
return false
}
func (r *reconciler) reconcile(ctx context.Context, c *eventingv1alpha1.Channel) error {
logger := r.logger.With(zap.Any("channel", c))
c.Status.InitializeConditions()
// We are syncing three things:
// 1. The K8s Service to talk to this Channel.
// 2. The Istio VirtualService to talk to this Channel.
// 3. The configuration of all Channel subscriptions.
// We always need to sync the Channel config, so do it first.
if err := r.syncChannelConfig(ctx); err != nil {
logger.Info("Error syncing the Channel config", zap.Error(err))
r.recorder.Eventf(c, corev1.EventTypeWarning, channelConfigSyncFailed, "Failed to sync Channel config: %v", err)
return err
}
if c.DeletionTimestamp != nil {
// K8s garbage collection will delete the K8s service and VirtualService for this channel.
// We use a finalizer to ensure the channel config has been synced.
util.RemoveFinalizer(c, finalizerName)
return nil
}
util.AddFinalizer(c, finalizerName)
svc, err := util.CreateK8sService(ctx, r.client, c)
if err != nil {
logger.Info("Error creating the Channel's K8s Service", zap.Error(err))
r.recorder.Eventf(c, corev1.EventTypeWarning, k8sServiceCreateFailed, "Failed to reconcile Channel's K8s Service: %v", err)
return err
}
c.Status.SetAddress(names.ServiceHostName(svc.Name, svc.Namespace))
if c.Spec.Provisioner.Name == defaultProvisionerName {
_, err = util.CreateVirtualService(ctx, r.client, c, svc)
if err != nil {
logger.Info("Error creating the Virtual Service for the Channel", zap.Error(err))
r.recorder.Eventf(c, corev1.EventTypeWarning, virtualServiceCreateFailed, "Failed to reconcile Virtual Service for the Channel: %v", err)
return err
}
} else {
// We need to have a single dispatcher that is pointed at by _both_
// ClusterChannelProvisioners. So fake the channel, by saying that its provisioner is the
// one with the single dispatcher. The faked provisioner is used only to determine the
// dispatcher Service's name.
cCopy := c.DeepCopy()
cCopy.Spec.Provisioner.Name = defaultProvisionerName
_, err = util.CreateVirtualService(ctx, r.client, cCopy, svc)
if err != nil {
logger.Info("Error creating the Virtual Service for the Channel", zap.Error(err))
r.recorder.Eventf(c, corev1.EventTypeWarning, virtualServiceCreateFailed, "Failed to reconcile Virtual Service for the Channel: %v", err)
return err
}
}
c.Status.MarkProvisioned()
return nil
}
func (r *reconciler) syncChannelConfig(ctx context.Context) error {
channels, err := r.listAllChannels(ctx)
if err != nil {
r.logger.Info("Unable to list channels", zap.Error(err))
return err
}
config := multiChannelFanoutConfig(channels)
return r.writeConfigMap(ctx, config)
}
func (r *reconciler) writeConfigMap(ctx context.Context, config *multichannelfanout.Config) error {
logger := r.logger.With(zap.Any("configMap", r.configMapKey))
updated, err := configmap.SerializeConfig(*config)
if err != nil {
r.logger.Error("Unable to serialize config", zap.Error(err), zap.Any("config", config))
return err
}
cm := &corev1.ConfigMap{}
err = r.client.Get(ctx, r.configMapKey, cm)
if errors.IsNotFound(err) {
cm = r.createNewConfigMap(updated)
err = r.client.Create(ctx, cm)
}
if err != nil {
logger.Info("Unable to get/create ConfigMap", zap.Error(err))
return err
}
if equality.Semantic.DeepEqual(cm.Data, updated) {
// Nothing to update.
return nil
}
cm.Data = updated
return r.client.Update(ctx, cm)
}
func (r *reconciler) createNewConfigMap(data map[string]string) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: r.configMapKey.Namespace,
Name: r.configMapKey.Name,
},
Data: data,
}
}
func multiChannelFanoutConfig(channels []eventingv1alpha1.Channel) *multichannelfanout.Config {
cc := make([]multichannelfanout.ChannelConfig, 0)
for _, c := range channels {
channelConfig := multichannelfanout.ChannelConfig{
Namespace: c.Namespace,
Name: c.Name,
}
if c.Spec.Subscribable != nil {
// TODO After in-memory-channel is retired, this logic must be refactored.
asyncHandler := false
if c.Spec.Provisioner.Name != defaultProvisionerName {
asyncHandler = true
}
channelConfig.FanoutConfig = fanout.Config{
Subscriptions: c.Spec.Subscribable.Subscribers,
AsyncHandler: asyncHandler,
}
}
cc = append(cc, channelConfig)
}
return &multichannelfanout.Config{
ChannelConfigs: cc,
}
}
func (r *reconciler) listAllChannels(ctx context.Context) ([]eventingv1alpha1.Channel, error) {
channels := make([]eventingv1alpha1.Channel, 0)
opts := &client.ListOptions{
// Set Raw because if we need to get more than one page, then we will put the continue token
// into opts.Raw.Continue.
Raw: &metav1.ListOptions{},
}
for {
cl := &eventingv1alpha1.ChannelList{}
if err := r.client.List(ctx, opts, cl); err != nil {
return nil, err
}
for _, c := range cl.Items {
if r.shouldReconcile(&c) {
channels = append(channels, c)
}
}
if cl.Continue != "" {
opts.Raw.Continue = cl.Continue
} else {
return channels, nil
}
}
}