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

@@ -304,7 +304,7 @@ func (f Filter) sameCurrentNamespaceAsReferrer() sieveFunc {
// selectReferral picks the best referral from a list of candidates. // selectReferral picks the best referral from a list of candidates.
func (f Filter) selectReferral( func (f Filter) selectReferral(
// The name referral that may need to be updated. // The name referral that may need to be updated.
oldName string, oldName string,
candidates []*resource.Resource) (*resource.Resource, error) { candidates []*resource.Resource) (*resource.Resource, error) {
candidates = doSieve(candidates, originalNameMatches(oldName)) candidates = doSieve(candidates, originalNameMatches(oldName))

View File

@@ -40,7 +40,7 @@ kind: NotSecret
metadata: metadata:
name: newName2 name: newName2
`, `,
originalNames: []string{"oldName", ""}, originalNames: []string{"oldName", "newName2"},
referrerFinal: ` referrerFinal: `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
@@ -79,7 +79,7 @@ kind: NotSecret
metadata: metadata:
name: newName2 name: newName2
`, `,
originalNames: []string{"oldName1", ""}, originalNames: []string{"oldName1", "newName2"},
referrerFinal: ` referrerFinal: `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
@@ -118,7 +118,7 @@ kind: NotSecret
metadata: metadata:
name: newName2 name: newName2
`, `,
originalNames: []string{"oldName", ""}, originalNames: []string{"oldName", "newName2"},
referrerFinal: ` referrerFinal: `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
@@ -204,7 +204,7 @@ kind: NotSecret
metadata: metadata:
name: newName2 name: newName2
`, `,
originalNames: []string{"oldName", ""}, originalNames: []string{"oldName", "newName2"},
referrerFinal: ` referrerFinal: `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment

View File

@@ -352,9 +352,9 @@ func TestResolveVarsWithNoambiguation(t *testing.T) {
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "sub-backendOne", "name": "sub-backendOne",
"annotations": map[string]interface{}{ "annotations": map[string]interface{}{
"config.kubernetes.io/originalName": "backendOne", "config.kubernetes.io/previousNames": "backendOne",
"config.kubernetes.io/originalNs": "default", "config.kubernetes.io/previousNamespaces": "default",
"config.kubernetes.io/prefixes": "sub-", "config.kubernetes.io/prefixes": "sub-",
}, },
}}).ResMap() }}).ResMap()

View File

@@ -168,10 +168,8 @@ func (m *resWrangler) GetMatchingResourcesByAnyId(
matches IdMatcher) []*resource.Resource { matches IdMatcher) []*resource.Resource {
var result []*resource.Resource var result []*resource.Resource
for _, r := range m.rList { for _, r := range m.rList {
prevIds := r.PrevIds() for _, id := range append(r.PrevIds(), r.CurId()) {
prevIds = append(prevIds, r.CurId()) if matches(id) {
for _, prevId := range prevIds {
if matches(prevId) {
result = append(result, r) result = append(result, r)
break break
} }

View File

@@ -331,7 +331,7 @@ func TestGetMatchingResourcesByCurrentId(t *testing.T) {
} }
} }
func TestGetMatchingResourcesByPreviousId(t *testing.T) { func TestGetMatchingResourcesByAnyId(t *testing.T) {
r1 := rf.FromMap( r1 := rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
@@ -339,8 +339,8 @@ func TestGetMatchingResourcesByPreviousId(t *testing.T) {
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "new-alice", "name": "new-alice",
"annotations": map[string]interface{}{ "annotations": map[string]interface{}{
"config.kubernetes.io/originalName": "alice", "config.kubernetes.io/previousNames": "alice",
"config.kubernetes.io/originalNs": "default", "config.kubernetes.io/previousNamespaces": "default",
}, },
}, },
}) })
@@ -351,8 +351,8 @@ func TestGetMatchingResourcesByPreviousId(t *testing.T) {
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "new-bob", "name": "new-bob",
"annotations": map[string]interface{}{ "annotations": map[string]interface{}{
"config.kubernetes.io/originalName": "bob,bob2", "config.kubernetes.io/previousNames": "bob,bob2",
"config.kubernetes.io/originalNs": "default,default", "config.kubernetes.io/previousNamespaces": "default,default",
}, },
}, },
}) })
@@ -364,8 +364,8 @@ func TestGetMatchingResourcesByPreviousId(t *testing.T) {
"name": "new-bob", "name": "new-bob",
"namespace": "new-happy", "namespace": "new-happy",
"annotations": map[string]interface{}{ "annotations": map[string]interface{}{
"config.kubernetes.io/originalName": "bob", "config.kubernetes.io/previousNames": "bob",
"config.kubernetes.io/originalNs": "happy", "config.kubernetes.io/previousNamespaces": "happy",
}, },
}, },
}) })
@@ -377,8 +377,8 @@ func TestGetMatchingResourcesByPreviousId(t *testing.T) {
"name": "charlie", "name": "charlie",
"namespace": "happy", "namespace": "happy",
"annotations": map[string]interface{}{ "annotations": map[string]interface{}{
"config.kubernetes.io/originalName": "charlie", "config.kubernetes.io/previousNames": "charlie",
"config.kubernetes.io/originalNs": "default", "config.kubernetes.io/previousNamespaces": "default",
}, },
}, },
}) })

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") return nil, fmt.Errorf("number of names doesn't match number of resources")
} }
for i, res := range result { for i, res := range result {
res.setPreviousNamespaceAndName("", names[i]) res.setPreviousNamespaceAndName(resid.DefaultNamespace, names[i])
} }
return result, nil return result, nil
} }

View File

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

View File

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

View File

@@ -63,9 +63,9 @@ apiVersion: v1
kind: Service kind: Service
metadata: metadata:
annotations: annotations:
config.kubernetes.io/originalName: apple
config.kubernetes.io/originalNs: default
config.kubernetes.io/prefixes: baked- config.kubernetes.io/prefixes: baked-
config.kubernetes.io/previousNames: apple
config.kubernetes.io/previousNamespaces: default
config.kubernetes.io/suffixes: -pie config.kubernetes.io/suffixes: -pie
name: baked-apple-pie name: baked-apple-pie
spec: spec:
@@ -86,9 +86,9 @@ apiVersion: v1
kind: ConfigMap kind: ConfigMap
metadata: metadata:
annotations: annotations:
config.kubernetes.io/originalName: cm
config.kubernetes.io/originalNs: default
config.kubernetes.io/prefixes: baked- config.kubernetes.io/prefixes: baked-
config.kubernetes.io/previousNames: cm
config.kubernetes.io/previousNamespaces: default
config.kubernetes.io/suffixes: -pie config.kubernetes.io/suffixes: -pie
name: baked-cm-pie name: baked-cm-pie
`) `)
@@ -137,9 +137,9 @@ apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
annotations: annotations:
config.kubernetes.io/originalName: deployment
config.kubernetes.io/originalNs: default
config.kubernetes.io/prefixes: test- config.kubernetes.io/prefixes: test-
config.kubernetes.io/previousNames: deployment
config.kubernetes.io/previousNamespaces: default
name: test-deployment name: test-deployment
spec: spec:
template: template: