Skip to content

Commit

Permalink
retry failed ansibleruns with ObserverAndDelete policy
Browse files Browse the repository at this point in the history
Signed-off-by: Dasha Komsa <komsa.darya@gmail.com>
  • Loading branch information
d-honeybadger committed Jan 31, 2024
1 parent 5e0d992 commit dd4143a
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions internal/controller/ansibleRun/ansibleRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,41 +448,41 @@ func getLastAppliedParameters(observed *v1alpha1.AnsibleRun) (*v1alpha1.AnsibleR
// nolint: gocyclo
// TODO reduce cyclomatic complexity
func (c *external) handleLastApplied(ctx context.Context, lastParameters *v1alpha1.AnsibleRunParameters, desired *v1alpha1.AnsibleRun) (managed.ExternalObservation, error) {
isUpToDate := false
if lastParameters != nil {
if equality.Semantic.DeepEqual(*lastParameters, desired.Spec.ForProvider) {
// Mark as up-to-date since last is equal to desired
isUpToDate = true
}
// Mark as up-to-date since last is equal to desired
isUpToDate := (lastParameters != nil && equality.Semantic.DeepEqual(*lastParameters, desired.Spec.ForProvider))

isLastSyncOK := (desired.GetCondition(xpv1.TypeSynced).Status == v1.ConditionTrue)

if isUpToDate && isLastSyncOK {
// nothing to do for this run
return managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: true}, nil
}

if !isUpToDate {
out, err := json.Marshal(desired.Spec.ForProvider)
if err != nil {
return managed.ExternalObservation{}, err
}
// set LastAppliedConfig Annotation to avoid useless cmd run
meta.AddAnnotations(desired, map[string]string{
v1.LastAppliedConfigAnnotation: string(out),
})
out, err := json.Marshal(desired.Spec.ForProvider)
if err != nil {
return managed.ExternalObservation{}, err
}
// set LastAppliedConfig Annotation to avoid useless cmd run
meta.AddAnnotations(desired, map[string]string{
v1.LastAppliedConfigAnnotation: string(out),
})

if err := c.kube.Update(ctx, desired); err != nil {
return managed.ExternalObservation{}, err
}
stateVar := make(map[string]string)
stateVar["state"] = "present"
nestedMap := make(map[string]interface{})
nestedMap[desired.GetName()] = stateVar
if err := c.runner.WriteExtraVar(nestedMap); err != nil {
return managed.ExternalObservation{}, err
}
dc, _, err := c.runner.Run()
if err != nil {
return managed.ExternalObservation{}, err
}
if err = dc.Wait(); err != nil {
return managed.ExternalObservation{}, err
}
if err := c.kube.Update(ctx, desired); err != nil {
return managed.ExternalObservation{}, err
}
stateVar := make(map[string]string)
stateVar["state"] = "present"
nestedMap := make(map[string]interface{})
nestedMap[desired.GetName()] = stateVar
if err := c.runner.WriteExtraVar(nestedMap); err != nil {
return managed.ExternalObservation{}, err
}
dc, _, err := c.runner.Run()
if err != nil {
return managed.ExternalObservation{}, err
}
if err = dc.Wait(); err != nil {
return managed.ExternalObservation{}, err
}

// The crossplane runtime is not aware of the external resource created by ansible content.
Expand Down

0 comments on commit dd4143a

Please sign in to comment.