-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathistiorevision_controller.go
614 lines (539 loc) · 25.3 KB
/
istiorevision_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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// Copyright Istio 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 istiorevision
import (
"context"
"errors"
"fmt"
"path"
"reflect"
"regexp"
"github.com/go-logr/logr"
v1 "github.com/istio-ecosystem/sail-operator/api/v1"
"github.com/istio-ecosystem/sail-operator/pkg/config"
"github.com/istio-ecosystem/sail-operator/pkg/constants"
"github.com/istio-ecosystem/sail-operator/pkg/enqueuelogger"
"github.com/istio-ecosystem/sail-operator/pkg/errlist"
"github.com/istio-ecosystem/sail-operator/pkg/helm"
"github.com/istio-ecosystem/sail-operator/pkg/kube"
predicate2 "github.com/istio-ecosystem/sail-operator/pkg/predicate"
"github.com/istio-ecosystem/sail-operator/pkg/reconciler"
"github.com/istio-ecosystem/sail-operator/pkg/revision"
"github.com/istio-ecosystem/sail-operator/pkg/validation"
admissionv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"istio.io/istio/pkg/ptr"
)
// Reconciler reconciles an IstioRevision object
type Reconciler struct {
client.Client
Config config.ReconcilerConfig
Scheme *runtime.Scheme
ChartManager *helm.ChartManager
}
func NewReconciler(cfg config.ReconcilerConfig, client client.Client, scheme *runtime.Scheme, chartManager *helm.ChartManager) *Reconciler {
return &Reconciler{
Config: cfg,
Client: client,
Scheme: scheme,
ChartManager: chartManager,
}
}
// +kubebuilder:rbac:groups=sailoperator.io,resources=istiorevisions,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=sailoperator.io,resources=istiorevisions/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=sailoperator.io,resources=istiorevisions/finalizers,verbs=update
// +kubebuilder:rbac:groups="",resources="*",verbs="*"
// +kubebuilder:rbac:groups="networking.k8s.io",resources="networkpolicies",verbs="*"
// +kubebuilder:rbac:groups="policy",resources="poddisruptionbudgets",verbs="*"
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=clusterroles;clusterrolebindings;roles;rolebindings,verbs="*"
// +kubebuilder:rbac:groups="apps",resources=deployments;daemonsets,verbs="*"
// +kubebuilder:rbac:groups="admissionregistration.k8s.io",resources=validatingwebhookconfigurations;mutatingwebhookconfigurations,verbs="*"
// +kubebuilder:rbac:groups="autoscaling",resources=horizontalpodautoscalers,verbs="*"
// +kubebuilder:rbac:groups="apiextensions.k8s.io",resources=customresourcedefinitions,verbs=get;list;watch
// +kubebuilder:rbac:groups="k8s.cni.cncf.io",resources=network-attachment-definitions,verbs="*"
// +kubebuilder:rbac:groups="security.openshift.io",resources=securitycontextconstraints,resourceNames=privileged,verbs=use
// +kubebuilder:rbac:groups="networking.istio.io",resources=envoyfilters,verbs="*"
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *Reconciler) Reconcile(ctx context.Context, rev *v1.IstioRevision) (ctrl.Result, error) {
log := logf.FromContext(ctx)
reconcileErr := r.doReconcile(ctx, rev)
log.Info("Reconciliation done. Updating status.")
statusErr := r.updateStatus(ctx, rev, reconcileErr)
return ctrl.Result{}, errors.Join(reconcileErr, statusErr)
}
func (r *Reconciler) doReconcile(ctx context.Context, rev *v1.IstioRevision) error {
log := logf.FromContext(ctx)
if err := r.validate(ctx, rev); err != nil {
return err
}
log.Info("Installing Helm chart")
return r.installHelmCharts(ctx, rev)
}
func (r *Reconciler) Finalize(ctx context.Context, rev *v1.IstioRevision) error {
return r.uninstallHelmCharts(ctx, rev)
}
func (r *Reconciler) validate(ctx context.Context, rev *v1.IstioRevision) error {
if rev.Spec.Version == "" {
return reconciler.NewValidationError("spec.version not set")
}
if rev.Spec.Namespace == "" {
return reconciler.NewValidationError("spec.namespace not set")
}
if err := validation.ValidateTargetNamespace(ctx, r.Client, rev.Spec.Namespace); err != nil {
return err
}
if rev.Spec.Values == nil {
return reconciler.NewValidationError("spec.values not set")
}
revName := rev.Spec.Values.Revision
if rev.Name == v1.DefaultRevision && (revName != nil && *revName != "") {
return reconciler.NewValidationError(fmt.Sprintf("spec.values.revision must be \"\" when IstioRevision name is %s", v1.DefaultRevision))
} else if rev.Name != v1.DefaultRevision && (revName == nil || *revName != rev.Name) {
return reconciler.NewValidationError("spec.values.revision does not match IstioRevision name")
}
if rev.Spec.Values.Global == nil || rev.Spec.Values.Global.IstioNamespace == nil || *rev.Spec.Values.Global.IstioNamespace != rev.Spec.Namespace {
return reconciler.NewValidationError("spec.values.global.istioNamespace does not match spec.namespace")
}
if tagExists, err := validation.IstioRevisionTagExists(ctx, r.Client, rev.Name); tagExists || err != nil {
return reconciler.NewValidationError("an IstioRevisionTag exists with this name")
}
return nil
}
func (r *Reconciler) installHelmCharts(ctx context.Context, rev *v1.IstioRevision) error {
ownerReference := metav1.OwnerReference{
APIVersion: v1.GroupVersion.String(),
Kind: v1.IstioRevisionKind,
Name: rev.Name,
UID: rev.UID,
Controller: ptr.Of(true),
BlockOwnerDeletion: ptr.Of(true),
}
values := helm.FromValues(rev.Spec.Values)
_, err := r.ChartManager.UpgradeOrInstallChart(ctx, r.getChartDir(rev),
values, rev.Spec.Namespace, getReleaseName(rev), ownerReference)
if err != nil {
return fmt.Errorf("failed to install/update Helm chart %q: %w", constants.IstiodChartName, err)
}
return nil
}
func getReleaseName(rev *v1.IstioRevision) string {
return fmt.Sprintf("%s-%s", rev.Name, constants.IstiodChartName)
}
func (r *Reconciler) getChartDir(rev *v1.IstioRevision) string {
return path.Join(r.Config.ResourceDirectory, rev.Spec.Version, "charts", constants.IstiodChartName)
}
func (r *Reconciler) uninstallHelmCharts(ctx context.Context, rev *v1.IstioRevision) error {
if _, err := r.ChartManager.UninstallChart(ctx, getReleaseName(rev), rev.Spec.Namespace); err != nil {
return fmt.Errorf("failed to uninstall Helm chart %q: %w", constants.IstiodChartName, err)
}
return nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
logger := mgr.GetLogger().WithName("ctrlr").WithName("istiorev")
// mainObjectHandler handles the IstioRevision watch events
mainObjectHandler := wrapEventHandler(logger, &handler.EnqueueRequestForObject{})
// ownedResourceHandler handles resources that are owned by the IstioRevision CR
ownedResourceHandler := wrapEventHandler(logger,
handler.EnqueueRequestForOwner(r.Scheme, r.RESTMapper(), &v1.IstioRevision{}, handler.OnlyControllerOwner()))
// nsHandler triggers reconciliation in two cases:
// - when a namespace that is referenced in IstioRevision.spec.namespace is
// created, so that the control plane is installed immediately.
// - when a namespace that references the IstioRevision CR via the istio.io/rev
// or istio-injection labels is updated, so that the InUse condition of
// the IstioRevision CR is updated.
nsHandler := wrapEventHandler(logger, handler.EnqueueRequestsFromMapFunc(r.mapNamespaceToReconcileRequest))
// podHandler handles pods that reference the IstioRevision CR via the istio.io/rev or sidecar.istio.io/inject labels.
// The handler triggers the reconciliation of the referenced IstioRevision CR so that its InUse condition is updated.
podHandler := wrapEventHandler(logger, handler.EnqueueRequestsFromMapFunc(r.mapPodToReconcileRequest))
// revisionTagHandler handles IstioRevisionTags that reference the IstioRevision CR via their targetRef.
// The handler triggers the reconciliation of the referenced IstioRevision CR so that its InUse condition is updated.
revisionTagHandler := wrapEventHandler(logger, handler.EnqueueRequestsFromMapFunc(r.mapRevisionTagToReconcileRequest))
return ctrl.NewControllerManagedBy(mgr).
WithOptions(controller.Options{
LogConstructor: func(req *reconcile.Request) logr.Logger {
log := logger
if req != nil {
log = log.WithValues("IstioRevision", req.Name)
}
return log
},
}).
// we use the Watches function instead of For(), so that we can wrap the handler so that events that cause the object to be enqueued are logged
// +lint-watches:ignore: IstioRevision (not found in charts, but this is the main resource watched by this controller)
Watches(&v1.IstioRevision{}, mainObjectHandler).
Named("istiorevision").
// namespaced resources
Watches(&corev1.ConfigMap{}, ownedResourceHandler).
Watches(&appsv1.Deployment{}, ownedResourceHandler). // we don't ignore the status here because we use it to calculate the IstioRevision status
Watches(&corev1.Endpoints{}, ownedResourceHandler).
Watches(&corev1.Service{}, ownedResourceHandler, builder.WithPredicates(ignoreStatusChange())).
// We use predicate.IgnoreUpdate() so that we skip the reconciliation when a pull secret is added to the ServiceAccount.
// This is necessary so that we don't remove the newly-added secret.
// TODO: this is a temporary hack until we implement the correct solution on the Helm-render side
Watches(&corev1.ServiceAccount{}, ownedResourceHandler, builder.WithPredicates(predicate2.IgnoreUpdate())).
Watches(&rbacv1.Role{}, ownedResourceHandler).
Watches(&rbacv1.RoleBinding{}, ownedResourceHandler).
Watches(&policyv1.PodDisruptionBudget{}, ownedResourceHandler, builder.WithPredicates(ignoreStatusChange())).
Watches(&autoscalingv2.HorizontalPodAutoscaler{}, ownedResourceHandler, builder.WithPredicates(ignoreStatusChange())).
// +lint-watches:ignore: Namespace (not found in charts, but must be watched to reconcile IstioRevision when its namespace is created)
Watches(&corev1.Namespace{}, nsHandler, builder.WithPredicates(ignoreStatusChange())).
// +lint-watches:ignore: Pod (not found in charts, but must be watched to reconcile IstioRevision when a pod references it)
Watches(&corev1.Pod{}, podHandler, builder.WithPredicates(ignoreStatusChange())).
// +lint-watches:ignore: IstioRevisionTag (not found in charts, but must be watched to reconcile IstioRevision when a pod references it)
Watches(&v1.IstioRevisionTag{}, revisionTagHandler).
// cluster-scoped resources
Watches(&rbacv1.ClusterRole{}, ownedResourceHandler).
Watches(&rbacv1.ClusterRoleBinding{}, ownedResourceHandler).
Watches(&admissionv1.MutatingWebhookConfiguration{}, ownedResourceHandler).
Watches(&admissionv1.ValidatingWebhookConfiguration{}, ownedResourceHandler, builder.WithPredicates(validatingWebhookConfigPredicate())).
// +lint-watches:ignore: ValidatingAdmissionPolicy (TODO: fix this when CI supports golang 1.22 and k8s 1.30)
// +lint-watches:ignore: ValidatingAdmissionPolicyBinding (TODO: fix this when CI supports golang 1.22 and k8s 1.30)
// +lint-watches:ignore: CustomResourceDefinition (prevents `make lint-watches` from bugging us about CRDs)
Complete(reconciler.NewStandardReconcilerWithFinalizer[*v1.IstioRevision](r.Client, r.Reconcile, r.Finalize, constants.FinalizerName))
}
func (r *Reconciler) determineStatus(ctx context.Context, rev *v1.IstioRevision, reconcileErr error) (v1.IstioRevisionStatus, error) {
var errs errlist.Builder
reconciledCondition := r.determineReconciledCondition(reconcileErr)
readyCondition, err := r.determineReadyCondition(ctx, rev)
errs.Add(err)
inUseCondition, err := r.determineInUseCondition(ctx, rev)
errs.Add(err)
status := *rev.Status.DeepCopy()
status.ObservedGeneration = rev.Generation
status.SetCondition(reconciledCondition)
status.SetCondition(readyCondition)
status.SetCondition(inUseCondition)
status.State = deriveState(reconciledCondition, readyCondition)
return status, errs.Error()
}
func (r *Reconciler) updateStatus(ctx context.Context, rev *v1.IstioRevision, reconcileErr error) error {
var errs errlist.Builder
status, err := r.determineStatus(ctx, rev, reconcileErr)
if err != nil {
errs.Add(fmt.Errorf("failed to determine status: %w", err))
}
if !reflect.DeepEqual(rev.Status, status) {
if err := r.Client.Status().Patch(ctx, rev, kube.NewStatusPatch(status)); err != nil {
errs.Add(fmt.Errorf("failed to patch status: %w", err))
}
}
return errs.Error()
}
func deriveState(reconciledCondition, readyCondition v1.IstioRevisionCondition) v1.IstioRevisionConditionReason {
if reconciledCondition.Status != metav1.ConditionTrue {
return reconciledCondition.Reason
} else if readyCondition.Status != metav1.ConditionTrue {
return readyCondition.Reason
}
return v1.IstioRevisionReasonHealthy
}
func (r *Reconciler) determineReconciledCondition(err error) v1.IstioRevisionCondition {
c := v1.IstioRevisionCondition{Type: v1.IstioRevisionConditionReconciled}
if err == nil {
c.Status = metav1.ConditionTrue
} else {
c.Status = metav1.ConditionFalse
c.Reason = v1.IstioRevisionReasonReconcileError
c.Message = fmt.Sprintf("error reconciling resource: %v", err)
}
return c
}
func (r *Reconciler) determineReadyCondition(ctx context.Context, rev *v1.IstioRevision) (v1.IstioRevisionCondition, error) {
c := v1.IstioRevisionCondition{
Type: v1.IstioRevisionConditionReady,
Status: metav1.ConditionFalse,
}
if !revision.IsUsingRemoteControlPlane(rev) {
istiod := appsv1.Deployment{}
if err := r.Client.Get(ctx, istiodDeploymentKey(rev), &istiod); err == nil {
if istiod.Status.Replicas == 0 {
c.Reason = v1.IstioRevisionReasonIstiodNotReady
c.Message = "istiod Deployment is scaled to zero replicas"
} else if istiod.Status.ReadyReplicas < istiod.Status.Replicas {
c.Reason = v1.IstioRevisionReasonIstiodNotReady
c.Message = "not all istiod pods are ready"
} else {
c.Status = metav1.ConditionTrue
}
} else if apierrors.IsNotFound(err) {
c.Reason = v1.IstioRevisionReasonIstiodNotReady
c.Message = "istiod Deployment not found"
} else {
c.Status = metav1.ConditionUnknown
c.Reason = v1.IstioRevisionReasonReadinessCheckFailed
c.Message = fmt.Sprintf("failed to get readiness: %v", err)
return c, fmt.Errorf("get failed: %w", err)
}
} else {
webhook := admissionv1.MutatingWebhookConfiguration{}
webhookKey := injectionWebhookKey(rev)
if err := r.Client.Get(ctx, webhookKey, &webhook); err == nil {
switch webhook.Annotations[constants.WebhookReadinessProbeStatusAnnotationKey] {
case "true":
c.Status = metav1.ConditionTrue
case "false":
c.Reason = v1.IstioRevisionReasonRemoteIstiodNotReady
c.Message = "readiness probe on remote istiod failed"
default:
c.Reason = v1.IstioRevisionReasonRemoteIstiodNotReady
c.Message = fmt.Sprintf("invalid or missing annotation %s on MutatingWebhookConfiguration %s",
constants.WebhookReadinessProbeStatusAnnotationKey, webhookKey.Name)
}
} else if apierrors.IsNotFound(err) {
c.Reason = v1.IstioRevisionReasonRemoteIstiodNotReady
c.Message = fmt.Sprintf("MutatingWebhookConfiguration %s not found", webhookKey.Name)
} else {
c.Status = metav1.ConditionUnknown
c.Reason = v1.IstioRevisionReasonReadinessCheckFailed
c.Message = fmt.Sprintf("failed to get readiness: %v", err)
return c, fmt.Errorf("get failed: %w", err)
}
}
return c, nil
}
func (r *Reconciler) determineInUseCondition(ctx context.Context, rev *v1.IstioRevision) (v1.IstioRevisionCondition, error) {
c := v1.IstioRevisionCondition{Type: v1.IstioRevisionConditionInUse}
isReferenced, err := r.isRevisionReferenced(ctx, rev)
if err == nil {
if isReferenced {
c.Status = metav1.ConditionTrue
c.Reason = v1.IstioRevisionReasonReferencedByWorkloads
c.Message = "Referenced by at least one pod or namespace"
} else {
c.Status = metav1.ConditionFalse
c.Reason = v1.IstioRevisionReasonNotReferenced
c.Message = "Not referenced by any pod or namespace"
}
return c, nil
}
c.Status = metav1.ConditionUnknown
c.Reason = v1.IstioRevisionReasonUsageCheckFailed
c.Message = fmt.Sprintf("failed to determine if revision is in use: %v", err)
return c, fmt.Errorf("failed to determine if IstioRevision is in use: %w", err)
}
func (r *Reconciler) isRevisionReferenced(ctx context.Context, rev *v1.IstioRevision) (bool, error) {
log := logf.FromContext(ctx)
nsList := corev1.NamespaceList{}
nsMap := map[string]corev1.Namespace{}
// if an IstioRevision is referenced by a revisionTag, it's considered as InUse
revisionTagList := v1.IstioRevisionTagList{}
if err := r.Client.List(ctx, &revisionTagList); err != nil {
return false, fmt.Errorf("failed to list IstioRevisionTags: %w", err)
}
for _, tag := range revisionTagList.Items {
if tag.Status.IstioRevision == rev.Name {
log.V(2).Info("Revision is referenced by IstioRevisionTag", "IstioRevisionTag", tag.Name)
return true, nil
}
}
if err := r.Client.List(ctx, &nsList); err != nil { // TODO: can we optimize this by specifying a label selector
return false, fmt.Errorf("failed to list namespaces: %w", err)
}
for _, ns := range nsList.Items {
if namespaceReferencesRevision(ns, rev) {
log.V(2).Info("Revision is referenced by Namespace", "Namespace", ns.Name)
return true, nil
}
nsMap[ns.Name] = ns
}
podList := corev1.PodList{}
if err := r.Client.List(ctx, &podList); err != nil { // TODO: can we optimize this by specifying a label selector
return false, fmt.Errorf("failed to list pods: %w", err)
}
for _, pod := range podList.Items {
if ns, found := nsMap[pod.Namespace]; found && podReferencesRevision(pod, ns, rev) {
log.V(2).Info("Revision is referenced by Pod", "Pod", client.ObjectKeyFromObject(&pod))
return true, nil
}
}
if rev.Name == v1.DefaultRevision && rev.Spec.Values != nil &&
rev.Spec.Values.SidecarInjectorWebhook != nil &&
rev.Spec.Values.SidecarInjectorWebhook.EnableNamespacesByDefault != nil &&
*rev.Spec.Values.SidecarInjectorWebhook.EnableNamespacesByDefault {
return true, nil
}
log.V(2).Info("Revision is not referenced by any Pod or Namespace")
return false, nil
}
func namespaceReferencesRevision(ns corev1.Namespace, rev *v1.IstioRevision) bool {
return rev.Name == revision.GetReferencedRevisionFromNamespace(ns.Labels)
}
func podReferencesRevision(pod corev1.Pod, ns corev1.Namespace, rev *v1.IstioRevision) bool {
if rev.Name == revision.GetInjectedRevisionFromPod(pod.GetAnnotations()) {
return true
}
if revision.GetReferencedRevisionFromNamespace(ns.Labels) == "" &&
rev.Name == revision.GetReferencedRevisionFromPod(pod.GetLabels()) {
return true
}
return false
}
func istiodDeploymentKey(rev *v1.IstioRevision) client.ObjectKey {
name := "istiod"
if rev.Spec.Values != nil && rev.Spec.Values.Revision != nil && *rev.Spec.Values.Revision != "" {
name += "-" + *rev.Spec.Values.Revision
}
return client.ObjectKey{
Namespace: rev.Spec.Namespace,
Name: name,
}
}
func injectionWebhookKey(rev *v1.IstioRevision) client.ObjectKey {
name := "istio-sidecar-injector"
if rev.Spec.Values != nil && rev.Spec.Values.Revision != nil && *rev.Spec.Values.Revision != "" {
name += "-" + *rev.Spec.Values.Revision
}
if rev.Spec.Namespace != "istio-system" {
name += "-" + rev.Spec.Namespace
}
return client.ObjectKey{
Name: name,
}
}
func (r *Reconciler) mapNamespaceToReconcileRequest(ctx context.Context, ns client.Object) []reconcile.Request {
log := logf.FromContext(ctx)
var requests []reconcile.Request
// Check if any IstioRevision references this namespace in .spec.namespace
revList := v1.IstioRevisionList{}
if err := r.Client.List(ctx, &revList); err != nil {
log.Error(err, "failed to list IstioRevisions")
return nil
}
for _, rev := range revList.Items {
if rev.Spec.Namespace == ns.GetName() {
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Name: rev.Name}})
}
}
// Check if the namespace references an IstioRevision in its labels
revisionName := revision.GetReferencedRevisionFromNamespace(ns.GetLabels())
if revisionName != "" {
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Name: revisionName}})
}
return requests
}
// mapPodToReconcileRequest will collect all referenced revisions from a pod and its namespace and trigger reconciliation
func (r *Reconciler) mapPodToReconcileRequest(ctx context.Context, pod client.Object) []reconcile.Request {
revisionNames := []string{}
revisionName := revision.GetInjectedRevisionFromPod(pod.GetAnnotations())
if revisionName != "" {
revisionNames = append(revisionNames, revisionName)
} else {
revisionName = revision.GetReferencedRevisionFromPod(pod.GetLabels())
if revisionName != "" {
revisionNames = append(revisionNames, revisionName)
}
}
ns := corev1.Namespace{}
err := r.Client.Get(ctx, types.NamespacedName{Name: pod.GetNamespace()}, &ns)
if err != nil {
return nil
}
revisionName = revision.GetReferencedRevisionFromNamespace(ns.GetLabels())
if revisionName != "" {
revisionNames = append(revisionNames, revisionName)
}
if len(revisionNames) > 0 {
reqs := []reconcile.Request{}
for _, revName := range revisionNames {
reqs = append(reqs, reconcile.Request{NamespacedName: types.NamespacedName{Name: revName}})
}
return reqs
}
return nil
}
func (r *Reconciler) mapRevisionTagToReconcileRequest(ctx context.Context, revisionTag client.Object) []reconcile.Request {
tag, ok := revisionTag.(*v1.IstioRevisionTag)
if ok && tag.Status.IstioRevision != "" {
return []reconcile.Request{{NamespacedName: types.NamespacedName{Name: tag.Status.IstioRevision}}}
}
return nil
}
// ignoreStatusChange returns a predicate that ignores watch events where only the resource status changes; if
// there are any other changes to the resource, the event is not ignored.
// This ensures that the controller doesn't reconcile the entire IstioRevision every time the status of an owned
// resource is updated. Without this predicate, the controller would continuously reconcile the IstioRevision
// because the status.currentMetrics of the HorizontalPodAutoscaler object was updated.
func ignoreStatusChange() predicate.Funcs {
return predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
return specWasUpdated(e.ObjectOld, e.ObjectNew) ||
!reflect.DeepEqual(e.ObjectNew.GetLabels(), e.ObjectOld.GetLabels()) ||
!reflect.DeepEqual(e.ObjectNew.GetAnnotations(), e.ObjectOld.GetAnnotations()) ||
!reflect.DeepEqual(e.ObjectNew.GetOwnerReferences(), e.ObjectOld.GetOwnerReferences()) ||
!reflect.DeepEqual(e.ObjectNew.GetFinalizers(), e.ObjectOld.GetFinalizers())
},
}
}
func specWasUpdated(oldObject client.Object, newObject client.Object) bool {
// for HPAs, k8s doesn't set metadata.generation, so we actually have to check whether the spec was updated
if oldHpa, ok := oldObject.(*autoscalingv2.HorizontalPodAutoscaler); ok {
if newHpa, ok := newObject.(*autoscalingv2.HorizontalPodAutoscaler); ok {
return !reflect.DeepEqual(oldHpa.Spec, newHpa.Spec)
}
}
// for other resources, comparing the metadata.generation suffices
return oldObject.GetGeneration() != newObject.GetGeneration()
}
func validatingWebhookConfigPredicate() predicate.Funcs {
return predicate.Funcs{
UpdateFunc: func(e event.TypedUpdateEvent[client.Object]) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}
if matched, _ := regexp.MatchString("istiod-.*-validator|istio-validator.*", e.ObjectNew.GetName()); matched {
// Istiod updates the caBundle and failurePolicy fields in istiod-<ns>-validator and istio-validator[-<rev>]-<ns>
// webhook configs. We must ignore changes to these fields to prevent an endless update loop.
clearIgnoredFields(e.ObjectOld)
clearIgnoredFields(e.ObjectNew)
return !reflect.DeepEqual(e.ObjectNew, e.ObjectOld)
}
return true
},
}
}
func clearIgnoredFields(obj client.Object) {
obj.SetResourceVersion("")
obj.SetGeneration(0)
obj.SetManagedFields(nil)
if webhookConfig, ok := obj.(*admissionv1.ValidatingWebhookConfiguration); ok {
for i := 0; i < len(webhookConfig.Webhooks); i++ {
webhookConfig.Webhooks[i].FailurePolicy = nil
}
}
}
func wrapEventHandler(logger logr.Logger, handler handler.EventHandler) handler.EventHandler {
return enqueuelogger.WrapIfNecessary(v1.IstioRevisionKind, logger, handler)
}