From c1b2fd2beb30e52635e414951144767570a51b9f Mon Sep 17 00:00:00 2001 From: fabriziopandini Date: Mon, 11 Nov 2024 12:38:25 +0100 Subject: [PATCH] Audit patch withOwnedConditions --- .../controllers/cluster/cluster_controller.go | 14 ++++++ .../machinehealthcheck_controller.go | 48 +++++++++++++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/internal/controllers/cluster/cluster_controller.go b/internal/controllers/cluster/cluster_controller.go index de05d0e5f4ce..1c53908e9180 100644 --- a/internal/controllers/cluster/cluster_controller.go +++ b/internal/controllers/cluster/cluster_controller.go @@ -251,6 +251,20 @@ func patchCluster(ctx context.Context, patchHelper *patch.Helper, cluster *clust clusterv1.ControlPlaneReadyCondition, clusterv1.InfrastructureReadyCondition, }}, + patch.WithOwnedV1Beta2Conditions{Conditions: []string{ + clusterv1.ClusterInfrastructureReadyV1Beta2Condition, + clusterv1.ClusterControlPlaneAvailableV1Beta2Condition, + clusterv1.ClusterControlPlaneInitializedV1Beta2Condition, + clusterv1.ClusterWorkersAvailableV1Beta2Condition, + clusterv1.ClusterMachinesReadyV1Beta2Condition, + clusterv1.ClusterMachinesUpToDateV1Beta2Condition, + clusterv1.ClusterRemoteConnectionProbeV1Beta2Condition, + clusterv1.ClusterScalingUpV1Beta2Condition, + clusterv1.ClusterScalingDownV1Beta2Condition, + clusterv1.ClusterRemediatingV1Beta2Condition, + clusterv1.ClusterDeletingV1Beta2Condition, + clusterv1.ClusterAvailableV1Beta2Condition, + }}, ) return patchHelper.Patch(ctx, cluster, options...) } diff --git a/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go b/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go index 28cd2c230a0f..6e2c135037c5 100644 --- a/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go +++ b/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go @@ -160,7 +160,14 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re defer func() { // Always attempt to patch the object and status after each reconciliation. // Patch ObservedGeneration only if the reconciliation completed successfully - patchOpts := []patch.Option{} + patchOpts := []patch.Option{ + patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{ + clusterv1.RemediationAllowedCondition, + }}, + patch.WithOwnedV1Beta2Conditions{Conditions: []string{ + clusterv1.MachineHealthCheckRemediationAllowedV1Beta2Condition, + }}, + } if reterr == nil { patchOpts = append(patchOpts, patch.WithStatusObservedGeneration{}) } @@ -300,7 +307,18 @@ func (r *Reconciler) reconcile(ctx context.Context, logger logr.Logger, cluster } errList := []error{} for _, t := range append(healthy, unhealthy...) { - if err := t.patchHelper.Patch(ctx, t.Machine); err != nil { + patchOpts := []patch.Option{ + patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{ + clusterv1.MachineHealthCheckSucceededCondition, + // Note: intentionally leaving out OwnerRemediated condition which is mostly controlled by the owner. + }}, + patch.WithOwnedV1Beta2Conditions{Conditions: []string{ + clusterv1.MachineHealthCheckSucceededV1Beta2Condition, + // Note: intentionally leaving out OwnerRemediated condition which is mostly controlled by the owner. + // (Same for ExternallyRemediated condition) + }}, + } + if err := t.patchHelper.Patch(ctx, t.Machine, patchOpts...); err != nil { errList = append(errList, errors.Wrapf(err, "failed to patch machine status for machine: %s/%s", t.Machine.Namespace, t.Machine.Name)) continue } @@ -380,7 +398,18 @@ func (r *Reconciler) patchHealthyTargets(ctx context.Context, logger logr.Logger } } - if err := t.patchHelper.Patch(ctx, t.Machine); err != nil { + patchOpts := []patch.Option{ + patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{ + clusterv1.MachineHealthCheckSucceededCondition, + // Note: intentionally leaving out OwnerRemediated condition which is mostly controlled by the owner. + }}, + patch.WithOwnedV1Beta2Conditions{Conditions: []string{ + clusterv1.MachineHealthCheckSucceededV1Beta2Condition, + // Note: intentionally leaving out OwnerRemediated condition which is mostly controlled by the owner. + // (Same for ExternallyRemediated condition) + }}, + } + if err := t.patchHelper.Patch(ctx, t.Machine, patchOpts...); err != nil { logger.Error(err, "failed to patch healthy machine status for machine", "Machine", klog.KObj(t.Machine)) errList = append(errList, errors.Wrapf(err, "failed to patch healthy machine status for machine: %s/%s", t.Machine.Namespace, t.Machine.Name)) } @@ -486,7 +515,18 @@ func (r *Reconciler) patchUnhealthyTargets(ctx context.Context, logger logr.Logg } } - if err := t.patchHelper.Patch(ctx, t.Machine); err != nil { + patchOpts := []patch.Option{ + patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{ + clusterv1.MachineHealthCheckSucceededCondition, + // Note: intentionally leaving out OwnerRemediated condition which is mostly controlled by the owner. + }}, + patch.WithOwnedV1Beta2Conditions{Conditions: []string{ + clusterv1.MachineHealthCheckSucceededV1Beta2Condition, + // Note: intentionally leaving out OwnerRemediated condition which is mostly controlled by the owner. + // (Same for ExternallyRemediated condition) + }}, + } + if err := t.patchHelper.Patch(ctx, t.Machine, patchOpts...); err != nil { errList = append(errList, errors.Wrapf(err, "failed to patch unhealthy machine status for machine: %s/%s", t.Machine.Namespace, t.Machine.Name)) continue }