|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 8 | + configv1client "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1" |
| 9 | + configv1informers "github.com/openshift/client-go/config/informers/externalversions/config/v1" |
| 10 | + applyoperatorv1 "github.com/openshift/client-go/operator/applyconfigurations/operator/v1" |
| 11 | + |
| 12 | + "github.com/openshift/library-go/pkg/controller/factory" |
| 13 | + "github.com/openshift/library-go/pkg/operator/events" |
| 14 | + operatorv1helpers "github.com/openshift/library-go/pkg/operator/v1helpers" |
| 15 | +) |
| 16 | + |
| 17 | +type kmsPreflightController struct { |
| 18 | + controllerInstanceName string |
| 19 | + |
| 20 | + operatorClient operatorv1helpers.OperatorClient |
| 21 | + apiServerClient configv1client.APIServerInterface |
| 22 | + |
| 23 | + provider Provider |
| 24 | + preconditionsFulfilledFn preconditionsFulfilled |
| 25 | +} |
| 26 | + |
| 27 | +// TODO: document NewKMSPreflightController |
| 28 | +func NewKMSPreflightController( |
| 29 | + instanceName string, |
| 30 | + provider Provider, |
| 31 | + preconditionsFulfilledFn preconditionsFulfilled, |
| 32 | + operatorClient operatorv1helpers.OperatorClient, |
| 33 | + apiServerClient configv1client.APIServerInterface, |
| 34 | + apiServerInformer configv1informers.APIServerInformer, |
| 35 | + eventRecorder events.Recorder, |
| 36 | +) factory.Controller { |
| 37 | + c := &kmsPreflightController{ |
| 38 | + controllerInstanceName: factory.ControllerInstanceName(instanceName, "EncryptionKMSPreflight"), |
| 39 | + |
| 40 | + operatorClient: operatorClient, |
| 41 | + apiServerClient: apiServerClient, |
| 42 | + |
| 43 | + provider: provider, |
| 44 | + preconditionsFulfilledFn: preconditionsFulfilledFn, |
| 45 | + } |
| 46 | + |
| 47 | + return factory.New().ResyncEvery(time.Minute).WithSync(c.sync).WithControllerInstanceName(c.controllerInstanceName).WithInformers( |
| 48 | + apiServerInformer.Informer(), |
| 49 | + operatorClient.Informer(), |
| 50 | + ).ToController( |
| 51 | + c.controllerInstanceName, |
| 52 | + eventRecorder.WithComponentSuffix("encryption-kms-preflight-controller"), |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +func (c *kmsPreflightController) sync(ctx context.Context, syncCtx factory.SyncContext) (err error) { |
| 57 | + degradedCondition := applyoperatorv1.OperatorCondition().WithType("KMSPreflightControllerDegraded") |
| 58 | + |
| 59 | + defer func() { |
| 60 | + if degradedCondition == nil { |
| 61 | + return |
| 62 | + } |
| 63 | + status := applyoperatorv1.OperatorStatus().WithConditions(degradedCondition) |
| 64 | + if applyError := c.operatorClient.ApplyOperatorStatus(ctx, c.controllerInstanceName, status); applyError != nil { |
| 65 | + err = applyError |
| 66 | + } |
| 67 | + }() |
| 68 | + |
| 69 | + if ready, err := shouldRunEncryptionController(c.operatorClient, c.preconditionsFulfilledFn, c.provider.ShouldRunEncryptionControllers); err != nil || !ready { |
| 70 | + if err != nil { |
| 71 | + degradedCondition = nil |
| 72 | + } else { |
| 73 | + degradedCondition = degradedCondition.WithStatus(operatorv1.ConditionFalse) |
| 74 | + } |
| 75 | + return err // we will get re-kicked when the operator status updates |
| 76 | + } |
| 77 | + |
| 78 | + preflightErr := c.runPreflightChecks(ctx) |
| 79 | + if preflightErr != nil { |
| 80 | + degradedCondition = degradedCondition. |
| 81 | + WithStatus(operatorv1.ConditionTrue). |
| 82 | + WithReason("Error"). |
| 83 | + WithMessage(preflightErr.Error()) |
| 84 | + } else { |
| 85 | + degradedCondition = degradedCondition. |
| 86 | + WithStatus(operatorv1.ConditionFalse) |
| 87 | + } |
| 88 | + return preflightErr |
| 89 | +} |
| 90 | + |
| 91 | +func (c *kmsPreflightController) runPreflightChecks(ctx context.Context) error { |
| 92 | + // TODO: implement me |
| 93 | + return nil |
| 94 | +} |
0 commit comments