Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set Ready condition after running ansible #294

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 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(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,12 +472,9 @@ 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(desired); err != nil {
return managed.ExternalObservation{}, fmt.Errorf("running ansible: %w", err)
}
}

Expand All @@ -492,6 +485,28 @@ func (c *external) handleLastApplied(ctx context.Context, lastParameters *v1alph
return managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: true}, nil
}

func (c *external) runAnsible(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
38 changes: 29 additions & 9 deletions internal/controller/ansibleRun/ansibleRun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"io"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/spf13/afero"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -531,8 +532,9 @@ func TestObserve(t *testing.T) {
}

type want struct {
o managed.ExternalObservation
err error
o managed.ExternalObservation
err error
conditions []xpv1.Condition
}

cases := map[string]struct {
Expand Down Expand Up @@ -580,7 +582,8 @@ func TestObserve(t *testing.T) {
mg: &v1alpha1.AnsibleRun{},
},
want: want{
err: fmt.Errorf("%s: %w", errGetAnsibleRun, errBoom),
err: fmt.Errorf("%s: %w", errGetAnsibleRun, errBoom),
conditions: []xpv1.Condition{xpv1.Unavailable()},
},
},
"GetObservedErrorWhenCheckWhenObservePolicy": {
Expand Down Expand Up @@ -640,8 +643,9 @@ func TestCreateOrUpdate(t *testing.T) {
}

type want struct {
o managed.ExternalCreation
err error
o managed.ExternalCreation
err error
conditions []xpv1.Condition
}

cases := map[string]struct {
Expand Down Expand Up @@ -678,7 +682,7 @@ func TestCreateOrUpdate(t *testing.T) {
},
},
want: want{
err: errBoom,
err: fmt.Errorf("running ansible: %w", errBoom),
},
},
"SuccessObserveAndDelete": {
Expand All @@ -702,7 +706,9 @@ func TestCreateOrUpdate(t *testing.T) {
},
},
},
want: want{},
want: want{
conditions: []xpv1.Condition{xpv1.Available()},
},
},
"RunErrorWithCheckWhenObservePolicy": {
reason: "We should return any error we encounter when running the runner",
Expand All @@ -723,7 +729,7 @@ func TestCreateOrUpdate(t *testing.T) {
},
},
want: want{
err: errBoom,
err: fmt.Errorf("running ansible: %w", errBoom),
},
},
"SuccessCheckWhenObserve": {
Expand All @@ -747,7 +753,9 @@ func TestCreateOrUpdate(t *testing.T) {
},
},
},
want: want{},
want: want{
conditions: []xpv1.Condition{xpv1.Available()},
},
},
}

Expand All @@ -761,6 +769,18 @@ func TestCreateOrUpdate(t *testing.T) {
if diff := cmp.Diff(tc.want.o, got); diff != "" {
t.Errorf("\n%s\ne.Observe(...): -want, +got:\n%s\n", tc.reason, diff)
}

if tc.args.mg == nil {
return
}

if diff := cmp.Diff(
tc.want.conditions,
tc.args.mg.(*v1alpha1.AnsibleRun).Status.Conditions,
cmpopts.IgnoreFields(xpv1.Condition{}, "LastTransitionTime"),
); diff != "" {
t.Errorf("ansiblerun conditions: (-want +got):\n%s", diff)
}
})
}
}
Expand Down
Loading