From e9b19281b27fd1ed3c7f81404543b2c49d3e1d47 Mon Sep 17 00:00:00 2001 From: Jeffrey Regan Date: Tue, 11 Dec 2018 09:10:49 -0800 Subject: [PATCH] Add some resId tests to support refactor. --- pkg/gvk/gvk.go | 19 ++++++++++---- pkg/resid/resid.go | 34 +++++++------------------ pkg/resid/resid_test.go | 38 ++++++++++++++++++++++++++++ pkg/resmap/resmap.go | 4 ++- pkg/target/kusttarget_test.go | 36 +++++++++++++++++++++----- pkg/transformers/prefixsuffixname.go | 29 +++++++++++++++------ 6 files changed, 115 insertions(+), 45 deletions(-) diff --git a/pkg/gvk/gvk.go b/pkg/gvk/gvk.go index 382f284d1..0bc515a35 100644 --- a/pkg/gvk/gvk.go +++ b/pkg/gvk/gvk.go @@ -35,6 +35,7 @@ func FromKind(k string) Gvk { } } +// Values that are brief but meaningful in logs. const ( noGroup = "~G" noVersion = "~V" @@ -113,11 +114,19 @@ func (x Gvk) IsLessThan(o Gvk) bool { // IsSelected returns true if `selector` selects `x`; otherwise, false. // If `selector` and `x` are the same, return true. -// If `selector` is nil, it is considered as a wildcard and always return true. -// e.g. selector CAN select -// . -// selector CANNOT select -// . +// If `selector` is nil, it is considered a wildcard match, returning true. +// If selector fields are empty, they are considered wildcards matching +// anything in the corresponding fields, .g. +// selector +// +// selects +// . +// +// while selector +// +// rejects +// . +// func (x Gvk) IsSelected(selector *Gvk) bool { if selector == nil { return true diff --git a/pkg/resid/resid.go b/pkg/resid/resid.go index 390fc2514..2af7c02a5 100644 --- a/pkg/resid/resid.go +++ b/pkg/resid/resid.go @@ -17,7 +17,6 @@ limitations under the License. package resid import ( - "fmt" "strings" "sigs.k8s.io/kustomize/pkg/gvk" @@ -125,44 +124,29 @@ func (n ResId) Name() string { return n.name } -// NameWithPrefixSuffix returns resource name with prefix and suffix. -func (n ResId) NameWithPrefixSuffix() string { - prefix := strings.Join(n.prefixList(), "") - suffix := strings.Join(n.suffixList(), "") - return fmt.Sprintf("%s%s%s", prefix, n.name, suffix) -} - -// Prefix returns name prefix. -func (n ResId) Prefix() string { - return n.prefix -} - -// Suffix returns name suffix. -func (n ResId) Suffix() string { - return n.suffix -} - // Namespace returns resource namespace. func (n ResId) Namespace() string { return n.namespace } -// CopyWithNewPrefixSuffix make a new copy from current ResId and append a new prefix and suffix +// CopyWithNewPrefixSuffix make a new copy from current ResId +// and append a new prefix and suffix func (n ResId) CopyWithNewPrefixSuffix(p, s string) ResId { - prefix := n.prefix + result := n if p != "" { - prefix = n.concatPrefix(p) + result.prefix = n.concatPrefix(p) } - suffix := n.suffix if s != "" { - suffix = n.concatSuffix(s) + result.suffix = n.concatSuffix(s) } - return ResId{gvKind: n.gvKind, name: n.name, prefix: prefix, suffix: suffix, namespace: n.namespace} + return result } // CopyWithNewNamespace make a new copy from current ResId and set a new namespace func (n ResId) CopyWithNewNamespace(ns string) ResId { - return ResId{gvKind: n.gvKind, name: n.name, prefix: n.prefix, suffix: n.suffix, namespace: ns} + result := n + result.namespace = ns + return result } // HasSameLeftmostPrefix check if two ResIds have the same diff --git a/pkg/resid/resid_test.go b/pkg/resid/resid_test.go index 165860b6c..85bafd447 100644 --- a/pkg/resid/resid_test.go +++ b/pkg/resid/resid_test.go @@ -111,3 +111,41 @@ func TestEquals(t *testing.T) { } } } + +func TestCopyWithNewPrefixSuffix(t *testing.T) { + r1 := ResId{ + gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"}, + name: "nm", + prefix: "a", + suffix: "b", + namespace: "X"} + r2 := r1.CopyWithNewPrefixSuffix("p-", "-s") + expected := ResId{ + gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"}, + name: "nm", + prefix: "p-a", + suffix: "b-s", + namespace: "X"} + if !r2.GvknEquals(expected) { + t.Fatalf("%v should equal %v", r2, expected) + } +} + +func TestCopyWithNewNamespace(t *testing.T) { + r1 := ResId{ + gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"}, + name: "nm", + prefix: "a", + suffix: "b", + namespace: "X"} + r2 := r1.CopyWithNewNamespace("zzz") + expected := ResId{ + gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"}, + name: "nm", + prefix: "a", + suffix: "b", + namespace: "zzz"} + if !r2.GvknEquals(expected) { + t.Fatalf("%v should equal %v", r2, expected) + } +} diff --git a/pkg/resmap/resmap.go b/pkg/resmap/resmap.go index 68c409063..696c5ec2c 100644 --- a/pkg/resmap/resmap.go +++ b/pkg/resmap/resmap.go @@ -131,7 +131,9 @@ func (m ResMap) FilterBy(inputId resid.ResId) ResMap { } result := ResMap{} for id, res := range m { - if id.Namespace() == inputId.Namespace() && id.HasSameLeftmostPrefix(inputId) && id.HasSameRightmostSuffix(inputId) { + if id.Namespace() == inputId.Namespace() && + id.HasSameLeftmostPrefix(inputId) && + id.HasSameRightmostSuffix(inputId) { result[id] = res } } diff --git a/pkg/target/kusttarget_test.go b/pkg/target/kusttarget_test.go index 9b7e5d39b..8a916a1fe 100644 --- a/pkg/target/kusttarget_test.go +++ b/pkg/target/kusttarget_test.go @@ -260,17 +260,41 @@ func TestSecretTimeout(t *testing.T) { } } +func findSecret(m resmap.ResMap) *resource.Resource { + for id, res := range m { + if id.Gvk().Kind == "Secret" { + return res + } + } + return nil +} + func TestDisableNameSuffixHash(t *testing.T) { kt := makeKustTarget(t, makeLoader1(t)) - kt.kustomization.GeneratorOptions = &types.GeneratorOptions{DisableNameSuffixHash: true} - actual, err := kt.MakeCustomizedResMap() + + m, err := kt.MakeCustomizedResMap() if err != nil { t.Fatalf("unexpected Resources error %v", err) } + secret := findSecret(m) + if secret == nil { + t.Errorf("Expected to find a Secret") + } + if secret.GetName() != "foo-secret-bar-9btc7bt4kb" { + t.Errorf("unexpected secret resource name: %s", secret.GetName()) + } - for id, r := range actual { - if r.GetName() != id.NameWithPrefixSuffix() { - t.Errorf("unexpected hash was added to %s: %s", id.NameWithPrefixSuffix(), r.GetName()) - } + kt.kustomization.GeneratorOptions = &types.GeneratorOptions{ + DisableNameSuffixHash: true} + m, err = kt.MakeCustomizedResMap() + if err != nil { + t.Fatalf("unexpected Resources error %v", err) + } + secret = findSecret(m) + if secret == nil { + t.Errorf("Expected to find a Secret") + } + if secret.GetName() != "foo-secret-bar" { // No hash at end. + t.Errorf("unexpected secret resource name: %s", secret.GetName()) } } diff --git a/pkg/transformers/prefixsuffixname.go b/pkg/transformers/prefixsuffixname.go index 4dc97aba6..ec3304178 100644 --- a/pkg/transformers/prefixsuffixname.go +++ b/pkg/transformers/prefixsuffixname.go @@ -43,20 +43,26 @@ var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{ }, } -// deprecateNamePrefixSuffixFieldSpec will be moved into prefixSuffixFieldSpecsToSkip in next release +// deprecateNamePrefixSuffixFieldSpec will be moved into +// prefixSuffixFieldSpecsToSkip in next release var deprecateNamePrefixSuffixFieldSpec = config.FieldSpec{ Gvk: gvk.Gvk{Kind: "Namespace"}, } -// NewNamePrefixSuffixTransformer construct a namePrefixSuffixTransformer. -func NewNamePrefixSuffixTransformer(np, ns string, pc []config.FieldSpec) (Transformer, error) { +// NewNamePrefixSuffixTransformer makes a namePrefixSuffixTransformer. +func NewNamePrefixSuffixTransformer( + np, ns string, fieldSpecs []config.FieldSpec) (Transformer, error) { if len(np) == 0 && len(ns) == 0 { return NewNoOpTransformer(), nil } - if pc == nil { + if fieldSpecs == nil { return nil, errors.New("fieldSpecs is not expected to be nil") } - return &namePrefixSuffixTransformer{fieldSpecsToUse: pc, prefix: np, suffix: ns, fieldSpecsToSkip: prefixSuffixFieldSpecsToSkip}, nil + return &namePrefixSuffixTransformer{ + prefix: np, + suffix: ns, + fieldSpecsToUse: fieldSpecs, + fieldSpecsToSkip: prefixSuffixFieldSpecsToSkip}, nil } // Transform prepends the name prefix and appends the name suffix. @@ -81,14 +87,20 @@ func (o *namePrefixSuffixTransformer) Transform(m resmap.ResMap) error { for id := range mf { if id.Gvk().IsSelected(&deprecateNamePrefixSuffixFieldSpec.Gvk) { - log.Println("Adding nameprefix and namesuffix to Namespace resource will be deprecated in next release.") + log.Println( + "Adding prefix and suffix to Namespace " + + "resource will be deprecated in next release.") } objMap := mf[id].Map() for _, path := range o.fieldSpecsToUse { if !id.Gvk().IsSelected(&path.Gvk) { continue } - err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addPrefixSuffix) + err := mutateField( + objMap, + path.PathSlice(), + path.CreateIfNotPresent, + o.addPrefixSuffix) if err != nil { return err } @@ -99,7 +111,8 @@ func (o *namePrefixSuffixTransformer) Transform(m resmap.ResMap) error { return nil } -func (o *namePrefixSuffixTransformer) addPrefixSuffix(in interface{}) (interface{}, error) { +func (o *namePrefixSuffixTransformer) addPrefixSuffix( + in interface{}) (interface{}, error) { s, ok := in.(string) if !ok { return nil, fmt.Errorf("%#v is expected to be %T", in, s)