Skip to content

pkg/ansible,*: Adding ability to specify re-queue period per GVK via watches file. #641

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

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: 2 additions & 1 deletion commands/operator-sdk/cmd/up/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"runtime"
"strings"
"syscall"
"time"

"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
ansibleOperator "github.com/operator-framework/operator-sdk/pkg/ansible/operator"
Expand Down Expand Up @@ -142,7 +143,7 @@ func upLocalAnsible() {
}

// start the operator
go ansibleOperator.Run(done, mgr, "./watches.yaml")
go ansibleOperator.Run(done, mgr, "./watches.yaml", time.Minute)

// wait for either to finish
err = <-done
Expand Down
16 changes: 11 additions & 5 deletions pkg/ansible/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
// Run - A blocking function which starts a controller-runtime manager
// It starts an Operator by reading in the values in `./watches.yaml`, adds a controller
// to the manager, and finally running the manager.
func Run(done chan error, mgr manager.Manager, watchesPath string) {
func Run(done chan error, mgr manager.Manager, watchesPath string, reconcilePeriod time.Duration) {
watches, err := runner.NewFromWatches(watchesPath)
if err != nil {
logrus.Error("Failed to get watches")
Expand All @@ -40,10 +40,16 @@ func Run(done chan error, mgr manager.Manager, watchesPath string) {
c := signals.SetupSignalHandler()

for gvk, runner := range watches {
controller.Add(mgr, controller.Options{
GVK: gvk,
Runner: runner,
})
o := controller.Options{
GVK: gvk,
Runner: runner,
ReconcilePeriod: reconcilePeriod,
}
d, ok := runner.GetReconcilePeriod()
if ok {
o.ReconcilePeriod = d
}
controller.Add(mgr, o)
}
done <- mgr.Start(c)
}
43 changes: 33 additions & 10 deletions pkg/ansible/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"path/filepath"
"strconv"
"strings"
"time"

"github.com/operator-framework/operator-sdk/pkg/ansible/paramconv"
"github.com/operator-framework/operator-sdk/pkg/ansible/runner/eventapi"
Expand All @@ -40,17 +41,19 @@ import (
type Runner interface {
Run(*unstructured.Unstructured, string) (chan eventapi.JobEvent, error)
GetFinalizer() (string, bool)
GetReconcilePeriod() (time.Duration, bool)
}

// watch holds data used to create a mapping of GVK to ansible playbook or role.
// The mapping is used to compose an ansible operator.
type watch struct {
Version string `yaml:"version"`
Group string `yaml:"group"`
Kind string `yaml:"kind"`
Playbook string `yaml:"playbook"`
Role string `yaml:"role"`
Finalizer *Finalizer `yaml:"finalizer"`
Version string `yaml:"version"`
Group string `yaml:"group"`
Kind string `yaml:"kind"`
Playbook string `yaml:"playbook"`
Role string `yaml:"role"`
ReconcilePeriod string `yaml:"reconcilePeriod"`
Finalizer *Finalizer `yaml:"finalizer"`
}

// Finalizer - Expose finalizer to be used by a user.
Expand Down Expand Up @@ -82,19 +85,28 @@ func NewFromWatches(path string) (map[schema.GroupVersionKind]Runner, error) {
Version: w.Version,
Kind: w.Kind,
}
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
}

// Check if schema is a duplicate
if _, ok := m[s]; ok {
return nil, fmt.Errorf("duplicate GVK: %v", s.String())
}
switch {
case w.Playbook != "":
r, err := NewForPlaybook(w.Playbook, s, w.Finalizer)
r, err := NewForPlaybook(w.Playbook, s, w.Finalizer, reconcilePeriod)
if err != nil {
return nil, err
}
m[s] = r
case w.Role != "":
r, err := NewForRole(w.Role, s, w.Finalizer)
r, err := NewForRole(w.Role, s, w.Finalizer, reconcilePeriod)
if err != nil {
return nil, err
}
Expand All @@ -107,7 +119,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) (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 @@ -117,6 +129,7 @@ func NewForPlaybook(path string, gvk schema.GroupVersionKind, finalizer *Finaliz
cmdFunc: func(ident, inputDirPath string) *exec.Cmd {
return exec.Command("ansible-runner", "-vv", "-p", path, "-i", ident, "run", inputDirPath)
},
reconcilePeriod: reconcilePeriod,
}
err := r.addFinalizer(finalizer)
if err != nil {
Expand All @@ -126,7 +139,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) (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 All @@ -138,6 +151,7 @@ func NewForRole(path string, gvk schema.GroupVersionKind, finalizer *Finalizer)
rolePath, roleName := filepath.Split(path)
return exec.Command("ansible-runner", "-vv", "--role", roleName, "--roles-path", rolePath, "--hosts", "localhost", "-i", ident, "run", inputDirPath)
},
reconcilePeriod: reconcilePeriod,
}
err := r.addFinalizer(finalizer)
if err != nil {
Expand All @@ -153,6 +167,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
}

func (r *runner) Run(u *unstructured.Unstructured, kubeconfig string) (chan eventapi.JobEvent, error) {
Expand Down Expand Up @@ -224,6 +239,14 @@ func (r *runner) Run(u *unstructured.Unstructured, kubeconfig string) (chan even
return receiver.Events, nil
}

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

func (r *runner) GetFinalizer() (string, bool) {
if r.Finalizer != nil {
return r.Finalizer.Name, true
Expand Down
162 changes: 162 additions & 0 deletions pkg/ansible/runner/runner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright 2018 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package runner

import (
"reflect"
"testing"
"time"

"k8s.io/apimachinery/pkg/runtime/schema"
)

func TestNewFromWatches(t *testing.T) {
testCases := []struct {
name string
path string
expectedMap map[schema.GroupVersionKind]runner
shouldError bool
}{
{
name: "error duplicate GVK",
path: "testdata/duplicate_gvk.yaml",
shouldError: true,
},
{
name: "error no file",
path: "testdata/please_don't_create_me_gvk.yaml",
shouldError: true,
},
{
name: "error invalid yaml",
path: "testdata/invalid.yaml",
shouldError: true,
},
{
name: "error invalid playbook path",
path: "testdata/invalid_playbook_path.yaml",
shouldError: true,
},
{
name: "error invalid playbook finalizer path",
path: "testdata/invalid_finalizer_playbook_path.yaml",
shouldError: true,
},
{
name: "error invalid role path",
path: "testdata/invalid_role_path.yaml",
shouldError: true,
},
{
name: "error invalid role finalizer path",
path: "testdata/invalid_finalizer_role_path.yaml",
shouldError: true,
},
{
name: "error invalid duration",
path: "testdata/invalid_duration.yaml",
shouldError: true,
},
{
name: "valid watches file",
path: "testdata/valid.yaml",
expectedMap: map[schema.GroupVersionKind]runner{
schema.GroupVersionKind{
Version: "v1alpha1",
Group: "app.example.com",
Kind: "NoFinalizer",
}: runner{
GVK: schema.GroupVersionKind{
Version: "v1alpha1",
Group: "app.example.com",
Kind: "NoFinalizer",
},
Path: "/opt/ansible/playbook.yaml",
reconcilePeriod: time.Second * 2,
},
schema.GroupVersionKind{
Version: "v1alpha1",
Group: "app.example.com",
Kind: "Playbook",
}: runner{
GVK: schema.GroupVersionKind{
Version: "v1alpha1",
Group: "app.example.com",
Kind: "Playbook",
},
Path: "/opt/ansible/playbook.yaml",
Finalizer: &Finalizer{
Name: "finalizer.app.example.com",
Role: "/opt/ansible/role",
Vars: map[string]interface{}{"sentinel": "finalizer_running"},
},
},
schema.GroupVersionKind{
Version: "v1alpha1",
Group: "app.example.com",
Kind: "Role",
}: runner{
GVK: schema.GroupVersionKind{
Version: "v1alpha1",
Group: "app.example.com",
Kind: "Role",
},
Path: "/opt/ansible/role",
Finalizer: &Finalizer{
Name: "finalizer.app.example.com",
Playbook: "/opt/ansible/playbook.yaml",
Vars: map[string]interface{}{"sentinel": "finalizer_running"},
},
},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
m, err := NewFromWatches(tc.path)
if err != nil && !tc.shouldError {
t.Fatalf("err: %v occurred unexpectedly", err)
}
if err != nil && tc.shouldError {
return
}
for k, expectedR := range tc.expectedMap {
r, ok := m[k]
if !ok {
t.Fatalf("did not find expected GVK: %v", k)
}
run, ok := r.(*runner)
if !ok {
t.Fatalf("here: %#v", r)
}
if run.Path != expectedR.Path {
t.Fatalf("the GVK: %v unexpected path: %v expected path: %v", k, run.Path, expectedR.Path)
}
if run.GVK != expectedR.GVK {
t.Fatalf("the GVK: %v\nunexpected GVK: %#v\nexpected GVK: %#v", k, run.GVK, expectedR.GVK)
}
if run.Finalizer != expectedR.Finalizer {
if run.Finalizer.Name != expectedR.Finalizer.Name || run.Finalizer.Playbook != expectedR.Finalizer.Playbook || run.Finalizer.Role != expectedR.Finalizer.Role || reflect.DeepEqual(run.Finalizer.Vars["sentinel"], expectedR.Finalizer.Vars["sentininel"]) {
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)
}
}
})
}
}
17 changes: 17 additions & 0 deletions pkg/ansible/runner/testdata/duplicate_gvk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- version: v1alpha1
group: app.example.com
kind: Database
playbook: /opt/ansible/playbook.yaml
finalizer:
name: finalizer.app.example.com
vars:
sentinel: finalizer_running
- version: v1alpha1
group: app.example.com
kind: Database
playbook: /opt/ansible/playbook.yaml
finalizer:
name: finalizer.app.example.com
vars:
sentinel: finalizer_running
9 changes: 9 additions & 0 deletions pkg/ansible/runner/testdata/invalid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
version: v1alpha1
group: app.example.com
kind: Database
playbook: /opt/ansible/playbook.yaml
finalizer:
name: finalizer.app.example.com
vars:
sentinel: finalizer_running
6 changes: 6 additions & 0 deletions pkg/ansible/runner/testdata/invalid_duration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- version: v1alpha1
group: app.example.com
kind: Database
playbook: /opt/ansible/playbook.yaml
reconcilePeriod: invalid
10 changes: 10 additions & 0 deletions pkg/ansible/runner/testdata/invalid_finalizer_playbook_path.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- version: v1alpha1
group: app.example.com
kind: Database
playbook: /opt/ansible/playbook.yaml
finalizer:
name: finalizer.app.example.com
playbook: opt/ansible/playbook.yaml
vars:
sentinel: finalizer_running
10 changes: 10 additions & 0 deletions pkg/ansible/runner/testdata/invalid_finalizer_role_path.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- version: v1alpha1
group: app.example.com
kind: Database
playbook: /ansible/playbook.yaml
finalizer:
name: finalizer.app.example.com
role: ansible/role
vars:
sentinel: finalizer_running
9 changes: 9 additions & 0 deletions pkg/ansible/runner/testdata/invalid_playbook_path.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- version: v1alpha1
group: app.example.com
kind: Database
playbook: opt/ansible/playbook.yaml
finalizer:
name: finalizer.app.example.com
vars:
sentinel: finalizer_running
Loading