Skip to content

Commit

Permalink
ansible: support disabling reconciliation
Browse files Browse the repository at this point in the history
- Remove duration check from controller
- Use pointer to time.Duration and check for nil from watches
  • Loading branch information
djzager committed Nov 13, 2018
1 parent fa72147 commit 76807e3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
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 *r.reconcilePeriod, false
}
return r.reconcilePeriod, true
return *r.reconcilePeriod, true
}

func (r *runner) GetFinalizer() (string, bool) {
Expand Down
17 changes: 16 additions & 1 deletion 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
6 changes: 6 additions & 0 deletions pkg/ansible/runner/testdata/negative_duration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- version: v1alpha1
group: app.example.com
kind: NegativeReconcile
playbook: /opt/ansible/playbook.yaml
reconcilePeriod: -2s
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

0 comments on commit 76807e3

Please sign in to comment.