Modify PathConfig.PathSlice method and add tests

This commit is contained in:
Jingfang Liu
2018-09-27 14:02:52 -07:00
parent 19197a490e
commit d8b27ef8fe
8 changed files with 106 additions and 6 deletions

View File

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

View File

@@ -32,5 +32,5 @@ func GetDefaultPathConfigs() []byte {
[]byte(varReferencePathConfigs),
[]byte(nameReferencePathConfigs),
}
return bytes.Join(configData, []byte(`\n`))
return bytes.Join(configData, []byte("\n"))
}

View File

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

View File

@@ -20,5 +20,6 @@ const (
namespacePathConfigs = `
namespace:
- path: metadata/namespace
create: true
`
)

View File

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

View File

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

View File

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

View File

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