@@ -25,6 +25,7 @@ import (
25
25
"path/filepath"
26
26
"strconv"
27
27
"strings"
28
+ "time"
28
29
29
30
"github.com/operator-framework/operator-sdk/pkg/ansible/paramconv"
30
31
"github.com/operator-framework/operator-sdk/pkg/ansible/runner/eventapi"
@@ -40,17 +41,19 @@ import (
40
41
type Runner interface {
41
42
Run (* unstructured.Unstructured , string ) (chan eventapi.JobEvent , error )
42
43
GetFinalizer () (string , bool )
44
+ GetReconcilePeriod () (time.Duration , bool )
43
45
}
44
46
45
47
// watch holds data used to create a mapping of GVK to ansible playbook or role.
46
48
// The mapping is used to compose an ansible operator.
47
49
type watch struct {
48
- Version string `yaml:"version"`
49
- Group string `yaml:"group"`
50
- Kind string `yaml:"kind"`
51
- Playbook string `yaml:"playbook"`
52
- Role string `yaml:"role"`
53
- Finalizer * Finalizer `yaml:"finalizer"`
50
+ Version string `yaml:"version"`
51
+ Group string `yaml:"group"`
52
+ Kind string `yaml:"kind"`
53
+ Playbook string `yaml:"playbook"`
54
+ Role string `yaml:"role"`
55
+ ReconcilePeriod string `yaml:"reconcilePeriod"`
56
+ Finalizer * Finalizer `yaml:"finalizer"`
54
57
}
55
58
56
59
// Finalizer - Expose finalizer to be used by a user.
@@ -82,19 +85,28 @@ func NewFromWatches(path string) (map[schema.GroupVersionKind]Runner, error) {
82
85
Version : w .Version ,
83
86
Kind : w .Kind ,
84
87
}
88
+ var reconcilePeriod time.Duration
89
+ if w .ReconcilePeriod != "" {
90
+ d , err := time .ParseDuration (w .ReconcilePeriod )
91
+ if err != nil {
92
+ return nil , fmt .Errorf ("unable to parse duration: %v - %v, setting to default" , w .ReconcilePeriod , err )
93
+ }
94
+ reconcilePeriod = d
95
+ }
96
+
85
97
// Check if schema is a duplicate
86
98
if _ , ok := m [s ]; ok {
87
99
return nil , fmt .Errorf ("duplicate GVK: %v" , s .String ())
88
100
}
89
101
switch {
90
102
case w .Playbook != "" :
91
- r , err := NewForPlaybook (w .Playbook , s , w .Finalizer )
103
+ r , err := NewForPlaybook (w .Playbook , s , w .Finalizer , reconcilePeriod )
92
104
if err != nil {
93
105
return nil , err
94
106
}
95
107
m [s ] = r
96
108
case w .Role != "" :
97
- r , err := NewForRole (w .Role , s , w .Finalizer )
109
+ r , err := NewForRole (w .Role , s , w .Finalizer , reconcilePeriod )
98
110
if err != nil {
99
111
return nil , err
100
112
}
@@ -107,7 +119,7 @@ func NewFromWatches(path string) (map[schema.GroupVersionKind]Runner, error) {
107
119
}
108
120
109
121
// NewForPlaybook returns a new Runner based on the path to an ansible playbook.
110
- func NewForPlaybook (path string , gvk schema.GroupVersionKind , finalizer * Finalizer ) (Runner , error ) {
122
+ func NewForPlaybook (path string , gvk schema.GroupVersionKind , finalizer * Finalizer , reconcilePeriod time. Duration ) (Runner , error ) {
111
123
if ! filepath .IsAbs (path ) {
112
124
return nil , fmt .Errorf ("playbook path must be absolute for %v" , gvk )
113
125
}
@@ -117,6 +129,7 @@ func NewForPlaybook(path string, gvk schema.GroupVersionKind, finalizer *Finaliz
117
129
cmdFunc : func (ident , inputDirPath string ) * exec.Cmd {
118
130
return exec .Command ("ansible-runner" , "-vv" , "-p" , path , "-i" , ident , "run" , inputDirPath )
119
131
},
132
+ reconcilePeriod : reconcilePeriod ,
120
133
}
121
134
err := r .addFinalizer (finalizer )
122
135
if err != nil {
@@ -126,7 +139,7 @@ func NewForPlaybook(path string, gvk schema.GroupVersionKind, finalizer *Finaliz
126
139
}
127
140
128
141
// NewForRole returns a new Runner based on the path to an ansible role.
129
- func NewForRole (path string , gvk schema.GroupVersionKind , finalizer * Finalizer ) (Runner , error ) {
142
+ func NewForRole (path string , gvk schema.GroupVersionKind , finalizer * Finalizer , reconcilePeriod time. Duration ) (Runner , error ) {
130
143
if ! filepath .IsAbs (path ) {
131
144
return nil , fmt .Errorf ("role path must be absolute for %v" , gvk )
132
145
}
@@ -138,6 +151,7 @@ func NewForRole(path string, gvk schema.GroupVersionKind, finalizer *Finalizer)
138
151
rolePath , roleName := filepath .Split (path )
139
152
return exec .Command ("ansible-runner" , "-vv" , "--role" , roleName , "--roles-path" , rolePath , "--hosts" , "localhost" , "-i" , ident , "run" , inputDirPath )
140
153
},
154
+ reconcilePeriod : reconcilePeriod ,
141
155
}
142
156
err := r .addFinalizer (finalizer )
143
157
if err != nil {
@@ -153,6 +167,7 @@ type runner struct {
153
167
Finalizer * Finalizer
154
168
cmdFunc func (ident , inputDirPath string ) * exec.Cmd // returns a Cmd that runs ansible-runner
155
169
finalizerCmdFunc func (ident , inputDirPath string ) * exec.Cmd
170
+ reconcilePeriod time.Duration
156
171
}
157
172
158
173
func (r * runner ) Run (u * unstructured.Unstructured , kubeconfig string ) (chan eventapi.JobEvent , error ) {
@@ -224,6 +239,14 @@ func (r *runner) Run(u *unstructured.Unstructured, kubeconfig string) (chan even
224
239
return receiver .Events , nil
225
240
}
226
241
242
+ // GetReconcilePeriod - new reconcile period.
243
+ func (r * runner ) GetReconcilePeriod () (time.Duration , bool ) {
244
+ if r .reconcilePeriod == time .Duration (0 ) {
245
+ return r .reconcilePeriod , false
246
+ }
247
+ return r .reconcilePeriod , true
248
+ }
249
+
227
250
func (r * runner ) GetFinalizer () (string , bool ) {
228
251
if r .Finalizer != nil {
229
252
return r .Finalizer .Name , true
0 commit comments