-
Notifications
You must be signed in to change notification settings - Fork 582
/
reconcile.go
153 lines (126 loc) · 5.37 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
/*
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/errors"
"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"
)
const (
finalizerName = controllerAgentName
// Name of the corev1.Events emitted from the reconciliation process
channelReconciled = "ChannelReconciled"
channelUpdateStatusFailed = "ChannelUpdateStatusFailed"
k8sServiceCreateFailed = "K8sServiceCreateFailed"
)
type reconciler struct {
client client.Client
recorder record.EventRecorder
logger *zap.Logger
}
// 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")
// Finalizer needs to be removed (even though no finalizers are added) to maintain backwards
// compatibility with v0.5 in which a finalizer was added. Or else channels will not get deleted
// after upgrading to 0.6+.
if result := util.RemoveFinalizer(c, finalizerName); result == util.FinalizerRemoved {
err = r.client.Update(ctx, c)
if err != nil {
logger.Info("Failed to remove finalizer", zap.Error(err))
return reconcile.Result{}, err
}
logger.Info("Channel reconciled. Finalizer Removed")
r.recorder.Eventf(c, corev1.EventTypeNormal, channelReconciled, "Channel reconciled: %q. Finalizer removed.", c.Name)
return reconcile.Result{Requeue: true}, nil
}
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 := r.client.Status().Update(ctx, 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()
if usesDeprecatedProvisioner(c) {
c.Status.MarkDeprecated("ClusterChannelProvisionerDeprecated", "The `in-memory-channel` ClusterChannelProvisioner is deprecated and will be removed in 0.7. Recommended replacement is `in-memory`.")
}
// We are syncing K8s Service to talk to this Channel.
svc, err := util.CreateK8sService(ctx, r.client, c, util.ExternalService(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))
c.Status.MarkProvisioned()
return nil
}
func usesDeprecatedProvisioner(c *eventingv1alpha1.Channel) bool {
return c.Spec.Provisioner != nil &&
c.Spec.Provisioner.Namespace == "" &&
c.Spec.Provisioner.Name == "in-memory-channel"
}