fix(kube): correctly extract namespace from namespace manifest [EE-6555] (#11675)

Co-authored-by: Prabhat Khera <prabhat.khera@portainer.io>
pull/11540/head
Matt Hook 2024-05-02 14:28:07 +12:00 committed by GitHub
parent d727cbc373
commit acaa564557
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 5 deletions

View File

@ -125,12 +125,27 @@ func GetNamespace(manifestYaml []byte) (string, error) {
return "", errors.Wrap(err, "failed to unmarshal yaml manifest when obtaining namespace") return "", errors.Wrap(err, "failed to unmarshal yaml manifest when obtaining namespace")
} }
if _, ok := m["metadata"]; ok { kind, ok := m["kind"].(string)
if namespace, ok := m["metadata"].(map[string]interface{})["namespace"]; ok { if !ok {
return namespace.(string), nil return "", errors.New("invalid kubernetes manifest, missing 'kind' field")
}
} }
if _, ok := m["metadata"]; ok {
var namespace interface{}
var ok bool
if strings.EqualFold(kind, "namespace") {
namespace, ok = m["metadata"].(map[string]interface{})["name"]
} else {
namespace, ok = m["metadata"].(map[string]interface{})["namespace"]
}
if ok {
if v, ok := namespace.(string); ok {
return v, nil
}
return "", errors.New("invalid kubernetes manifest, 'namespace' field is not a string")
}
}
return "", nil return "", nil
} }

View File

@ -648,7 +648,7 @@ func Test_GetNamespace(t *testing.T) {
input: `apiVersion: v1 input: `apiVersion: v1
kind: Namespace kind: Namespace
metadata: metadata:
namespace: test-namespace name: test-namespace
`, `,
want: "test-namespace", want: "test-namespace",
}, },