mirror of https://github.com/portainer/portainer
fix(kube): correctly extract namespace from namespace manifest [EE-6555] (#11675)
Co-authored-by: Prabhat Khera <prabhat.khera@portainer.io>pull/11540/head
parent
d727cbc373
commit
acaa564557
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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",
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue