Skip to content

Commit aea0038

Browse files
committed
update the installer state controller to use Apply
1 parent 2402ef3 commit aea0038

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

pkg/operator/staticpod/controller/installerstate/installer_state_controller.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
1515

1616
operatorv1 "github.com/openshift/api/operator/v1"
17-
17+
applyoperatorv1 "github.com/openshift/client-go/operator/applyconfigurations/operator/v1"
1818
"github.com/openshift/library-go/pkg/controller/factory"
1919
"github.com/openshift/library-go/pkg/operator/events"
2020
"github.com/openshift/library-go/pkg/operator/v1helpers"
@@ -27,35 +27,38 @@ var maxToleratedPodPendingDuration = 5 * time.Minute
2727

2828
// InstallerStateController analyzes installer pods and sets degraded conditions suggesting different root causes.
2929
type InstallerStateController struct {
30-
podsGetter corev1client.PodsGetter
31-
eventsGetter corev1client.EventsGetter
32-
targetNamespace string
33-
operatorClient v1helpers.StaticPodOperatorClient
30+
controllerInstanceName string
31+
podsGetter corev1client.PodsGetter
32+
eventsGetter corev1client.EventsGetter
33+
targetNamespace string
34+
operatorClient v1helpers.StaticPodOperatorClient
3435

3536
timeNowFn func() time.Time
3637
}
3738

38-
func NewInstallerStateController(kubeInformersForTargetNamespace informers.SharedInformerFactory,
39+
func NewInstallerStateController(instanceName string,
40+
kubeInformersForTargetNamespace informers.SharedInformerFactory,
3941
podsGetter corev1client.PodsGetter,
4042
eventsGetter corev1client.EventsGetter,
4143
operatorClient v1helpers.StaticPodOperatorClient,
4244
targetNamespace string,
4345
recorder events.Recorder,
4446
) factory.Controller {
4547
c := &InstallerStateController{
46-
podsGetter: podsGetter,
47-
eventsGetter: eventsGetter,
48-
targetNamespace: targetNamespace,
49-
operatorClient: operatorClient,
50-
timeNowFn: time.Now,
48+
controllerInstanceName: factory.ControllerInstanceName(instanceName, "InstallerState"),
49+
podsGetter: podsGetter,
50+
eventsGetter: eventsGetter,
51+
targetNamespace: targetNamespace,
52+
operatorClient: operatorClient,
53+
timeNowFn: time.Now,
5154
}
5255

5356
return factory.New().
5457
WithInformers(kubeInformersForTargetNamespace.Core().V1().Pods().Informer()).
5558
WithSync(c.sync).
5659
ResyncEvery(1*time.Minute).
5760
ToController(
58-
"InstallerStateController", // don't change what is passed here unless you also remove the old FooDegraded condition
61+
c.controllerInstanceName,
5962
recorder,
6063
)
6164
}
@@ -128,26 +131,25 @@ func (c *InstallerStateController) sync(ctx context.Context, syncCtx factory.Syn
128131
}
129132
foundConditions = append(foundConditions, networkConditions...)
130133

131-
updateConditionFuncs := []v1helpers.UpdateStaticPodStatusFunc{}
132-
134+
updateConditions := []*applyoperatorv1.OperatorConditionApplyConfiguration{}
133135
// check the supported degraded foundConditions and check if any pending pod matching them.
134136
for _, degradedConditionName := range degradedConditionNames {
135137
// clean up existing foundConditions
136-
updatedCondition := operatorv1.OperatorCondition{
137-
Type: degradedConditionName,
138-
Status: operatorv1.ConditionFalse,
139-
}
138+
updatedCondition := applyoperatorv1.OperatorCondition().
139+
WithType(degradedConditionName).
140+
WithStatus(operatorv1.ConditionFalse)
141+
140142
if condition := v1helpers.FindOperatorCondition(foundConditions, degradedConditionName); condition != nil {
141-
updatedCondition = *condition
143+
updatedCondition = updatedCondition.
144+
WithStatus(condition.Status).
145+
WithReason(condition.Reason).
146+
WithMessage(condition.Message)
142147
}
143-
updateConditionFuncs = append(updateConditionFuncs, v1helpers.UpdateStaticPodConditionFn(updatedCondition))
144-
}
145-
146-
if _, _, err := v1helpers.UpdateStaticPodStatus(ctx, c.operatorClient, updateConditionFuncs...); err != nil {
147-
return err
148+
updateConditions = append(updateConditions, updatedCondition)
148149
}
149150

150-
return nil
151+
status := applyoperatorv1.StaticPodOperatorStatus().WithConditions(updateConditions...)
152+
return c.operatorClient.ApplyStaticPodOperatorStatus(ctx, c.controllerInstanceName, status)
151153
}
152154

153155
func (c *InstallerStateController) handlePendingInstallerPodsNetworkEvents(ctx context.Context, recorder events.Recorder, pods []*v1.Pod) ([]operatorv1.OperatorCondition, error) {

pkg/operator/staticpod/controller/installerstate/installer_state_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func TestInstallerStateController(t *testing.T) {
188188

189189
fakeStaticPodOperatorClient := v1helpers.NewFakeStaticPodOperatorClient(&operatorv1.StaticPodOperatorSpec{}, &operatorv1.StaticPodOperatorStatus{}, nil, nil)
190190
eventRecorder := eventstesting.NewTestingEventRecorder(t)
191-
controller := NewInstallerStateController(kubeInformers, kubeClient.CoreV1(), kubeClient.CoreV1(), fakeStaticPodOperatorClient, "test", eventRecorder)
191+
controller := NewInstallerStateController("unit-test", kubeInformers, kubeClient.CoreV1(), kubeClient.CoreV1(), fakeStaticPodOperatorClient, "test", eventRecorder)
192192
if err := controller.Sync(context.TODO(), factory.NewSyncContext("InstallerStateController", eventRecorder)); err != nil {
193193
t.Error(err)
194194
return

pkg/operator/staticpod/controllers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ func (b *staticPodOperatorControllerBuilder) ToControllers() (manager.Controller
269269
), 1)
270270

271271
manager.WithController(installerstate.NewInstallerStateController(
272+
b.operandName,
272273
operandInformers,
273274
podClient,
274275
eventsClient,

pkg/operator/v1helpers/test_helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func (c *fakeStaticPodOperatorClient) ApplyStaticPodOperatorSpec(ctx context.Con
171171
}
172172

173173
func (c *fakeStaticPodOperatorClient) ApplyStaticPodOperatorStatus(ctx context.Context, fieldManager string, applyConfiguration *applyoperatorv1.StaticPodOperatorStatusApplyConfiguration) (err error) {
174+
c.fakeStaticPodOperatorStatus.OperatorStatus = *convertOperatorStatusApplyConfiguration(&applyConfiguration.OperatorStatusApplyConfiguration)
174175
return nil
175176
}
176177

0 commit comments

Comments
 (0)