mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
Add TypeMeta.IsNamespaceable
This commit is contained in:
@@ -97,6 +97,46 @@ type TypeMeta struct {
|
|||||||
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
|
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hardcoded list.
|
||||||
|
// TODO: replace this with data acquired from openapi.
|
||||||
|
var notNamespaceableKinds = []string{
|
||||||
|
"APIService",
|
||||||
|
"CSIDriver",
|
||||||
|
"CSINode",
|
||||||
|
"CertificateSigningRequest",
|
||||||
|
"Cluster",
|
||||||
|
"ClusterRole",
|
||||||
|
"ClusterRoleBinding",
|
||||||
|
"ComponentStatus",
|
||||||
|
"CustomResourceDefinition",
|
||||||
|
"MutatingWebhookConfiguration",
|
||||||
|
"Namespace",
|
||||||
|
"Node",
|
||||||
|
"PersistentVolume",
|
||||||
|
"PodSecurityPolicy",
|
||||||
|
"PriorityClass",
|
||||||
|
"RuntimeClass",
|
||||||
|
"SelfSubjectAccessReview",
|
||||||
|
"SelfSubjectRulesReview",
|
||||||
|
"StorageClass",
|
||||||
|
"SubjectAccessReview",
|
||||||
|
"TokenReview",
|
||||||
|
"ValidatingWebhookConfiguration",
|
||||||
|
"VolumeAttachment",
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsNamespaceable returns true if this TypeMeta is for an object
|
||||||
|
// that can be placed in a namespace.
|
||||||
|
// Implements https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#not-all-objects-are-in-a-namespace
|
||||||
|
func (tm TypeMeta) IsNamespaceable() bool {
|
||||||
|
for _, k := range notNamespaceableKinds {
|
||||||
|
if k == tm.Kind {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// NameMeta contains name information.
|
// NameMeta contains name information.
|
||||||
type NameMeta struct {
|
type NameMeta struct {
|
||||||
// Name is the metadata.name field of a Resource
|
// Name is the metadata.name field of a Resource
|
||||||
|
|||||||
@@ -61,3 +61,78 @@ func TestIsYNodeEmptySeq(t *testing.T) {
|
|||||||
t.Fatalf("a node with content isn't empty")
|
t.Fatalf("a node with content isn't empty")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsNameSpaceable1(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
tm TypeMeta
|
||||||
|
isNamespaceable bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
tm: TypeMeta{Kind: "ClusterRole"},
|
||||||
|
isNamespaceable: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tm: TypeMeta{Kind: "Namespace"},
|
||||||
|
isNamespaceable: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tm: TypeMeta{Kind: "Deployment"},
|
||||||
|
isNamespaceable: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
if tc.tm.IsNamespaceable() {
|
||||||
|
if !tc.isNamespaceable {
|
||||||
|
t.Fatalf("%v is namespaceable, but shouldn't be", tc.tm)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if tc.isNamespaceable {
|
||||||
|
t.Fatalf("%v is not namespaceable, but should be", tc.tm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsNameSpaceable2(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
yammy string
|
||||||
|
isNamespaceable bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
yammy: `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: bilbo
|
||||||
|
namespace: middleEarth
|
||||||
|
`,
|
||||||
|
isNamespaceable: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
yammy: `apiVersion: v1
|
||||||
|
kind: Namespace
|
||||||
|
metadata:
|
||||||
|
name: middleEarth
|
||||||
|
`,
|
||||||
|
isNamespaceable: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
rn, err := Parse(tc.yammy)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected parse error %v from json: %s", err, tc.yammy)
|
||||||
|
}
|
||||||
|
meta, err := rn.GetMeta()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected meta error %v", err)
|
||||||
|
}
|
||||||
|
if meta.IsNamespaceable() {
|
||||||
|
if !tc.isNamespaceable {
|
||||||
|
t.Fatalf("%v is namespaceable, but shouldn't be", meta)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if tc.isNamespaceable {
|
||||||
|
t.Fatalf("%v is not namespaceable, but should be", meta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user