mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Add annotation transformer.
This commit is contained in:
@@ -343,23 +343,17 @@ 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.NewAnnotationsMapTransformer(
|
lts, err := kt.configureBuiltinTransformers(tConfig)
|
||||||
kt.kustomization.CommonAnnotations, tConfig.CommonAnnotations)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
r = append(r, t)
|
r = append(r, lts...)
|
||||||
t, err = patchtransformer.NewPatchJson6902Factory(kt.ldr).
|
t, err = patchtransformer.NewPatchJson6902Factory(kt.ldr).
|
||||||
MakePatchJson6902Transformer(kt.kustomization.PatchesJson6902)
|
MakePatchJson6902Transformer(kt.kustomization.PatchesJson6902)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
r = append(r, t)
|
r = append(r, t)
|
||||||
lts, err := kt.configureBuiltinTransformers(tConfig)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
r = append(r, lts...)
|
|
||||||
tp, err := kt.loadTransformerPlugins()
|
tp, err := kt.loadTransformerPlugins()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -63,11 +63,11 @@ func (kt *KustTarget) configureBuiltinTransformers(
|
|||||||
// with tests:
|
// with tests:
|
||||||
// patch SMP
|
// patch SMP
|
||||||
// patch JSON
|
// patch JSON
|
||||||
// annos
|
|
||||||
configurators := []transformerConfigurator{
|
configurators := []transformerConfigurator{
|
||||||
kt.configureBuiltinNameTransformer,
|
kt.configureBuiltinNameTransformer,
|
||||||
kt.configureBuiltinImageTagTransformer,
|
kt.configureBuiltinImageTagTransformer,
|
||||||
kt.configureBuiltinLabelTransformer,
|
kt.configureBuiltinLabelTransformer,
|
||||||
|
kt.configureBuiltinAnnotationsTransformer,
|
||||||
}
|
}
|
||||||
var result []transformers.Transformer
|
var result []transformers.Transformer
|
||||||
for _, f := range configurators {
|
for _, f := range configurators {
|
||||||
@@ -140,6 +140,24 @@ func (kt *KustTarget) configureBuiltinLabelTransformer(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (kt *KustTarget) configureBuiltinAnnotationsTransformer(
|
||||||
|
tConfig *config.TransformerConfig) (
|
||||||
|
result []transformers.Transformer, err error) {
|
||||||
|
var c struct {
|
||||||
|
Annotations map[string]string
|
||||||
|
FieldSpecs []config.FieldSpec
|
||||||
|
}
|
||||||
|
c.Annotations = kt.kustomization.CommonAnnotations
|
||||||
|
c.FieldSpecs = tConfig.CommonAnnotations
|
||||||
|
p := builtin.NewAnnotationsTransformerPlugin()
|
||||||
|
err = kt.configureBuiltinPlugin(p, c, "annotations")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result = append(result, p)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (kt *KustTarget) configureBuiltinNameTransformer(
|
func (kt *KustTarget) configureBuiltinNameTransformer(
|
||||||
tConfig *config.TransformerConfig) (
|
tConfig *config.TransformerConfig) (
|
||||||
result []transformers.Transformer, err error) {
|
result []transformers.Transformer, err error) {
|
||||||
|
|||||||
38
plugin/builtin/AnnotationsTransformer.go
Normal file
38
plugin/builtin/AnnotationsTransformer.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// Code generated by pluginator on AnnotationsTransformer; DO NOT EDIT.
|
||||||
|
package builtin
|
||||||
|
|
||||||
|
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 annotations to the given field specifications.
|
||||||
|
type AnnotationsTransformerPlugin struct {
|
||||||
|
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
|
||||||
|
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAnnotationsTransformerPlugin() *AnnotationsTransformerPlugin {
|
||||||
|
return &AnnotationsTransformerPlugin{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *AnnotationsTransformerPlugin) Config(
|
||||||
|
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
|
||||||
|
p.Annotations = nil
|
||||||
|
p.FieldSpecs = nil
|
||||||
|
return yaml.Unmarshal(c, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||||
|
t, err := transformers.NewMapTransformer(
|
||||||
|
p.FieldSpecs,
|
||||||
|
p.Annotations,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return t.Transform(m)
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
//go:generate go run sigs.k8s.io/kustomize/plugin/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 annotations to the given field specifications.
|
||||||
|
type plugin struct {
|
||||||
|
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,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.Annotations = nil
|
||||||
|
p.FieldSpecs = nil
|
||||||
|
return yaml.Unmarshal(c, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *plugin) Transform(m resmap.ResMap) error {
|
||||||
|
t, err := transformers.NewMapTransformer(
|
||||||
|
p.FieldSpecs,
|
||||||
|
p.Annotations,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return t.Transform(m)
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package main_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/pkg/kusttest"
|
||||||
|
"sigs.k8s.io/kustomize/plugin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAnnotationsTransformer(t *testing.T) {
|
||||||
|
tc := plugin.NewEnvForTest(t).Set()
|
||||||
|
defer tc.Reset()
|
||||||
|
|
||||||
|
tc.BuildGoPlugin(
|
||||||
|
"builtin", "", "AnnotationsTransformer")
|
||||||
|
|
||||||
|
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
|
||||||
|
|
||||||
|
rm := th.LoadAndRunTransformer(`
|
||||||
|
apiVersion: builtin
|
||||||
|
kind: AnnotationsTransformer
|
||||||
|
metadata:
|
||||||
|
name: notImportantHere
|
||||||
|
annotations:
|
||||||
|
app: myApp
|
||||||
|
greeting/morning: a string with blanks
|
||||||
|
fieldSpecs:
|
||||||
|
- path: metadata/annotations
|
||||||
|
create: true
|
||||||
|
`, `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: myService
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 7002
|
||||||
|
`)
|
||||||
|
|
||||||
|
th.AssertActualEqualsExpected(rm, `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
annotations:
|
||||||
|
app: myApp
|
||||||
|
greeting/morning: a string with blanks
|
||||||
|
name: myService
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 7002
|
||||||
|
`)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user