Skip to content

Commit 73dd6f9

Browse files
dhaiducekopenshift-merge-bot[bot]
authored andcommitted
fix: Resolve panic when manifest has an int
ref: https://issues.redhat.com/browse/ACM-18816 Signed-off-by: Dale Haiducek <[email protected]>
1 parent cfd150c commit 73dd6f9

File tree

19 files changed

+84
-8
lines changed

19 files changed

+84
-8
lines changed

cmd/template-resolver/client_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,14 @@ func cliTest(testName string) func(t *testing.T) {
107107
// If an error file is provided, overwrite the
108108
// expected and resolved with the error contents
109109
expectedBytes = errorBytes
110+
110111
errMatch := regexp.MustCompile("template: tmpl:[0-9]+:[0-9]+: .*")
111112
resolvedYAML = errMatch.Find([]byte(err.Error()))
113+
114+
// If nothing is matched, use the entire error
115+
if len(resolvedYAML) == 0 {
116+
resolvedYAML = []byte(err.Error())
117+
}
112118
}
113119

114120
// Compare the saved resources. Use hubCluster resources
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invalid policy-templates entry was provided at index 0: objectDefinition key not found
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: invalid-object-definition
5+
spec:
6+
policy-templates: [{}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invalid policy-templates entry was provided at index 0: .objectDefinition accessor error: 0 is of the type float64, expected map[string]interface{}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: invalid-object-definition
5+
spec:
6+
policy-templates:
7+
- objectDefinition: 0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invalid policy-templates array was provided: spec.policy-templates keys not found
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: no-spec
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invalid policy-templates array was provided: .spec.policy-templates accessor error: [] is of the type []interface {}, expected map[string]interface{}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: invalid-spec
5+
spec: []
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invalid policy-templates entry was provided at index 0: could not parse to map[string]interface{}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: invalid-policy-template-entry
5+
spec:
6+
policy-templates:
7+
- whee!!!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invalid policy-templates array was provided: spec.policy-templates keys not found
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: missing-policy-templates
5+
spec: {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invalid policy-templates array was provided: .spec.policy-templates accessor error: map[] is of the type map[string]interface {}, expected []interface{}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: invalid-policy-templates
5+
spec:
6+
policy-templates: {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: invalid-spec
5+
spec:
6+
policy-templates:
7+
- objectDefinition:
8+
int-map: 100
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: invalid-spec
5+
spec:
6+
policy-templates:
7+
- objectDefinition:
8+
int-map: 100

cmd/template-resolver/utils/resolver_utils.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/apimachinery/pkg/runtime/schema"
1818
"k8s.io/client-go/dynamic"
1919
"k8s.io/client-go/tools/clientcmd"
20+
k8syaml "sigs.k8s.io/yaml"
2021

2122
"github.com/stolostron/go-template-utils/v7/pkg/templates"
2223
)
@@ -77,7 +78,7 @@ func HandleFile(yamlFile string) ([]byte, error) {
7778
func (t *TemplateResolver) ProcessTemplate(yamlBytes []byte) ([]byte, error) {
7879
policy := unstructured.Unstructured{}
7980

80-
err := yaml.Unmarshal(yamlBytes, &policy.Object)
81+
err := k8syaml.Unmarshal(yamlBytes, &policy.Object)
8182
if err != nil {
8283
return nil, fmt.Errorf("failed to parse input to YAML: %w", err)
8384
}
@@ -285,20 +286,26 @@ func processPolicyTemplate(
285286
resolver *templates.TemplateResolver,
286287
tempCtx templates.TemplateContext,
287288
) error {
288-
policyTemplates, _, err := unstructured.NestedSlice(policy.Object, "spec", "policy-templates")
289+
policyTemplates, found, err := unstructured.NestedSlice(policy.Object, "spec", "policy-templates")
289290
if err != nil {
290291
return fmt.Errorf("invalid policy-templates array was provided: %w", err)
292+
} else if !found {
293+
return errors.New("invalid policy-templates array was provided: spec.policy-templates keys not found")
291294
}
292295

293296
for i := range policyTemplates {
294-
policyTemplate, ok := policyTemplates[i].(map[string]interface{})
297+
policyTemplate, ok := policyTemplates[i].(map[string]any)
295298
if !ok {
296-
return fmt.Errorf("invalid policy-templates entry was provided: %w", err)
299+
return fmt.Errorf("invalid policy-templates entry was provided at index %d: "+
300+
"could not parse to map[string]interface{}", i)
297301
}
298302

299-
objectDefinition, ok := policyTemplate["objectDefinition"].(map[string]interface{})
300-
if !ok {
301-
return fmt.Errorf("invalid policy-templates entry was provided: %w", err)
303+
objectDefinition, found, err := unstructured.NestedMap(policyTemplate, "objectDefinition")
304+
if err != nil {
305+
return fmt.Errorf("invalid policy-templates entry was provided at index %d: %w", i, err)
306+
} else if !found {
307+
return fmt.Errorf("invalid policy-templates entry was provided at index %d: "+
308+
"objectDefinition key not found", i)
302309
}
303310

304311
templateObj := unstructured.Unstructured{Object: objectDefinition}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
k8s.io/client-go v0.31.9
1515
k8s.io/klog v1.0.0
1616
sigs.k8s.io/controller-runtime v0.19.7
17+
sigs.k8s.io/yaml v1.4.0
1718
)
1819

1920
require (
@@ -64,5 +65,4 @@ require (
6465
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
6566
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
6667
sigs.k8s.io/structured-merge-diff/v4 v4.4.3 // indirect
67-
sigs.k8s.io/yaml v1.4.0 // indirect
6868
)

0 commit comments

Comments
 (0)