Merge pull request #3884 from monopole/nitterFix

Simplify kind and name change code.
This commit is contained in:
Jeff Regan
2021-05-09 16:21:52 -07:00
committed by GitHub
5 changed files with 24 additions and 27 deletions

View File

@@ -62,10 +62,10 @@ func (p *PatchTransformerPlugin) Config(
if errSM == nil { if errSM == nil {
p.loadedPatch = patchSM p.loadedPatch = patchSM
if p.Options["allowNameChange"] { if p.Options["allowNameChange"] {
p.loadedPatch.SetAllowNameChange("true") p.loadedPatch.AllowNameChange()
} }
if p.Options["allowKindChange"] { if p.Options["allowKindChange"] {
p.loadedPatch.SetAllowKindChange("true") p.loadedPatch.AllowKindChange()
} }
} else { } else {
p.decodedPatch = patchJson p.decodedPatch = patchJson

View File

@@ -158,7 +158,8 @@ func UpdateResMapValues(pluginName string, h *resmap.PluginHelpers, output []byt
} }
for _, r := range resources { 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 // Add to the new map, checking for duplicates
if err := newMap.Append(r); err != nil { 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) 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 { func (m *resWrangler) GroupedByCurrentNamespace() map[string][]*resource.Resource {
items := m.groupedByCurrentNamespace() items := m.groupedByCurrentNamespace()
delete(items, resid.TotallyNotANamespace) delete(items, resid.TotallyNotANamespace)
return items return items
} }
// ClusterScoped implements ResMap.ClusterScoped // ClusterScoped implements ResMap.
func (m *resWrangler) ClusterScoped() []*resource.Resource { func (m *resWrangler) ClusterScoped() []*resource.Resource {
return m.groupedByCurrentNamespace()[resid.TotallyNotANamespace] return m.groupedByCurrentNamespace()[resid.TotallyNotANamespace]
} }
@@ -255,7 +255,7 @@ func (m *resWrangler) groupedByCurrentNamespace() map[string][]*resource.Resourc
return byNamespace return byNamespace
} }
// GroupedByNamespace implements ResMap.GroupByOrginalNamespace // GroupedByOriginalNamespace implements ResMap.
func (m *resWrangler) GroupedByOriginalNamespace() map[string][]*resource.Resource { func (m *resWrangler) GroupedByOriginalNamespace() map[string][]*resource.Resource {
items := m.groupedByOriginalNamespace() items := m.groupedByOriginalNamespace()
delete(items, resid.TotallyNotANamespace) delete(items, resid.TotallyNotANamespace)
@@ -336,7 +336,7 @@ func (m *resWrangler) ErrorIfNotEqualSets(other ResMap) error {
return nil return nil
} }
// ErrorIfNotEqualList implements ResMap. // ErrorIfNotEqualLists implements ResMap.
func (m *resWrangler) ErrorIfNotEqualLists(other ResMap) error { func (m *resWrangler) ErrorIfNotEqualLists(other ResMap) error {
m2, ok := other.(*resWrangler) m2, ok := other.(*resWrangler)
if !ok { if !ok {
@@ -409,8 +409,7 @@ func (m *resWrangler) SubsetThatCouldBeReferencedByResource(
// The two objects are namespaced (not cluster-scoped), AND // The two objects are namespaced (not cluster-scoped), AND
// are in different namespaces. // are in different namespaces.
// There's still a chance they can refer to each other. // There's still a chance they can refer to each other.
ns := possibleTarget.GetNamespace() if roleBindingNamespaces[possibleTarget.GetNamespace()] {
if roleBindingNamespaces[ns] {
result.append(possibleTarget) result.append(possibleTarget)
} }
} }

View File

@@ -41,6 +41,7 @@ const (
// and kinds of their targets // and kinds of their targets
buildAnnotationAllowNameChange = konfig.ConfigAnnoDomain + "/allowNameChange" buildAnnotationAllowNameChange = konfig.ConfigAnnoDomain + "/allowNameChange"
buildAnnotationAllowKindChange = konfig.ConfigAnnoDomain + "/allowKindChange" buildAnnotationAllowKindChange = konfig.ConfigAnnoDomain + "/allowKindChange"
allowed = "allowed"
) )
var buildAnnotations = []string{ var buildAnnotations = []string{
@@ -210,11 +211,11 @@ func (r *Resource) DeepCopy() *Resource {
rc := &Resource{ rc := &Resource{
node: r.node.Copy(), node: r.node.Copy(),
} }
rc.copyOtherFields(r) rc.copyKustomizeSpecificFields(r)
return rc return rc
} }
// CopyMergeMetaDataFields copies everything but the non-metadata in // CopyMergeMetaDataFieldsFrom copies everything but the non-metadata in
// the resource. // the resource.
func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) { func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) {
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels())) r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
@@ -222,10 +223,10 @@ func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) {
mergeStringMaps(other.GetAnnotations(), r.GetAnnotations())) mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
r.SetName(other.GetName()) r.SetName(other.GetName())
r.SetNamespace(other.GetNamespace()) 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.options = other.options
r.refBy = other.copyRefBy() r.refBy = other.copyRefBy()
r.refVarNames = copyStringSlice(other.refVarNames) r.refVarNames = copyStringSlice(other.refVarNames)
@@ -390,32 +391,28 @@ func (r *Resource) setPreviousId(ns string, n string, k string) *Resource {
return r return r
} }
func (r *Resource) SetAllowNameChange(value string) { func (r *Resource) AllowNameChange() {
annotations := r.GetAnnotations() annotations := r.GetAnnotations()
annotations[buildAnnotationAllowNameChange] = value annotations[buildAnnotationAllowNameChange] = allowed
r.SetAnnotations(annotations) r.SetAnnotations(annotations)
} }
func (r *Resource) NameChangeAllowed() bool { func (r *Resource) NameChangeAllowed() bool {
annotations := r.GetAnnotations() annotations := r.GetAnnotations()
if allowed, set := annotations[buildAnnotationAllowNameChange]; set && allowed == "true" { v, ok := annotations[buildAnnotationAllowNameChange]
return true return ok && v == allowed
}
return false
} }
func (r *Resource) SetAllowKindChange(value string) { func (r *Resource) AllowKindChange() {
annotations := r.GetAnnotations() annotations := r.GetAnnotations()
annotations[buildAnnotationAllowKindChange] = value annotations[buildAnnotationAllowKindChange] = allowed
r.SetAnnotations(annotations) r.SetAnnotations(annotations)
} }
func (r *Resource) KindChangeAllowed() bool { func (r *Resource) KindChangeAllowed() bool {
annotations := r.GetAnnotations() annotations := r.GetAnnotations()
if allowed, set := annotations[buildAnnotationAllowKindChange]; set && allowed == "true" { v, ok := annotations[buildAnnotationAllowKindChange]
return true return ok && v == allowed
}
return false
} }
// String returns resource as JSON. // String returns resource as JSON.

View File

@@ -66,10 +66,10 @@ func (p *plugin) Config(
if errSM == nil { if errSM == nil {
p.loadedPatch = patchSM p.loadedPatch = patchSM
if p.Options["allowNameChange"] { if p.Options["allowNameChange"] {
p.loadedPatch.SetAllowNameChange("true") p.loadedPatch.AllowNameChange()
} }
if p.Options["allowKindChange"] { if p.Options["allowKindChange"] {
p.loadedPatch.SetAllowKindChange("true") p.loadedPatch.AllowKindChange()
} }
} else { } else {
p.decodedPatch = patchJson p.decodedPatch = patchJson