Skip to content

Commit 7f7a6e8

Browse files
JustinKuliopenshift-merge-robot
authored andcommitted
Fix metadata.name requirement in manifests
Names are *not* required in all manifests. It is possible (and desired) to create `mustnothave` configurationpolicies which target objects with no name specified - all instances of that type will be checked for the other details in the objectDefinition. Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent 7532f1c commit 7f7a6e8

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

internal/utils.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func getPolicyTemplates(policyConf *types.PolicyConfig, ns string) ([]map[string
267267
previousTemplate.Namespace = prevNS
268268
}
269269

270-
// these fields are already known to exist from previous validation
270+
// these fields are known to exist since the plugin created them
271271
previousTemplate.Name, _, _ = unstructured.NestedString(tmpl, "objectDefinition", "metadata", "name")
272272
previousTemplate.APIVersion, _, _ = unstructured.NestedString(tmpl, "objectDefinition", "apiVersion")
273273
previousTemplate.Kind, _, _ = unstructured.NestedString(tmpl, "objectDefinition", "kind")
@@ -289,7 +289,8 @@ func setTemplateOptions(tmpl map[string]interface{}, ignorePending bool, extraDe
289289

290290
// isPolicyTypeManifest determines if the manifest is a non-root policy manifest
291291
// by checking apiVersion and kind fields.
292-
// Return error when apiVersion and kind fields aren't string.
292+
// Return error when apiVersion and kind fields aren't string, or if the manifest
293+
// is a non-root policy manifest, but it is invalid because it is missing a name.
293294
func isPolicyTypeManifest(manifest map[string]interface{}) (bool, error) {
294295
apiVersion, found, err := unstructured.NestedString(manifest, "apiVersion")
295296
if !found || err != nil {
@@ -301,16 +302,18 @@ func isPolicyTypeManifest(manifest map[string]interface{}) (bool, error) {
301302
return false, errors.New("invalid or not found kind")
302303
}
303304

304-
// Name is required in manifests, of all types.
305-
_, found, err = unstructured.NestedString(manifest, "metadata", "name")
306-
if !found || err != nil {
307-
return false, errors.New("invalid or not found metadata.name")
308-
}
309-
310305
isPolicy := strings.HasPrefix(apiVersion, "policy.open-cluster-management.io") &&
311306
kind != "Policy" &&
312307
strings.HasSuffix(kind, "Policy")
313308

309+
if isPolicy {
310+
// metadata.name is required on policy manifests
311+
_, found, err = unstructured.NestedString(manifest, "metadata", "name")
312+
if !found || err != nil {
313+
return true, errors.New("invalid or not found metadata.name")
314+
}
315+
}
316+
314317
return isPolicy, nil
315318
}
316319

internal/utils_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,17 +673,28 @@ func TestIsPolicyTypeManifest(t *testing.T) {
673673
wantVal: false,
674674
wantErr: "invalid or not found apiVersion",
675675
},
676-
"missing name": {
676+
"missing name in ConfigurationPolicy": {
677677
manifest: map[string]interface{}{
678678
"apiVersion": policyAPIVersion,
679679
"kind": "ConfigurationPolicy",
680680
"metadata": map[string]interface{}{
681681
"namespace": "foo",
682682
},
683683
},
684-
wantVal: false,
684+
wantVal: true,
685685
wantErr: "invalid or not found metadata.name",
686686
},
687+
"missing name in non-policy": {
688+
manifest: map[string]interface{}{
689+
"apiVersion": "apps.open-cluster-management.io/v1",
690+
"kind": "PlacementRule",
691+
"metadata": map[string]interface{}{
692+
"name": "foo",
693+
},
694+
},
695+
wantVal: false,
696+
wantErr: "",
697+
},
687698
}
688699

689700
for name, test := range tests {

0 commit comments

Comments
 (0)