-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnamespace.go
60 lines (54 loc) · 1.84 KB
/
namespace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package kubernetes
import (
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// SetNamespaceOnManifest updates the namespace on the given manifest.
//
// If no namespace is set on the manifest and no namespace override is passed
// in then we set the namespace to 'default'.
//
// If namespaceOverride is empty it will NOT override the namespace set
// on the manifest.
//
// We only override the namespace if the manifest is NOT cluster scoped
// (i.e. a ClusterRole) and namespaceOverride is NOT an empty string.
func SetNamespaceOnManifest(u *unstructured.Unstructured, namespaceOverride string) {
if namespaceOverride == "" {
setDefaultNamespaceIfScopedAndNoneSet(u)
} else {
setNamespaceIfScoped(namespaceOverride, u)
}
}
func setDefaultNamespaceIfScopedAndNoneSet(u *unstructured.Unstructured) {
namespace := u.GetNamespace()
if isNamespaceScoped(u.GetKind()) && namespace == "" {
namespace = "default"
u.SetNamespace(namespace)
}
}
func setNamespaceIfScoped(namespace string, u *unstructured.Unstructured) {
if isNamespaceScoped(u.GetKind()) {
u.SetNamespace(namespace)
}
}
// isNamespaceScoped returns true if the kind is namespace-scoped.
//
// Cluster-scoped kinds are:
// - apiService
// - clusterRole
// - clusterRoleBinding
// - customResourceDefinition
// - mutatingWebhookConfiguration
// - namespace
// - persistentVolume
// - podSecurityPolicy
// - storageClass
// - validatingWebhookConfiguration
//
// See https://github.com/spinnaker/clouddriver/blob/58ab154b0ec0d62772201b5b319af349498a4e3f/clouddriver-kubernetes/src/main/java/com/netflix/spinnaker/clouddriver/kubernetes/description/manifest/KubernetesKindProperties.java#L31
// for clouddriver OSS namespace-scoped kinds.
func isNamespaceScoped(kind string) bool {
_, clusterScoped := clusterScopedKinds[strings.ToLower(kind)]
return !clusterScoped
}