Add transformerconfig package and default configs

This commit is contained in:
Jingfang Liu
2018-09-18 16:58:11 -07:00
parent 6b1ffe13a0
commit 8cc6df51f3
5 changed files with 249 additions and 0 deletions

View File

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

View File

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

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 (
"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, "/")
}

View File

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

View File

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