mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +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 (
|
const (
|
||||||
noGroup = "~G"
|
noGroup = "~G"
|
||||||
noVersion = "~V"
|
noVersion = "~V"
|
||||||
@@ -113,11 +114,19 @@ func (x Gvk) IsLessThan(o Gvk) bool {
|
|||||||
|
|
||||||
// IsSelected returns true if `selector` selects `x`; otherwise, false.
|
// IsSelected returns true if `selector` selects `x`; otherwise, false.
|
||||||
// If `selector` and `x` are the same, return true.
|
// If `selector` and `x` are the same, return true.
|
||||||
// If `selector` is nil, it is considered as a wildcard and always return true.
|
// If `selector` is nil, it is considered a wildcard match, returning true.
|
||||||
// e.g. selector <Group: "", Version: "", Kind: "Deployment"> CAN select
|
// If selector fields are empty, they are considered wildcards matching
|
||||||
// <Group: "extensions", Version: "v1beta1", Kind: "Deployment">.
|
// anything in the corresponding fields, .g.
|
||||||
// selector <Group: "apps", Version: "", Kind: "Deployment"> CANNOT select
|
// selector
|
||||||
// <Group: "extensions", Version: "v1beta1", Kind: "Deployment">.
|
// <Group: "", Version: "", Kind: "Deployment">
|
||||||
|
// selects
|
||||||
|
// <Group: "extensions", Version: "v1beta1", Kind: "Deployment">.
|
||||||
|
//
|
||||||
|
// while selector
|
||||||
|
// <Group: "apps", Version: "", Kind: "Deployment">
|
||||||
|
// rejects
|
||||||
|
// <Group: "extensions", Version: "v1beta1", Kind: "Deployment">.
|
||||||
|
//
|
||||||
func (x Gvk) IsSelected(selector *Gvk) bool {
|
func (x Gvk) IsSelected(selector *Gvk) bool {
|
||||||
if selector == nil {
|
if selector == nil {
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package resid
|
package resid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/pkg/gvk"
|
"sigs.k8s.io/kustomize/pkg/gvk"
|
||||||
@@ -125,44 +124,29 @@ func (n ResId) Name() string {
|
|||||||
return n.name
|
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.
|
// Namespace returns resource namespace.
|
||||||
func (n ResId) Namespace() string {
|
func (n ResId) Namespace() string {
|
||||||
return n.namespace
|
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 {
|
func (n ResId) CopyWithNewPrefixSuffix(p, s string) ResId {
|
||||||
prefix := n.prefix
|
result := n
|
||||||
if p != "" {
|
if p != "" {
|
||||||
prefix = n.concatPrefix(p)
|
result.prefix = n.concatPrefix(p)
|
||||||
}
|
}
|
||||||
suffix := n.suffix
|
|
||||||
if s != "" {
|
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
|
// CopyWithNewNamespace make a new copy from current ResId and set a new namespace
|
||||||
func (n ResId) CopyWithNewNamespace(ns string) ResId {
|
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
|
// 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{}
|
result := ResMap{}
|
||||||
for id, res := range m {
|
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
|
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) {
|
func TestDisableNameSuffixHash(t *testing.T) {
|
||||||
kt := makeKustTarget(t, makeLoader1(t))
|
kt := makeKustTarget(t, makeLoader1(t))
|
||||||
kt.kustomization.GeneratorOptions = &types.GeneratorOptions{DisableNameSuffixHash: true}
|
|
||||||
actual, err := kt.MakeCustomizedResMap()
|
m, err := kt.MakeCustomizedResMap()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected Resources error %v", err)
|
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 {
|
kt.kustomization.GeneratorOptions = &types.GeneratorOptions{
|
||||||
if r.GetName() != id.NameWithPrefixSuffix() {
|
DisableNameSuffixHash: true}
|
||||||
t.Errorf("unexpected hash was added to %s: %s", id.NameWithPrefixSuffix(), r.GetName())
|
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{
|
var deprecateNamePrefixSuffixFieldSpec = config.FieldSpec{
|
||||||
Gvk: gvk.Gvk{Kind: "Namespace"},
|
Gvk: gvk.Gvk{Kind: "Namespace"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNamePrefixSuffixTransformer construct a namePrefixSuffixTransformer.
|
// NewNamePrefixSuffixTransformer makes a namePrefixSuffixTransformer.
|
||||||
func NewNamePrefixSuffixTransformer(np, ns string, pc []config.FieldSpec) (Transformer, error) {
|
func NewNamePrefixSuffixTransformer(
|
||||||
|
np, ns string, fieldSpecs []config.FieldSpec) (Transformer, error) {
|
||||||
if len(np) == 0 && len(ns) == 0 {
|
if len(np) == 0 && len(ns) == 0 {
|
||||||
return NewNoOpTransformer(), nil
|
return NewNoOpTransformer(), nil
|
||||||
}
|
}
|
||||||
if pc == nil {
|
if fieldSpecs == nil {
|
||||||
return nil, errors.New("fieldSpecs is not expected to be 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.
|
// 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 {
|
for id := range mf {
|
||||||
if id.Gvk().IsSelected(&deprecateNamePrefixSuffixFieldSpec.Gvk) {
|
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()
|
objMap := mf[id].Map()
|
||||||
for _, path := range o.fieldSpecsToUse {
|
for _, path := range o.fieldSpecsToUse {
|
||||||
if !id.Gvk().IsSelected(&path.Gvk) {
|
if !id.Gvk().IsSelected(&path.Gvk) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addPrefixSuffix)
|
err := mutateField(
|
||||||
|
objMap,
|
||||||
|
path.PathSlice(),
|
||||||
|
path.CreateIfNotPresent,
|
||||||
|
o.addPrefixSuffix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -99,7 +111,8 @@ func (o *namePrefixSuffixTransformer) Transform(m resmap.ResMap) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *namePrefixSuffixTransformer) addPrefixSuffix(in interface{}) (interface{}, error) {
|
func (o *namePrefixSuffixTransformer) addPrefixSuffix(
|
||||||
|
in interface{}) (interface{}, error) {
|
||||||
s, ok := in.(string)
|
s, ok := in.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
|
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
|
||||||
|
|||||||
Reference in New Issue
Block a user