From 833d549051c05db7e16a774cae1df9764a72badd Mon Sep 17 00:00:00 2001 From: Lukasz Szaszkiewicz Date: Thu, 3 Oct 2024 15:09:18 +0200 Subject: [PATCH] update the static pod state controller to use Apply --- .../staticpodstate_controller.go | 47 ++++++++++--------- pkg/operator/staticpod/controllers.go | 1 + 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pkg/operator/staticpod/controller/staticpodstate/staticpodstate_controller.go b/pkg/operator/staticpod/controller/staticpodstate/staticpodstate_controller.go index 72176a4c2b..3a7c323203 100644 --- a/pkg/operator/staticpod/controller/staticpodstate/staticpodstate_controller.go +++ b/pkg/operator/staticpod/controller/staticpodstate/staticpodstate_controller.go @@ -7,6 +7,7 @@ import ( "time" operatorv1 "github.com/openshift/api/operator/v1" + applyoperatorv1 "github.com/openshift/client-go/operator/applyconfigurations/operator/v1" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -25,9 +26,10 @@ import ( // StaticPodStateController is a controller that watches static pods and will produce a failing status if the // // static pods start crashing for some reason. type StaticPodStateController struct { - targetNamespace string - staticPodName string - operandName string + controllerInstanceName string + targetNamespace string + staticPodName string + operandName string operatorClient v1helpers.StaticPodOperatorClient podsGetter corev1client.PodsGetter @@ -37,7 +39,7 @@ type StaticPodStateController struct { // NewStaticPodStateController creates a controller that watches static pods and will produce a failing status if the // static pods start crashing for some reason. func NewStaticPodStateController( - targetNamespace, staticPodName, operandName string, + instanceName, targetNamespace, staticPodName, operandName string, kubeInformersForTargetNamespace informers.SharedInformerFactory, operatorClient v1helpers.StaticPodOperatorClient, podsGetter corev1client.PodsGetter, @@ -45,12 +47,13 @@ func NewStaticPodStateController( eventRecorder events.Recorder, ) factory.Controller { c := &StaticPodStateController{ - targetNamespace: targetNamespace, - staticPodName: staticPodName, - operandName: operandName, - operatorClient: operatorClient, - podsGetter: podsGetter, - versionRecorder: versionRecorder, + controllerInstanceName: factory.ControllerInstanceName(instanceName, "StaticPodState"), + targetNamespace: targetNamespace, + staticPodName: staticPodName, + operandName: operandName, + operatorClient: operatorClient, + podsGetter: podsGetter, + versionRecorder: versionRecorder, } return factory.New(). WithInformers( @@ -60,7 +63,7 @@ func NewStaticPodStateController( WithSync(c.sync). ResyncEvery(time.Minute). ToController( - "StaticPodStateController", // don't change what is passed here unless you also remove the old FooDegraded condition + c.controllerInstanceName, eventRecorder, ) } @@ -162,25 +165,25 @@ func (c *StaticPodStateController) sync(ctx context.Context, syncCtx factory.Syn } // update failing condition - cond := operatorv1.OperatorCondition{ - Type: condition.StaticPodsDegradedConditionType, - Status: operatorv1.ConditionFalse, - } + cond := applyoperatorv1.OperatorCondition(). + WithType(condition.StaticPodsDegradedConditionType). + WithStatus(operatorv1.ConditionFalse) // Failing errors if failingErrorCount > 0 { - cond.Status = operatorv1.ConditionTrue - cond.Reason = "Error" - cond.Message = v1helpers.NewMultiLineAggregate(errs).Error() + cond = cond.WithStatus(operatorv1.ConditionTrue). + WithReason("Error"). + WithMessage(v1helpers.NewMultiLineAggregate(errs).Error()) } // Not failing errors if failingErrorCount == 0 && len(errs) > 0 { - cond.Reason = "Error" - cond.Message = v1helpers.NewMultiLineAggregate(errs).Error() + cond = cond.WithReason("Error"). + WithMessage(v1helpers.NewMultiLineAggregate(errs).Error()) } - if _, _, updateError := v1helpers.UpdateStaticPodStatus(ctx, c.operatorClient, v1helpers.UpdateStaticPodConditionFn(cond), v1helpers.UpdateStaticPodConditionFn(cond)); updateError != nil { + + status := applyoperatorv1.StaticPodOperatorStatus().WithConditions(cond) + if updateError := c.operatorClient.ApplyStaticPodOperatorStatus(ctx, c.controllerInstanceName, status); updateError != nil { return updateError } - return err } diff --git a/pkg/operator/staticpod/controllers.go b/pkg/operator/staticpod/controllers.go index 1c6a23cffa..6a1c693c55 100644 --- a/pkg/operator/staticpod/controllers.go +++ b/pkg/operator/staticpod/controllers.go @@ -285,6 +285,7 @@ func (b *staticPodOperatorControllerBuilder) ToControllers() (manager.Controller if len(b.operandName) > 0 { // TODO add handling for operator configmap changes to get version-mapping changes manager.WithController(staticpodstate.NewStaticPodStateController( + b.operandName, b.operandNamespace, b.staticPodName, b.operandName,