mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Add builtin NameTransformer plugin.
This commit is contained in:
@@ -355,15 +355,6 @@ func (kt *KustTarget) newTransformer(
|
|||||||
r = append(r, t)
|
r = append(r, t)
|
||||||
r = append(r, transformers.NewNamespaceTransformer(
|
r = append(r, transformers.NewNamespaceTransformer(
|
||||||
string(kt.kustomization.Namespace), tConfig.NameSpace))
|
string(kt.kustomization.Namespace), tConfig.NameSpace))
|
||||||
t, err = transformers.NewNamePrefixSuffixTransformer(
|
|
||||||
string(kt.kustomization.NamePrefix),
|
|
||||||
string(kt.kustomization.NameSuffix),
|
|
||||||
tConfig.NamePrefix,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
r = append(r, t)
|
|
||||||
t, err = transformers.NewLabelsMapTransformer(
|
t, err = transformers.NewLabelsMapTransformer(
|
||||||
kt.kustomization.CommonLabels, tConfig.CommonLabels)
|
kt.kustomization.CommonLabels, tConfig.CommonLabels)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -73,9 +73,13 @@ func (kt *KustTarget) configureBuiltinTransformers(
|
|||||||
tConfig *config.TransformerConfig) (
|
tConfig *config.TransformerConfig) (
|
||||||
[]transformers.Transformer, error) {
|
[]transformers.Transformer, error) {
|
||||||
// TODO: Convert remaining legacy transformers to plugins
|
// TODO: Convert remaining legacy transformers to plugins
|
||||||
// (patch SMP/JSON, name prefix/suffix, labels/annos).
|
// with tests:
|
||||||
// with tests.
|
// patch SMP
|
||||||
|
// patch JSON
|
||||||
|
// labels
|
||||||
|
// annos
|
||||||
configurators := []transformerConfigurator{
|
configurators := []transformerConfigurator{
|
||||||
|
kt.configureBuiltinNameTransformer,
|
||||||
kt.configureBuiltinImageTagTransformer,
|
kt.configureBuiltinImageTagTransformer,
|
||||||
}
|
}
|
||||||
var result []transformers.Transformer
|
var result []transformers.Transformer
|
||||||
@@ -131,6 +135,26 @@ func (kt *KustTarget) configureBuiltinConfigMapGenerator() (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (kt *KustTarget) configureBuiltinNameTransformer(
|
||||||
|
tConfig *config.TransformerConfig) (
|
||||||
|
result []transformers.Transformer, err error) {
|
||||||
|
var c struct {
|
||||||
|
Prefix string
|
||||||
|
Suffix string
|
||||||
|
FieldSpecs []config.FieldSpec
|
||||||
|
}
|
||||||
|
c.Prefix = kt.kustomization.NamePrefix
|
||||||
|
c.Suffix = kt.kustomization.NameSuffix
|
||||||
|
c.FieldSpecs = tConfig.NamePrefix
|
||||||
|
p := builtingen.NewNameTransformerPlugin()
|
||||||
|
err = kt.configureBuiltinPlugin(p, c, "name")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result = append(result, p)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (kt *KustTarget) configureBuiltinImageTagTransformer(
|
func (kt *KustTarget) configureBuiltinImageTagTransformer(
|
||||||
tConfig *config.TransformerConfig) (
|
tConfig *config.TransformerConfig) (
|
||||||
result []transformers.Transformer, err error) {
|
result []transformers.Transformer, err error) {
|
||||||
|
|||||||
44
plugin/builtin/NameTransformer.go
Normal file
44
plugin/builtin/NameTransformer.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
// +build plugin
|
||||||
|
|
||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
//go:generate go run sigs.k8s.io/kustomize/cmd/pluginator
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/transformers"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Add the given prefix and suffix to the resource name.
|
||||||
|
type plugin struct {
|
||||||
|
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
|
||||||
|
Suffix string `json:"suffix,omitempty" yaml:"suffix,omitempty"`
|
||||||
|
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var KustomizePlugin plugin
|
||||||
|
|
||||||
|
func (p *plugin) Config(
|
||||||
|
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
|
||||||
|
p.Prefix = ""
|
||||||
|
p.Suffix = ""
|
||||||
|
p.FieldSpecs = nil
|
||||||
|
return yaml.Unmarshal(c, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *plugin) Transform(m resmap.ResMap) error {
|
||||||
|
t, err := transformers.NewNamePrefixSuffixTransformer(
|
||||||
|
p.Prefix,
|
||||||
|
p.Suffix,
|
||||||
|
p.FieldSpecs,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return t.Transform(m)
|
||||||
|
}
|
||||||
54
plugin/builtin/NameTransformer_test.go
Normal file
54
plugin/builtin/NameTransformer_test.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package main_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/internal/plugintest"
|
||||||
|
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/kusttest"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/loader"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNameTransformer(t *testing.T) {
|
||||||
|
tc := plugintest_test.NewPluginTestEnv(t).Set()
|
||||||
|
defer tc.Reset()
|
||||||
|
|
||||||
|
tc.BuildGoPlugin(
|
||||||
|
"builtin", "", "NameTransformer")
|
||||||
|
|
||||||
|
th := kusttest_test.NewKustTestHarnessFull(
|
||||||
|
t, "/app", loader.RestrictionRootOnly,
|
||||||
|
plugin.ActivePluginConfig())
|
||||||
|
|
||||||
|
rm := th.LoadAndRunTransformer(`
|
||||||
|
apiVersion: builtin
|
||||||
|
kind: NameTransformer
|
||||||
|
metadata:
|
||||||
|
name: notImportantHere
|
||||||
|
prefix: baked-
|
||||||
|
suffix: -pie
|
||||||
|
fieldSpecs:
|
||||||
|
- path: metadata/name
|
||||||
|
`, `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: apple
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 7002
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.AssertActualEqualsExpected(rm, `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: baked-apple-pie
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 7002
|
||||||
|
`)
|
||||||
|
}
|
||||||
41
plugin/builtingen/NameTransformer.go
Normal file
41
plugin/builtingen/NameTransformer.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
// Code generated by pluginator on NameTransformer; DO NOT EDIT.
|
||||||
|
package builtingen
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/transformers"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Add the given prefix and suffix to the resource name.
|
||||||
|
type NameTransformerPlugin struct {
|
||||||
|
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
|
||||||
|
Suffix string `json:"suffix,omitempty" yaml:"suffix,omitempty"`
|
||||||
|
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNameTransformerPlugin() *NameTransformerPlugin {
|
||||||
|
return &NameTransformerPlugin{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *NameTransformerPlugin) Config(
|
||||||
|
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
|
||||||
|
p.Prefix = ""
|
||||||
|
p.Suffix = ""
|
||||||
|
p.FieldSpecs = nil
|
||||||
|
return yaml.Unmarshal(c, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *NameTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||||
|
t, err := transformers.NewNamePrefixSuffixTransformer(
|
||||||
|
p.Prefix,
|
||||||
|
p.Suffix,
|
||||||
|
p.FieldSpecs,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return t.Transform(m)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user