|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package capiutils |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/go-logr/logr" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "k8s.io/klog/v2" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client/apiutil" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 28 | + |
| 29 | + "sigs.k8s.io/cluster-api/util/predicates" |
| 30 | + |
| 31 | + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta1" |
| 32 | +) |
| 33 | + |
| 34 | +// ClusterUpdateInfraReady returns a predicate that returns true for an update event when a cluster has Status.InfrastructureReady changed from false to true |
| 35 | +// it also returns true if the resource provided is not a Cluster to allow for use with controller-runtime NewControllerManagedBy. |
| 36 | +// Deprecated: replace with predicates.ClusterUpdateInfraReady when we move to v1beta2 |
| 37 | +func ClusterUpdateInfraReady(scheme *runtime.Scheme, logger logr.Logger) predicate.Funcs { |
| 38 | + return predicate.Funcs{ |
| 39 | + UpdateFunc: func(e event.UpdateEvent) bool { |
| 40 | + log := logger.WithValues("predicate", "ClusterUpdateInfraReady", "eventType", "update") |
| 41 | + if gvk, err := apiutil.GVKForObject(e.ObjectOld, scheme); err == nil { |
| 42 | + log = log.WithValues(gvk.Kind, klog.KObj(e.ObjectOld)) |
| 43 | + } |
| 44 | + |
| 45 | + oldCluster, ok := e.ObjectOld.(*clusterv1.Cluster) |
| 46 | + if !ok { |
| 47 | + log.V(4).Info("Expected Cluster", "type", fmt.Sprintf("%T", e.ObjectOld)) |
| 48 | + return false |
| 49 | + } |
| 50 | + |
| 51 | + newCluster := e.ObjectNew.(*clusterv1.Cluster) |
| 52 | + |
| 53 | + if !oldCluster.Status.InfrastructureReady && newCluster.Status.InfrastructureReady { |
| 54 | + log.V(6).Info("Cluster infrastructure became ready, allowing further processing") |
| 55 | + return true |
| 56 | + } |
| 57 | + |
| 58 | + log.V(4).Info("Cluster infrastructure did not become ready, blocking further processing") |
| 59 | + return false |
| 60 | + }, |
| 61 | + CreateFunc: func(event.CreateEvent) bool { return false }, |
| 62 | + DeleteFunc: func(event.DeleteEvent) bool { return false }, |
| 63 | + GenericFunc: func(event.GenericEvent) bool { return false }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// ClusterPausedTransitions returns a predicate that returns true for an update event when a cluster has Spec.Paused changed. |
| 68 | +// Deprecated: replace with predicates.ClusterPausedTransitions when we move to v1beta2 |
| 69 | +func ClusterPausedTransitions(scheme *runtime.Scheme, logger logr.Logger) predicate.Funcs { |
| 70 | + return predicate.Funcs{ |
| 71 | + UpdateFunc: func(e event.UpdateEvent) bool { |
| 72 | + log := logger.WithValues("predicate", "ClusterPausedTransitions", "eventType", "update") |
| 73 | + if gvk, err := apiutil.GVKForObject(e.ObjectOld, scheme); err == nil { |
| 74 | + log = log.WithValues(gvk.Kind, klog.KObj(e.ObjectOld)) |
| 75 | + } |
| 76 | + |
| 77 | + oldCluster, ok := e.ObjectOld.(*clusterv1.Cluster) |
| 78 | + if !ok { |
| 79 | + log.V(4).Info("Expected Cluster", "type", fmt.Sprintf("%T", e.ObjectOld)) |
| 80 | + return false |
| 81 | + } |
| 82 | + |
| 83 | + newCluster := e.ObjectNew.(*clusterv1.Cluster) |
| 84 | + |
| 85 | + if oldCluster.Spec.Paused && !newCluster.Spec.Paused { |
| 86 | + log.V(6).Info("Cluster unpausing, allowing further processing") |
| 87 | + return true |
| 88 | + } |
| 89 | + |
| 90 | + if !oldCluster.Spec.Paused && newCluster.Spec.Paused { |
| 91 | + log.V(6).Info("Cluster pausing, allowing further processing") |
| 92 | + return true |
| 93 | + } |
| 94 | + |
| 95 | + // This predicate always work in "or" with Paused predicates |
| 96 | + // so the logs are adjusted to not provide false negatives/verbosity at V<=5. |
| 97 | + log.V(6).Info("Cluster paused state was not changed, blocking further processing") |
| 98 | + return false |
| 99 | + }, |
| 100 | + CreateFunc: func(event.CreateEvent) bool { return false }, |
| 101 | + DeleteFunc: func(event.DeleteEvent) bool { return false }, |
| 102 | + GenericFunc: func(event.GenericEvent) bool { return false }, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// ClusterPausedTransitionsOrInfrastructureReady returns a Predicate that returns true on Cluster Update events where |
| 107 | +// either Cluster.Spec.Paused transitions or Cluster.Status.InfrastructureReady transitions to true. |
| 108 | +// This implements a common requirement for some cluster-api and provider controllers (such as Machine Infrastructure |
| 109 | +// controllers) to resume reconciliation when the Cluster gets paused or unpaused and when the infrastructure becomes ready. |
| 110 | +// Example use: |
| 111 | +// |
| 112 | +// err := controller.Watch( |
| 113 | +// source.Kind(cache, &clusterv1.Cluster{}), |
| 114 | +// handler.EnqueueRequestsFromMapFunc(clusterToMachines) |
| 115 | +// predicates.ClusterPausedTransitionsOrInfrastructureReady(mgr.GetScheme(), r.Log), |
| 116 | +// ) |
| 117 | +// |
| 118 | +// Deprecated: replace with predicates.ClusterPausedTransitionsOrInfrastructureReady when we move to v1beta2 |
| 119 | +func ClusterPausedTransitionsOrInfrastructureReady(scheme *runtime.Scheme, logger logr.Logger) predicate.Funcs { |
| 120 | + log := logger.WithValues("predicate", "ClusterPausedTransitionsOrInfrastructureReady") |
| 121 | + |
| 122 | + return predicates.Any(scheme, log, ClusterPausedTransitions(scheme, log), ClusterUpdateInfraReady(scheme, log)) |
| 123 | +} |
0 commit comments