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

ansible: support disabling reconciliation #739

Merged
merged 3 commits into from
Nov 13, 2018
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
3 changes: 0 additions & 3 deletions pkg/ansible/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ func Add(mgr manager.Manager, options Options) {
options.EventHandlers = []events.EventHandler{}
}
eventHandlers := append(options.EventHandlers, events.NewLoggingEventHandler(options.LoggingLevel))
if options.ReconcilePeriod == time.Duration(0) {
options.ReconcilePeriod = time.Minute
}

aor := &AnsibleOperatorReconciler{
Client: mgr.GetClient(),
Expand Down
16 changes: 8 additions & 8 deletions pkg/ansible/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func NewFromWatches(path string) (map[schema.GroupVersionKind]Runner, error) {
Version: w.Version,
Kind: w.Kind,
}
var reconcilePeriod time.Duration
var reconcilePeriod *time.Duration
if w.ReconcilePeriod != "" {
d, err := time.ParseDuration(w.ReconcilePeriod)
if err != nil {
return nil, fmt.Errorf("unable to parse duration: %v - %v, setting to default", w.ReconcilePeriod, err)
}
reconcilePeriod = d
reconcilePeriod = &d
}

// Check if schema is a duplicate
Expand Down Expand Up @@ -117,7 +117,7 @@ func NewFromWatches(path string) (map[schema.GroupVersionKind]Runner, error) {
}

// NewForPlaybook returns a new Runner based on the path to an ansible playbook.
func NewForPlaybook(path string, gvk schema.GroupVersionKind, finalizer *Finalizer, reconcilePeriod time.Duration) (Runner, error) {
func NewForPlaybook(path string, gvk schema.GroupVersionKind, finalizer *Finalizer, reconcilePeriod *time.Duration) (Runner, error) {
if !filepath.IsAbs(path) {
return nil, fmt.Errorf("playbook path must be absolute for %v", gvk)
}
Expand All @@ -140,7 +140,7 @@ func NewForPlaybook(path string, gvk schema.GroupVersionKind, finalizer *Finaliz
}

// NewForRole returns a new Runner based on the path to an ansible role.
func NewForRole(path string, gvk schema.GroupVersionKind, finalizer *Finalizer, reconcilePeriod time.Duration) (Runner, error) {
func NewForRole(path string, gvk schema.GroupVersionKind, finalizer *Finalizer, reconcilePeriod *time.Duration) (Runner, error) {
if !filepath.IsAbs(path) {
return nil, fmt.Errorf("role path must be absolute for %v", gvk)
}
Expand Down Expand Up @@ -171,7 +171,7 @@ type runner struct {
Finalizer *Finalizer
cmdFunc func(ident, inputDirPath string) *exec.Cmd // returns a Cmd that runs ansible-runner
finalizerCmdFunc func(ident, inputDirPath string) *exec.Cmd
reconcilePeriod time.Duration
reconcilePeriod *time.Duration
}

func (r *runner) Run(ident string, u *unstructured.Unstructured, kubeconfig string) (*RunResult, error) {
Expand Down Expand Up @@ -248,10 +248,10 @@ func (r *runner) Run(ident string, u *unstructured.Unstructured, kubeconfig stri

// GetReconcilePeriod - new reconcile period.
func (r *runner) GetReconcilePeriod() (time.Duration, bool) {
if r.reconcilePeriod == time.Duration(0) {
return r.reconcilePeriod, false
if r.reconcilePeriod == nil {
return time.Duration(0), false
}
return r.reconcilePeriod, true
return *r.reconcilePeriod, true
}

func (r *runner) GetFinalizer() (string, bool) {
Expand Down
23 changes: 20 additions & 3 deletions pkg/ansible/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestNewFromWatches(t *testing.T) {
return
}

zeroSeconds := time.Duration(0)
twoSeconds := time.Second * 2
testCases := []struct {
name string
path string
Expand Down Expand Up @@ -114,7 +116,7 @@ func TestNewFromWatches(t *testing.T) {
Kind: "NoFinalizer",
},
Path: validTemplate.ValidPlaybook,
reconcilePeriod: time.Second * 2,
reconcilePeriod: &twoSeconds,
},
schema.GroupVersionKind{
Version: "v1alpha1",
Expand All @@ -133,6 +135,19 @@ func TestNewFromWatches(t *testing.T) {
Vars: map[string]interface{}{"sentinel": "finalizer_running"},
},
},
schema.GroupVersionKind{
Version: "v1alpha1",
Group: "app.example.com",
Kind: "NoReconcile",
}: runner{
GVK: schema.GroupVersionKind{
Version: "v1alpha1",
Group: "app.example.com",
Kind: "NoReconcile",
},
Path: validTemplate.ValidPlaybook,
reconcilePeriod: &zeroSeconds,
},
schema.GroupVersionKind{
Version: "v1alpha1",
Group: "app.example.com",
Expand Down Expand Up @@ -183,8 +198,10 @@ func TestNewFromWatches(t *testing.T) {
t.Fatalf("the GVK: %v\nunexpected finalizer: %#v\nexpected finalizer: %#v", k, run.Finalizer, expectedR.Finalizer)
}
}
if run.reconcilePeriod != expectedR.reconcilePeriod {
t.Fatalf("the GVK: %v unexpected reconcile period: %v expected reconcile period: %v", k, run.reconcilePeriod, expectedR.reconcilePeriod)
if expectedR.reconcilePeriod != nil {
if *run.reconcilePeriod != *expectedR.reconcilePeriod {
t.Fatalf("the GVK: %v unexpected reconcile period: %v expected reconcile period: %v", k, run.reconcilePeriod, expectedR.reconcilePeriod)
}
}
}
})
Expand Down
5 changes: 5 additions & 0 deletions pkg/ansible/runner/testdata/valid.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
role: {{ .ValidRole }}
vars:
sentinel: finalizer_running
- version: v1alpha1
group: app.example.com
kind: NoReconcile
playbook: {{ .ValidPlaybook }}
reconcilePeriod: 0s
- version: v1alpha1
group: app.example.com
kind: Role
Expand Down