Skip to content

Commit

Permalink
Merge pull request #1816 from p0lyn0mial/apply-static-pod-state
Browse files Browse the repository at this point in the history
API-1835: Update the static pod state controller to use Apply
  • Loading branch information
openshift-merge-bot[bot] authored Oct 7, 2024
2 parents 45b605e + 833d549 commit 636f95d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -37,20 +39,21 @@ 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,
versionRecorder status.VersionGetter,
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(
Expand All @@ -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,
)
}
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions pkg/operator/staticpod/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 636f95d

Please sign in to comment.