Update pathconfigs library

implement CRD support and add unit tests

Add integration test for crd support

address comments
This commit is contained in:
Jingfang Liu
2018-06-14 10:08:20 -07:00
parent e7ecceb0c2
commit ea00134776
19 changed files with 740 additions and 13 deletions

View File

@@ -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)
}

View File

@@ -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...)
}

View File

@@ -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")
}

View File

@@ -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",

View File

@@ -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
}

View File

@@ -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,
}
}

View File

@@ -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...)
}

View File

@@ -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)
}
}