cleaned up resource refactoring

This commit is contained in:
Natasha Sarkar
2021-02-03 14:30:25 -08:00
parent cd918483f9
commit 43157f5d35
9 changed files with 62 additions and 62 deletions

View File

@@ -153,7 +153,7 @@ func (rf *Factory) SliceFromBytesWithNames(names []string, in []byte) ([]*Resour
return nil, fmt.Errorf("number of names doesn't match number of resources")
}
for i, res := range result {
res.setPreviousNamespaceAndName("", names[i])
res.setPreviousNamespaceAndName(resid.DefaultNamespace, names[i])
}
return result, nil
}

View File

@@ -4,6 +4,7 @@
package resource
import (
"errors"
"fmt"
"log"
"reflect"
@@ -32,17 +33,17 @@ type Resource struct {
}
const (
buildAnnotationPreviousName = konfig.ConfigAnnoDomain + "/originalName"
buildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
buildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
buildAnnotationPreviousNamespace = konfig.ConfigAnnoDomain + "/originalNs"
buildAnnotationPreviousNames = konfig.ConfigAnnoDomain + "/previousNames"
buildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
buildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
buildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
)
var buildAnnotations = []string{
buildAnnotationPreviousName,
buildAnnotationPreviousNames,
buildAnnotationPrefixes,
buildAnnotationSuffixes,
buildAnnotationPreviousNamespace,
buildAnnotationPreviousNamespaces,
}
func (r *Resource) ResetPrimaryData(incoming *Resource) {
@@ -338,13 +339,8 @@ func (r *Resource) RemoveBuildAnnotations() {
}
func (r *Resource) setPreviousNamespaceAndName(ns string, n string) *Resource {
// name
r.appendCsvAnnotation(buildAnnotationPreviousName, n)
// namespace
if ns == "" {
ns = resid.DefaultNamespace
}
r.appendCsvAnnotation(buildAnnotationPreviousNamespace, ns)
r.appendCsvAnnotation(buildAnnotationPreviousNames, n)
r.appendCsvAnnotation(buildAnnotationPreviousNamespaces, ns)
return r
}
@@ -412,15 +408,20 @@ func (r *Resource) OrgId() resid.ResId {
// PrevIds returns a list of ResIds that includes every
// previous ResId the resource has had through all of its
// GVKN transformations, in the order that it had that ID.
// I.e. the oldest ID is first.
// The returned array does not include the resource's current
// ID.
// If there are no previous IDs, this will return nil.
// ID. If there are no previous IDs, this will return nil.
func (r *Resource) PrevIds() []resid.ResId {
var ids []resid.ResId
// names and ns should always be the same length
names := r.getCsvAnnotation(buildAnnotationPreviousName)
ns := r.getCsvAnnotation(buildAnnotationPreviousNamespace)
// TODO: merge previous names and namespaces into one list of
// pairs on one annotation so there is no chance of error
names := r.getCsvAnnotation(buildAnnotationPreviousNames)
ns := r.getCsvAnnotation(buildAnnotationPreviousNamespaces)
if len(names) != len(ns) {
panic(errors.New(
"number of previous names not equal to " +
"number of previous namespaces"))
}
for i := range names {
ids = append(ids, resid.NewResIdWithNamespace(
r.GetGvk(), names[i], ns[i]))
@@ -430,7 +431,8 @@ func (r *Resource) PrevIds() []resid.ResId {
// StorePreviousId stores the resource's current ID via build annotations.
func (r *Resource) StorePreviousId() {
r.setPreviousNamespaceAndName(r.GetNamespace(), r.GetName())
id := r.CurId()
r.setPreviousNamespaceAndName(id.EffectiveNamespace(), id.Name)
}
// CurId returns a ResId for the resource using the

View File

@@ -714,8 +714,8 @@ metadata:
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
config.kubernetes.io/originalNs: default
config.kubernetes.io/previousNames: oldName
config.kubernetes.io/previousNamespaces: default
name: newName
`,
},
@@ -725,8 +725,8 @@ metadata:
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
config.kubernetes.io/originalNs: default
config.kubernetes.io/previousNames: oldName
config.kubernetes.io/previousNamespaces: default
name: oldName2
`,
newName: "newName",
@@ -735,8 +735,8 @@ metadata:
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName,oldName2
config.kubernetes.io/originalNs: default,default
config.kubernetes.io/previousNames: oldName,oldName2
config.kubernetes.io/previousNamespaces: default,default
name: newName
`,
},
@@ -746,8 +746,8 @@ metadata:
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
config.kubernetes.io/originalNs: default
config.kubernetes.io/previousNames: oldName
config.kubernetes.io/previousNamespaces: default
name: oldName2
namespace: oldNamespace
`,
@@ -757,8 +757,8 @@ metadata:
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName,oldName2
config.kubernetes.io/originalNs: default,oldNamespace
config.kubernetes.io/previousNames: oldName,oldName2
config.kubernetes.io/previousNamespaces: default,oldNamespace
name: newName
namespace: newNamespace
`,
@@ -806,8 +806,8 @@ metadata:
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName
config.kubernetes.io/originalNs: default
config.kubernetes.io/previousNames: oldName
config.kubernetes.io/previousNamespaces: default
name: newName
`,
expected: []resid.ResId{
@@ -824,8 +824,8 @@ metadata:
kind: Secret
metadata:
annotations:
config.kubernetes.io/originalName: oldName,oldName2
config.kubernetes.io/originalNs: default,oldNamespace
config.kubernetes.io/previousNames: oldName,oldName2
config.kubernetes.io/previousNamespaces: default,oldNamespace
name: newName
namespace: newNamespace
`,