From 82abd7e9ea959d6ad58b09f205e2996476d62826 Mon Sep 17 00:00:00 2001 From: monopole Date: Sun, 9 May 2021 16:18:34 -0700 Subject: [PATCH] Simplify kind and name change code. --- api/builtins/PatchTransformer.go | 4 +-- api/internal/plugins/utils/utils.go | 3 +- api/resmap/reswrangler.go | 11 ++++--- api/resource/resource.go | 29 +++++++++---------- cmd/depprobcheck/go.mod | 6 ++-- .../patchtransformer/PatchTransformer.go | 4 +-- 6 files changed, 27 insertions(+), 30 deletions(-) diff --git a/api/builtins/PatchTransformer.go b/api/builtins/PatchTransformer.go index e9a3bbdbd..07038af1e 100644 --- a/api/builtins/PatchTransformer.go +++ b/api/builtins/PatchTransformer.go @@ -62,10 +62,10 @@ func (p *PatchTransformerPlugin) Config( if errSM == nil { p.loadedPatch = patchSM if p.Options["allowNameChange"] { - p.loadedPatch.SetAllowNameChange("true") + p.loadedPatch.AllowNameChange() } if p.Options["allowKindChange"] { - p.loadedPatch.SetAllowKindChange("true") + p.loadedPatch.AllowKindChange() } } else { p.decodedPatch = patchJson diff --git a/api/internal/plugins/utils/utils.go b/api/internal/plugins/utils/utils.go index 7fa461191..1046e82be 100644 --- a/api/internal/plugins/utils/utils.go +++ b/api/internal/plugins/utils/utils.go @@ -158,7 +158,8 @@ func UpdateResMapValues(pluginName string, h *resmap.PluginHelpers, output []byt } for _, r := range resources { - removeIDAnnotation(r) // stale--not manipulated by plugin transformers + // stale--not manipulated by plugin transformers + removeIDAnnotation(r) // Add to the new map, checking for duplicates if err := newMap.Append(r); err != nil { diff --git a/api/resmap/reswrangler.go b/api/resmap/reswrangler.go index 17ed49ca9..378ecf0e3 100644 --- a/api/resmap/reswrangler.go +++ b/api/resmap/reswrangler.go @@ -231,14 +231,14 @@ func demandOneMatch( return nil, fmt.Errorf("no matches for %s %s", s, id) } -// GroupedByCurrentNamespace implements ResMap.GroupByCurrentNamespace +// GroupedByCurrentNamespace implements ResMap. func (m *resWrangler) GroupedByCurrentNamespace() map[string][]*resource.Resource { items := m.groupedByCurrentNamespace() delete(items, resid.TotallyNotANamespace) return items } -// ClusterScoped implements ResMap.ClusterScoped +// ClusterScoped implements ResMap. func (m *resWrangler) ClusterScoped() []*resource.Resource { return m.groupedByCurrentNamespace()[resid.TotallyNotANamespace] } @@ -255,7 +255,7 @@ func (m *resWrangler) groupedByCurrentNamespace() map[string][]*resource.Resourc return byNamespace } -// GroupedByNamespace implements ResMap.GroupByOrginalNamespace +// GroupedByOriginalNamespace implements ResMap. func (m *resWrangler) GroupedByOriginalNamespace() map[string][]*resource.Resource { items := m.groupedByOriginalNamespace() delete(items, resid.TotallyNotANamespace) @@ -336,7 +336,7 @@ func (m *resWrangler) ErrorIfNotEqualSets(other ResMap) error { return nil } -// ErrorIfNotEqualList implements ResMap. +// ErrorIfNotEqualLists implements ResMap. func (m *resWrangler) ErrorIfNotEqualLists(other ResMap) error { m2, ok := other.(*resWrangler) if !ok { @@ -409,8 +409,7 @@ func (m *resWrangler) SubsetThatCouldBeReferencedByResource( // The two objects are namespaced (not cluster-scoped), AND // are in different namespaces. // There's still a chance they can refer to each other. - ns := possibleTarget.GetNamespace() - if roleBindingNamespaces[ns] { + if roleBindingNamespaces[possibleTarget.GetNamespace()] { result.append(possibleTarget) } } diff --git a/api/resource/resource.go b/api/resource/resource.go index 456c274d1..8667b9f1e 100644 --- a/api/resource/resource.go +++ b/api/resource/resource.go @@ -41,6 +41,7 @@ const ( // and kinds of their targets buildAnnotationAllowNameChange = konfig.ConfigAnnoDomain + "/allowNameChange" buildAnnotationAllowKindChange = konfig.ConfigAnnoDomain + "/allowKindChange" + allowed = "allowed" ) var buildAnnotations = []string{ @@ -210,11 +211,11 @@ func (r *Resource) DeepCopy() *Resource { rc := &Resource{ node: r.node.Copy(), } - rc.copyOtherFields(r) + rc.copyKustomizeSpecificFields(r) return rc } -// CopyMergeMetaDataFields copies everything but the non-metadata in +// CopyMergeMetaDataFieldsFrom copies everything but the non-metadata in // the resource. func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) { r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels())) @@ -222,10 +223,10 @@ func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) { mergeStringMaps(other.GetAnnotations(), r.GetAnnotations())) r.SetName(other.GetName()) r.SetNamespace(other.GetNamespace()) - r.copyOtherFields(other) + r.copyKustomizeSpecificFields(other) } -func (r *Resource) copyOtherFields(other *Resource) { +func (r *Resource) copyKustomizeSpecificFields(other *Resource) { r.options = other.options r.refBy = other.copyRefBy() r.refVarNames = copyStringSlice(other.refVarNames) @@ -390,32 +391,28 @@ func (r *Resource) setPreviousId(ns string, n string, k string) *Resource { return r } -func (r *Resource) SetAllowNameChange(value string) { +func (r *Resource) AllowNameChange() { annotations := r.GetAnnotations() - annotations[buildAnnotationAllowNameChange] = value + annotations[buildAnnotationAllowNameChange] = allowed r.SetAnnotations(annotations) } func (r *Resource) NameChangeAllowed() bool { annotations := r.GetAnnotations() - if allowed, set := annotations[buildAnnotationAllowNameChange]; set && allowed == "true" { - return true - } - return false + v, ok := annotations[buildAnnotationAllowNameChange] + return ok && v == allowed } -func (r *Resource) SetAllowKindChange(value string) { +func (r *Resource) AllowKindChange() { annotations := r.GetAnnotations() - annotations[buildAnnotationAllowKindChange] = value + annotations[buildAnnotationAllowKindChange] = allowed r.SetAnnotations(annotations) } func (r *Resource) KindChangeAllowed() bool { annotations := r.GetAnnotations() - if allowed, set := annotations[buildAnnotationAllowKindChange]; set && allowed == "true" { - return true - } - return false + v, ok := annotations[buildAnnotationAllowKindChange] + return ok && v == allowed } // String returns resource as JSON. diff --git a/cmd/depprobcheck/go.mod b/cmd/depprobcheck/go.mod index fef443dfe..958bbe1e0 100644 --- a/cmd/depprobcheck/go.mod +++ b/cmd/depprobcheck/go.mod @@ -5,7 +5,7 @@ go 1.16 require ( k8s.io/cli-runtime v0.20.4 k8s.io/kube-openapi v0.0.0-20210323165736-1a6458611d18 - // k8s.io/kube-openapi v0.0.0-20210419153605-00de3ae54c30 - // k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e - // github.com/go-openapi/swag v0.19.5 +// k8s.io/kube-openapi v0.0.0-20210419153605-00de3ae54c30 +// k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e +// github.com/go-openapi/swag v0.19.5 ) diff --git a/plugin/builtin/patchtransformer/PatchTransformer.go b/plugin/builtin/patchtransformer/PatchTransformer.go index 0e254a7c6..54fe9dedb 100644 --- a/plugin/builtin/patchtransformer/PatchTransformer.go +++ b/plugin/builtin/patchtransformer/PatchTransformer.go @@ -66,10 +66,10 @@ func (p *plugin) Config( if errSM == nil { p.loadedPatch = patchSM if p.Options["allowNameChange"] { - p.loadedPatch.SetAllowNameChange("true") + p.loadedPatch.AllowNameChange() } if p.Options["allowKindChange"] { - p.loadedPatch.SetAllowKindChange("true") + p.loadedPatch.AllowKindChange() } } else { p.decodedPatch = patchJson