diff --git a/pkg/transformerconfig/defaultconfig/commonlabels.go b/pkg/transformerconfig/defaultconfig/commonlabels.go index d3edca2c4..d70c9ff17 100644 --- a/pkg/transformerconfig/defaultconfig/commonlabels.go +++ b/pkg/transformerconfig/defaultconfig/commonlabels.go @@ -115,7 +115,7 @@ commonLabels: group: batch kind: Job -- path: spec/template/metadata/Labels +- path: spec/template/metadata/labels create: true group: batch kind: Job diff --git a/pkg/transformerconfig/defaultconfig/defaultconfig.go b/pkg/transformerconfig/defaultconfig/defaultconfig.go index f29e97bd7..c257bc144 100644 --- a/pkg/transformerconfig/defaultconfig/defaultconfig.go +++ b/pkg/transformerconfig/defaultconfig/defaultconfig.go @@ -32,5 +32,5 @@ func GetDefaultPathConfigs() []byte { []byte(varReferencePathConfigs), []byte(nameReferencePathConfigs), } - return bytes.Join(configData, []byte(`\n`)) + return bytes.Join(configData, []byte("\n")) } diff --git a/pkg/transformerconfig/defaultconfig/namereference.go b/pkg/transformerconfig/defaultconfig/namereference.go index 190753884..6289b1037 100644 --- a/pkg/transformerconfig/defaultconfig/namereference.go +++ b/pkg/transformerconfig/defaultconfig/namereference.go @@ -123,10 +123,10 @@ nameReference: - path: spec/volumes/secret/secretName version: v1 kind: Pod - - path: spec/containers/env/valueFrom/secretkeyRef/name + - path: spec/containers/env/valueFrom/secretKeyRef/name version: v1 kind: Pod - - path: spec/initContainers/env/valueFrom/secretkeyRef/name + - path: spec/initContainers/env/valueFrom/secretKeyRef/name version: v1 kind: Pod - path: spec/containers/envFrom/secretRef/name @@ -142,7 +142,7 @@ nameReference: kind: Deployment - path: spec/template/spec/containers/env/valueFrom/secretKeyRef/name kind: Deployment - - path: spec/template/spec/initContainers/env/valueFrom/secretkeyRef/name + - path: spec/template/spec/initContainers/env/valueFrom/secretKeyRef/name kind: Deployment - path: spec/template/spec/containers/envFrom/secretRef/name kind: Deployment diff --git a/pkg/transformerconfig/defaultconfig/namespace.go b/pkg/transformerconfig/defaultconfig/namespace.go index d308d2f09..911bd1d9b 100644 --- a/pkg/transformerconfig/defaultconfig/namespace.go +++ b/pkg/transformerconfig/defaultconfig/namespace.go @@ -20,5 +20,6 @@ const ( namespacePathConfigs = ` namespace: - path: metadata/namespace + create: true ` ) diff --git a/pkg/transformerconfig/pathconfig.go b/pkg/transformerconfig/pathconfig.go index 0be251a44..906eacb24 100644 --- a/pkg/transformerconfig/pathconfig.go +++ b/pkg/transformerconfig/pathconfig.go @@ -30,7 +30,31 @@ type PathConfig struct { CreateIfNotPresent bool `json:"create,omitempty" yaml:"create,omitempty"` } +const ( + escapedForwardSlash = "\\/" + tempSlashReplacement = "???" +) + // PathSlice converts the path string to a slice of strings, separated by "/" +// "/" can be contained in a fieldname. +// such as ingress.kubernetes.io/auth-secret in Ingress annotations. +// To deal with this special case, the path to this field should be formatted as +// +// metadata/annotations/ingress.kubernetes.io\/auth-secret +// +// Then PathSlice will return +// +// []string{"metadata", "annotations", "ingress.auth-secretkubernetes.io/auth-secret"} func (p PathConfig) PathSlice() []string { - return strings.Split(p.Path, "/") + if !strings.Contains(p.Path, escapedForwardSlash) { + return strings.Split(p.Path, "/") + } + + s := strings.Replace(p.Path, escapedForwardSlash, tempSlashReplacement, -1) + paths := strings.Split(s, "/") + var result []string + for _, path := range paths { + result = append(result, strings.Replace(path, tempSlashReplacement, "/", -1)) + } + return result } diff --git a/pkg/transformerconfig/pathconfig_test.go b/pkg/transformerconfig/pathconfig_test.go new file mode 100644 index 000000000..08c374ff2 --- /dev/null +++ b/pkg/transformerconfig/pathconfig_test.go @@ -0,0 +1,46 @@ +/* +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 transformerconfig + +import ( + "reflect" + "testing" +) + +func TestPathSlice(t *testing.T) { + type path struct { + input string + parsed []string + } + paths := []path{ + { + input: "spec/metadata/annotations", + parsed: []string{"spec", "metadata", "annotations"}, + }, + { + input: `metadata/annotations/nginx.ingress.kubernetes.io\/auth-secret`, + parsed: []string{"metadata", "annotations", "nginx.ingress.kubernetes.io/auth-secret"}, + }, + } + for _, p := range paths { + pathConfig := PathConfig{Path: p.input} + actual := pathConfig.PathSlice() + if !reflect.DeepEqual(actual, p.parsed) { + t.Fatalf("expected %v, but got %v", p.parsed, actual) + } + } +} diff --git a/pkg/transformerconfig/transformerconfig.go b/pkg/transformerconfig/transformerconfig.go index 8cfb56b90..b20a0f634 100644 --- a/pkg/transformerconfig/transformerconfig.go +++ b/pkg/transformerconfig/transformerconfig.go @@ -21,6 +21,7 @@ package transformerconfig import ( "github.com/ghodss/yaml" "sigs.k8s.io/kustomize/pkg/loader" + "sigs.k8s.io/kustomize/pkg/transformerconfig/defaultconfig" ) // TransformerConfig represents the path configurations for different transformations @@ -96,3 +97,8 @@ func MakeTransformerConfigFromBytes(data []byte) (*TransformerConfig, error) { func MakeEmptyTransformerConfig() *TransformerConfig { return &TransformerConfig{} } + +// MakeDefaultTransformerConfig returns a TransformerConfig object from the default configurations +func MakeDefaultTransformerConfig() (*TransformerConfig, error) { + return MakeTransformerConfigFromBytes(defaultconfig.GetDefaultPathConfigs()) +} diff --git a/pkg/transformerconfig/transformerconfig_test.go b/pkg/transformerconfig/transformerconfig_test.go index 99455254e..a1bdde578 100644 --- a/pkg/transformerconfig/transformerconfig_test.go +++ b/pkg/transformerconfig/transformerconfig_test.go @@ -1,3 +1,19 @@ +/* +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 transformerconfig import ( @@ -125,3 +141,10 @@ func TestMerge(t *testing.T) { t.Fatalf("expected: %v\n but got: %v\n", expected, actual) } } + +func TestMakeDefaultTransformerConfig(t *testing.T) { + _, err := MakeDefaultTransformerConfig() + if err != nil { + t.Fatalf("unexpected error %v\n", err) + } +}