This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
/
controller.go
331 lines (303 loc) · 13 KB
/
controller.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
// Copyright Project Contour 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 contour
import (
"context"
"fmt"
operatorv1alpha1 "github.com/projectcontour/contour-operator/api/v1alpha1"
objutil "github.com/projectcontour/contour-operator/internal/objects"
objcm "github.com/projectcontour/contour-operator/internal/objects/configmap"
objcontour "github.com/projectcontour/contour-operator/internal/objects/contour"
objds "github.com/projectcontour/contour-operator/internal/objects/daemonset"
objdeploy "github.com/projectcontour/contour-operator/internal/objects/deployment"
objgc "github.com/projectcontour/contour-operator/internal/objects/gatewayclass"
objjob "github.com/projectcontour/contour-operator/internal/objects/job"
objns "github.com/projectcontour/contour-operator/internal/objects/namespace"
objsvc "github.com/projectcontour/contour-operator/internal/objects/service"
"github.com/projectcontour/contour-operator/internal/operator/status"
retryable "github.com/projectcontour/contour-operator/internal/retryableerror"
"github.com/projectcontour/contour-operator/pkg/validation"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
controllerName = "contour_controller"
)
// Config holds all the things necessary for the controller to run.
type Config struct {
// ContourImage is the name of the Contour container image.
ContourImage string
// EnvoyImage is the name of the Envoy container image.
EnvoyImage string
}
// reconciler reconciles a Contour object.
type reconciler struct {
config Config
client client.Client
log logr.Logger
}
// New creates the contour controller from mgr and cfg. The controller will be pre-configured
// to watch for Contour objects across all namespaces.
func New(mgr manager.Manager, cfg Config) (controller.Controller, error) {
r := &reconciler{
config: cfg,
client: mgr.GetClient(),
log: ctrl.Log.WithName(controllerName),
}
c, err := controller.New(controllerName, mgr, controller.Options{Reconciler: r})
if err != nil {
return nil, err
}
if err := c.Watch(&source.Kind{Type: &operatorv1alpha1.Contour{}}, &handler.EnqueueRequestForObject{}); err != nil {
return nil, err
}
// Watch the Contour deployment and Envoy daemonset to properly surface Contour status conditions.
if err := c.Watch(&source.Kind{Type: &appsv1.Deployment{}}, r.enqueueRequestForOwningContour()); err != nil {
return nil, err
}
if err := c.Watch(&source.Kind{Type: &appsv1.DaemonSet{}}, r.enqueueRequestForOwningContour()); err != nil {
return nil, err
}
return c, nil
}
// enqueueRequestForOwningContour returns an event handler that maps events to
// objects containing Contour owner labels.
func (r *reconciler) enqueueRequestForOwningContour() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
labels := a.GetLabels()
ns, nsFound := labels[operatorv1alpha1.OwningContourNsLabel]
name, nameFound := labels[operatorv1alpha1.OwningContourNameLabel]
if nsFound && nameFound {
r.log.Info("queueing contour", "namespace", ns, "name", name, "related", a.GetSelfLink())
return []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Namespace: ns,
Name: name,
},
},
}
}
return []reconcile.Request{}
})
}
// Reconcile reconciles watched objects and attempts to make the current state of
// the object match the desired state.
func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.log.WithValues("contour", req.NamespacedName)
r.log.Info("reconciling", "request", req)
// Only proceed if we can get the state of contour.
contour := &operatorv1alpha1.Contour{}
if err := r.client.Get(ctx, req.NamespacedName, contour); err != nil {
if errors.IsNotFound(err) {
// Sync gatewayclass status if this contour is referenced by any gatewayclasses.
gc, exists, err := objgc.ParameterRefExists(ctx, r.client, req.Name, req.Namespace)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to verify the existence of gatewayclasses for contour %s/%s: %w",
req.Namespace, req.Name, err)
}
if exists {
var errs []error
owned := objgc.IsController(gc)
valid := false
if owned {
if err := validation.GatewayClass(gc); err != nil {
errs = append(errs, fmt.Errorf("invalid gatewayclass %s: %w", gc.Name, err))
} else {
valid = true
}
}
if err := status.SyncGatewayClass(ctx, r.client, gc, owned, valid); err != nil {
errs = append(errs, fmt.Errorf("failed to sync status for contour %s/%s: %w", req.Namespace,
req.Name, err))
}
if len(errs) != 0 {
return ctrl.Result{}, retryable.NewMaybeRetryableAggregate(errs)
}
}
// This means the contour was already deleted/finalized and there are
// stale queue entries (or something edge triggering from a related
// resource that got deleted async).
r.log.Info("contour not found; reconciliation will be skipped", "request", req)
return ctrl.Result{}, nil
}
// Error reading the object, so requeue the request.
return ctrl.Result{}, fmt.Errorf("failed to get contour %s: %w", req, err)
}
// The contour is safe to process, so ensure current state matches desired state.
desired := contour.ObjectMeta.DeletionTimestamp.IsZero()
if desired {
if err := validation.Contour(ctx, r.client, contour); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to validate contour %s/%s: %w", contour.Namespace, contour.Name, err)
}
switch {
case contour.GatewayClassSet():
if err := r.ensureContourForGatewayClass(ctx, contour); err != nil {
switch e := err.(type) {
case retryable.Error:
r.log.Error(e, "got retryable error; requeueing", "after", e.After())
return ctrl.Result{RequeueAfter: e.After()}, nil
default:
return ctrl.Result{}, err
}
}
default:
if !contour.IsFinalized() {
// Before doing anything with the contour, ensure it has a finalizer
// so it can cleaned-up later.
if err := objcontour.EnsureFinalizer(ctx, r.client, contour); err != nil {
return ctrl.Result{}, err
}
r.log.Info("finalized contour", "namespace", contour.Namespace, "name", contour.Name)
} else {
r.log.Info("contour finalized", "namespace", contour.Namespace, "name", contour.Name)
if err := r.ensureContour(ctx, contour); err != nil {
switch e := err.(type) {
case retryable.Error:
r.log.Error(e, "got retryable error; requeueing", "after", e.After())
return ctrl.Result{RequeueAfter: e.After()}, nil
default:
return ctrl.Result{}, err
}
}
r.log.Info("ensured contour", "namespace", contour.Namespace, "name", contour.Name)
}
}
} else {
switch {
case contour.GatewayClassSet():
// Do nothing since the contour is a dependent resource of the associated gatewayclass.
return ctrl.Result{}, nil
default:
if err := r.ensureContourDeleted(ctx, contour); err != nil {
switch e := err.(type) {
case retryable.Error:
r.log.Error(e, "got retryable error; requeueing", "after", e.After())
return ctrl.Result{RequeueAfter: e.After()}, nil
default:
return ctrl.Result{}, err
}
}
r.log.Info("deleted contour", "namespace", contour.Namespace, "name", contour.Name)
}
}
return ctrl.Result{}, nil
}
// ensureContour ensures all necessary resources exist for the given contour.
func (r *reconciler) ensureContour(ctx context.Context, contour *operatorv1alpha1.Contour) error {
var errs []error
cli := r.client
handleResult := func(resource string, err error) {
if err != nil {
errs = append(errs, fmt.Errorf("failed to ensure %s for contour %s/%s: %w", resource, contour.Namespace, contour.Name, err))
} else {
r.log.Info(fmt.Sprintf("ensured %s for contour", resource), "namespace", contour.Namespace, "name", contour.Name)
}
}
syncContourStatus := func() error {
if err := status.SyncContour(ctx, cli, contour); err != nil {
errs = append(errs, fmt.Errorf("failed to sync status for contour %s/%s: %w", contour.Namespace, contour.Name, err))
} else {
r.log.Info("synced status for contour", "namespace", contour.Namespace, "name", contour.Name)
}
return retryable.NewMaybeRetryableAggregate(errs)
}
handleResult("namespace", objns.EnsureNamespace(ctx, cli, contour))
handleResult("rbac", objutil.EnsureRBAC(ctx, cli, contour))
if len(errs) > 0 {
return syncContourStatus()
}
contourImage := r.config.ContourImage
envoyImage := r.config.EnvoyImage
handleResult("configmap", objcm.Ensure(ctx, cli, objcm.NewCfgForContour(contour)))
handleResult("job", objjob.EnsureJob(ctx, cli, contour, contourImage))
handleResult("deployment", objdeploy.EnsureDeployment(ctx, cli, contour, contourImage))
handleResult("daemonset", objds.EnsureDaemonSet(ctx, cli, contour, contourImage, envoyImage))
handleResult("contour service", objsvc.EnsureContourService(ctx, cli, contour))
switch contour.Spec.NetworkPublishing.Envoy.Type {
case operatorv1alpha1.LoadBalancerServicePublishingType, operatorv1alpha1.NodePortServicePublishingType, operatorv1alpha1.ClusterIPServicePublishingType:
handleResult("envoy service", objsvc.EnsureEnvoyService(ctx, cli, contour))
}
return syncContourStatus()
}
// ensureContourForGatewayClass ensures all necessary resources exist for the given contour
// when the contour is being managed by a GatewayClass.
func (r *reconciler) ensureContourForGatewayClass(ctx context.Context, contour *operatorv1alpha1.Contour) error {
if !contour.GatewayClassSet() {
return fmt.Errorf("gatewayclass not set for contour %s/%s", contour.Namespace, contour.Name)
}
var errs []error
cli := r.client
gcRef := *contour.Spec.GatewayClassRef
gc, err := objgc.Get(ctx, cli, gcRef)
if err != nil {
errs = append(errs, fmt.Errorf("failed to verify the existence of gatewayclass %s: %w", gcRef, err))
} else {
owned := objgc.IsController(gc)
if owned {
if err := validation.GatewayClass(gc); err != nil {
errs = append(errs, fmt.Errorf("invalid gatewayclass %s: %w", gc.Name, err))
}
}
}
if err := status.SyncContour(ctx, cli, contour); err != nil {
errs = append(errs, fmt.Errorf("failed to sync status for contour %s/%s: %w", contour.Namespace, contour.Name, err))
} else {
r.log.Info("synced status for contour", "namespace", contour.Namespace, "name", contour.Name)
}
return retryable.NewMaybeRetryableAggregate(errs)
}
// ensureContourDeleted ensures contour and all child resources have been deleted.
func (r *reconciler) ensureContourDeleted(ctx context.Context, contour *operatorv1alpha1.Contour) error {
if contour.GatewayClassSet() {
return nil
}
var errs []error
cli := r.client
handleResult := func(resource string, err error) {
if err != nil {
errs = append(errs, fmt.Errorf("failed to delete %s for contour %s/%s: %w", resource, contour.Namespace, contour.Name, err))
} else {
r.log.Info(fmt.Sprintf("deleted %s for contour", resource), "namespace", contour.Namespace, "name", contour.Name)
}
}
switch contour.Spec.NetworkPublishing.Envoy.Type {
case operatorv1alpha1.LoadBalancerServicePublishingType, operatorv1alpha1.NodePortServicePublishingType, operatorv1alpha1.ClusterIPServicePublishingType:
handleResult("envoy service", objsvc.EnsureEnvoyServiceDeleted(ctx, cli, contour))
}
handleResult("service", objsvc.EnsureContourServiceDeleted(ctx, cli, contour))
handleResult("daemonset", objds.EnsureDaemonSetDeleted(ctx, cli, contour))
handleResult("deployment", objdeploy.EnsureDeploymentDeleted(ctx, cli, contour))
handleResult("job", objjob.EnsureJobDeleted(ctx, cli, contour))
handleResult("configmap", objcm.Delete(ctx, cli, objcm.NewCfgForContour(contour)))
handleResult("rbac", objutil.EnsureRBACDeleted(ctx, cli, contour))
handleResult("namespace", objns.EnsureNamespaceDeleted(ctx, cli, contour))
if len(errs) == 0 {
if err := objcontour.EnsureFinalizerRemoved(ctx, cli, contour); err != nil {
errs = append(errs, fmt.Errorf("failed to remove finalizer from contour %s/%s: %w", contour.Namespace, contour.Name, err))
} else {
r.log.Info("removed finalizer from contour", "namespace", contour.Namespace, "name", contour.Name)
}
}
return utilerrors.NewAggregate(errs)
}