|
| 1 | +/* |
| 2 | +SPDX-FileCopyrightText: Copyright 2024 SAP SE or an SAP affiliate company and cobaltcore-dev contributors |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package ready |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + |
| 23 | + "k8s.io/apimachinery/pkg/api/equality" |
| 24 | + "k8s.io/apimachinery/pkg/api/meta" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 29 | + k8sclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 31 | + logger "sigs.k8s.io/controller-runtime/pkg/log" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 33 | + |
| 34 | + kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + ControllerName = "ready" |
| 39 | +) |
| 40 | + |
| 41 | +// StatusChangedPredicate triggers reconciliation only when the status subresource changes. |
| 42 | +// This is the inverse of GenerationChangedPredicate which triggers on spec changes. |
| 43 | +type StatusChangedPredicate struct{} |
| 44 | + |
| 45 | +var _ predicate.Predicate = StatusChangedPredicate{} |
| 46 | + |
| 47 | +func (StatusChangedPredicate) Create(_ event.CreateEvent) bool { |
| 48 | + // Reconcile on create to compute initial Ready condition |
| 49 | + return true |
| 50 | +} |
| 51 | + |
| 52 | +func (StatusChangedPredicate) Delete(_ event.DeleteEvent) bool { |
| 53 | + // No need to reconcile on delete |
| 54 | + return false |
| 55 | +} |
| 56 | + |
| 57 | +func (StatusChangedPredicate) Update(e event.UpdateEvent) bool { |
| 58 | + if e.ObjectOld == nil || e.ObjectNew == nil { |
| 59 | + return false |
| 60 | + } |
| 61 | + |
| 62 | + oldHv, ok := e.ObjectOld.(*kvmv1.Hypervisor) |
| 63 | + if !ok { |
| 64 | + return false |
| 65 | + } |
| 66 | + newHv, ok := e.ObjectNew.(*kvmv1.Hypervisor) |
| 67 | + if !ok { |
| 68 | + return false |
| 69 | + } |
| 70 | + |
| 71 | + // Only trigger if status changed (ignore spec-only changes) |
| 72 | + return !equality.Semantic.DeepEqual(oldHv.Status, newHv.Status) |
| 73 | +} |
| 74 | + |
| 75 | +func (StatusChangedPredicate) Generic(_ event.GenericEvent) bool { |
| 76 | + return true |
| 77 | +} |
| 78 | + |
| 79 | +// Controller reconciles the Ready condition based on other conditions |
| 80 | +type Controller struct { |
| 81 | + k8sclient.Client |
| 82 | + Scheme *runtime.Scheme |
| 83 | +} |
| 84 | + |
| 85 | +// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors,verbs=get;list;watch |
| 86 | +// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors/status,verbs=get;list;watch;patch |
| 87 | + |
| 88 | +func (r *Controller) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 89 | + log := logger.FromContext(ctx).WithName(req.Name) |
| 90 | + ctx = logger.IntoContext(ctx, log) |
| 91 | + |
| 92 | + hv := &kvmv1.Hypervisor{} |
| 93 | + if err := r.Get(ctx, req.NamespacedName, hv); err != nil { |
| 94 | + return ctrl.Result{}, k8sclient.IgnoreNotFound(err) |
| 95 | + } |
| 96 | + |
| 97 | + base := hv.DeepCopy() |
| 98 | + |
| 99 | + // Compute Ready condition based on other conditions |
| 100 | + readyCondition := ComputeReadyCondition(hv) |
| 101 | + meta.SetStatusCondition(&hv.Status.Conditions, readyCondition) |
| 102 | + |
| 103 | + if equality.Semantic.DeepEqual(hv.Status, base.Status) { |
| 104 | + return ctrl.Result{}, nil |
| 105 | + } |
| 106 | + |
| 107 | + log.Info("Updating Ready condition", "status", readyCondition.Status, "reason", readyCondition.Reason) |
| 108 | + return ctrl.Result{}, r.Status().Patch(ctx, hv, k8sclient.MergeFromWithOptions(base, |
| 109 | + k8sclient.MergeFromWithOptimisticLock{}), k8sclient.FieldOwner(ControllerName)) |
| 110 | +} |
| 111 | + |
| 112 | +// ComputeReadyCondition determines the Ready condition based on other conditions |
| 113 | +func ComputeReadyCondition(hv *kvmv1.Hypervisor) metav1.Condition { |
| 114 | + // Priority 1: Offboarded |
| 115 | + if meta.IsStatusConditionTrue(hv.Status.Conditions, kvmv1.ConditionTypeOffboarded) { |
| 116 | + return metav1.Condition{ |
| 117 | + Type: kvmv1.ConditionTypeReady, |
| 118 | + Status: metav1.ConditionFalse, |
| 119 | + Reason: "Offboarded", |
| 120 | + Message: "Hypervisor has been offboarded", |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Priority 2: Terminating |
| 125 | + if meta.IsStatusConditionTrue(hv.Status.Conditions, kvmv1.ConditionTypeTerminating) { |
| 126 | + return metav1.Condition{ |
| 127 | + Type: kvmv1.ConditionTypeReady, |
| 128 | + Status: metav1.ConditionFalse, |
| 129 | + Reason: kvmv1.ConditionReasonTerminating, |
| 130 | + Message: "Hypervisor is terminating", |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Priority 3/5: Onboarding state |
| 135 | + onboardingCondition := meta.FindStatusCondition(hv.Status.Conditions, kvmv1.ConditionTypeOnboarding) |
| 136 | + if onboardingCondition == nil { |
| 137 | + // Onboarding not started |
| 138 | + return metav1.Condition{ |
| 139 | + Type: kvmv1.ConditionTypeReady, |
| 140 | + Status: metav1.ConditionFalse, |
| 141 | + Reason: kvmv1.ConditionReasonOnboarding, |
| 142 | + Message: "Onboarding not started", |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Active onboarding (Status=True means onboarding is in progress) |
| 147 | + if onboardingCondition.Status == metav1.ConditionTrue { |
| 148 | + return metav1.Condition{ |
| 149 | + Type: kvmv1.ConditionTypeReady, |
| 150 | + Status: metav1.ConditionFalse, |
| 151 | + Reason: kvmv1.ConditionReasonOnboarding, |
| 152 | + Message: "Onboarding in progress: " + onboardingCondition.Message, |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // Onboarding aborted |
| 157 | + if onboardingCondition.Reason == kvmv1.ConditionReasonAborted { |
| 158 | + return metav1.Condition{ |
| 159 | + Type: kvmv1.ConditionTypeReady, |
| 160 | + Status: metav1.ConditionFalse, |
| 161 | + Reason: kvmv1.ConditionReasonOnboarding, |
| 162 | + Message: "Onboarding was aborted", |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Priority 4: Maintenance mode |
| 167 | + if hv.Spec.Maintenance != "" && hv.Spec.Maintenance != kvmv1.MaintenanceUnset { |
| 168 | + evictingCondition := meta.FindStatusCondition(hv.Status.Conditions, kvmv1.ConditionTypeEvicting) |
| 169 | + if evictingCondition != nil { |
| 170 | + if evictingCondition.Status == metav1.ConditionTrue { |
| 171 | + return metav1.Condition{ |
| 172 | + Type: kvmv1.ConditionTypeReady, |
| 173 | + Status: metav1.ConditionFalse, |
| 174 | + Reason: kvmv1.ConditionReasonReadyEvicting, |
| 175 | + Message: "Hypervisor is disabled and evicting", |
| 176 | + } |
| 177 | + } |
| 178 | + if evictingCondition.Status == metav1.ConditionFalse { |
| 179 | + return metav1.Condition{ |
| 180 | + Type: kvmv1.ConditionTypeReady, |
| 181 | + Status: metav1.ConditionFalse, |
| 182 | + Reason: kvmv1.ConditionReasonReadyEvicted, |
| 183 | + Message: "Hypervisor is disabled and evicted", |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + return metav1.Condition{ |
| 188 | + Type: kvmv1.ConditionTypeReady, |
| 189 | + Status: metav1.ConditionFalse, |
| 190 | + Reason: kvmv1.ConditionReasonReadyMaintenance, |
| 191 | + Message: "Hypervisor is in maintenance mode", |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + // Check onboarding succeeded |
| 196 | + if onboardingCondition.Reason != kvmv1.ConditionReasonSucceeded { |
| 197 | + return metav1.Condition{ |
| 198 | + Type: kvmv1.ConditionTypeReady, |
| 199 | + Status: metav1.ConditionFalse, |
| 200 | + Reason: kvmv1.ConditionReasonOnboarding, |
| 201 | + Message: "Onboarding not yet completed", |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + // Priority 6: All checks passed - Ready |
| 206 | + return metav1.Condition{ |
| 207 | + Type: kvmv1.ConditionTypeReady, |
| 208 | + Status: metav1.ConditionTrue, |
| 209 | + Reason: kvmv1.ConditionReasonReadyReady, |
| 210 | + Message: "Hypervisor is ready", |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +func (r *Controller) SetupWithManager(mgr ctrl.Manager) error { |
| 215 | + return ctrl.NewControllerManagedBy(mgr). |
| 216 | + Named(ControllerName). |
| 217 | + For(&kvmv1.Hypervisor{}, builder.WithPredicates(StatusChangedPredicate{})). |
| 218 | + Complete(r) |
| 219 | +} |
0 commit comments