mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-01 18:30:15 +00:00
Merge pull request #2870 from monopole/deleteNewlyUnused
Delete newly orphaned code.
This commit is contained in:
@@ -41,32 +41,63 @@ type Loader interface {
|
|||||||
// Kunstructured allows manipulation of k8s objects
|
// Kunstructured allows manipulation of k8s objects
|
||||||
// that do not have Golang structs.
|
// that do not have Golang structs.
|
||||||
type Kunstructured interface {
|
type Kunstructured interface {
|
||||||
|
// Several uses.
|
||||||
Copy() Kunstructured
|
Copy() Kunstructured
|
||||||
|
|
||||||
|
// Used by Resource.Replace, in turned used by resWrangler.AbsorbAll,
|
||||||
|
// called by runGenerators to merge maps. Hmm.
|
||||||
GetAnnotations() map[string]string
|
GetAnnotations() map[string]string
|
||||||
GetBool(path string) (bool, error)
|
|
||||||
|
// Used by ResAccumulator and ReplacementTransformer.
|
||||||
GetFieldValue(string) (interface{}, error)
|
GetFieldValue(string) (interface{}, error)
|
||||||
GetFloat64(path string) (float64, error)
|
|
||||||
|
// Used by Resource.OrgId
|
||||||
GetGvk() resid.Gvk
|
GetGvk() resid.Gvk
|
||||||
GetInt64(path string) (int64, error)
|
|
||||||
|
// Used by resource.Factory.SliceFromBytes
|
||||||
GetKind() string
|
GetKind() string
|
||||||
|
|
||||||
|
// Used by Resource.Replace
|
||||||
GetLabels() map[string]string
|
GetLabels() map[string]string
|
||||||
GetMap(path string) (map[string]interface{}, error)
|
|
||||||
|
// Used by Resource.CurId and resource factory.
|
||||||
GetName() string
|
GetName() string
|
||||||
|
|
||||||
|
// Used by special case code used by ResMap.SubsetThatCouldBeReferencedByResource
|
||||||
GetSlice(path string) ([]interface{}, error)
|
GetSlice(path string) ([]interface{}, error)
|
||||||
|
|
||||||
|
// GetString returns the value of a string field.
|
||||||
|
// Used by Resource.GetNamespace
|
||||||
GetString(string) (string, error)
|
GetString(string) (string, error)
|
||||||
GetStringMap(path string) (map[string]string, error)
|
|
||||||
GetStringSlice(string) ([]string, error)
|
// Several uses.
|
||||||
Map() map[string]interface{}
|
Map() map[string]interface{}
|
||||||
|
|
||||||
|
// Used by Resource.AsYAML and Resource.String
|
||||||
MarshalJSON() ([]byte, error)
|
MarshalJSON() ([]byte, error)
|
||||||
|
|
||||||
|
// Used by resWrangler.Select
|
||||||
MatchesAnnotationSelector(selector string) (bool, error)
|
MatchesAnnotationSelector(selector string) (bool, error)
|
||||||
|
|
||||||
|
// Used by resWrangler.Select
|
||||||
MatchesLabelSelector(selector string) (bool, error)
|
MatchesLabelSelector(selector string) (bool, error)
|
||||||
Patch(Kunstructured) error
|
|
||||||
|
// Used by Resource.Replace.
|
||||||
SetAnnotations(map[string]string)
|
SetAnnotations(map[string]string)
|
||||||
|
|
||||||
|
// Used by PatchStrategicMergeTransformer.
|
||||||
SetGvk(resid.Gvk)
|
SetGvk(resid.Gvk)
|
||||||
|
|
||||||
|
// Used by Resource.Replace and used to remove "validated by" labels.
|
||||||
SetLabels(map[string]string)
|
SetLabels(map[string]string)
|
||||||
SetMap(map[string]interface{})
|
|
||||||
|
// Used by Resource.Replace.
|
||||||
SetName(string)
|
SetName(string)
|
||||||
|
|
||||||
|
// Used by Resource.Replace.
|
||||||
SetNamespace(string)
|
SetNamespace(string)
|
||||||
|
|
||||||
|
// Needed, for now, by kyaml/filtersutil.ApplyToJSON.
|
||||||
UnmarshalJSON([]byte) error
|
UnmarshalJSON([]byte) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
"sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/transform"
|
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
@@ -258,8 +257,7 @@ func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r = append(r, lts...)
|
r = append(r, lts...)
|
||||||
t := transform.NewMultiTransformer(r)
|
return ra.Transform(newMultiTransformer(r))
|
||||||
return ra.Transform(t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kt *KustTarget) configureExternalTransformers(transformers []string) ([]resmap.Transformer, error) {
|
func (kt *KustTarget) configureExternalTransformers(transformers []string) ([]resmap.Transformer, error) {
|
||||||
@@ -294,7 +292,6 @@ func (kt *KustTarget) runValidators(ra *accumulator.ResAccumulator) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (kt *KustTarget) removeValidatedByLabel(rm resmap.ResMap) {
|
func (kt *KustTarget) removeValidatedByLabel(rm resmap.ResMap) {
|
||||||
|
|
||||||
resources := rm.Resources()
|
resources := rm.Resources()
|
||||||
for _, r := range resources {
|
for _, r := range resources {
|
||||||
labels := r.GetLabels()
|
labels := r.GetLabels()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package transform
|
package target
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -17,8 +17,8 @@ type multiTransformer struct {
|
|||||||
|
|
||||||
var _ resmap.Transformer = &multiTransformer{}
|
var _ resmap.Transformer = &multiTransformer{}
|
||||||
|
|
||||||
// NewMultiTransformer constructs a multiTransformer.
|
// newMultiTransformer constructs a multiTransformer.
|
||||||
func NewMultiTransformer(t []resmap.Transformer) resmap.Transformer {
|
func newMultiTransformer(t []resmap.Transformer) resmap.Transformer {
|
||||||
r := &multiTransformer{
|
r := &multiTransformer{
|
||||||
transformers: make([]resmap.Transformer, len(t)),
|
transformers: make([]resmap.Transformer, len(t)),
|
||||||
checkConflictEnabled: false}
|
checkConflictEnabled: false}
|
||||||
@@ -466,278 +466,6 @@ func TestGetString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetInt64(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
pathToField string
|
|
||||||
expectedValue int64
|
|
||||||
errorExpected bool
|
|
||||||
errorMsg string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "numberAsValue",
|
|
||||||
pathToField: "this.is.aNumber",
|
|
||||||
errorExpected: false,
|
|
||||||
expectedValue: int64(1000),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validStructSubFieldIndexSubfield",
|
|
||||||
pathToField: "complextree[1].field2[1].intsubfield",
|
|
||||||
errorExpected: false,
|
|
||||||
expectedValue: int64(1121),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "twoFieldsOneMissing",
|
|
||||||
pathToField: "metadata.banana",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'metadata.banana'",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validStructSubFieldOutOfBoundIndex",
|
|
||||||
pathToField: "these[1].field2[99]",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'these[1].field2[99]'",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validDownwardAPISpecs",
|
|
||||||
pathToField: `spec.ports['port']`,
|
|
||||||
errorExpected: false,
|
|
||||||
expectedValue: int64(80),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
s, err := kunstructured.GetInt64(test.pathToField)
|
|
||||||
if test.errorExpected {
|
|
||||||
compareExpectedError(t, test.name, test.pathToField, err, test.errorMsg)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
unExpectedError(t, test.name, test.pathToField, err)
|
|
||||||
}
|
|
||||||
compareValues(t, test.name, test.pathToField, test.expectedValue, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetFloat64(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
pathToField string
|
|
||||||
expectedValue float64
|
|
||||||
errorExpected bool
|
|
||||||
errorMsg string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "floatAsValue",
|
|
||||||
pathToField: "this.is.aFloat",
|
|
||||||
errorExpected: false,
|
|
||||||
expectedValue: float64(1.001),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validStructSubFieldIndexSubfield",
|
|
||||||
pathToField: "complextree[1].field2[1].floatsubfield",
|
|
||||||
errorExpected: false,
|
|
||||||
expectedValue: float64(1.1121),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validDownwardAPIThis",
|
|
||||||
pathToField: `this.is[aFloat]`,
|
|
||||||
errorExpected: false,
|
|
||||||
expectedValue: float64(1.001),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "twoFieldsOneMissing",
|
|
||||||
pathToField: "metadata.banana",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'metadata.banana'",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validStructSubFieldOutOfBoundIndex",
|
|
||||||
pathToField: "these[1].field2[99]",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "index 99 is out of bounds",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
s, err := kunstructured.GetFloat64(test.pathToField)
|
|
||||||
if test.errorExpected {
|
|
||||||
compareExpectedError(t, test.name, test.pathToField, err, test.errorMsg)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
unExpectedError(t, test.name, test.pathToField, err)
|
|
||||||
}
|
|
||||||
compareValues(t, test.name, test.pathToField, test.expectedValue, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetBool(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
pathToField string
|
|
||||||
expectedValue bool
|
|
||||||
errorExpected bool
|
|
||||||
errorMsg string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "boolAsValue",
|
|
||||||
pathToField: "this.is.aBool",
|
|
||||||
errorExpected: false,
|
|
||||||
expectedValue: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validStructSubFieldIndexSubfield",
|
|
||||||
pathToField: "complextree[1].field2[1].boolfield",
|
|
||||||
errorExpected: false,
|
|
||||||
expectedValue: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "twoFieldsOneMissing",
|
|
||||||
pathToField: "metadata.banana",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'metadata.banana'",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validStructSubFieldOutOfBoundIndex",
|
|
||||||
pathToField: "these[1].field2[99]",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'these[1].field2[99]'",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
s, err := kunstructured.GetBool(test.pathToField)
|
|
||||||
if test.errorExpected {
|
|
||||||
compareExpectedError(t, test.name, test.pathToField, err, test.errorMsg)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
unExpectedError(t, test.name, test.pathToField, err)
|
|
||||||
}
|
|
||||||
compareValues(t, test.name, test.pathToField, test.expectedValue, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetStringMap(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
pathToField string
|
|
||||||
errorExpected bool
|
|
||||||
errorMsg string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "validStringMap",
|
|
||||||
pathToField: "those[2]",
|
|
||||||
errorExpected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "twoFieldsOneMissing",
|
|
||||||
pathToField: "metadata.banana",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'metadata.banana'",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validStructSubFieldOutOfBoundIndex",
|
|
||||||
pathToField: "these[1].field2[99]",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'these[1].field2[99]'",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
_, err := kunstructured.GetStringMap(test.pathToField)
|
|
||||||
if test.errorExpected {
|
|
||||||
compareExpectedError(t, test.name, test.pathToField, err, test.errorMsg)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
unExpectedError(t, test.name, test.pathToField, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetMap(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
pathToField string
|
|
||||||
errorExpected bool
|
|
||||||
errorMsg string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "validMap",
|
|
||||||
pathToField: "those[2]",
|
|
||||||
errorExpected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validStructSubFieldIndexSubfield",
|
|
||||||
pathToField: "complextree[1].field2[1]",
|
|
||||||
errorExpected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "twoFieldsOneMissing",
|
|
||||||
pathToField: "metadata.banana",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'metadata.banana'",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validStructSubFieldOutOfBoundIndex",
|
|
||||||
pathToField: "these[1].field2[99]",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'these[1].field2[99]'",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
_, err := kunstructured.GetMap(test.pathToField)
|
|
||||||
if test.errorExpected {
|
|
||||||
compareExpectedError(t, test.name, test.pathToField, err, test.errorMsg)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
unExpectedError(t, test.name, test.pathToField, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetStringSlice(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
pathToField string
|
|
||||||
errorExpected bool
|
|
||||||
errorMsg string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "validStringSlice",
|
|
||||||
pathToField: "that",
|
|
||||||
errorExpected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "twoFieldsOneMissing",
|
|
||||||
pathToField: "metadata.banana",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'metadata.banana'",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "validStructSubFieldOutOfBoundIndex",
|
|
||||||
pathToField: "these[1].field2[99]",
|
|
||||||
errorExpected: true,
|
|
||||||
errorMsg: "no field named 'these[1].field2[99]'",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
_, err := kunstructured.GetStringSlice(test.pathToField)
|
|
||||||
if test.errorExpected {
|
|
||||||
compareExpectedError(t, test.name, test.pathToField, err, test.errorMsg)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
unExpectedError(t, test.name, test.pathToField, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetSlice(t *testing.T) {
|
func TestGetSlice(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
@@ -86,26 +86,14 @@ func (r *Resource) Copy() ifc.Kunstructured {
|
|||||||
return r.kunStr.Copy()
|
return r.kunStr.Copy()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) GetBool(p string) (bool, error) {
|
|
||||||
return r.kunStr.GetBool(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) GetFieldValue(f string) (interface{}, error) {
|
func (r *Resource) GetFieldValue(f string) (interface{}, error) {
|
||||||
return r.kunStr.GetFieldValue(f)
|
return r.kunStr.GetFieldValue(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) GetFloat64(p string) (float64, error) {
|
|
||||||
return r.kunStr.GetFloat64(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) GetGvk() resid.Gvk {
|
func (r *Resource) GetGvk() resid.Gvk {
|
||||||
return r.kunStr.GetGvk()
|
return r.kunStr.GetGvk()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) GetInt64(p string) (int64, error) {
|
|
||||||
return r.kunStr.GetInt64(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) GetKind() string {
|
func (r *Resource) GetKind() string {
|
||||||
return r.kunStr.GetKind()
|
return r.kunStr.GetKind()
|
||||||
}
|
}
|
||||||
@@ -114,10 +102,6 @@ func (r *Resource) GetLabels() map[string]string {
|
|||||||
return r.kunStr.GetLabels()
|
return r.kunStr.GetLabels()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) GetMap(p string) (map[string]interface{}, error) {
|
|
||||||
return r.kunStr.GetMap(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) GetName() string {
|
func (r *Resource) GetName() string {
|
||||||
return r.kunStr.GetName()
|
return r.kunStr.GetName()
|
||||||
}
|
}
|
||||||
@@ -130,14 +114,6 @@ func (r *Resource) GetString(p string) (string, error) {
|
|||||||
return r.kunStr.GetString(p)
|
return r.kunStr.GetString(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) GetStringMap(p string) (map[string]string, error) {
|
|
||||||
return r.kunStr.GetStringMap(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) GetStringSlice(p string) ([]string, error) {
|
|
||||||
return r.kunStr.GetStringSlice(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) Map() map[string]interface{} {
|
func (r *Resource) Map() map[string]interface{} {
|
||||||
return r.kunStr.Map()
|
return r.kunStr.Map()
|
||||||
}
|
}
|
||||||
@@ -154,10 +130,6 @@ func (r *Resource) MatchesAnnotationSelector(selector string) (bool, error) {
|
|||||||
return r.kunStr.MatchesAnnotationSelector(selector)
|
return r.kunStr.MatchesAnnotationSelector(selector)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) Patch(other ifc.Kunstructured) error {
|
|
||||||
return r.kunStr.Patch(other)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) SetAnnotations(m map[string]string) {
|
func (r *Resource) SetAnnotations(m map[string]string) {
|
||||||
r.kunStr.SetAnnotations(m)
|
r.kunStr.SetAnnotations(m)
|
||||||
}
|
}
|
||||||
@@ -170,10 +142,6 @@ func (r *Resource) SetLabels(m map[string]string) {
|
|||||||
r.kunStr.SetLabels(m)
|
r.kunStr.SetLabels(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) SetMap(m map[string]interface{}) {
|
|
||||||
r.kunStr.SetMap(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) SetName(n string) {
|
func (r *Resource) SetName(n string) {
|
||||||
r.kunStr.SetName(n)
|
r.kunStr.SetName(n)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,77 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package transform
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type mutateFunc func(interface{}) (interface{}, error)
|
|
||||||
|
|
||||||
func MutateField(
|
|
||||||
m map[string]interface{},
|
|
||||||
pathToField []string,
|
|
||||||
createIfNotPresent bool,
|
|
||||||
fns ...mutateFunc) error {
|
|
||||||
if len(pathToField) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
firstPathSegment, isArray := getFirstPathSegment(pathToField)
|
|
||||||
|
|
||||||
_, found := m[firstPathSegment]
|
|
||||||
if !found {
|
|
||||||
if !createIfNotPresent || isArray {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
m[firstPathSegment] = map[string]interface{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(pathToField) == 1 {
|
|
||||||
var err error
|
|
||||||
for _, fn := range fns {
|
|
||||||
m[firstPathSegment], err = fn(m[firstPathSegment])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
v := m[firstPathSegment]
|
|
||||||
newPathToField := pathToField[1:]
|
|
||||||
switch typedV := v.(type) {
|
|
||||||
case nil:
|
|
||||||
log.Printf(
|
|
||||||
"nil value at `%s` ignored in mutation attempt",
|
|
||||||
strings.Join(pathToField, "."))
|
|
||||||
return nil
|
|
||||||
case map[string]interface{}:
|
|
||||||
return MutateField(typedV, newPathToField, createIfNotPresent, fns...)
|
|
||||||
case []interface{}:
|
|
||||||
for i := range typedV {
|
|
||||||
item := typedV[i]
|
|
||||||
typedItem, ok := item.(map[string]interface{})
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("%#v is expected to be %T", item, typedItem)
|
|
||||||
}
|
|
||||||
err := MutateField(typedItem, newPathToField, createIfNotPresent, fns...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("%#v is not expected to be a primitive type", typedV)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getFirstPathSegment(pathToField []string) (string, bool) {
|
|
||||||
if strings.HasSuffix(pathToField[0], "[]") {
|
|
||||||
return pathToField[0][:len(pathToField[0])-2], true
|
|
||||||
}
|
|
||||||
return pathToField[0], false
|
|
||||||
}
|
|
||||||
@@ -1,157 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package transform_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
|
||||||
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
|
||||||
. "sigs.k8s.io/kustomize/api/transform"
|
|
||||||
)
|
|
||||||
|
|
||||||
type noopMutator struct {
|
|
||||||
wasCalled bool
|
|
||||||
errorToReturn error
|
|
||||||
}
|
|
||||||
|
|
||||||
var errExpected = fmt.Errorf("oops")
|
|
||||||
|
|
||||||
const originalValue = "tomato"
|
|
||||||
const newValue = "notThe" + originalValue
|
|
||||||
|
|
||||||
func (m *noopMutator) mutate(_ interface{}) (interface{}, error) {
|
|
||||||
m.wasCalled = true
|
|
||||||
return newValue, m.errorToReturn
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeTestDeployment() ifc.Kunstructured {
|
|
||||||
factory := kunstruct.NewKunstructuredFactoryImpl()
|
|
||||||
return factory.FromMap(
|
|
||||||
map[string]interface{}{
|
|
||||||
"group": "apps",
|
|
||||||
"apiVersion": "v1",
|
|
||||||
"kind": "Deployment",
|
|
||||||
"metadata": map[string]interface{}{
|
|
||||||
"name": originalValue,
|
|
||||||
},
|
|
||||||
"spec": map[string]interface{}{
|
|
||||||
"template": map[string]interface{}{
|
|
||||||
"env": []interface{}{
|
|
||||||
map[string]interface{}{
|
|
||||||
"name": "HELLO",
|
|
||||||
"value": "hi there",
|
|
||||||
},
|
|
||||||
map[string]interface{}{
|
|
||||||
"name": "GOODBYE",
|
|
||||||
"value": "adios!",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"metadata": map[string]interface{}{
|
|
||||||
"labels": map[string]interface{}{
|
|
||||||
"vegetable": originalValue,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"spec": map[string]interface{}{
|
|
||||||
"containers": []interface{}{
|
|
||||||
map[string]interface{}{
|
|
||||||
"name": "tangerine",
|
|
||||||
"image": originalValue,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func getFieldValue(t *testing.T, obj ifc.Kunstructured, fieldName string) string {
|
|
||||||
v, err := obj.GetString(fieldName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected field error: %v", err)
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNoPath(t *testing.T) {
|
|
||||||
obj := makeTestDeployment()
|
|
||||||
m := &noopMutator{}
|
|
||||||
err := MutateField(
|
|
||||||
obj.Map(), []string{}, false, m.mutate)
|
|
||||||
if m.wasCalled {
|
|
||||||
t.Fatalf("mutator should not have been called.")
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHappyPath(t *testing.T) {
|
|
||||||
obj := makeTestDeployment()
|
|
||||||
v := getFieldValue(t, obj, "metadata.name")
|
|
||||||
if v != originalValue {
|
|
||||||
t.Fatalf("unexpected original value: %v", v)
|
|
||||||
}
|
|
||||||
v = getFieldValue(t, obj, "spec.template.metadata.labels.vegetable")
|
|
||||||
if v != originalValue {
|
|
||||||
t.Fatalf("unexpected original value: %v", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
m := &noopMutator{}
|
|
||||||
err := MutateField(
|
|
||||||
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
|
|
||||||
if !m.wasCalled {
|
|
||||||
t.Fatalf("mutator should have been called.")
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected mutate error: %v", err)
|
|
||||||
}
|
|
||||||
v = getFieldValue(t, obj, "metadata.name")
|
|
||||||
if v != newValue {
|
|
||||||
t.Fatalf("unexpected new value: %v", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
m = &noopMutator{}
|
|
||||||
err = MutateField(
|
|
||||||
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
|
|
||||||
if !m.wasCalled {
|
|
||||||
t.Fatalf("mutator should have been called.")
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected mutate error: %v", err)
|
|
||||||
}
|
|
||||||
v = getFieldValue(t, obj, "spec.template.metadata.labels.vegetable")
|
|
||||||
if v != newValue {
|
|
||||||
t.Fatalf("unexpected new value: %v", v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithError(t *testing.T) {
|
|
||||||
obj := makeTestDeployment()
|
|
||||||
m := noopMutator{errorToReturn: errExpected}
|
|
||||||
err := MutateField(
|
|
||||||
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
|
|
||||||
if !m.wasCalled {
|
|
||||||
t.Fatalf("mutator was not called!")
|
|
||||||
}
|
|
||||||
if err != errExpected {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithNil(t *testing.T) {
|
|
||||||
obj := makeTestDeployment()
|
|
||||||
foo := obj.Map()["spec"]
|
|
||||||
foo = foo.(map[string]interface{})["template"]
|
|
||||||
foo = foo.(map[string]interface{})["metadata"]
|
|
||||||
foo.(map[string]interface{})["labels"] = nil
|
|
||||||
|
|
||||||
m := &noopMutator{}
|
|
||||||
err := MutateField(
|
|
||||||
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -369,6 +369,7 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
|
|||||||
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
|
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
|
||||||
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
||||||
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
|||||||
@@ -239,7 +239,6 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ
|
|||||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||||
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
|
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
|
||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
|
|
||||||
github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
|
github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
|
||||||
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
|
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
@@ -459,6 +458,7 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
|
|||||||
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
|
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
|
||||||
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
||||||
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
|||||||
@@ -185,6 +185,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
|
|||||||
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
|
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
|
||||||
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
|||||||
@@ -920,10 +920,10 @@ func TestCmd_Execute_enableLogSteps(t *testing.T) {
|
|||||||
|
|
||||||
logs := &bytes.Buffer{}
|
logs := &bytes.Buffer{}
|
||||||
instance := RunFns{
|
instance := RunFns{
|
||||||
Path: dir,
|
Path: dir,
|
||||||
functionFilterProvider: getFilterProvider(t),
|
functionFilterProvider: getFilterProvider(t),
|
||||||
LogSteps: true,
|
LogSteps: true,
|
||||||
LogWriter: logs,
|
LogWriter: logs,
|
||||||
}
|
}
|
||||||
if !assert.NoError(t, instance.Execute()) {
|
if !assert.NoError(t, instance.Execute()) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
|
|||||||
@@ -334,6 +334,7 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
|
|||||||
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
|
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
|
||||||
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
||||||
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
|||||||
Reference in New Issue
Block a user