diff --git a/pkg/app/application.go b/pkg/app/application.go index 204b4fc91..31ffcddf5 100644 --- a/pkg/app/application.go +++ b/pkg/app/application.go @@ -37,6 +37,7 @@ import ( patchtransformer "sigs.k8s.io/kustomize/pkg/patch/transformer" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" + "sigs.k8s.io/kustomize/pkg/transformerconfig" "sigs.k8s.io/kustomize/pkg/transformers" "sigs.k8s.io/kustomize/pkg/types" ) @@ -49,10 +50,11 @@ type Application struct { kustomization *types.Kustomization ldr loader.Loader fSys fs.FileSystem + tcfg *transformerconfig.TransformerConfig } // 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) if err != nil { return nil, err @@ -63,7 +65,12 @@ func NewApplication(ldr loader.Loader, fSys fs.FileSystem) (*Application, error) if err != nil { 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 { @@ -95,7 +102,7 @@ func (a *Application) resolveRefsToGeneratedResources(m resmap.ResMap) (resmap.R } var r []transformers.Transformer - t, err := transformers.NewDefaultingNameReferenceTransformer() + t, err := transformers.NewNameReferenceTransformer(a.tcfg.NameReference) if err != nil { return nil, err } @@ -105,10 +112,7 @@ func (a *Application) resolveRefsToGeneratedResources(m resmap.ResMap) (resmap.R if err != nil { return nil, err } - t, err = transformers.NewRefVarTransformer(refVars) - if err != nil { - return nil, err - } + t = transformers.NewRefVarTransformer(refVars, a.tcfg.VarReference) r = append(r, t) err = transformers.NewMultiTransformer(r).Transform(m) @@ -125,7 +129,8 @@ func (a *Application) loadCustomizedResMap() (resmap.ResMap, error) { if err != nil { 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 { 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)) continue } - app, err := NewApplication(ldr, a.fSys) + app, err := NewApplication(ldr, a.fSys, a.tcfg) if err != nil { errs.Append(errors.Wrap(err, "couldn't make app for "+path)) continue @@ -238,7 +243,7 @@ func (a *Application) loadBasesAsFlatList() ([]*Application, error) { errs.Append(err) continue } - a, err := NewApplication(ldr, a.fSys) + a, err := NewApplication(ldr, a.fSys, a.tcfg) if err != nil { errs.Append(err) continue @@ -259,18 +264,18 @@ func (a *Application) newTransformer(patches []*resource.Resource) (transformers return nil, err } r = append(r, t) - r = append(r, transformers.NewNamespaceTransformer(string(a.kustomization.Namespace))) - t, err = transformers.NewDefaultingNamePrefixTransformer(string(a.kustomization.NamePrefix)) + r = append(r, transformers.NewNamespaceTransformer(string(a.kustomization.Namespace), a.tcfg.NameSpace)) + t, err = transformers.NewNamePrefixTransformer(string(a.kustomization.NamePrefix), a.tcfg.NamePrefix) if err != nil { return nil, err } r = append(r, t) - t, err = transformers.NewDefaultingLabelsMapTransformer(a.kustomization.CommonLabels) + t, err = transformers.NewLabelsMapTransformer(a.kustomization.CommonLabels, a.tcfg.CommonLabels) if err != nil { return nil, err } r = append(r, t) - t, err = transformers.NewDefaultingAnnotationsMapTransformer(a.kustomization.CommonAnnotations) + t, err = transformers.NewAnnotationsMapTransformer(a.kustomization.CommonAnnotations, a.tcfg.CommonAnnotations) if err != nil { return nil, err } diff --git a/pkg/app/application_test.go b/pkg/app/application_test.go index 9694ea5ea..91e72add9 100644 --- a/pkg/app/application_test.go +++ b/pkg/app/application_test.go @@ -30,6 +30,7 @@ import ( "sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) const ( @@ -203,7 +204,8 @@ func TestResources1(t *testing.T) { l := makeLoader1(t) fakeFs := fs.MakeFakeFS() fakeFs.Mkdir("/") - app, err := NewApplication(l, fakeFs) + cfg, _ := transformerconfig.MakeDefaultTransformerConfig() + app, err := NewApplication(l, fakeFs, cfg) if err != nil { t.Fatalf("Unexpected construction error %v", err) } @@ -226,7 +228,8 @@ func TestResourceNotFound(t *testing.T) { } fakeFs := fs.MakeFakeFS() fakeFs.Mkdir("/") - app, err := NewApplication(l, fakeFs) + cfg, _ := transformerconfig.MakeDefaultTransformerConfig() + app, err := NewApplication(l, fakeFs, cfg) if err != nil { t.Fatalf("Unexpected construction error %v", err) } @@ -247,7 +250,8 @@ func TestSecretTimeout(t *testing.T) { } fakeFs := fs.MakeFakeFS() fakeFs.Mkdir("/") - app, err := NewApplication(l, fakeFs) + cfg, _ := transformerconfig.MakeDefaultTransformerConfig() + app, err := NewApplication(l, fakeFs, cfg) if err != nil { t.Fatalf("Unexpected construction error %v", err) } diff --git a/pkg/commands/build.go b/pkg/commands/build.go index fdf82a024..310db2288 100644 --- a/pkg/commands/build.go +++ b/pkg/commands/build.go @@ -27,6 +27,7 @@ import ( "sigs.k8s.io/kustomize/pkg/constants" "sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/loader" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) type buildOptions struct { @@ -94,7 +95,11 @@ func (o *buildOptions) RunBuild(out io.Writer, fSys fs.FileSystem) error { } 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 { return err } diff --git a/pkg/transformers/labelsandannotations.go b/pkg/transformers/labelsandannotations.go index 26e5b9e97..4ff008ea2 100644 --- a/pkg/transformers/labelsandannotations.go +++ b/pkg/transformers/labelsandannotations.go @@ -21,29 +21,30 @@ import ( "fmt" "sigs.k8s.io/kustomize/pkg/resmap" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) // mapTransformer contains a map string->string and path configs // The map will be applied to the fields specified in path configs. type mapTransformer struct { m map[string]string - pathConfigs []PathConfig + pathConfigs []transformerconfig.PathConfig } var _ Transformer = &mapTransformer{} -// NewDefaultingLabelsMapTransformer construct a mapTransformer with defaultLabelsPathConfigs. -func NewDefaultingLabelsMapTransformer(m map[string]string) (Transformer, error) { - return NewMapTransformer(defaultLabelsPathConfigs, m) +// NewLabelsMapTransformer construct a mapTransformer with a given pathConfig slice +func NewLabelsMapTransformer(m map[string]string, p []transformerconfig.PathConfig) (Transformer, error) { + return NewMapTransformer(p, m) } -// NewDefaultingAnnotationsMapTransformer construct a mapTransformer with defaultAnnotationsPathConfigs. -func NewDefaultingAnnotationsMapTransformer(m map[string]string) (Transformer, error) { - return NewMapTransformer(defaultAnnotationsPathConfigs, m) +// NewAnnotationsMapTransformer construct a mapTransformer with a given pathConfig slice +func NewAnnotationsMapTransformer(m map[string]string, p []transformerconfig.PathConfig) (Transformer, error) { + return NewMapTransformer(p, m) } // 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 { return NewNoOpTransformer(), nil } @@ -59,10 +60,10 @@ func (o *mapTransformer) Transform(m resmap.ResMap) error { for id := range m { objMap := m[id].UnstructuredContent() for _, path := range o.pathConfigs { - if !id.Gvk().IsSelected(path.GroupVersionKind) { + if !id.Gvk().IsSelected(&path.Gvk) { continue } - err := mutateField(objMap, path.Path, path.CreateIfNotPresent, o.addMap) + err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addMap) if err != nil { return err } diff --git a/pkg/transformers/labelsandannotations_test.go b/pkg/transformers/labelsandannotations_test.go index afb16d18b..3d75bee6e 100644 --- a/pkg/transformers/labelsandannotations_test.go +++ b/pkg/transformers/labelsandannotations_test.go @@ -23,6 +23,7 @@ import ( "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) 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 { 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 { t.Fatalf("unexpected error: %v", err) } diff --git a/pkg/transformers/labelsandannotationsconfig.go b/pkg/transformers/labelsandannotationsconfig.go deleted file mode 100644 index 6c7997692..000000000 --- a/pkg/transformers/labelsandannotationsconfig.go +++ /dev/null @@ -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, - }, -} diff --git a/pkg/transformers/namereference.go b/pkg/transformers/namereference.go index a4e12cf95..609248017 100644 --- a/pkg/transformers/namereference.go +++ b/pkg/transformers/namereference.go @@ -23,23 +23,19 @@ import ( "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) // nameReferenceTransformer contains the referencing info between 2 GroupVersionKinds type nameReferenceTransformer struct { - pathConfigs []ReferencePathConfig + pathConfigs []transformerconfig.ReferencePathConfig } var _ Transformer = &nameReferenceTransformer{} -// NewDefaultingNameReferenceTransformer constructs a nameReferenceTransformer -// with defaultNameReferencepathConfigs. -func NewDefaultingNameReferenceTransformer() (Transformer, error) { - return NewNameReferenceTransformer(defaultNameReferencePathConfigs) -} - -// NewNameReferenceTransformer construct a nameReferenceTransformer. -func NewNameReferenceTransformer(pc []ReferencePathConfig) (Transformer, error) { +// NewNameReferenceTransformer constructs a nameReferenceTransformer +// with a given Reference PathConfig slice +func NewNameReferenceTransformer(pc []transformerconfig.ReferencePathConfig) (Transformer, error) { if pc == 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 { objMap := m[id].UnstructuredContent() for _, referencePathConfig := range o.pathConfigs { - for _, path := range referencePathConfig.pathConfigs { - if !id.Gvk().IsSelected(path.GroupVersionKind) { + for _, path := range referencePathConfig.PathConfigs { + if !id.Gvk().IsSelected(&path.Gvk) { continue } - err := mutateField(objMap, path.Path, path.CreateIfNotPresent, - o.updateNameReference(referencePathConfig.referencedGVK, m.FilterBy(id))) + err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, + o.updateNameReference(referencePathConfig.Gvk, m.FilterBy(id))) if err != nil { return err } diff --git a/pkg/transformers/namereference_test.go b/pkg/transformers/namereference_test.go index 9a8209fe5..f33baa2f4 100644 --- a/pkg/transformers/namereference_test.go +++ b/pkg/transformers/namereference_test.go @@ -22,6 +22,7 @@ import ( "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) 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 { t.Fatalf("unexpected error: %v", err) } diff --git a/pkg/transformers/namereferenceconfig.go b/pkg/transformers/namereferenceconfig.go deleted file mode 100644 index 57686cb2b..000000000 --- a/pkg/transformers/namereferenceconfig.go +++ /dev/null @@ -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, - }, - }, - }, -} diff --git a/pkg/transformers/namespace.go b/pkg/transformers/namespace.go index 2ae36cc67..16366a0a8 100644 --- a/pkg/transformers/namespace.go +++ b/pkg/transformers/namespace.go @@ -19,39 +19,33 @@ package transformers import ( "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/resmap" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) type namespaceTransformer struct { namespace string - pathConfigs []PathConfig - skipPathConfigs []PathConfig + pathConfigs []transformerconfig.PathConfig + skipPathConfigs []transformerconfig.PathConfig } -var namespacePathConfigs = []PathConfig{ +var skipNamespacePathConfigs = []transformerconfig.PathConfig{ { - Path: []string{"metadata", "namespace"}, - CreateIfNotPresent: true, - }, -} - -var skipNamespacePathConfigs = []PathConfig{ - { - GroupVersionKind: &gvk.Gvk{ + Gvk: gvk.Gvk{ Kind: "Namespace", }, }, { - GroupVersionKind: &gvk.Gvk{ + Gvk: gvk.Gvk{ Kind: "ClusterRoleBinding", }, }, { - GroupVersionKind: &gvk.Gvk{ + Gvk: gvk.Gvk{ Kind: "ClusterRole", }, }, { - GroupVersionKind: &gvk.Gvk{ + Gvk: gvk.Gvk{ Kind: "CustomResourceDefinition", }, }, @@ -60,14 +54,14 @@ var skipNamespacePathConfigs = []PathConfig{ var _ Transformer = &namespaceTransformer{} // NewNamespaceTransformer construct a namespaceTransformer. -func NewNamespaceTransformer(ns string) Transformer { +func NewNamespaceTransformer(ns string, cf []transformerconfig.PathConfig) Transformer { if len(ns) == 0 { return NewNoOpTransformer() } return &namespaceTransformer{ namespace: ns, - pathConfigs: namespacePathConfigs, + pathConfigs: cf, skipPathConfigs: skipNamespacePathConfigs, } } @@ -79,7 +73,7 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error { for id := range m { found := false for _, path := range o.skipPathConfigs { - if id.Gvk().IsSelected(path.GroupVersionKind) { + if id.Gvk().IsSelected(&path.Gvk) { found = true break } @@ -93,11 +87,11 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error { for id := range mf { objMap := mf[id].UnstructuredContent() for _, path := range o.pathConfigs { - if !id.Gvk().IsSelected(path.GroupVersionKind) { + if !id.Gvk().IsSelected(&path.Gvk) { 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 }) if err != nil { diff --git a/pkg/transformers/namespace_test.go b/pkg/transformers/namespace_test.go index 3191259ec..56d995f77 100644 --- a/pkg/transformers/namespace_test.go +++ b/pkg/transformers/namespace_test.go @@ -22,6 +22,7 @@ import ( "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) func TestNamespaceRun(t *testing.T) { @@ -183,8 +184,12 @@ func TestNamespaceRun(t *testing.T) { }), } - nst := NewNamespaceTransformer("test") - err := nst.Transform(m) + tcfg, err := transformerconfig.MakeDefaultTransformerConfig() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + nst := NewNamespaceTransformer("test", tcfg.NameSpace) + err = nst.Transform(m) if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/pkg/transformers/pathconfig.go b/pkg/transformers/pathconfig.go deleted file mode 100644 index 9dbc1569f..000000000 --- a/pkg/transformers/pathconfig.go +++ /dev/null @@ -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() -} diff --git a/pkg/transformers/prefixname.go b/pkg/transformers/prefixname.go index 2f405ca51..7e294812f 100644 --- a/pkg/transformers/prefixname.go +++ b/pkg/transformers/prefixname.go @@ -23,43 +23,32 @@ import ( "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/resmap" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) // namePrefixTransformer contains the prefix and the path config for each field that // the name prefix will be applied. type namePrefixTransformer struct { prefix string - pathConfigs []PathConfig - skipPathConfigs []PathConfig + pathConfigs []transformerconfig.PathConfig + skipPathConfigs []transformerconfig.PathConfig } var _ Transformer = &namePrefixTransformer{} -var defaultNamePrefixPathConfigs = []PathConfig{ +var skipNamePrefixPathConfigs = []transformerconfig.PathConfig{ { - Path: []string{"metadata", "name"}, - CreateIfNotPresent: false, - }, -} - -var skipNamePrefixPathConfigs = []PathConfig{ - { - GroupVersionKind: &gvk.Gvk{Kind: "CustomResourceDefinition"}, + Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"}, }, } // deprecateNamePrefixPathConfig will be moved into skipNamePrefixPathConfigs in next release -var deprecateNamePrefixPathConfig = PathConfig{ - GroupVersionKind: &gvk.Gvk{Kind: "Namespace"}, -} - -// NewDefaultingNamePrefixTransformer construct a namePrefixTransformer with defaultNamePrefixPathConfigs. -func NewDefaultingNamePrefixTransformer(nameprefix string) (Transformer, error) { - return NewNamePrefixTransformer(defaultNamePrefixPathConfigs, nameprefix) +var deprecateNamePrefixPathConfig = transformerconfig.PathConfig{ + Gvk: gvk.Gvk{Kind: "Namespace"}, } // 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 { return NewNoOpTransformer(), nil } @@ -76,7 +65,7 @@ func (o *namePrefixTransformer) Transform(m resmap.ResMap) error { for id := range m { found := false for _, path := range o.skipPathConfigs { - if id.Gvk().IsSelected(path.GroupVersionKind) { + if id.Gvk().IsSelected(&path.Gvk) { found = true break } @@ -88,15 +77,15 @@ func (o *namePrefixTransformer) Transform(m resmap.ResMap) error { } 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.") } objMap := mf[id].UnstructuredContent() for _, path := range o.pathConfigs { - if !id.Gvk().IsSelected(path.GroupVersionKind) { + if !id.Gvk().IsSelected(&path.Gvk) { continue } - err := mutateField(objMap, path.Path, path.CreateIfNotPresent, o.addPrefix) + err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addPrefix) if err != nil { return err } diff --git a/pkg/transformers/prefixname_test.go b/pkg/transformers/prefixname_test.go index 5624afa68..d77841a96 100644 --- a/pkg/transformers/prefixname_test.go +++ b/pkg/transformers/prefixname_test.go @@ -22,6 +22,7 @@ import ( "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) 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 { t.Fatalf("unexpected error: %v", err) } diff --git a/pkg/transformers/refvars.go b/pkg/transformers/refvars.go index bf7a9f30a..47e131e4a 100644 --- a/pkg/transformers/refvars.go +++ b/pkg/transformers/refvars.go @@ -4,106 +4,21 @@ import ( "fmt" "sigs.k8s.io/kustomize/pkg/expansion" - "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/resmap" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) type refvarTransformer struct { - pathConfigs []PathConfig + pathConfigs []transformerconfig.PathConfig vars map[string]string } // 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{ - vars: vars, - pathConfigs: []PathConfig{ - { - 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 + vars: vars, + pathConfigs: p, + } } // Transform determines the final values of variables: @@ -119,10 +34,10 @@ func (rv *refvarTransformer) Transform(resources resmap.ResMap) error { for resId := range resources { objMap := resources[resId].UnstructuredContent() for _, pc := range rv.pathConfigs { - if !resId.Gvk().IsSelected(pc.GroupVersionKind) { + if !resId.Gvk().IsSelected(&pc.Gvk) { continue } - err := mutateField(objMap, pc.Path, false, func(in interface{}) (interface{}, error) { + err := mutateField(objMap, pc.PathSlice(), false, func(in interface{}) (interface{}, error) { var ( mappingFunc = expansion.MappingFuncFor(rv.vars) )