Skip to content

Commit e9b1b07

Browse files
committed
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 2eb24f8 commit e9b1b07

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
@@ -83,8 +83,14 @@ func cliTest(testName string) func(t *testing.T) {
8383
// If an error file is provided, overwrite the
8484
// expected and resolved with the error contents
8585
expectedBytes = errorBytes
86+
8687
errMatch := regexp.MustCompile("template: tmpl:[0-9]+:[0-9]+: .*")
8788
resolvedYAML = errMatch.Find([]byte(err.Error()))
89+
90+
// If nothing is matched, use the entire error
91+
if len(resolvedYAML) == 0 {
92+
resolvedYAML = []byte(err.Error())
93+
}
8894
}
8995

9096
if !bytes.Equal(expectedBytes, resolvedYAML) {
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
@@ -14,6 +14,7 @@ import (
1414
"k8s.io/apimachinery/pkg/runtime/schema"
1515
"k8s.io/client-go/dynamic"
1616
"k8s.io/client-go/tools/clientcmd"
17+
k8syaml "sigs.k8s.io/yaml"
1718

1819
"github.com/stolostron/go-template-utils/v6/pkg/templates"
1920
)
@@ -74,7 +75,7 @@ func HandleFile(yamlFile string) ([]byte, error) {
7475
func ProcessTemplate(yamlBytes []byte, hubKubeConfigPath, clusterName, hubNS string) ([]byte, error) {
7576
policy := unstructured.Unstructured{}
7677

77-
err := yaml.Unmarshal(yamlBytes, &policy.Object)
78+
err := k8syaml.Unmarshal(yamlBytes, &policy.Object)
7879
if err != nil {
7980
return nil, fmt.Errorf("failed to parse input to YAML: %w", err)
8081
}
@@ -229,20 +230,26 @@ func processPolicyTemplate(
229230
policy *unstructured.Unstructured,
230231
resolver *templates.TemplateResolver,
231232
) error {
232-
policyTemplates, _, err := unstructured.NestedSlice(policy.Object, "spec", "policy-templates")
233+
policyTemplates, found, err := unstructured.NestedSlice(policy.Object, "spec", "policy-templates")
233234
if err != nil {
234235
return fmt.Errorf("invalid policy-templates array was provided: %w", err)
236+
} else if !found {
237+
return errors.New("invalid policy-templates array was provided: spec.policy-templates keys not found")
235238
}
236239

237240
for i := range policyTemplates {
238-
policyTemplate, ok := policyTemplates[i].(map[string]interface{})
241+
policyTemplate, ok := policyTemplates[i].(map[string]any)
239242
if !ok {
240-
return fmt.Errorf("invalid policy-templates entry was provided: %w", err)
243+
return fmt.Errorf("invalid policy-templates entry was provided at index %d: "+
244+
"could not parse to map[string]interface{}", i)
241245
}
242246

243-
objectDefinition, ok := policyTemplate["objectDefinition"].(map[string]interface{})
244-
if !ok {
245-
return fmt.Errorf("invalid policy-templates entry was provided: %w", err)
247+
objectDefinition, found, err := unstructured.NestedMap(policyTemplate, "objectDefinition")
248+
if err != nil {
249+
return fmt.Errorf("invalid policy-templates entry was provided at index %d: %w", i, err)
250+
} else if !found {
251+
return fmt.Errorf("invalid policy-templates entry was provided at index %d: "+
252+
"objectDefinition key not found", i)
246253
}
247254

248255
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.0
1515
k8s.io/klog v1.0.0
1616
sigs.k8s.io/controller-runtime v0.19.0
17+
sigs.k8s.io/yaml v1.4.0
1718
)
1819

1920
require (
@@ -65,5 +66,4 @@ require (
6566
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
6667
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
6768
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
68-
sigs.k8s.io/yaml v1.4.0 // indirect
6969
)

0 commit comments

Comments
 (0)