|
| 1 | +package openshift |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + |
| 8 | + configv1 "github.com/openshift/api/config/v1" |
| 9 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 10 | + "k8s.io/apimachinery/pkg/runtime" |
| 11 | + utilerrors "k8s.io/apimachinery/pkg/util/errors" |
| 12 | + ctrl "sigs.k8s.io/controller-runtime" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 17 | + |
| 18 | + operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 19 | + olmversion "github.com/operator-framework/operator-lifecycle-manager/pkg/version" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + localSchemeBuilder = runtime.NewSchemeBuilder( |
| 24 | + configv1.AddToScheme, |
| 25 | + operatorsv1alpha1.AddToScheme, |
| 26 | + ) |
| 27 | + |
| 28 | + // AddToScheme adds all types necessary for the controller to operate. |
| 29 | + AddToScheme = localSchemeBuilder.AddToScheme |
| 30 | +) |
| 31 | + |
| 32 | +type ClusterOperatorReconciler struct { |
| 33 | + *ReconcilerConfig |
| 34 | + |
| 35 | + delayRequeue reconcile.Result |
| 36 | + mutate MutateFunc |
| 37 | + syncTracker *SyncTracker |
| 38 | + co *ClusterOperator |
| 39 | +} |
| 40 | + |
| 41 | +func NewClusterOperatorReconciler(opts ...ReconcilerOption) (*ClusterOperatorReconciler, error) { |
| 42 | + config := new(ReconcilerConfig) |
| 43 | + config.apply(opts) |
| 44 | + if err := config.complete(); err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + co := NewClusterOperator(config.Name) |
| 49 | + r := &ClusterOperatorReconciler{ |
| 50 | + ReconcilerConfig: config, |
| 51 | + delayRequeue: reconcile.Result{RequeueAfter: config.RequeueDelay}, |
| 52 | + co: co, |
| 53 | + syncTracker: NewSyncTracker(config.SyncCh, co), |
| 54 | + } |
| 55 | + |
| 56 | + var mutations SerialMutations |
| 57 | + if config.Mutator != nil { |
| 58 | + mutations = append(mutations, config.Mutator) |
| 59 | + } |
| 60 | + mutations = append(mutations, |
| 61 | + MutateFunc(r.setVersions), |
| 62 | + MutateFunc(r.setProgressing), |
| 63 | + MutateFunc(r.setAvailable), |
| 64 | + MutateFunc(r.setDegraded), |
| 65 | + MutateFunc(r.setUpgradeable), |
| 66 | + ) |
| 67 | + r.mutate = mutations.Mutate |
| 68 | + |
| 69 | + return r, nil |
| 70 | +} |
| 71 | + |
| 72 | +func (r *ClusterOperatorReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 73 | + if err := mgr.Add(r.syncTracker); err != nil { |
| 74 | + return fmt.Errorf("failed to add %T to manager: %s", r.syncTracker, err) |
| 75 | + } |
| 76 | + |
| 77 | + bldr := ctrl.NewControllerManagedBy(mgr). |
| 78 | + For(&configv1.ClusterOperator{}, builder.WithPredicates(watchName(&r.Name))). |
| 79 | + Watches(&source.Channel{Source: r.syncTracker.Events()}, &handler.EnqueueRequestForObject{}) |
| 80 | + |
| 81 | + return r.TweakBuilder(bldr).Complete(r) |
| 82 | +} |
| 83 | + |
| 84 | +func (r *ClusterOperatorReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { |
| 85 | + log := r.Log.WithValues("request", req) |
| 86 | + noRequeue := reconcile.Result{} |
| 87 | + if req.NamespacedName.Name != r.co.GetName() { |
| 88 | + // Throw away requests to reconcile all ClusterOperators but our own |
| 89 | + // These should already be filtered by controller-runtime by this point |
| 90 | + return noRequeue, nil |
| 91 | + } |
| 92 | + |
| 93 | + // Get the ClusterOperator |
| 94 | + in := &configv1.ClusterOperator{} |
| 95 | + if err := r.Client.Get(ctx, req.NamespacedName, in); err != nil { |
| 96 | + if apierrors.IsNotFound(err) { |
| 97 | + // The ClusterOperator is missing, let's create it |
| 98 | + stripObject(r.co.ClusterOperator) |
| 99 | + err = r.Client.Create(ctx, r.co.ClusterOperator) |
| 100 | + } |
| 101 | + |
| 102 | + // Transient error or successful creation, requeue |
| 103 | + return r.delayRequeue, err |
| 104 | + } |
| 105 | + |
| 106 | + r.co.ClusterOperator = in.DeepCopy() |
| 107 | + |
| 108 | + var errs []error |
| 109 | + res := reconcile.Result{} |
| 110 | + if err := r.mutate(ctx, r.co); err != nil { |
| 111 | + // Transitive error, requeue |
| 112 | + log.Error(err, "Error mutating ClusterOperator") |
| 113 | + errs = append(errs, err) |
| 114 | + res = r.delayRequeue |
| 115 | + } |
| 116 | + |
| 117 | + if !reflect.DeepEqual(r.co.Status, in.Status) { |
| 118 | + // Status change detected, update |
| 119 | + if err := r.Client.Status().Update(ctx, r.co.ClusterOperator); err != nil { |
| 120 | + // Transitive error, requeue |
| 121 | + errs = append(errs, err) |
| 122 | + res = r.delayRequeue |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return res, utilerrors.NewAggregate(errs) |
| 127 | +} |
| 128 | + |
| 129 | +func (r *ClusterOperatorReconciler) setVersions(_ context.Context, co *ClusterOperator) error { |
| 130 | + // If we've successfully synced, we know our operator is working properly, so we can update the version |
| 131 | + if r.syncTracker.SuccessfulSyncs() > 0 && !reflect.DeepEqual(co.Status.Versions, r.TargetVersions) { |
| 132 | + co.Status.Versions = r.TargetVersions |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func (r *ClusterOperatorReconciler) setProgressing(_ context.Context, co *ClusterOperator) error { |
| 139 | + desired := &configv1.ClusterOperatorStatusCondition{ |
| 140 | + Type: configv1.OperatorProgressing, |
| 141 | + LastTransitionTime: r.Now(), |
| 142 | + } |
| 143 | + |
| 144 | + if r.syncTracker.SuccessfulSyncs() > 0 && reflect.DeepEqual(co.Status.Versions, r.TargetVersions) { |
| 145 | + desired.Status = configv1.ConditionFalse |
| 146 | + desired.Message = fmt.Sprintf("Deployed %s", olmversion.OLMVersion) |
| 147 | + } else { |
| 148 | + desired.Status = configv1.ConditionTrue |
| 149 | + desired.Message = fmt.Sprintf("Waiting to see update %s succeed", olmversion.OLMVersion) |
| 150 | + } |
| 151 | + |
| 152 | + current := co.GetCondition(configv1.OperatorProgressing) |
| 153 | + if conditionsEqual(current, desired) { // Comparison ignores lastUpdated |
| 154 | + return nil |
| 155 | + } |
| 156 | + |
| 157 | + co.SetCondition(desired) |
| 158 | + |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +func (r *ClusterOperatorReconciler) setAvailable(_ context.Context, co *ClusterOperator) error { |
| 163 | + desired := &configv1.ClusterOperatorStatusCondition{ |
| 164 | + Type: configv1.OperatorAvailable, |
| 165 | + LastTransitionTime: r.Now(), |
| 166 | + } |
| 167 | + |
| 168 | + if r.syncTracker.SuccessfulSyncs() > 0 && reflect.DeepEqual(co.Status.Versions, r.TargetVersions) { |
| 169 | + desired.Status = configv1.ConditionTrue |
| 170 | + } else { |
| 171 | + desired.Status = configv1.ConditionFalse |
| 172 | + } |
| 173 | + |
| 174 | + current := co.GetCondition(configv1.OperatorAvailable) |
| 175 | + if conditionsEqual(current, desired) { // Comparison ignores lastUpdated |
| 176 | + return nil |
| 177 | + } |
| 178 | + |
| 179 | + co.SetCondition(desired) |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +func (r *ClusterOperatorReconciler) setDegraded(_ context.Context, co *ClusterOperator) error { |
| 185 | + desired := &configv1.ClusterOperatorStatusCondition{ |
| 186 | + Type: configv1.OperatorDegraded, |
| 187 | + LastTransitionTime: r.Now(), |
| 188 | + } |
| 189 | + |
| 190 | + if r.syncTracker.SuccessfulSyncs() > 0 && reflect.DeepEqual(co.Status.Versions, r.TargetVersions) { |
| 191 | + desired.Status = configv1.ConditionFalse |
| 192 | + } else { |
| 193 | + desired.Status = configv1.ConditionTrue |
| 194 | + desired.Message = "Waiting for updates to take effect" |
| 195 | + } |
| 196 | + |
| 197 | + current := co.GetCondition(configv1.OperatorDegraded) |
| 198 | + if conditionsEqual(current, desired) { // Comparison ignores lastUpdated |
| 199 | + return nil |
| 200 | + } |
| 201 | + |
| 202 | + co.SetCondition(desired) |
| 203 | + |
| 204 | + return nil |
| 205 | +} |
| 206 | + |
| 207 | +const ( |
| 208 | + IncompatibleOperatorsInstalled = "IncompatibleOperatorsInstalled" |
| 209 | +) |
| 210 | + |
| 211 | +func (r *ClusterOperatorReconciler) setUpgradeable(ctx context.Context, co *ClusterOperator) error { |
| 212 | + desired := &configv1.ClusterOperatorStatusCondition{ |
| 213 | + Type: configv1.OperatorUpgradeable, |
| 214 | + Status: configv1.ConditionTrue, |
| 215 | + LastTransitionTime: r.Now(), |
| 216 | + } |
| 217 | + |
| 218 | + // Set upgradeable=false if (either/or): |
| 219 | + // 1. OLM currently upgrading (takes priorty in the status message) |
| 220 | + // 2. Operators currently installed that are incompatible with the next OCP minor version |
| 221 | + if r.syncTracker.SuccessfulSyncs() < 1 || !reflect.DeepEqual(co.Status.Versions, r.TargetVersions) { |
| 222 | + // OLM is still upgrading |
| 223 | + desired.Status = configv1.ConditionFalse |
| 224 | + desired.Message = "Waiting for updates to take effect" |
| 225 | + } else { |
| 226 | + incompatible, err := incompatibleOperators(ctx, r.Client) |
| 227 | + if err != nil { |
| 228 | + return err |
| 229 | + } |
| 230 | + |
| 231 | + if len(incompatible) > 0 { |
| 232 | + // Some incompatible operator is installed |
| 233 | + desired.Status = configv1.ConditionFalse |
| 234 | + desired.Reason = IncompatibleOperatorsInstalled |
| 235 | + desired.Message = incompatible.String() // TODO: Truncate message to field length |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + current := co.GetCondition(configv1.OperatorUpgradeable) |
| 240 | + if conditionsEqual(current, desired) { // Comparison ignores lastUpdated |
| 241 | + return nil |
| 242 | + } |
| 243 | + |
| 244 | + co.SetCondition(desired) |
| 245 | + |
| 246 | + return nil |
| 247 | +} |
0 commit comments