diff --git a/docs/kustomization.yaml b/docs/kustomization.yaml index 315c6599d..ac377f5e8 100644 --- a/docs/kustomization.yaml +++ b/docs/kustomization.yaml @@ -115,3 +115,19 @@ patches: - service_port_8888.yaml - deployment_increase_replicas.yaml - deployment_increase_memory.yaml + + +# Each entry in this list should be a relative path to +# a file for custom resource definition(CRD). +# +# The presence of this field is to allow kustomize be +# aware of CRDs and apply proper +# transformation for any objects in those types. +# +# Typical use case: A CRD object refers to a ConfigMap object. +# In kustomization, the ConfigMap object name may change by adding namePrefix or hashing +# The name reference for this ConfigMap object in CRD object need to be +# updated with namePrefix or hashing in the same way. +crds: +- crds/typeA.yaml +- crds/typeB.yaml diff --git a/pkg/app/application.go b/pkg/app/application.go index 27bbf6ce5..d458853f9 100644 --- a/pkg/app/application.go +++ b/pkg/app/application.go @@ -27,6 +27,7 @@ import ( "github.com/golang/glog" "github.com/kubernetes-sigs/kustomize/pkg/constants" + "github.com/kubernetes-sigs/kustomize/pkg/crds" interror "github.com/kubernetes-sigs/kustomize/pkg/internal/error" "github.com/kubernetes-sigs/kustomize/pkg/loader" "github.com/kubernetes-sigs/kustomize/pkg/resmap" @@ -102,7 +103,11 @@ func (a *Application) loadCustomizedResMap() (resmap.ResMap, error) { errs := &interror.KustomizationErrors{} result, err := a.loadResMapFromBasesAndResources() if err != nil { - errs.Append(errors.Wrap(err, "rawResources")) + errs.Append(errors.Wrap(err, "loadResMapFromBasesAndResources")) + } + err = crds.RegisterCRDs(a.loader, a.kustomization.CRDs) + if err != nil { + errs.Append(errors.Wrap(err, "RegisterCRDs")) } cms, err := resmap.NewResMapFromConfigMapArgs(a.loader, a.kustomization.ConfigMapGenerator) diff --git a/pkg/crds/crds.go b/pkg/crds/crds.go new file mode 100644 index 000000000..847bfad19 --- /dev/null +++ b/pkg/crds/crds.go @@ -0,0 +1,27 @@ +/* +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 crds read in files for CRD schemas and parse annotations from it +package crds + +import ( + "github.com/kubernetes-sigs/kustomize/pkg/loader" +) + +// RegisterCRDs parse CRD schemas from paths and update various pathConfigs +func RegisterCRDs(loader loader.Loader, paths []string) error { + return nil +} diff --git a/pkg/transformers/labelsandannotations_test.go b/pkg/transformers/labelsandannotations_test.go index d4886f9b0..68037e05b 100644 --- a/pkg/transformers/labelsandannotations_test.go +++ b/pkg/transformers/labelsandannotations_test.go @@ -311,3 +311,23 @@ func TestAnnotationsRun(t *testing.T) { t.Fatalf("actual doesn't match expected: %v", err) } } + +func TestAddPathConfigs(t *testing.T) { + aexpected := len(defaultAnnotationsPathConfigs) + 1 + lexpected := len(defaultLabelsPathConfigs) + 1 + pathConfigs := []PathConfig{ + { + GroupVersionKind: &schema.GroupVersionKind{Group: "GroupA", Kind: "KindB"}, + Path: []string{"path", "to", "a", "field"}, + CreateIfNotPresent: true, + }, + } + AddLabelsPathConfigs(pathConfigs) + AddAnnotationsPathConfigs(pathConfigs) + if len(defaultAnnotationsPathConfigs) != aexpected { + t.Fatalf("actual %v doesn't match expected: %v", len(defaultAnnotationsPathConfigs), aexpected) + } + if len(defaultLabelsPathConfigs) != lexpected { + t.Fatalf("actual %v doesn't match expected: %v", len(defaultLabelsPathConfigs), lexpected) + } +} diff --git a/pkg/transformers/labelsandannotationsconfig.go b/pkg/transformers/labelsandannotationsconfig.go index 7156fee78..8248ffb2d 100644 --- a/pkg/transformers/labelsandannotationsconfig.go +++ b/pkg/transformers/labelsandannotationsconfig.go @@ -162,3 +162,13 @@ var defaultAnnotationsPathConfigs = []PathConfig{ CreateIfNotPresent: true, }, } + +// AddLabelsPathConfigs adds extra path configs to the default one +func AddLabelsPathConfigs(pathConfigs []PathConfig) { + defaultLabelsPathConfigs = append(defaultLabelsPathConfigs, pathConfigs...) +} + +// AddAnnotationsPathConfigs adds extra path configs to the default one +func AddAnnotationsPathConfigs(pathConfigs []PathConfig) { + defaultAnnotationsPathConfigs = append(defaultAnnotationsPathConfigs, pathConfigs...) +} diff --git a/pkg/transformers/namereference_test.go b/pkg/transformers/namereference_test.go index f7ce0edca..a66d3780c 100644 --- a/pkg/transformers/namereference_test.go +++ b/pkg/transformers/namereference_test.go @@ -22,6 +22,7 @@ import ( "github.com/kubernetes-sigs/kustomize/pkg/resmap" "github.com/kubernetes-sigs/kustomize/pkg/resource" + "k8s.io/apimachinery/pkg/runtime/schema" ) func TestNameReferenceRun(t *testing.T) { @@ -199,3 +200,29 @@ func TestNameReferenceRun(t *testing.T) { t.Fatalf("actual doesn't match expected: %v", err) } } + +func TestAddNameReferencePathConfigs(t *testing.T) { + expected := len(defaultNameReferencePathConfigs) + 1 + + pathConfigs := []referencePathConfig{ + { + referencedGVK: schema.GroupVersionKind{ + Kind: "KindA", + }, + pathConfigs: []PathConfig{ + { + GroupVersionKind: &schema.GroupVersionKind{ + Kind: "KindB", + }, + Path: []string{"path", "to", "a", "field"}, + CreateIfNotPresent: false, + }, + }, + }, + } + + AddNameReferencePathConfigs(pathConfigs) + if len(defaultNameReferencePathConfigs) != expected { + t.Fatalf("actual %v doesn't match expected: %v", len(defaultAnnotationsPathConfigs), expected) + } +} diff --git a/pkg/transformers/namereferenceconfig.go b/pkg/transformers/namereferenceconfig.go index 100f91d4a..dd0f83a03 100644 --- a/pkg/transformers/namereferenceconfig.go +++ b/pkg/transformers/namereferenceconfig.go @@ -733,3 +733,8 @@ var defaultNameReferencePathConfigs = []referencePathConfig{ }, }, } + +// AddNameReferencePathConfigs adds extra reference path configs to the default one +func AddNameReferencePathConfigs(r []referencePathConfig) { + defaultNameReferencePathConfigs = append(defaultNameReferencePathConfigs, r...) +} diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index 146c302ef..52e5a4963 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -44,6 +44,9 @@ type Kustomization struct { // URLs and globs. Resources []string `json:"resources,omitempty" yaml:"resources,omitempty"` + // CRDs specifies relative paths to custom resource definition files. + CRDs []string `json:"crds,omitempty" yaml:"crds,omitempty"` + // An Patch entry is very similar to an Resource entry. // It specifies the relative paths within the package, and could be any // format that kubectl -f allows.