Merge pull request #380 from Liujingfang1/pathconfig

remove hard coded pathconfig
This commit is contained in:
k8s-ci-robot
2018-09-28 10:01:41 -07:00
committed by GitHub
15 changed files with 115 additions and 1388 deletions

View File

@@ -37,6 +37,7 @@ import (
patchtransformer "sigs.k8s.io/kustomize/pkg/patch/transformer" patchtransformer "sigs.k8s.io/kustomize/pkg/patch/transformer"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
"sigs.k8s.io/kustomize/pkg/transformers" "sigs.k8s.io/kustomize/pkg/transformers"
"sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/kustomize/pkg/types"
) )
@@ -49,10 +50,11 @@ type Application struct {
kustomization *types.Kustomization kustomization *types.Kustomization
ldr loader.Loader ldr loader.Loader
fSys fs.FileSystem fSys fs.FileSystem
tcfg *transformerconfig.TransformerConfig
} }
// NewApplication returns a new instance of Application primed with a Loader. // NewApplication returns a new instance of Application primed with a Loader.
func NewApplication(ldr loader.Loader, fSys fs.FileSystem) (*Application, error) { func NewApplication(ldr loader.Loader, fSys fs.FileSystem, tcfg *transformerconfig.TransformerConfig) (*Application, error) {
content, err := ldr.Load(constants.KustomizationFileName) content, err := ldr.Load(constants.KustomizationFileName)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -63,7 +65,12 @@ func NewApplication(ldr loader.Loader, fSys fs.FileSystem) (*Application, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Application{kustomization: &m, ldr: ldr, fSys: fSys}, nil return &Application{
kustomization: &m,
ldr: ldr,
fSys: fSys,
tcfg: tcfg,
}, nil
} }
func unmarshal(y []byte, o interface{}) error { func unmarshal(y []byte, o interface{}) error {
@@ -95,7 +102,7 @@ func (a *Application) resolveRefsToGeneratedResources(m resmap.ResMap) (resmap.R
} }
var r []transformers.Transformer var r []transformers.Transformer
t, err := transformers.NewDefaultingNameReferenceTransformer() t, err := transformers.NewNameReferenceTransformer(a.tcfg.NameReference)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -105,10 +112,7 @@ func (a *Application) resolveRefsToGeneratedResources(m resmap.ResMap) (resmap.R
if err != nil { if err != nil {
return nil, err return nil, err
} }
t, err = transformers.NewRefVarTransformer(refVars) t = transformers.NewRefVarTransformer(refVars, a.tcfg.VarReference)
if err != nil {
return nil, err
}
r = append(r, t) r = append(r, t)
err = transformers.NewMultiTransformer(r).Transform(m) err = transformers.NewMultiTransformer(r).Transform(m)
@@ -125,7 +129,8 @@ func (a *Application) loadCustomizedResMap() (resmap.ResMap, error) {
if err != nil { if err != nil {
errs.Append(errors.Wrap(err, "loadResMapFromBasesAndResources")) errs.Append(errors.Wrap(err, "loadResMapFromBasesAndResources"))
} }
_, err = crds.RegisterCRDs(a.ldr, a.kustomization.Crds) crdPathConfigs, err := crds.RegisterCRDs(a.ldr, a.kustomization.Crds)
a.tcfg = a.tcfg.Merge(crdPathConfigs)
if err != nil { if err != nil {
errs.Append(errors.Wrap(err, "RegisterCRDs")) errs.Append(errors.Wrap(err, "RegisterCRDs"))
} }
@@ -209,7 +214,7 @@ func (a *Application) loadCustomizedBases() (resmap.ResMap, *interror.Kustomizat
errs.Append(errors.Wrap(err, "couldn't make ldr for "+path)) errs.Append(errors.Wrap(err, "couldn't make ldr for "+path))
continue continue
} }
app, err := NewApplication(ldr, a.fSys) app, err := NewApplication(ldr, a.fSys, a.tcfg)
if err != nil { if err != nil {
errs.Append(errors.Wrap(err, "couldn't make app for "+path)) errs.Append(errors.Wrap(err, "couldn't make app for "+path))
continue continue
@@ -238,7 +243,7 @@ func (a *Application) loadBasesAsFlatList() ([]*Application, error) {
errs.Append(err) errs.Append(err)
continue continue
} }
a, err := NewApplication(ldr, a.fSys) a, err := NewApplication(ldr, a.fSys, a.tcfg)
if err != nil { if err != nil {
errs.Append(err) errs.Append(err)
continue continue
@@ -259,18 +264,18 @@ func (a *Application) newTransformer(patches []*resource.Resource) (transformers
return nil, err return nil, err
} }
r = append(r, t) r = append(r, t)
r = append(r, transformers.NewNamespaceTransformer(string(a.kustomization.Namespace))) r = append(r, transformers.NewNamespaceTransformer(string(a.kustomization.Namespace), a.tcfg.NameSpace))
t, err = transformers.NewDefaultingNamePrefixTransformer(string(a.kustomization.NamePrefix)) t, err = transformers.NewNamePrefixTransformer(string(a.kustomization.NamePrefix), a.tcfg.NamePrefix)
if err != nil { if err != nil {
return nil, err return nil, err
} }
r = append(r, t) r = append(r, t)
t, err = transformers.NewDefaultingLabelsMapTransformer(a.kustomization.CommonLabels) t, err = transformers.NewLabelsMapTransformer(a.kustomization.CommonLabels, a.tcfg.CommonLabels)
if err != nil { if err != nil {
return nil, err return nil, err
} }
r = append(r, t) r = append(r, t)
t, err = transformers.NewDefaultingAnnotationsMapTransformer(a.kustomization.CommonAnnotations) t, err = transformers.NewAnnotationsMapTransformer(a.kustomization.CommonAnnotations, a.tcfg.CommonAnnotations)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -30,6 +30,7 @@ import (
"sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/loader"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
const ( const (
@@ -203,7 +204,8 @@ func TestResources1(t *testing.T) {
l := makeLoader1(t) l := makeLoader1(t)
fakeFs := fs.MakeFakeFS() fakeFs := fs.MakeFakeFS()
fakeFs.Mkdir("/") fakeFs.Mkdir("/")
app, err := NewApplication(l, fakeFs) cfg, _ := transformerconfig.MakeDefaultTransformerConfig()
app, err := NewApplication(l, fakeFs, cfg)
if err != nil { if err != nil {
t.Fatalf("Unexpected construction error %v", err) t.Fatalf("Unexpected construction error %v", err)
} }
@@ -226,7 +228,8 @@ func TestResourceNotFound(t *testing.T) {
} }
fakeFs := fs.MakeFakeFS() fakeFs := fs.MakeFakeFS()
fakeFs.Mkdir("/") fakeFs.Mkdir("/")
app, err := NewApplication(l, fakeFs) cfg, _ := transformerconfig.MakeDefaultTransformerConfig()
app, err := NewApplication(l, fakeFs, cfg)
if err != nil { if err != nil {
t.Fatalf("Unexpected construction error %v", err) t.Fatalf("Unexpected construction error %v", err)
} }
@@ -247,7 +250,8 @@ func TestSecretTimeout(t *testing.T) {
} }
fakeFs := fs.MakeFakeFS() fakeFs := fs.MakeFakeFS()
fakeFs.Mkdir("/") fakeFs.Mkdir("/")
app, err := NewApplication(l, fakeFs) cfg, _ := transformerconfig.MakeDefaultTransformerConfig()
app, err := NewApplication(l, fakeFs, cfg)
if err != nil { if err != nil {
t.Fatalf("Unexpected construction error %v", err) t.Fatalf("Unexpected construction error %v", err)
} }

View File

@@ -27,6 +27,7 @@ import (
"sigs.k8s.io/kustomize/pkg/constants" "sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/loader"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
type buildOptions struct { type buildOptions struct {
@@ -94,7 +95,11 @@ func (o *buildOptions) RunBuild(out io.Writer, fSys fs.FileSystem) error {
} }
defer rootLoader.Cleanup() defer rootLoader.Cleanup()
application, err := app.NewApplication(rootLoader, fSys) cfg, err := transformerconfig.MakeDefaultTransformerConfig()
if err != nil {
return err
}
application, err := app.NewApplication(rootLoader, fSys, cfg)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -21,29 +21,30 @@ import (
"fmt" "fmt"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
// mapTransformer contains a map string->string and path configs // mapTransformer contains a map string->string and path configs
// The map will be applied to the fields specified in path configs. // The map will be applied to the fields specified in path configs.
type mapTransformer struct { type mapTransformer struct {
m map[string]string m map[string]string
pathConfigs []PathConfig pathConfigs []transformerconfig.PathConfig
} }
var _ Transformer = &mapTransformer{} var _ Transformer = &mapTransformer{}
// NewDefaultingLabelsMapTransformer construct a mapTransformer with defaultLabelsPathConfigs. // NewLabelsMapTransformer construct a mapTransformer with a given pathConfig slice
func NewDefaultingLabelsMapTransformer(m map[string]string) (Transformer, error) { func NewLabelsMapTransformer(m map[string]string, p []transformerconfig.PathConfig) (Transformer, error) {
return NewMapTransformer(defaultLabelsPathConfigs, m) return NewMapTransformer(p, m)
} }
// NewDefaultingAnnotationsMapTransformer construct a mapTransformer with defaultAnnotationsPathConfigs. // NewAnnotationsMapTransformer construct a mapTransformer with a given pathConfig slice
func NewDefaultingAnnotationsMapTransformer(m map[string]string) (Transformer, error) { func NewAnnotationsMapTransformer(m map[string]string, p []transformerconfig.PathConfig) (Transformer, error) {
return NewMapTransformer(defaultAnnotationsPathConfigs, m) return NewMapTransformer(p, m)
} }
// NewMapTransformer construct a mapTransformer. // NewMapTransformer construct a mapTransformer.
func NewMapTransformer(pc []PathConfig, m map[string]string) (Transformer, error) { func NewMapTransformer(pc []transformerconfig.PathConfig, m map[string]string) (Transformer, error) {
if m == nil { if m == nil {
return NewNoOpTransformer(), nil return NewNoOpTransformer(), nil
} }
@@ -59,10 +60,10 @@ func (o *mapTransformer) Transform(m resmap.ResMap) error {
for id := range m { for id := range m {
objMap := m[id].UnstructuredContent() objMap := m[id].UnstructuredContent()
for _, path := range o.pathConfigs { for _, path := range o.pathConfigs {
if !id.Gvk().IsSelected(path.GroupVersionKind) { if !id.Gvk().IsSelected(&path.Gvk) {
continue continue
} }
err := mutateField(objMap, path.Path, path.CreateIfNotPresent, o.addMap) err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addMap)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -23,6 +23,7 @@ import (
"sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
var service = gvk.Gvk{Version: "v1", Kind: "Service"} var service = gvk.Gvk{Version: "v1", Kind: "Service"}
@@ -414,7 +415,11 @@ func TestLabelsRun(t *testing.T) {
}), }),
} }
lt, err := NewDefaultingLabelsMapTransformer(map[string]string{"label-key1": "label-value1", "label-key2": "label-value2"}) tcfg, err := transformerconfig.MakeDefaultTransformerConfig()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
lt, err := NewLabelsMapTransformer(map[string]string{"label-key1": "label-value1", "label-key2": "label-value2"}, tcfg.CommonLabels)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
@@ -549,7 +554,11 @@ func TestAnnotationsRun(t *testing.T) {
}, },
}), }),
} }
at, err := NewDefaultingAnnotationsMapTransformer(map[string]string{"anno-key1": "anno-value1", "anno-key2": "anno-value2"}) tcfg, err := transformerconfig.MakeDefaultTransformerConfig()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
at, err := NewAnnotationsMapTransformer(map[string]string{"anno-key1": "anno-value1", "anno-key2": "anno-value2"}, tcfg.CommonAnnotations)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }

View File

@@ -1,226 +0,0 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transformers
import (
"sigs.k8s.io/kustomize/pkg/gvk"
)
// defaultLabelsPathConfigs is the default configuration for mutating labels and
// selector fields for native k8s APIs.
var defaultLabelsPathConfigs = []PathConfig{
{
Path: []string{"metadata", "labels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Version: "v1", Kind: "Service"},
Path: []string{"spec", "selector"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Version: "v1", Kind: "ReplicationController"},
Path: []string{"spec", "selector"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Version: "v1", Kind: "ReplicationController"},
Path: []string{"spec", "template", "metadata", "labels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Deployment"},
Path: []string{"spec", "selector", "matchLabels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Deployment"},
Path: []string{"spec", "template", "metadata", "labels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "Deployment"},
Path: []string{"spec", "template", "spec", "affinity", "podAffinity",
"preferredDuringSchedulingIgnoredDuringExecution",
"podAffinityTerm", "labelSelector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "Deployment"},
Path: []string{"spec", "template", "spec", "affinity", "podAffinity",
"requiredDuringSchedulingIgnoredDuringExecution", "labelSelector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "Deployment"},
Path: []string{"spec", "template", "spec", "affinity", "podAntiAffinity",
"preferredDuringSchedulingIgnoredDuringExecution",
"podAffinityTerm", "labelSelector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "Deployment"},
Path: []string{"spec", "template", "spec", "affinity", "podAntiAffinity",
"requiredDuringSchedulingIgnoredDuringExecution", "labelSelector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Kind: "ReplicaSet"},
Path: []string{"spec", "selector", "matchLabels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Kind: "ReplicaSet"},
Path: []string{"spec", "template", "metadata", "labels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Kind: "DaemonSet"},
Path: []string{"spec", "selector", "matchLabels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Kind: "DaemonSet"},
Path: []string{"spec", "template", "metadata", "labels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "StatefulSet"},
Path: []string{"spec", "selector", "matchLabels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "StatefulSet"},
Path: []string{"spec", "template", "metadata", "labels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "StatefulSet"},
Path: []string{"spec", "template", "spec", "affinity", "podAffinity",
"preferredDuringSchedulingIgnoredDuringExecution",
"podAffinityTerm", "labelSelector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "StatefulSet"},
Path: []string{"spec", "template", "spec", "affinity", "podAffinity",
"requiredDuringSchedulingIgnoredDuringExecution", "labelSelector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "StatefulSet"},
Path: []string{"spec", "template", "spec", "affinity", "podAntiAffinity",
"preferredDuringSchedulingIgnoredDuringExecution",
"podAffinityTerm", "labelSelector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "StatefulSet"},
Path: []string{"spec", "template", "spec", "affinity", "podAntiAffinity",
"requiredDuringSchedulingIgnoredDuringExecution", "labelSelector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "batch", Kind: "Job"},
Path: []string{"spec", "selector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "batch", Kind: "Job"},
Path: []string{"spec", "template", "metadata", "labels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Group: "batch", Kind: "CronJob"},
Path: []string{"spec", "jobTemplate", "spec", "selector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "batch", Kind: "CronJob"},
Path: []string{"spec", "jobTemplate", "spec", "template", "metadata", "labels"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Group: "policy", Kind: "PodDisruptionBudget"},
Path: []string{"spec", "selector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "networking.k8s.io", Kind: "NetworkPolicy"},
Path: []string{"spec", "podSelector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "networking.k8s.io", Kind: "NetworkPolicy"},
Path: []string{"spec", "ingress", "from", "podSelector", "matchLabels"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{Group: "networking.k8s.io", Kind: "NetworkPolicy"},
Path: []string{"spec", "egress", "to", "podSelector", "matchLabels"},
CreateIfNotPresent: false,
},
}
// defaultLabelsPathConfigs is the default configuration for mutating annotations
// fields for native k8s APIs.
var defaultAnnotationsPathConfigs = []PathConfig{
{
Path: []string{"metadata", "annotations"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Version: "v1", Kind: "ReplicationController"},
Path: []string{"spec", "template", "metadata", "annotations"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Deployment"},
Path: []string{"spec", "template", "metadata", "annotations"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Kind: "ReplicaSet"},
Path: []string{"spec", "template", "metadata", "annotations"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Kind: "DaemonSet"},
Path: []string{"spec", "template", "metadata", "annotations"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Group: "apps", Kind: "StatefulSet"},
Path: []string{"spec", "template", "metadata", "annotations"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Group: "batch", Kind: "Job"},
Path: []string{"spec", "template", "metadata", "annotations"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Group: "batch", Kind: "CronJob"},
Path: []string{"spec", "jobTemplate", "metadata", "annotations"},
CreateIfNotPresent: true,
},
{
GroupVersionKind: &gvk.Gvk{Group: "batch", Kind: "CronJob"},
Path: []string{"spec", "jobTemplate", "spec", "template", "metadata", "annotations"},
CreateIfNotPresent: true,
},
}

View File

@@ -23,23 +23,19 @@ import (
"sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
// nameReferenceTransformer contains the referencing info between 2 GroupVersionKinds // nameReferenceTransformer contains the referencing info between 2 GroupVersionKinds
type nameReferenceTransformer struct { type nameReferenceTransformer struct {
pathConfigs []ReferencePathConfig pathConfigs []transformerconfig.ReferencePathConfig
} }
var _ Transformer = &nameReferenceTransformer{} var _ Transformer = &nameReferenceTransformer{}
// NewDefaultingNameReferenceTransformer constructs a nameReferenceTransformer // NewNameReferenceTransformer constructs a nameReferenceTransformer
// with defaultNameReferencepathConfigs. // with a given Reference PathConfig slice
func NewDefaultingNameReferenceTransformer() (Transformer, error) { func NewNameReferenceTransformer(pc []transformerconfig.ReferencePathConfig) (Transformer, error) {
return NewNameReferenceTransformer(defaultNameReferencePathConfigs)
}
// NewNameReferenceTransformer construct a nameReferenceTransformer.
func NewNameReferenceTransformer(pc []ReferencePathConfig) (Transformer, error) {
if pc == nil { if pc == nil {
return nil, errors.New("pathConfigs is not expected to be nil") return nil, errors.New("pathConfigs is not expected to be nil")
} }
@@ -54,12 +50,12 @@ func (o *nameReferenceTransformer) Transform(m resmap.ResMap) error {
for id := range m { for id := range m {
objMap := m[id].UnstructuredContent() objMap := m[id].UnstructuredContent()
for _, referencePathConfig := range o.pathConfigs { for _, referencePathConfig := range o.pathConfigs {
for _, path := range referencePathConfig.pathConfigs { for _, path := range referencePathConfig.PathConfigs {
if !id.Gvk().IsSelected(path.GroupVersionKind) { if !id.Gvk().IsSelected(&path.Gvk) {
continue continue
} }
err := mutateField(objMap, path.Path, path.CreateIfNotPresent, err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent,
o.updateNameReference(referencePathConfig.referencedGVK, m.FilterBy(id))) o.updateNameReference(referencePathConfig.Gvk, m.FilterBy(id)))
if err != nil { if err != nil {
return err return err
} }

View File

@@ -22,6 +22,7 @@ import (
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
func TestNameReferenceRun(t *testing.T) { func TestNameReferenceRun(t *testing.T) {
@@ -322,7 +323,11 @@ func TestNameReferenceRun(t *testing.T) {
}, },
) )
nrt, err := NewDefaultingNameReferenceTransformer() tcfg, err := transformerconfig.MakeDefaultTransformerConfig()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
nrt, err := NewNameReferenceTransformer(tcfg.NameReference)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }

View File

@@ -1,920 +0,0 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transformers
import (
"sigs.k8s.io/kustomize/pkg/gvk"
)
// defaultNameReferencePathConfigs is the default configuration for updating
// the fields reference the name of other resources.
var defaultNameReferencePathConfigs = []ReferencePathConfig{
{
referencedGVK: gvk.Gvk{
Kind: "Deployment",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Kind: "HorizontalPodAutoscaler",
},
Path: []string{"spec", "scaleTargetRef", "name"},
CreateIfNotPresent: false,
},
},
},
{
referencedGVK: gvk.Gvk{
Kind: "ReplicationController",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Kind: "HorizontalPodAutoscaler",
},
Path: []string{"spec", "scaleTargetRef", "name"},
CreateIfNotPresent: false,
},
},
},
{
referencedGVK: gvk.Gvk{
Kind: "ReplicaSet",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Kind: "HorizontalPodAutoscaler",
},
Path: []string{"spec", "scaleTargetRef", "name"},
CreateIfNotPresent: false,
},
},
},
{
referencedGVK: gvk.Gvk{
Version: "v1",
Kind: "ConfigMap",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "volumes", "configMap", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "containers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "initContainers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "containers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "initContainers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "volumes", "configMap", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "containers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "initContainers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "containers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "initContainers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "volumes", "projected", "sources", "configMap", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "volumes", "configMap", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "containers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "containers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "volumes", "configMap", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "containers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "containers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "volumes", "configMap", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "containers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "containers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "volumes", "projected", "sources", "configMap", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "volumes", "configMap", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "containers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "initContainers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "containers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "initContainers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "volumes", "configMap", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "containers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "initContainers", "env", "valueFrom", "configMapKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "containers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "initContainers", "envFrom", "configMapRef", "name"},
CreateIfNotPresent: false,
},
},
},
{
referencedGVK: gvk.Gvk{
Version: "v1",
Kind: "Secret",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "volumes", "secret", "secretName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "containers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "initContainers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "containers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "initContainers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Version: "v1",
Kind: "Pod",
},
Path: []string{"spec", "imagePullSecrets", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "volumes", "secret", "secretName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "containers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "initContainers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "containers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "initContainers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "imagePullSecrets", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "volumes", "secret", "secretName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "containers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "containers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicaSet",
},
Path: []string{"spec", "template", "spec", "imagePullSecrets", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "volumes", "secret", "secretName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "containers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "containers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "imagePullSecrets", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "volumes", "secret", "secretName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "containers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "containers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "initContainers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "imagePullSecrets", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "volumes", "secret", "secretName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "containers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "initContainers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "containers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "initContainers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "imagePullSecrets", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "volumes", "secret", "secretName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "containers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "initContainers", "env", "valueFrom", "secretKeyRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "containers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "initContainers", "envFrom", "secretRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "imagePullSecrets", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Ingress",
},
Path: []string{"spec", "tls", "secretName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Ingress",
},
Path: []string{"metadata", "annotations", "ingress.kubernetes.io/auth-secret"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Ingress",
},
Path: []string{"metadata", "annotations", "nginx.ingress.kubernetes.io/auth-secret"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ServiceAccount",
},
Path: []string{"imagePullSecrets", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StorageClass",
},
Path: []string{"parameters", "secretName"}, // This is for Glusterfs,
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StorageClass",
},
Path: []string{"parameters", "adminSecretName"}, // This is for Quobyte, CephRBD, StorageOS
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StorageClass",
},
Path: []string{"parameters", "userSecretName"}, // This is for CephRBD
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StorageClass",
},
Path: []string{"parameters", "secretRef"}, // This is for ScaleIO
CreateIfNotPresent: false,
},
},
},
{
// StatefulSet references headless service, so need to update the references.
referencedGVK: gvk.Gvk{
Version: "v1",
Kind: "Service",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Group: "apps",
Kind: "StatefulSet",
},
Path: []string{"spec", "serviceName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Ingress",
},
Path: []string{"spec", "rules", "http", "paths", "backend", "serviceName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Ingress",
},
Path: []string{"spec", "backend", "serviceName"},
CreateIfNotPresent: false,
},
},
},
{
referencedGVK: gvk.Gvk{
Group: "rbac.authorization.k8s.io",
Kind: "Role",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Group: "rbac.authorization.k8s.io",
Kind: "RoleBinding",
},
Path: []string{"roleRef", "name"},
CreateIfNotPresent: false,
},
},
},
{
referencedGVK: gvk.Gvk{
Group: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Group: "rbac.authorization.k8s.io",
Kind: "RoleBinding",
},
Path: []string{"roleRef", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Group: "rbac.authorization.k8s.io",
Kind: "ClusterRoleBinding",
},
Path: []string{"roleRef", "name"},
CreateIfNotPresent: false,
},
},
},
{
referencedGVK: gvk.Gvk{
Version: "v1",
Kind: "ServiceAccount",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Group: "rbac.authorization.k8s.io",
Kind: "RoleBinding",
},
Path: []string{"subjects", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Group: "rbac.authorization.k8s.io",
Kind: "ClusterRoleBinding",
},
Path: []string{"subjects", "name"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Pod",
},
Path: []string{"spec", "serviceAccountName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "serviceAccountName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "serviceAccountName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicationController",
},
Path: []string{"spec", "template", "spec", "serviceAccountName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "serviceAccountName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "serviceAccountName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "serviceAccountName"},
CreateIfNotPresent: false,
},
},
},
{
referencedGVK: gvk.Gvk{
Version: "v1",
Kind: "PersistentVolumeClaim",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Kind: "Pod",
},
Path: []string{"spec", "volumes", "persistentVolumeClaim", "claimName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "StatefulSet",
},
Path: []string{"spec", "template", "spec", "volumes", "persistentVolumeClaim", "claimName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Deployment",
},
Path: []string{"spec", "template", "spec", "volumes", "persistentVolumeClaim", "claimName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "ReplicationController",
},
Path: []string{"spec", "template", "spec", "volumes", "persistentVolumeClaim", "claimName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "CronJob",
},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "volumes", "persistentVolumeClaim", "claimName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "Job",
},
Path: []string{"spec", "template", "spec", "volumes", "persistentVolumeClaim", "claimName"},
CreateIfNotPresent: false,
},
{
GroupVersionKind: &gvk.Gvk{
Kind: "DaemonSet",
},
Path: []string{"spec", "template", "spec", "volumes", "persistentVolumeClaim", "claimName"},
CreateIfNotPresent: false,
},
},
},
{
referencedGVK: gvk.Gvk{
Version: "v1",
Kind: "PersistentVolume",
},
pathConfigs: []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Kind: "PersistentVolumeClaim",
},
Path: []string{"spec", "volumeName"},
CreateIfNotPresent: false,
},
},
},
}

View File

@@ -19,39 +19,33 @@ package transformers
import ( import (
"sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
type namespaceTransformer struct { type namespaceTransformer struct {
namespace string namespace string
pathConfigs []PathConfig pathConfigs []transformerconfig.PathConfig
skipPathConfigs []PathConfig skipPathConfigs []transformerconfig.PathConfig
} }
var namespacePathConfigs = []PathConfig{ var skipNamespacePathConfigs = []transformerconfig.PathConfig{
{ {
Path: []string{"metadata", "namespace"}, Gvk: gvk.Gvk{
CreateIfNotPresent: true,
},
}
var skipNamespacePathConfigs = []PathConfig{
{
GroupVersionKind: &gvk.Gvk{
Kind: "Namespace", Kind: "Namespace",
}, },
}, },
{ {
GroupVersionKind: &gvk.Gvk{ Gvk: gvk.Gvk{
Kind: "ClusterRoleBinding", Kind: "ClusterRoleBinding",
}, },
}, },
{ {
GroupVersionKind: &gvk.Gvk{ Gvk: gvk.Gvk{
Kind: "ClusterRole", Kind: "ClusterRole",
}, },
}, },
{ {
GroupVersionKind: &gvk.Gvk{ Gvk: gvk.Gvk{
Kind: "CustomResourceDefinition", Kind: "CustomResourceDefinition",
}, },
}, },
@@ -60,14 +54,14 @@ var skipNamespacePathConfigs = []PathConfig{
var _ Transformer = &namespaceTransformer{} var _ Transformer = &namespaceTransformer{}
// NewNamespaceTransformer construct a namespaceTransformer. // NewNamespaceTransformer construct a namespaceTransformer.
func NewNamespaceTransformer(ns string) Transformer { func NewNamespaceTransformer(ns string, cf []transformerconfig.PathConfig) Transformer {
if len(ns) == 0 { if len(ns) == 0 {
return NewNoOpTransformer() return NewNoOpTransformer()
} }
return &namespaceTransformer{ return &namespaceTransformer{
namespace: ns, namespace: ns,
pathConfigs: namespacePathConfigs, pathConfigs: cf,
skipPathConfigs: skipNamespacePathConfigs, skipPathConfigs: skipNamespacePathConfigs,
} }
} }
@@ -79,7 +73,7 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
for id := range m { for id := range m {
found := false found := false
for _, path := range o.skipPathConfigs { for _, path := range o.skipPathConfigs {
if id.Gvk().IsSelected(path.GroupVersionKind) { if id.Gvk().IsSelected(&path.Gvk) {
found = true found = true
break break
} }
@@ -93,11 +87,11 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
for id := range mf { for id := range mf {
objMap := mf[id].UnstructuredContent() objMap := mf[id].UnstructuredContent()
for _, path := range o.pathConfigs { for _, path := range o.pathConfigs {
if !id.Gvk().IsSelected(path.GroupVersionKind) { if !id.Gvk().IsSelected(&path.Gvk) {
continue continue
} }
err := mutateField(objMap, path.Path, path.CreateIfNotPresent, func(_ interface{}) (interface{}, error) { err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, func(_ interface{}) (interface{}, error) {
return o.namespace, nil return o.namespace, nil
}) })
if err != nil { if err != nil {

View File

@@ -22,6 +22,7 @@ import (
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
func TestNamespaceRun(t *testing.T) { func TestNamespaceRun(t *testing.T) {
@@ -183,8 +184,12 @@ func TestNamespaceRun(t *testing.T) {
}), }),
} }
nst := NewNamespaceTransformer("test") tcfg, err := transformerconfig.MakeDefaultTransformerConfig()
err := nst.Transform(m) if err != nil {
t.Fatalf("unexpected error: %v", err)
}
nst := NewNamespaceTransformer("test", tcfg.NameSpace)
err = nst.Transform(m)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }

View File

@@ -1,60 +0,0 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transformers
import (
"sigs.k8s.io/kustomize/pkg/gvk"
)
// PathConfig contains the configuration of a field, including the gvk it ties to,
// path to the field, etc.
type PathConfig struct {
// If true, it will create the path if it is not found.
CreateIfNotPresent bool
// The gvk that this path tied to.
// If unset, it applied to any gvk
// If some fields are set, it applies to all matching gvk.
GroupVersionKind *gvk.Gvk
// Path to the field that will be munged.
Path []string
}
// 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{
// referencedGVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
// pathConfigs: []PathConfig{
// {
// GroupVersionKind: &schema.GroupVersionKind{Version: "v1", Kind: "Pod"},
// Path: []string{"spec", "volumes", "configMap", "name"},
// },
// }
type ReferencePathConfig struct {
// referencedGVK is the GroupVersionKind that is referenced by
// the PathConfig's gvk in the path of PathConfig.Path.
referencedGVK gvk.Gvk
// PathConfig is the gvk that is referencing the referencedGVK object's name.
pathConfigs []PathConfig
}
// GVK returns the Group version kind of a Reference PathConfig
func (r ReferencePathConfig) GVK() string {
return r.referencedGVK.String()
}

View File

@@ -23,43 +23,32 @@ import (
"sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
// namePrefixTransformer contains the prefix and the path config for each field that // namePrefixTransformer contains the prefix and the path config for each field that
// the name prefix will be applied. // the name prefix will be applied.
type namePrefixTransformer struct { type namePrefixTransformer struct {
prefix string prefix string
pathConfigs []PathConfig pathConfigs []transformerconfig.PathConfig
skipPathConfigs []PathConfig skipPathConfigs []transformerconfig.PathConfig
} }
var _ Transformer = &namePrefixTransformer{} var _ Transformer = &namePrefixTransformer{}
var defaultNamePrefixPathConfigs = []PathConfig{ var skipNamePrefixPathConfigs = []transformerconfig.PathConfig{
{ {
Path: []string{"metadata", "name"}, Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"},
CreateIfNotPresent: false,
},
}
var skipNamePrefixPathConfigs = []PathConfig{
{
GroupVersionKind: &gvk.Gvk{Kind: "CustomResourceDefinition"},
}, },
} }
// deprecateNamePrefixPathConfig will be moved into skipNamePrefixPathConfigs in next release // deprecateNamePrefixPathConfig will be moved into skipNamePrefixPathConfigs in next release
var deprecateNamePrefixPathConfig = PathConfig{ var deprecateNamePrefixPathConfig = transformerconfig.PathConfig{
GroupVersionKind: &gvk.Gvk{Kind: "Namespace"}, Gvk: gvk.Gvk{Kind: "Namespace"},
}
// NewDefaultingNamePrefixTransformer construct a namePrefixTransformer with defaultNamePrefixPathConfigs.
func NewDefaultingNamePrefixTransformer(nameprefix string) (Transformer, error) {
return NewNamePrefixTransformer(defaultNamePrefixPathConfigs, nameprefix)
} }
// NewNamePrefixTransformer construct a namePrefixTransformer. // NewNamePrefixTransformer construct a namePrefixTransformer.
func NewNamePrefixTransformer(pc []PathConfig, np string) (Transformer, error) { func NewNamePrefixTransformer(np string, pc []transformerconfig.PathConfig) (Transformer, error) {
if len(np) == 0 { if len(np) == 0 {
return NewNoOpTransformer(), nil return NewNoOpTransformer(), nil
} }
@@ -76,7 +65,7 @@ func (o *namePrefixTransformer) Transform(m resmap.ResMap) error {
for id := range m { for id := range m {
found := false found := false
for _, path := range o.skipPathConfigs { for _, path := range o.skipPathConfigs {
if id.Gvk().IsSelected(path.GroupVersionKind) { if id.Gvk().IsSelected(&path.Gvk) {
found = true found = true
break break
} }
@@ -88,15 +77,15 @@ func (o *namePrefixTransformer) Transform(m resmap.ResMap) error {
} }
for id := range mf { for id := range mf {
if id.Gvk().IsSelected(deprecateNamePrefixPathConfig.GroupVersionKind) { if id.Gvk().IsSelected(&deprecateNamePrefixPathConfig.Gvk) {
log.Println("Adding nameprefix to Namespace resource will be deprecated in next release.") log.Println("Adding nameprefix to Namespace resource will be deprecated in next release.")
} }
objMap := mf[id].UnstructuredContent() objMap := mf[id].UnstructuredContent()
for _, path := range o.pathConfigs { for _, path := range o.pathConfigs {
if !id.Gvk().IsSelected(path.GroupVersionKind) { if !id.Gvk().IsSelected(&path.Gvk) {
continue continue
} }
err := mutateField(objMap, path.Path, path.CreateIfNotPresent, o.addPrefix) err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addPrefix)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -22,6 +22,7 @@ import (
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
func TestPrefixNameRun(t *testing.T) { func TestPrefixNameRun(t *testing.T) {
@@ -78,7 +79,11 @@ func TestPrefixNameRun(t *testing.T) {
}), }),
} }
npt, err := NewDefaultingNamePrefixTransformer("someprefix-") tcfg, err := transformerconfig.MakeDefaultTransformerConfig()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
npt, err := NewNamePrefixTransformer("someprefix-", tcfg.NamePrefix)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }

View File

@@ -4,106 +4,21 @@ import (
"fmt" "fmt"
"sigs.k8s.io/kustomize/pkg/expansion" "sigs.k8s.io/kustomize/pkg/expansion"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
) )
type refvarTransformer struct { type refvarTransformer struct {
pathConfigs []PathConfig pathConfigs []transformerconfig.PathConfig
vars map[string]string vars map[string]string
} }
// NewRefVarTransformer returns a Trasformer that replaces $(VAR) style variables with values. // NewRefVarTransformer returns a Trasformer that replaces $(VAR) style variables with values.
func NewRefVarTransformer(vars map[string]string) (Transformer, error) { func NewRefVarTransformer(vars map[string]string, p []transformerconfig.PathConfig) Transformer {
return &refvarTransformer{ return &refvarTransformer{
vars: vars, vars: vars,
pathConfigs: []PathConfig{ pathConfigs: p,
{ }
GroupVersionKind: &gvk.Gvk{Kind: "StatefulSet"},
Path: []string{"spec", "template", "spec", "initContainers", "command"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "StatefulSet"},
Path: []string{"spec", "template", "spec", "containers", "command"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Deployment"},
Path: []string{"spec", "template", "spec", "initContainers", "command"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Deployment"},
Path: []string{"spec", "template", "spec", "containers", "command"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Job"},
Path: []string{"spec", "template", "spec", "containers", "command"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "CronJob"},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "containers", "command"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "StatefulSet"},
Path: []string{"spec", "template", "spec", "initContainers", "args"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "StatefulSet"},
Path: []string{"spec", "template", "spec", "containers", "args"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Deployment"},
Path: []string{"spec", "template", "spec", "initContainers", "args"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Deployment"},
Path: []string{"spec", "template", "spec", "containers", "args"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Job"},
Path: []string{"spec", "template", "spec", "containers", "args"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "CronJob"},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "containers", "args"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "StatefulSet"},
Path: []string{"spec", "template", "spec", "initContainers", "env", "value"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "StatefulSet"},
Path: []string{"spec", "template", "spec", "containers", "env", "value"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Deployment"},
Path: []string{"spec", "template", "spec", "initContainers", "env", "value"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Deployment"},
Path: []string{"spec", "template", "spec", "containers", "env", "value"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Job"},
Path: []string{"spec", "template", "spec", "containers", "env", "value"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "CronJob"},
Path: []string{"spec", "jobTemplate", "spec", "template", "spec", "containers", "env", "value"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Pod"},
Path: []string{"spec", "containers", "command"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Pod"},
Path: []string{"spec", "containers", "args"},
},
{
GroupVersionKind: &gvk.Gvk{Kind: "Pod"},
Path: []string{"spec", "containers", "env", "value"},
},
},
}, nil
} }
// Transform determines the final values of variables: // Transform determines the final values of variables:
@@ -119,10 +34,10 @@ func (rv *refvarTransformer) Transform(resources resmap.ResMap) error {
for resId := range resources { for resId := range resources {
objMap := resources[resId].UnstructuredContent() objMap := resources[resId].UnstructuredContent()
for _, pc := range rv.pathConfigs { for _, pc := range rv.pathConfigs {
if !resId.Gvk().IsSelected(pc.GroupVersionKind) { if !resId.Gvk().IsSelected(&pc.Gvk) {
continue continue
} }
err := mutateField(objMap, pc.Path, false, func(in interface{}) (interface{}, error) { err := mutateField(objMap, pc.PathSlice(), false, func(in interface{}) (interface{}, error) {
var ( var (
mappingFunc = expansion.MappingFuncFor(rv.vars) mappingFunc = expansion.MappingFuncFor(rv.vars)
) )