Skip to content

Commit 97594dc

Browse files
authoredMar 26, 2020
Properly handle resources with '-' in the group name (#2712)
* Properly handle resources with '-' in the group name Resources with '-' in the APIGroup now are properly formatted when passed to Ansible. For example ``` apiVersion: cert-manager.io/v1alpha2 kind: Certificate ``` will now result in a variabled called `_cert_manager_io_certificaterequest` instead of `_cert-manager_io_certificaterequest` Fixes #2486
1 parent b64896c commit 97594dc

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
### Bug Fixes
2323

24+
- Resources that use '-' in the APIGroup name can now be directly accessed by Ansible. ([#2712](https://github.com/operator-framework/operator-sdk/pull/2712))
2425
- Fixed issue in CSV generation that caused an incorrect path to be generated for descriptors on types that are fields in array elements. ([#2721](https://github.com/operator-framework/operator-sdk/pull/2721))
2526

2627
## v0.16.0

‎pkg/ansible/runner/runner.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ func (r *runner) makeParameters(u *unstructured.Unstructured) map[string]interfa
310310
parameters := paramconv.MapToSnake(spec)
311311
parameters["meta"] = map[string]string{"namespace": u.GetNamespace(), "name": u.GetName()}
312312

313-
objKey := fmt.Sprintf("_%v_%v", strings.Replace(r.GVK.Group, ".", "_", -1),
314-
strings.ToLower(r.GVK.Kind))
313+
objKey := escapeAnsibleKey(fmt.Sprintf("_%v_%v", r.GVK.Group, strings.ToLower(r.GVK.Kind)))
315314
parameters[objKey] = u.Object
316315

317316
specKey := fmt.Sprintf("%s_spec", objKey)
@@ -328,6 +327,16 @@ func (r *runner) makeParameters(u *unstructured.Unstructured) map[string]interfa
328327
return parameters
329328
}
330329

330+
// escapeAnsibleKey - replaces characters that would result in an inaccessible Ansible parameter with underscores
331+
// ie, _cert-manager.k8s.io would be converted to _cert_manager_k8s_io
332+
func escapeAnsibleKey(key string) string {
333+
disallowed := []string{".", "-"}
334+
for _, c := range disallowed {
335+
key = strings.ReplaceAll(key, c, "_")
336+
}
337+
return key
338+
}
339+
331340
func (r *runner) GetFinalizer() (string, bool) {
332341
if r.Finalizer != nil {
333342
return r.Finalizer.Name, true

‎pkg/ansible/runner/runner_test.go

+27-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"reflect"
2222
"testing"
2323

24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2425
"k8s.io/apimachinery/pkg/runtime/schema"
2526

2627
"github.com/operator-framework/operator-sdk/pkg/ansible/watches"
@@ -57,12 +58,13 @@ func TestNew(t *testing.T) {
5758
validPlaybook := filepath.Join(cwd, "testdata", "playbook.yml")
5859
validRole := filepath.Join(cwd, "testdata", "roles", "role")
5960
testCases := []struct {
60-
name string
61-
gvk schema.GroupVersionKind
62-
playbook string
63-
role string
64-
vars map[string]interface{}
65-
finalizer *watches.Finalizer
61+
name string
62+
gvk schema.GroupVersionKind
63+
playbook string
64+
role string
65+
vars map[string]interface{}
66+
finalizer *watches.Finalizer
67+
desiredObjectKey string
6668
}{
6769
{
6870
name: "basic runner with playbook",
@@ -141,6 +143,16 @@ func TestNew(t *testing.T) {
141143
},
142144
},
143145
},
146+
{
147+
name: "basic runner with a dash in the group name",
148+
gvk: schema.GroupVersionKind{
149+
Group: "operator-with-dash.example.com",
150+
Version: "v1alpha1",
151+
Kind: "Example",
152+
},
153+
playbook: validPlaybook,
154+
desiredObjectKey: "_operator_with_dash_example_com_example",
155+
},
144156
}
145157

146158
for _, tc := range testCases {
@@ -167,6 +179,15 @@ func TestNew(t *testing.T) {
167179
}
168180
}
169181

182+
// check that the group + kind are properly formatted into a parameter
183+
if tc.desiredObjectKey != "" {
184+
parameters := testRunnerStruct.makeParameters(&unstructured.Unstructured{})
185+
if _, ok := parameters[tc.desiredObjectKey]; !ok {
186+
t.Fatalf("Did not find expected objKey %v in parameters %+v", tc.desiredObjectKey, parameters)
187+
}
188+
189+
}
190+
170191
if testRunnerStruct.GVK != testWatch.GroupVersionKind {
171192
t.Fatalf("Unexpected GVK %v expected GVK %v", testRunnerStruct.GVK, testWatch.GroupVersionKind)
172193
}

0 commit comments

Comments
 (0)