mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 17:34:21 +00:00
Update pathconfigs library
implement CRD support and add unit tests Add integration test for crd support address comments
This commit is contained in:
@@ -322,8 +322,8 @@ func TestAddPathConfigs(t *testing.T) {
|
||||
CreateIfNotPresent: true,
|
||||
},
|
||||
}
|
||||
AddLabelsPathConfigs(pathConfigs)
|
||||
AddAnnotationsPathConfigs(pathConfigs)
|
||||
AddLabelsPathConfigs(pathConfigs...)
|
||||
AddAnnotationsPathConfigs(pathConfigs[0])
|
||||
if len(defaultAnnotationsPathConfigs) != aexpected {
|
||||
t.Fatalf("actual %v doesn't match expected: %v", len(defaultAnnotationsPathConfigs), aexpected)
|
||||
}
|
||||
|
||||
@@ -164,11 +164,11 @@ var defaultAnnotationsPathConfigs = []PathConfig{
|
||||
}
|
||||
|
||||
// AddLabelsPathConfigs adds extra path configs to the default one
|
||||
func AddLabelsPathConfigs(pathConfigs []PathConfig) {
|
||||
func AddLabelsPathConfigs(pathConfigs ...PathConfig) {
|
||||
defaultLabelsPathConfigs = append(defaultLabelsPathConfigs, pathConfigs...)
|
||||
}
|
||||
|
||||
// AddAnnotationsPathConfigs adds extra path configs to the default one
|
||||
func AddAnnotationsPathConfigs(pathConfigs []PathConfig) {
|
||||
func AddAnnotationsPathConfigs(pathConfigs ...PathConfig) {
|
||||
defaultAnnotationsPathConfigs = append(defaultAnnotationsPathConfigs, pathConfigs...)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
|
||||
// nameReferenceTransformer contains the referencing info between 2 GroupVersionKinds
|
||||
type nameReferenceTransformer struct {
|
||||
pathConfigs []referencePathConfig
|
||||
pathConfigs []ReferencePathConfig
|
||||
}
|
||||
|
||||
var _ Transformer = &nameReferenceTransformer{}
|
||||
@@ -38,7 +38,7 @@ func NewDefaultingNameReferenceTransformer() (Transformer, error) {
|
||||
}
|
||||
|
||||
// NewNameReferenceTransformer construct a nameReferenceTransformer.
|
||||
func NewNameReferenceTransformer(pc []referencePathConfig) (Transformer, error) {
|
||||
func NewNameReferenceTransformer(pc []ReferencePathConfig) (Transformer, error) {
|
||||
if pc == nil {
|
||||
return nil, errors.New("pathConfigs is not expected to be nil")
|
||||
}
|
||||
|
||||
@@ -207,7 +207,7 @@ func TestNameReferenceRun(t *testing.T) {
|
||||
func TestAddNameReferencePathConfigs(t *testing.T) {
|
||||
expected := len(defaultNameReferencePathConfigs) + 1
|
||||
|
||||
pathConfigs := []referencePathConfig{
|
||||
pathConfigs := []ReferencePathConfig{
|
||||
{
|
||||
referencedGVK: schema.GroupVersionKind{
|
||||
Kind: "KindA",
|
||||
|
||||
@@ -22,7 +22,7 @@ import (
|
||||
|
||||
// defaultNameReferencePathConfigs is the default configuration for updating
|
||||
// the fields reference the name of other resources.
|
||||
var defaultNameReferencePathConfigs = []referencePathConfig{
|
||||
var defaultNameReferencePathConfigs = []ReferencePathConfig{
|
||||
{
|
||||
referencedGVK: schema.GroupVersionKind{
|
||||
Kind: "Deployment",
|
||||
@@ -735,6 +735,27 @@ var defaultNameReferencePathConfigs = []referencePathConfig{
|
||||
}
|
||||
|
||||
// AddNameReferencePathConfigs adds extra reference path configs to the default one
|
||||
func AddNameReferencePathConfigs(r []referencePathConfig) {
|
||||
defaultNameReferencePathConfigs = append(defaultNameReferencePathConfigs, r...)
|
||||
func AddNameReferencePathConfigs(r []ReferencePathConfig) {
|
||||
for _, p := range r {
|
||||
defaultNameReferencePathConfigs = MergeNameReferencePathConfigs(defaultNameReferencePathConfigs, p)
|
||||
}
|
||||
}
|
||||
|
||||
// MergeNameReferencePathConfigs merges one ReferencePathConfig into a slice of ReferencePathConfig
|
||||
func MergeNameReferencePathConfigs(configs []ReferencePathConfig, config ReferencePathConfig) []ReferencePathConfig {
|
||||
result := []ReferencePathConfig{}
|
||||
|
||||
found := false
|
||||
for _, c := range configs {
|
||||
if c.referencedGVK == config.referencedGVK {
|
||||
c.pathConfigs = append(c.pathConfigs, config.pathConfigs...)
|
||||
found = true
|
||||
}
|
||||
result = append(result, c)
|
||||
}
|
||||
|
||||
if !found {
|
||||
result = append(result, config)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -33,12 +33,12 @@ type PathConfig struct {
|
||||
Path []string
|
||||
}
|
||||
|
||||
// referencePathConfig contains the configuration of a field that references
|
||||
// ReferencePathConfig contains the configuration of a field that references
|
||||
// the name of another resource whose GroupVersionKind is specified in referencedGVK.
|
||||
// e.g. pod.spec.template.volumes.configMap.name references the name of a configmap
|
||||
// Its corresponding referencePathConfig will look like:
|
||||
//
|
||||
// referencePathConfig{
|
||||
// ReferencePathConfig{
|
||||
// referencedGVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
|
||||
// pathConfigs: []PathConfig{
|
||||
// {
|
||||
@@ -46,10 +46,18 @@ type PathConfig struct {
|
||||
// Path: []string{"spec", "volumes", "configMap", "name"},
|
||||
// },
|
||||
// }
|
||||
type referencePathConfig struct {
|
||||
type ReferencePathConfig struct {
|
||||
// referencedGVK is the GroupVersionKind that is referenced by
|
||||
// the PathConfig's gvk in the path of PathConfig.Path.
|
||||
referencedGVK schema.GroupVersionKind
|
||||
// PathConfig is the gvk that is referencing the referencedGVK object's name.
|
||||
pathConfigs []PathConfig
|
||||
}
|
||||
|
||||
// NewReferencePathConfig creates a new ReferencePathConfig object
|
||||
func NewReferencePathConfig(gvk schema.GroupVersionKind, pathconfigs []PathConfig) ReferencePathConfig {
|
||||
return ReferencePathConfig{
|
||||
referencedGVK: gvk,
|
||||
pathConfigs: pathconfigs,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,3 +100,8 @@ func (o *namePrefixTransformer) addPrefix(in interface{}) (interface{}, error) {
|
||||
}
|
||||
return o.prefix + s, nil
|
||||
}
|
||||
|
||||
// AddPrefixPathConfigs adds extra path configs to the default one
|
||||
func AddPrefixPathConfigs(pathConfigs ...PathConfig) {
|
||||
defaultNamePrefixPathConfigs = append(defaultNamePrefixPathConfigs, pathConfigs...)
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/resmap"
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/resource"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
func TestPrefixNameRun(t *testing.T) {
|
||||
@@ -91,3 +92,19 @@ func TestPrefixNameRun(t *testing.T) {
|
||||
t.Fatalf("actual doesn't match expected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddPrefixPathConfigs(t *testing.T) {
|
||||
expected := len(defaultNamePrefixPathConfigs) + 1
|
||||
|
||||
pathConfigs := []PathConfig{
|
||||
{
|
||||
GroupVersionKind: &schema.GroupVersionKind{Group: "GroupA", Kind: "KindB"},
|
||||
Path: []string{"path", "to", "a", "field"},
|
||||
CreateIfNotPresent: true,
|
||||
},
|
||||
}
|
||||
AddPrefixPathConfigs(pathConfigs...)
|
||||
if len(defaultNamePrefixPathConfigs) != expected {
|
||||
t.Fatalf("actual %v doesn't match expected: %v", len(defaultNamePrefixPathConfigs), expected)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user