Skip to content
This repository was archived by the owner on Nov 28, 2024. It is now read-only.

Commit 92b8ab8

Browse files
committed
Re-init conditions each reconcile
This patch follows the same pattern applied to the other operators, where we re-init the condition at each reconcile loop. Conditions are re-evaluated and updated, keeping the LastTransitionTime for those that haven't changed (it avoids the transition from True to Unknown to True again). In addition, the "observedGeneration" field is introduced, and it is used by the openstack-operator to check the IsReady() function for a particular CR in case a minor update is triggered. All the conditions are evaluated during the main Reconcile loop ( or the reconcileNormal function in some circumstances), hence the main ReadyCondition is updated within the same flow. The defer function still updates the Resource and Mirror the condition to the top-level CR. Signed-off-by: Francesco Pantano <[email protected]>
1 parent b3e5d74 commit 92b8ab8

File tree

5 files changed

+46
-22
lines changed

5 files changed

+46
-22
lines changed

api/bases/ansibleee.openstack.org_openstackansibleees.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,9 @@ spec:
15851585
type: string
15861586
type: array
15871587
type: object
1588+
observedGeneration:
1589+
format: int64
1590+
type: integer
15881591
type: object
15891592
type: object
15901593
served: true

api/v1beta1/openstack_ansibleee_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ type OpenStackAnsibleEEStatus struct {
134134
// +kubebuilder:default:=Pending
135135
// JobStatus status of the executed job (Pending/Running/Succeeded/Failed)
136136
JobStatus string `json:"JobStatus,omitempty" optional:"true"`
137+
138+
// ObservedGeneration - the most recent generation observed for this
139+
// service. If the observed generation is less than the spec generation,
140+
// then the controller has not processed the latest changes injected by
141+
// the opentack-operator in the top-level CR (e.g. the ContainerImage)
142+
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
137143
}
138144

139145
//+kubebuilder:object:root=true

config/crd/bases/ansibleee.openstack.org_openstackansibleees.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,9 @@ spec:
15851585
type: string
15861586
type: array
15871587
type: object
1588+
observedGeneration:
1589+
format: int64
1590+
type: integer
15881591
type: object
15891592
type: object
15901593
served: true

controllers/openstack_ansibleee_controller.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,42 +103,43 @@ func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.R
103103
return ctrl.Result{}, err
104104
}
105105

106+
// initialize status if Conditions is nil, but do not reset if it already
107+
// exists
108+
isNewInstance := instance.Status.Conditions == nil
109+
if isNewInstance {
110+
instance.Status.Conditions = condition.Conditions{}
111+
}
112+
113+
// Save a copy of the condtions so that we can restore the LastTransitionTime
114+
// when a condition's state doesn't change.
115+
savedConditions := instance.Status.Conditions.DeepCopy()
116+
106117
// Always patch the instance status when exiting this function so we can
107118
// persist any changes.
108119
defer func() {
109-
// update the overall status condition if service is ready
110-
if instance.IsReady() {
111-
instance.Status.Conditions.MarkTrue(condition.ReadyCondition, ansibleeev1.AnsibleExecutionJobReadyMessage)
112-
} else {
113-
// something is not ready so reset the Ready condition
114-
instance.Status.Conditions.MarkUnknown(
115-
condition.ReadyCondition, condition.InitReason, condition.ReadyInitMessage)
116-
// and recalculate it based on the state of the rest of the conditions
117-
instance.Status.Conditions.Set(instance.Status.Conditions.Mirror(condition.ReadyCondition))
120+
condition.RestoreLastTransitionTimes(
121+
&instance.Status.Conditions, savedConditions)
122+
if instance.Status.Conditions.IsUnknown(condition.ReadyCondition) {
123+
instance.Status.Conditions.Set(
124+
instance.Status.Conditions.Mirror(condition.ReadyCondition))
118125
}
119-
120126
err := helper.PatchInstance(ctx, instance)
121127
if err != nil {
122-
Log.Error(_err, "PatchInstance error")
123128
_err = err
124129
return
125130
}
126131
}()
127132

128133
// Initialize Status
129-
if instance.Status.Conditions == nil {
130-
instance.Status.Conditions = condition.Conditions{}
131134

132-
cl := condition.CreateList(
133-
condition.UnknownCondition(ansibleeev1.AnsibleExecutionJobReadyCondition, condition.InitReason, ansibleeev1.AnsibleExecutionJobInitMessage),
134-
)
135+
cl := condition.CreateList(
136+
condition.UnknownCondition(condition.ReadyCondition, condition.InitReason, condition.ReadyInitMessage),
137+
condition.UnknownCondition(ansibleeev1.AnsibleExecutionJobReadyCondition, condition.InitReason, ansibleeev1.AnsibleExecutionJobInitMessage),
138+
)
135139

136-
instance.Status.Conditions.Init(&cl)
137-
138-
// Register overall status immediately to have an early feedback e.g.
139-
// in the cli
140-
return ctrl.Result{}, nil
141-
}
140+
instance.Status.Conditions.Init(&cl)
141+
// Bump the ObservedGeneration as the new Reconciliation started
142+
instance.Status.ObservedGeneration = instance.Generation
142143

143144
// Initialize Status fields
144145
util.InitMap(&instance.Status.Hash)
@@ -234,6 +235,12 @@ func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.R
234235
instance.Status.Conditions.MarkTrue(ansibleeev1.AnsibleExecutionJobReadyCondition, ansibleeev1.AnsibleExecutionJobReadyMessage)
235236
instance.Status.JobStatus = ansibleeev1.JobStatusSucceeded
236237

238+
// We reached the end of the Reconcile, update the Ready condition based on
239+
// the sub conditions
240+
if instance.Status.Conditions.AllSubConditionIsTrue() {
241+
instance.Status.Conditions.MarkTrue(
242+
condition.ReadyCondition, condition.ReadyMessage)
243+
}
237244
Log.Info(fmt.Sprintf("Reconciled AnsibleEE '%s' successfully", instance.Name))
238245
return ctrl.Result{}, nil
239246
}

docs/assemblies/openstack_ansibleee.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ OpenStackAnsibleEEStatus defines the observed state of OpenStackAnsibleEE
212212
| JobStatus status of the executed job (Pending/Running/Succeeded/Failed)
213213
| string
214214
| false
215+
216+
| observedGeneration
217+
| ObservedGeneration - the most recent generation observed for this service. If the observed generation is less than the spec generation, then the controller has not processed the latest changes injected by the opentack-operator in the top-level CR (e.g. the ContainerImage)
218+
| int64
219+
| false
215220
|===
216221
217222
<<custom-resources,Back to Custom Resources>>

0 commit comments

Comments
 (0)