Skip to content

Commit c7822c3

Browse files
map deployment status according to that of operator
1 parent 583d4b9 commit c7822c3

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

config/samples/operators_v1alpha1_operator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ metadata:
1010
name: operator-sample
1111
spec:
1212
# TODO(user): Add fields here
13-
packageName: prometheus
13+
packageName: lightbend-console-operator

controllers/operator_controller.go

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,35 @@ func (r *OperatorReconciler) reconcile(ctx context.Context, op *operatorsv1alpha
162162
return ctrl.Result{}, err
163163
}
164164

165-
// update operator status
165+
// convert existing unstructured object into bundleDeployment for easier mapping of status.
166+
existingTypedBundleDeployment := &rukpakv1alpha1.BundleDeployment{}
167+
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(dep.UnstructuredContent(), existingTypedBundleDeployment); err != nil {
168+
return ctrl.Result{}, err
169+
}
170+
171+
// update operator status:
172+
// 1. If the Operator "Ready" status is "Unknown": The status of successful bundleDeployment is unknown, requeue the request.
173+
// 2. If the Operator "Ready" status is "True": Update the "successful resolution" status and return the result.
174+
// 3. If the Operator "Ready" status is "False": There is error observed from Rukpak. Update the status accordingly.
175+
status, message := verifyBDStatus(existingTypedBundleDeployment)
176+
var reason string
177+
if status == metav1.ConditionTrue || status == metav1.ConditionUnknown {
178+
reason = operatorsv1alpha1.ReasonResolutionSucceeded
179+
} else {
180+
reason = operatorsv1alpha1.ReasonBundleDeploymentFailed
181+
}
182+
166183
apimeta.SetStatusCondition(&op.Status.Conditions, metav1.Condition{
167184
Type: operatorsv1alpha1.TypeReady,
168-
Status: metav1.ConditionTrue,
169-
Reason: operatorsv1alpha1.ReasonResolutionSucceeded,
170-
Message: "resolution was successful",
185+
Status: status,
186+
Reason: reason,
187+
Message: message,
171188
ObservedGeneration: op.GetGeneration(),
172189
})
190+
191+
if status == metav1.ConditionUnknown || status == metav1.ConditionFalse {
192+
return ctrl.Result{Requeue: true}, nil
193+
}
173194
return ctrl.Result{}, nil
174195
}
175196

@@ -259,14 +280,11 @@ func (r *OperatorReconciler) ensureBundleDeployment(ctx context.Context, desired
259280
// If the existing BD already has everything that the desired BD has, no need to contact the API server.
260281
// Make sure the status of the existingBD from the server is as expected.
261282
if equality.Semantic.DeepDerivative(desiredBundleDeployment, existingBundleDeployment) {
262-
return verifySuccessfulBDStatus(existingBundleDeployment)
263-
}
264-
265-
if err = r.Client.Patch(ctx, desiredBundleDeployment, client.Apply, client.ForceOwnership, client.FieldOwner("operator-controller")); err != nil {
266-
return err
283+
*desiredBundleDeployment = *existingBundleDeployment
284+
return nil
267285
}
268286

269-
return verifySuccessfulBDStatus(desiredBundleDeployment)
287+
return r.Client.Patch(ctx, desiredBundleDeployment, client.Apply, client.ForceOwnership, client.FieldOwner("operator-controller"))
270288
}
271289

272290
func (r *OperatorReconciler) existingBundleDeploymentUnstructured(ctx context.Context, name string) (*unstructured.Unstructured, error) {
@@ -284,29 +302,26 @@ func (r *OperatorReconciler) existingBundleDeploymentUnstructured(ctx context.Co
284302
return &unstructured.Unstructured{Object: unstrExistingBundleDeploymentObj}, nil
285303
}
286304

287-
func verifySuccessfulBDStatus(dep *unstructured.Unstructured) error {
288-
// convert the unstructured object from patch call to typed bundledeployment to make checking of Status easier.
289-
existingTypedBundleDeployment := &rukpakv1alpha1.BundleDeployment{}
305+
// verifyBDStatus reads the various possibilities of status in bundle deployment and translates
306+
// into corresponding operator condition status and message.
307+
func verifyBDStatus(dep *rukpakv1alpha1.BundleDeployment) (metav1.ConditionStatus, string) {
308+
isValidBundleCond := apimeta.FindStatusCondition(dep.Status.Conditions, rukpakv1alpha1.TypeHasValidBundle)
309+
isInstalledCond := apimeta.FindStatusCondition(dep.Status.Conditions, rukpakv1alpha1.TypeInstalled)
290310

291-
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(dep.UnstructuredContent(), existingTypedBundleDeployment); err != nil {
292-
return err
311+
if isValidBundleCond == nil && isInstalledCond == nil {
312+
return metav1.ConditionUnknown, fmt.Sprintf("waiting for bundleDeployment %q status to be updated", dep.Name)
293313
}
294314

295-
ers := []error{}
296-
conditions := existingTypedBundleDeployment.Status.Conditions
315+
if isValidBundleCond != nil && isValidBundleCond.Status == metav1.ConditionFalse {
316+
return metav1.ConditionFalse, isValidBundleCond.Message
317+
}
297318

298-
// Do we error here?
299-
if conditions == nil || len(conditions) == 0 {
300-
return nil
319+
if isInstalledCond != nil && isInstalledCond.Status == metav1.ConditionFalse {
320+
return metav1.ConditionFalse, isInstalledCond.Message
301321
}
302322

303-
for _, condition := range conditions {
304-
// Currently we are checking for only these two types, if any other condition is added to Rukpak, validate accordingly.
305-
if condition.Type == rukpakv1alpha1.TypeHasValidBundle || condition.Type == rukpakv1alpha1.TypeInstalled {
306-
if condition.Status == metav1.ConditionFalse {
307-
ers = append(ers, fmt.Errorf("error observed by Rukpak: %v", condition.Message))
308-
}
309-
}
323+
if isInstalledCond != nil && isInstalledCond.Status == metav1.ConditionTrue {
324+
return metav1.ConditionTrue, "resolution was successful"
310325
}
311-
return utilerrors.NewAggregate(ers)
326+
return metav1.ConditionUnknown, fmt.Sprintf("waiting for rukpak to install bundleDeployment successfully %s", dep.Name)
312327
}

controllers/operator_controller_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ var _ = Describe("Reconcile Test", func() {
347347
By("running reconcile")
348348
res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: opKey})
349349
Expect(res).To(Equal(ctrl.Result{}))
350-
Expect(err).To(HaveOccurred())
351-
Expect(err.Error()).To(ContainSubstring(`error observed by Rukpak`))
350+
Expect(err).NotTo(HaveOccurred())
352351

353352
By("fetching the updated operator after reconcile")
354353
op := &operatorsv1alpha1.Operator{}

0 commit comments

Comments
 (0)