mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 09:24:23 +00:00
Add some resId tests to support refactor.
This commit is contained in:
@@ -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 <Group: "", Version: "", Kind: "Deployment"> CAN 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
|
||||
// <Group: "", Version: "", Kind: "Deployment">
|
||||
// selects
|
||||
// <Group: "extensions", Version: "v1beta1", Kind: "Deployment">.
|
||||
// selector <Group: "apps", Version: "", Kind: "Deployment"> CANNOT select
|
||||
//
|
||||
// while selector
|
||||
// <Group: "apps", Version: "", Kind: "Deployment">
|
||||
// rejects
|
||||
// <Group: "extensions", Version: "v1beta1", Kind: "Deployment">.
|
||||
//
|
||||
func (x Gvk) IsSelected(selector *Gvk) bool {
|
||||
if selector == nil {
|
||||
return true
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
for id, r := range actual {
|
||||
if r.GetName() != id.NameWithPrefixSuffix() {
|
||||
t.Errorf("unexpected hash was added to %s: %s", id.NameWithPrefixSuffix(), r.GetName())
|
||||
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())
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user