Skip to content

Commit

Permalink
Merge pull request #4002 from natasha41575/resref
Browse files Browse the repository at this point in the history
replace Resource.refBy with annotations
  • Loading branch information
k8s-ci-robot authored Jul 9, 2021
2 parents e1804cb + a84badb commit 259fcfc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
2 changes: 2 additions & 0 deletions api/internal/accumulator/namereferencetransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,9 @@ func TestNameReferenceClusterWide(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

expected.RemoveBuildAnnotations()
m.RemoveBuildAnnotations()

if err = expected.ErrorIfNotEqualLists(m); err != nil {
t.Fatalf(notEqualErrFmt, err)
}
Expand Down
1 change: 1 addition & 0 deletions api/internal/utils/makeResIds.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
BuildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
BuildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
BuildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
BuildAnnotationsRefBy = konfig.ConfigAnnoDomain + "/refBy"

// the following are only for patches, to specify whether they can change names
// and kinds of their targets
Expand Down
28 changes: 12 additions & 16 deletions api/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
type Resource struct {
kyaml.RNode
options *types.GenArgs
refBy []resid.ResId
refVarNames []string
}

Expand All @@ -35,6 +34,7 @@ var BuildAnnotations = []string{
utils.BuildAnnotationPreviousNamespaces,
utils.BuildAnnotationAllowNameChange,
utils.BuildAnnotationAllowKindChange,
utils.BuildAnnotationsRefBy,
}

func (r *Resource) ResetRNode(incoming *Resource) {
Expand Down Expand Up @@ -102,7 +102,6 @@ func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) error {

func (r *Resource) copyKustomizeSpecificFields(other *Resource) {
r.options = other.options
r.refBy = other.copyRefBy()
r.refVarNames = copyStringSlice(other.refVarNames)
}

Expand Down Expand Up @@ -144,10 +143,10 @@ func (r *Resource) ErrIfNotEquals(o *Resource) error {
func (r *Resource) ReferencesEqual(other *Resource) bool {
setSelf := make(map[resid.ResId]bool)
setOther := make(map[resid.ResId]bool)
for _, ref := range other.refBy {
for _, ref := range other.GetRefBy() {
setOther[ref] = true
}
for _, ref := range r.refBy {
for _, ref := range r.GetRefBy() {
if _, ok := setOther[ref]; !ok {
return false
}
Expand All @@ -156,15 +155,6 @@ func (r *Resource) ReferencesEqual(other *Resource) bool {
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 {
if s == nil {
return nil
Expand Down Expand Up @@ -363,12 +353,18 @@ func (r *Resource) CurId() resid.ResId {

// GetRefBy returns the ResIds that referred to current resource
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
func (r *Resource) AppendRefBy(id resid.ResId) {
r.refBy = append(r.refBy, id)
// Using any type except fmt.Stringer here results in a compilation error
func (r *Resource) AppendRefBy(id fmt.Stringer) {
r.appendCsvAnnotation(utils.BuildAnnotationsRefBy, id.String())
}

// GetRefVarNames returns vars that refer to current resource
Expand Down
38 changes: 38 additions & 0 deletions api/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,3 +1133,41 @@ spec:
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"),
})
}

0 comments on commit 259fcfc

Please sign in to comment.