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

Commit

Permalink
Re-init conditions each reconcile
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
fmount committed Apr 5, 2024
1 parent b3e5d74 commit 4c211d6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
3 changes: 3 additions & 0 deletions api/bases/ansibleee.openstack.org_openstackansibleees.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,9 @@ spec:
type: string
type: array
type: object
observedGeneration:
format: int64
type: integer
type: object
type: object
served: true
Expand Down
6 changes: 6 additions & 0 deletions api/v1beta1/openstack_ansibleee_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ type OpenStackAnsibleEEStatus struct {
// +kubebuilder:default:=Pending
// JobStatus status of the executed job (Pending/Running/Succeeded/Failed)
JobStatus string `json:"JobStatus,omitempty" optional:"true"`

// 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)
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,9 @@ spec:
type: string
type: array
type: object
observedGeneration:
format: int64
type: integer
type: object
type: object
served: true
Expand Down
51 changes: 29 additions & 22 deletions controllers/openstack_ansibleee_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,43 +102,44 @@ func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.R
Log.Error(err, fmt.Sprintf("Unable to acquire helper for OpenStackAnsibleEE %s", instance.Name))
return ctrl.Result{}, err
}
// Bump the ObservedGeneration as the new Reconciliation started
instance.Status.ObservedGeneration = instance.Generation

// initialize status if Conditions is nil, but do not reset if it already
// exists
isNewInstance := instance.Status.Conditions == nil
if isNewInstance {
instance.Status.Conditions = condition.Conditions{}
}

// Save a copy of the condtions so that we can restore the LastTransitionTime
// when a condition's state doesn't change.
savedConditions := instance.Status.Conditions.DeepCopy()

// Always patch the instance status when exiting this function so we can
// persist any changes.
defer func() {
// update the overall status condition if service is ready
if instance.IsReady() {
instance.Status.Conditions.MarkTrue(condition.ReadyCondition, ansibleeev1.AnsibleExecutionJobReadyMessage)
} else {
// something is not ready so reset the Ready condition
instance.Status.Conditions.MarkUnknown(
condition.ReadyCondition, condition.InitReason, condition.ReadyInitMessage)
// and recalculate it based on the state of the rest of the conditions
instance.Status.Conditions.Set(instance.Status.Conditions.Mirror(condition.ReadyCondition))
condition.RestoreLastTransitionTimes(
&instance.Status.Conditions, savedConditions)
if instance.Status.Conditions.IsUnknown(condition.ReadyCondition) {
instance.Status.Conditions.Set(
instance.Status.Conditions.Mirror(condition.ReadyCondition))
}

err := helper.PatchInstance(ctx, instance)
if err != nil {
Log.Error(_err, "PatchInstance error")
_err = err
return
}
}()

// Initialize Status
if instance.Status.Conditions == nil {
instance.Status.Conditions = condition.Conditions{}

cl := condition.CreateList(
condition.UnknownCondition(ansibleeev1.AnsibleExecutionJobReadyCondition, condition.InitReason, ansibleeev1.AnsibleExecutionJobInitMessage),
)
cl := condition.CreateList(
condition.UnknownCondition(condition.ReadyCondition, condition.InitReason, condition.ReadyInitMessage),
condition.UnknownCondition(ansibleeev1.AnsibleExecutionJobReadyCondition, condition.InitReason, ansibleeev1.AnsibleExecutionJobInitMessage),
)

instance.Status.Conditions.Init(&cl)

// Register overall status immediately to have an early feedback e.g.
// in the cli
return ctrl.Result{}, nil
}
instance.Status.Conditions.Init(&cl)

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

// We reached the end of the Reconcile, update the Ready condition based on
// the sub conditions
if instance.Status.Conditions.AllSubConditionIsTrue() {
instance.Status.Conditions.MarkTrue(
condition.ReadyCondition, condition.ReadyMessage)
}
Log.Info(fmt.Sprintf("Reconciled AnsibleEE '%s' successfully", instance.Name))
return ctrl.Result{}, nil
}
Expand Down
5 changes: 5 additions & 0 deletions docs/assemblies/openstack_ansibleee.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ OpenStackAnsibleEEStatus defines the observed state of OpenStackAnsibleEE
| JobStatus status of the executed job (Pending/Running/Succeeded/Failed)
| string
| false
| observedGeneration
| 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)
| int64
| false
|===
<<custom-resources,Back to Custom Resources>>

0 comments on commit 4c211d6

Please sign in to comment.