Consolidate IsClusterKind and IsNamespaceableKind method to avoid duplication

This commit is contained in:
Jerome Brette
2019-06-25 13:46:49 -05:00
parent 2bba0a6aa3
commit 74f5e74b89
4 changed files with 5 additions and 26 deletions

View File

@@ -160,27 +160,6 @@ func (x Gvk) IsSelected(selector *Gvk) bool {
return true return true
} }
var clusterLevelKinds = []string{
"APIService",
"ClusterRoleBinding",
"ClusterRole",
"CustomResourceDefinition",
"Namespace",
"PersistentVolume",
"MutatingWebhookConfiguration",
"ValidatingWebhookConfiguration",
}
// IsClusterKind returns true if x is a cluster-level Gvk
func (x Gvk) IsClusterKind() bool {
for _, k := range clusterLevelKinds {
if k == x.Kind {
return true
}
}
return false
}
var notNamespaceableKinds = []string{ var notNamespaceableKinds = []string{
"APIService", "APIService",
"CSIDriver", "CSIDriver",
@@ -207,7 +186,7 @@ var notNamespaceableKinds = []string{
"VolumeAttachment", "VolumeAttachment",
} }
// IsNamespaceableKind returns true if x is a namespable Gvk // IsNamespaceableKind returns true if x is a namespeable Gvk
// Implements https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#not-all-objects-are-in-a-namespace // Implements https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#not-all-objects-are-in-a-namespace
func (x Gvk) IsNamespaceableKind() bool { func (x Gvk) IsNamespaceableKind() bool {
for _, k := range notNamespaceableKinds { for _, k := range notNamespaceableKinds {

View File

@@ -488,12 +488,12 @@ func (m *resWrangler) makeCopy(copier resCopier) ResMap {
func (m *resWrangler) SubsetThatCouldBeReferencedByResource( func (m *resWrangler) SubsetThatCouldBeReferencedByResource(
inputRes *resource.Resource) ResMap { inputRes *resource.Resource) ResMap {
inputId := inputRes.OrgId() inputId := inputRes.OrgId()
if inputId.IsClusterKind() { if !inputId.IsNamespaceableKind() {
return m return m
} }
result := New() result := New()
for _, r := range m.Resources() { for _, r := range m.Resources() {
if r.OrgId().IsClusterKind() || inputRes.InSameFuzzyNamespace(r) { if !r.OrgId().IsNamespaceableKind() || inputRes.InSameFuzzyNamespace(r) {
err := result.Append(r) err := result.Append(r)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@@ -62,7 +62,7 @@ const metaNamespace = "metadata/namespace"
// object itself doesn't live in a namespace). // object itself doesn't live in a namespace).
func doIt(id resid.ResId, fs *config.FieldSpec) bool { func doIt(id resid.ResId, fs *config.FieldSpec) bool {
return fs.Path != metaNamespace || return fs.Path != metaNamespace ||
(fs.Path == metaNamespace && !id.IsClusterKind()) (fs.Path == metaNamespace && id.IsNamespaceableKind())
} }
func (p *NamespaceTransformerPlugin) changeNamespace( func (p *NamespaceTransformerPlugin) changeNamespace(

View File

@@ -63,7 +63,7 @@ const metaNamespace = "metadata/namespace"
// object itself doesn't live in a namespace). // object itself doesn't live in a namespace).
func doIt(id resid.ResId, fs *config.FieldSpec) bool { func doIt(id resid.ResId, fs *config.FieldSpec) bool {
return fs.Path != metaNamespace || return fs.Path != metaNamespace ||
(fs.Path == metaNamespace && !id.IsClusterKind()) (fs.Path == metaNamespace && id.IsNamespaceableKind())
} }
func (p *plugin) changeNamespace( func (p *plugin) changeNamespace(