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

Commit

Permalink
Merge pull request #345 from fmount/conditions
Browse files Browse the repository at this point in the history
Re-init conditions each reconcile
  • Loading branch information
openshift-merge-bot[bot] authored Apr 5, 2024
2 parents b3e5d74 + 92b8ab8 commit a61ca86
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 @@ -103,42 +103,43 @@ func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.R
return ctrl.Result{}, err
}

// 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)
// Bump the ObservedGeneration as the new Reconciliation started
instance.Status.ObservedGeneration = instance.Generation

// 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 a61ca86

Please sign in to comment.