Skip to content

Commit

Permalink
Make marking of variables unsafe optional
Browse files Browse the repository at this point in the history
Signed-off-by: varshaprasad96 <varshaprasad96@gmail.com>
  • Loading branch information
varshaprasad96 committed Mar 3, 2021
1 parent 6b87135 commit 6e1441c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/ansible/watches/testdata/valid.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
kind: NoFinalizer
playbook: {{ .ValidPlaybook }}
reconcilePeriod: 2s
- version: v1alpha1
group: app.example.com
kind: WithUnsafeMarked
playbook: {{ .ValidPlaybook }}
reconcilePeriod: 2s
markUnsafe: True
- version: v1alpha1
group: app.example.com
kind: Playbook
Expand Down
9 changes: 9 additions & 0 deletions internal/ansible/watches/watches.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Watch struct {
WatchDependentResources bool `yaml:"watchDependentResources"`
WatchClusterScopedResources bool `yaml:"watchClusterScopedResources"`
SnakeCaseParameters bool `yaml:"snakeCaseParameters"`
MarkUnsafe bool `yaml:"markUnsafe"`
Selector metav1.LabelSelector `yaml:"selector"`

// Not configurable via watches.yaml
Expand All @@ -76,6 +77,7 @@ var (
watchDependentResourcesDefault = true
watchClusterScopedResourcesDefault = false
snakeCaseParametersDefault = true
markUnsafeDefault = false
selectorDefault = metav1.LabelSelector{}

// these are overridden by cmdline flags
Expand Down Expand Up @@ -127,6 +129,7 @@ type alias struct {
WatchDependentResources *bool `yaml:"watchDependentResources,omitempty"`
WatchClusterScopedResources *bool `yaml:"watchClusterScopedResources,omitempty"`
SnakeCaseParameters *bool `yaml:"snakeCaseParameters"`
MarkUnsafe *bool `yaml:"markUnsafe"`
Blacklist []schema.GroupVersionKind `yaml:"blacklist,omitempty"`
Finalizer *Finalizer `yaml:"finalizer"`
Selector tempLabelSelector `yaml:"selector"`
Expand Down Expand Up @@ -162,6 +165,10 @@ func (w *Watch) setValuesFromAlias(tmp alias) error {
tmp.SnakeCaseParameters = &snakeCaseParametersDefault
}

if tmp.MarkUnsafe == nil {
tmp.MarkUnsafe = &markUnsafeDefault
}

gvk := schema.GroupVersionKind{
Group: tmp.Group,
Version: tmp.Version,
Expand All @@ -183,6 +190,7 @@ func (w *Watch) setValuesFromAlias(tmp alias) error {
w.ManageStatus = *tmp.ManageStatus
w.WatchDependentResources = *tmp.WatchDependentResources
w.SnakeCaseParameters = *tmp.SnakeCaseParameters
w.MarkUnsafe = *tmp.MarkUnsafe
w.WatchClusterScopedResources = *tmp.WatchClusterScopedResources
w.Finalizer = tmp.Finalizer
w.AnsibleVerbosity = getAnsibleVerbosity(gvk, ansibleVerbosityDefault)
Expand Down Expand Up @@ -316,6 +324,7 @@ func New(gvk schema.GroupVersionKind, role, playbook string, vars map[string]int
WatchDependentResources: watchDependentResourcesDefault,
WatchClusterScopedResources: watchClusterScopedResourcesDefault,
SnakeCaseParameters: snakeCaseParametersDefault,
MarkUnsafe: markUnsafeDefault,
Finalizer: finalizer,
AnsibleVerbosity: ansibleVerbosityDefault,
Selector: selectorDefault,
Expand Down
19 changes: 19 additions & 0 deletions internal/ansible/watches/watches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func TestNew(t *testing.T) {
t.Fatalf("Unexpected snakeCaseParameters %v expected %v", watch.SnakeCaseParameters,
snakeCaseParametersDefault)
}
if watch.MarkUnsafe != markUnsafeDefault {
t.Fatalf("Unexpected markUnsafe %v expected %v", watch.MarkUnsafe, markUnsafeDefault)
}
if watch.WatchClusterScopedResources != watchClusterScopedResourcesDefault {
t.Fatalf("Unexpected watchClusterScopedResources %v expected %v",
watch.WatchClusterScopedResources, watchClusterScopedResourcesDefault)
Expand Down Expand Up @@ -147,6 +150,18 @@ func TestLoad(t *testing.T) { //nolint:gocyclo
WatchDependentResources: true,
WatchClusterScopedResources: false,
SnakeCaseParameters: true,
MarkUnsafe: false,
},
Watch{
GroupVersionKind: schema.GroupVersionKind{
Version: "v1alpha1",
Group: "app.example.com",
Kind: "WithUnsafeMarked",
},
Playbook: validTemplate.ValidPlaybook,
ManageStatus: true,
ReconcilePeriod: twoSeconds,
MarkUnsafe: true,
},
Watch{
GroupVersionKind: schema.GroupVersionKind{
Expand Down Expand Up @@ -536,6 +551,10 @@ func TestLoad(t *testing.T) { //nolint:gocyclo
t.Fatalf("The GVK: %v unexpected reconcile period: %v expected reconcile period: %v", gvk,
gotWatch.ReconcilePeriod, expectedWatch.ReconcilePeriod)
}
if gotWatch.MarkUnsafe != expectedWatch.MarkUnsafe {
t.Fatalf("The GVK: %v unexpected mark unsafe: %v expected mark unsafe: %v", gvk,
gotWatch.MarkUnsafe, expectedWatch.MarkUnsafe)
}

for i, val := range expectedWatch.Blacklist {
if val != gotWatch.Blacklist[i] {
Expand Down

0 comments on commit 6e1441c

Please sign in to comment.