replace Resource.refBy with annotations

This commit is contained in:
Natasha Sarkar
2021-06-16 17:46:40 -07:00
parent 60038d44f9
commit a84badb834
4 changed files with 53 additions and 16 deletions

View File

@@ -897,7 +897,9 @@ func TestNameReferenceClusterWide(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
expected.RemoveBuildAnnotations()
m.RemoveBuildAnnotations() m.RemoveBuildAnnotations()
if err = expected.ErrorIfNotEqualLists(m); err != nil { if err = expected.ErrorIfNotEqualLists(m); err != nil {
t.Fatalf(notEqualErrFmt, err) t.Fatalf(notEqualErrFmt, err)
} }

View File

@@ -15,6 +15,7 @@ const (
BuildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes" BuildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
BuildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes" BuildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
BuildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces" BuildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
BuildAnnotationsRefBy = konfig.ConfigAnnoDomain + "/refBy"
// the following are only for patches, to specify whether they can change names // the following are only for patches, to specify whether they can change names
// and kinds of their targets // and kinds of their targets

View File

@@ -23,7 +23,6 @@ import (
type Resource struct { type Resource struct {
kyaml.RNode kyaml.RNode
options *types.GenArgs options *types.GenArgs
refBy []resid.ResId
refVarNames []string refVarNames []string
} }
@@ -35,6 +34,7 @@ var BuildAnnotations = []string{
utils.BuildAnnotationPreviousNamespaces, utils.BuildAnnotationPreviousNamespaces,
utils.BuildAnnotationAllowNameChange, utils.BuildAnnotationAllowNameChange,
utils.BuildAnnotationAllowKindChange, utils.BuildAnnotationAllowKindChange,
utils.BuildAnnotationsRefBy,
} }
func (r *Resource) ResetRNode(incoming *Resource) { func (r *Resource) ResetRNode(incoming *Resource) {
@@ -102,7 +102,6 @@ func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) error {
func (r *Resource) copyKustomizeSpecificFields(other *Resource) { func (r *Resource) copyKustomizeSpecificFields(other *Resource) {
r.options = other.options r.options = other.options
r.refBy = other.copyRefBy()
r.refVarNames = copyStringSlice(other.refVarNames) r.refVarNames = copyStringSlice(other.refVarNames)
} }
@@ -144,10 +143,10 @@ func (r *Resource) ErrIfNotEquals(o *Resource) error {
func (r *Resource) ReferencesEqual(other *Resource) bool { func (r *Resource) ReferencesEqual(other *Resource) bool {
setSelf := make(map[resid.ResId]bool) setSelf := make(map[resid.ResId]bool)
setOther := make(map[resid.ResId]bool) setOther := make(map[resid.ResId]bool)
for _, ref := range other.refBy { for _, ref := range other.GetRefBy() {
setOther[ref] = true setOther[ref] = true
} }
for _, ref := range r.refBy { for _, ref := range r.GetRefBy() {
if _, ok := setOther[ref]; !ok { if _, ok := setOther[ref]; !ok {
return false return false
} }
@@ -156,15 +155,6 @@ func (r *Resource) ReferencesEqual(other *Resource) bool {
return len(setSelf) == len(setOther) return len(setSelf) == len(setOther)
} }
func (r *Resource) copyRefBy() []resid.ResId {
if r.refBy == nil {
return nil
}
s := make([]resid.ResId, len(r.refBy))
copy(s, r.refBy)
return s
}
func copyStringSlice(s []string) []string { func copyStringSlice(s []string) []string {
if s == nil { if s == nil {
return nil return nil
@@ -363,12 +353,18 @@ func (r *Resource) CurId() resid.ResId {
// GetRefBy returns the ResIds that referred to current resource // GetRefBy returns the ResIds that referred to current resource
func (r *Resource) GetRefBy() []resid.ResId { func (r *Resource) GetRefBy() []resid.ResId {
return r.refBy var resIds []resid.ResId
asStrings := r.getCsvAnnotation(utils.BuildAnnotationsRefBy)
for _, s := range asStrings {
resIds = append(resIds, resid.FromString(s))
}
return resIds
} }
// AppendRefBy appends a ResId into the refBy list // AppendRefBy appends a ResId into the refBy list
func (r *Resource) AppendRefBy(id resid.ResId) { // Using any type except fmt.Stringer here results in a compilation error
r.refBy = append(r.refBy, id) func (r *Resource) AppendRefBy(id fmt.Stringer) {
r.appendCsvAnnotation(utils.BuildAnnotationsRefBy, id.String())
} }
// GetRefVarNames returns vars that refer to current resource // GetRefVarNames returns vars that refer to current resource

View File

@@ -1133,3 +1133,41 @@ spec:
t.Fatalf("expected '%s', got '%s'", expected, actual) t.Fatalf("expected '%s', got '%s'", expected, actual)
} }
} }
func TestRefBy(t *testing.T) {
r, err := factory.FromBytes([]byte(`
apiVersion: v1
kind: Deployment
metadata:
name: clown
spec:
numReplicas: 1
`))
assert.NoError(t, err)
r.AppendRefBy(resid.FromString("gr1_ver1_knd1|ns1|name1"))
assert.Equal(t, r.RNode.MustString(), `apiVersion: v1
kind: Deployment
metadata:
name: clown
annotations:
config.kubernetes.io/refBy: gr1_ver1_knd1|ns1|name1
spec:
numReplicas: 1
`)
assert.Equal(t, r.GetRefBy(), []resid.ResId{resid.FromString("gr1_ver1_knd1|ns1|name1")})
r.AppendRefBy(resid.FromString("gr2_ver2_knd2|ns2|name2"))
assert.Equal(t, r.RNode.MustString(), `apiVersion: v1
kind: Deployment
metadata:
name: clown
annotations:
config.kubernetes.io/refBy: gr1_ver1_knd1|ns1|name1,gr2_ver2_knd2|ns2|name2
spec:
numReplicas: 1
`)
assert.Equal(t, r.GetRefBy(), []resid.ResId{
resid.FromString("gr1_ver1_knd1|ns1|name1"),
resid.FromString("gr2_ver2_knd2|ns2|name2"),
})
}