Simplify kind and name change code.

This commit is contained in:
monopole
2021-05-09 16:18:34 -07:00
parent fcfdf6be51
commit 82abd7e9ea
6 changed files with 27 additions and 30 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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)
}
}

View File

@@ -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.

View File

@@ -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
)

View File

@@ -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