Skip to content

Commit

Permalink
set Ready condition after running ansible
Browse files Browse the repository at this point in the history
  • Loading branch information
Dasha Komsa committed Jan 18, 2024
1 parent 5e0d992 commit b7f9d34
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions internal/controller/ansibleRun/ansibleRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,15 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
}

func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) {
_, ok := mg.(*v1alpha1.AnsibleRun)
cr, ok := mg.(*v1alpha1.AnsibleRun)
if !ok {
return managed.ExternalUpdate{}, errors.New(errNotAnsibleRun)
}

// disable checkMode for real action
c.runner.EnableCheckMode(false)
dc, _, err := c.runner.Run()
if err != nil {
return managed.ExternalUpdate{}, err
}
if err = dc.Wait(); err != nil {
return managed.ExternalUpdate{}, err
if err := c.runAnsible(ctx, cr); err != nil {
return managed.ExternalUpdate{}, fmt.Errorf("running ansible: %w", err)
}

// TODO handle ConnectionDetails https://github.com/multicloudlab/crossplane-provider-ansible/pull/74#discussion_r888467991
Expand Down Expand Up @@ -476,13 +472,11 @@ func (c *external) handleLastApplied(ctx context.Context, lastParameters *v1alph
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.runAnsible(ctx, desired); err != nil {
return managed.ExternalObservation{}, fmt.Errorf("running ansible: %w", err)
}

// The crossplane runtime is not aware of the external resource created by ansible content.
Expand All @@ -492,6 +486,28 @@ func (c *external) handleLastApplied(ctx context.Context, lastParameters *v1alph
return managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: true}, nil
}

func (c *external) runAnsible(ctx context.Context, cr *v1alpha1.AnsibleRun) error {
dc, _, err := c.runner.Run()
if err != nil {
return err
}

if err = dc.Wait(); err != nil {
cond := xpv1.Unavailable()
cond.Message = err.Error()
cr.SetConditions(cond)

return err
}

cr.SetConditions(xpv1.Available())

// no need to persist status update explicitly, cr modifications are in-place and will
// be persisted by crossplane-runtime

return nil
}

func addBehaviorVars(pc *v1alpha1.ProviderConfig) map[string]string {
behaviorVars := make(map[string]string, len(pc.Spec.Vars))
for _, v := range pc.Spec.Vars {
Expand Down

0 comments on commit b7f9d34

Please sign in to comment.