From 8cc6df51f3b2ebc74542cfd53227efac86107503 Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Tue, 18 Sep 2018 16:58:11 -0700 Subject: [PATCH] Add transformerconfig package and default configs --- .../defaultconfig/defaultconfig.go | 31 ++++++++ .../defaultconfig/namePrefix.go | 24 ++++++ pkg/transformerconfig/pathconfig.go | 46 ++++++++++++ pkg/transformerconfig/referencepathconfig.go | 75 +++++++++++++++++++ pkg/transformerconfig/transformerconfig.go | 73 ++++++++++++++++++ 5 files changed, 249 insertions(+) create mode 100644 pkg/transformerconfig/defaultconfig/defaultconfig.go create mode 100644 pkg/transformerconfig/defaultconfig/namePrefix.go create mode 100644 pkg/transformerconfig/pathconfig.go create mode 100644 pkg/transformerconfig/referencepathconfig.go create mode 100644 pkg/transformerconfig/transformerconfig.go diff --git a/pkg/transformerconfig/defaultconfig/defaultconfig.go b/pkg/transformerconfig/defaultconfig/defaultconfig.go new file mode 100644 index 000000000..cd00f4d60 --- /dev/null +++ b/pkg/transformerconfig/defaultconfig/defaultconfig.go @@ -0,0 +1,31 @@ +/* +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 defaultconfig provides the default +// transformer configurations +package defaultconfig + +import ( + "bytes" +) + +// GetDefaultPathConfigs returns the default pathConfigs data +func GetDefaultPathConfigs() []byte { + configData := [][]byte{ + []byte(namePrefixPathConfigs), + } + return bytes.Join(configData, []byte(`\n`)) +} diff --git a/pkg/transformerconfig/defaultconfig/namePrefix.go b/pkg/transformerconfig/defaultconfig/namePrefix.go new file mode 100644 index 000000000..e81219d9b --- /dev/null +++ b/pkg/transformerconfig/defaultconfig/namePrefix.go @@ -0,0 +1,24 @@ +/* +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 defaultconfig + +const ( + namePrefixPathConfigs = ` +namePrefix: +- path: metadata/name +` +) diff --git a/pkg/transformerconfig/pathconfig.go b/pkg/transformerconfig/pathconfig.go new file mode 100644 index 000000000..dd0bbca82 --- /dev/null +++ b/pkg/transformerconfig/pathconfig.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 ( + "k8s.io/apimachinery/pkg/runtime/schema" + "strings" +) + +// PathConfig contains the configuration of a field, including the gvk it ties to, +// path to the field, etc. +type PathConfig struct { + Group string `json:"group,omitempty" yaml:"group,omitempty"` + Version string `json:"version,omitempty" yaml:"version,omitempty"` + Kind string `json:"kind,omitempty" yaml:"kind,omitempty"` + Path string `json:"path,omitempty" yaml:"path,omitempty"` + CreateIfNotPresent bool `json:"create,omitempty" yaml:"create,omitempty"` +} + +// Gvk returns GroupVersionKind of the pathConfig +func (p PathConfig) Gvk() *schema.GroupVersionKind { + return &schema.GroupVersionKind{ + Group: p.Group, + Version: p.Version, + Kind: p.Kind, + } +} + +// PathSlice converts the path string to a slice of strings, separated by "/" +func (p PathConfig) PathSlice() []string { + return strings.Split(p.Path, "/") +} diff --git a/pkg/transformerconfig/referencepathconfig.go b/pkg/transformerconfig/referencepathconfig.go new file mode 100644 index 000000000..3fe103b90 --- /dev/null +++ b/pkg/transformerconfig/referencepathconfig.go @@ -0,0 +1,75 @@ +/* +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 ( + "k8s.io/apimachinery/pkg/runtime/schema" +) + +// 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 { + Group string `json:"group,omitempty" yaml:"group,omitempty"` + Version string `json:"version,omitempty" yaml:"version,omitempty"` + Kind string `json:"kind,omitempty" yaml:"kind,omitempty"` + // PathConfig is the gvk that is referencing the referencedGVK object's name. + PathConfigs []PathConfig `json:"pathConfigs,omitempty" yaml:"pathConfigs,omitempty"` +} + +// Gvk returns GroupVersionKind of the reference pathConfig +func (p ReferencePathConfig) Gvk() *schema.GroupVersionKind { + return &schema.GroupVersionKind{ + Group: p.Group, + Version: p.Version, + Kind: p.Kind, + } +} + +func merge(configs []ReferencePathConfig, config ReferencePathConfig) []ReferencePathConfig { + var result []ReferencePathConfig + found := false + for _, c := range configs { + if c.Gvk() == config.Gvk() { + c.PathConfigs = append(c.PathConfigs, config.PathConfigs...) + found = true + } + result = append(result, c) + } + + if !found { + result = append(result, config) + } + return result +} + +func mergeNameReferencePathConfigs(a, b []ReferencePathConfig) []ReferencePathConfig { + for _, r := range b { + a = merge(a, r) + } + return a +} diff --git a/pkg/transformerconfig/transformerconfig.go b/pkg/transformerconfig/transformerconfig.go new file mode 100644 index 000000000..e40f9cb25 --- /dev/null +++ b/pkg/transformerconfig/transformerconfig.go @@ -0,0 +1,73 @@ +/* +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 provides the functions to load default or user provided configurations +// for different transformers +package transformerconfig + +import ( + "github.com/ghodss/yaml" + "sigs.k8s.io/kustomize/pkg/loader" +) + +// TransformerConfig represents the path configurations for different transformations +type TransformerConfig struct { + NamePrefix []PathConfig `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"` + NameSpace []PathConfig `json:"namespace,omitempty" yaml:"namespace,omitempty"` + CommonLabels []PathConfig `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"` + CommonAnnotations []PathConfig `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"` + NameReference []ReferencePathConfig `json:"nameReference,omitempty" yaml:"nameReference,omitempty"` + VarReference []PathConfig `json:"varReference,omitempty" yaml:"varReference,omitempty"` +} + +// Merge merges two TransformerConfigs objects into a new TransformerConfig object +func (t *TransformerConfig) Merge(input *TransformerConfig) *TransformerConfig { + merged := &TransformerConfig{} + merged.NamePrefix = append(t.NamePrefix, input.NamePrefix...) + merged.NameSpace = append(t.NameSpace, input.NameSpace...) + merged.CommonAnnotations = append(t.CommonAnnotations, input.CommonAnnotations...) + merged.CommonLabels = append(t.CommonLabels, input.CommonLabels...) + merged.VarReference = append(t.VarReference, input.VarReference...) + merged.NameReference = mergeNameReferencePathConfigs(t.NameReference, input.NameReference) + return merged +} + +// MakeTransformerConfigFromFiles returns a TranformerConfig object from a list of files +func MakeTransformerConfigFromFiles(ldr loader.Loader, paths []string) (*TransformerConfig, error) { + result := &TransformerConfig{} + for _, path := range paths { + data, err := ldr.Load(path) + if err != nil { + return nil, err + } + t, err := MakeTransformerConfigFromBytes(data) + if err != nil { + return nil, err + } + result = result.Merge(t) + } + return result, nil +} + +// MakeTransformerConfigFromBytes returns a TransformerConfig object from bytes +func MakeTransformerConfigFromBytes(data []byte) (*TransformerConfig, error) { + var t TransformerConfig + err := yaml.Unmarshal(data, &t) + if err != nil { + return nil, err + } + return &t, nil +}