mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Merge pull request #4357 from natasha41575/TransformerAnnotations
define methods, types, and unit tests for transformer annotations
This commit is contained in:
@@ -21,6 +21,13 @@ type Transformer interface {
|
||||
Transform(m ResMap) error
|
||||
}
|
||||
|
||||
// A TransformerWithProperties contains a Transformer and stores
|
||||
// some of its properties
|
||||
type TransformerWithProperties struct {
|
||||
Transformer
|
||||
Origin *resource.Origin
|
||||
}
|
||||
|
||||
// A Generator creates an instance of ResMap.
|
||||
type Generator interface {
|
||||
Generate() (ResMap, error)
|
||||
@@ -144,10 +151,24 @@ type ResMap interface {
|
||||
AbsorbAll(ResMap) error
|
||||
|
||||
// AddOriginAnnotation will add the provided origin as
|
||||
// an annotation to all resources in the Resmap, if
|
||||
// an origin annotation to all resources in the ResMap, if
|
||||
// the origin is not nil.
|
||||
AddOriginAnnotation(origin *resource.Origin) error
|
||||
|
||||
// RemoveOriginAnnotation will remove the origin annotation
|
||||
// from all resources in the ResMap
|
||||
RemoveOriginAnnotations() error
|
||||
|
||||
// AddTransformerAnnotation will add the provided origin as
|
||||
// an origin annotation if the resource doesn't have one; a
|
||||
// transformer annotation otherwise; to all resources in
|
||||
// ResMap
|
||||
AddTransformerAnnotation(origin *resource.Origin) error
|
||||
|
||||
// RemoveTransformerAnnotation will remove the transformer annotation
|
||||
// from all resources in the ResMap
|
||||
RemoveTransformerAnnotations() error
|
||||
|
||||
// AnnotateAll annotates all resources in the ResMap with
|
||||
// the provided key value pair.
|
||||
AnnotateAll(key string, value string) error
|
||||
|
||||
@@ -510,6 +510,50 @@ func (m *resWrangler) AddOriginAnnotation(origin *resource.Origin) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveOriginAnnotation implements ResMap
|
||||
func (m *resWrangler) RemoveOriginAnnotations() error {
|
||||
for _, res := range m.rList {
|
||||
if err := res.SetOrigin(nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddTransformerAnnotation implements ResMap
|
||||
func (m *resWrangler) AddTransformerAnnotation(origin *resource.Origin) error {
|
||||
for _, res := range m.rList {
|
||||
or, err := res.GetOrigin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if or == nil {
|
||||
// the resource does not have an origin annotation, so
|
||||
// we assume that the transformer generated the resource
|
||||
// rather than modifying it
|
||||
err = res.SetOrigin(origin)
|
||||
} else {
|
||||
// the resource already has an origin annotation, so we
|
||||
// record the provided origin as a transformation
|
||||
err = res.AddTransformation(origin)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveTransformerAnnotations implements ResMap
|
||||
func (m *resWrangler) RemoveTransformerAnnotations() error {
|
||||
for _, res := range m.rList {
|
||||
if err := res.ClearTransformations(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *resWrangler) appendReplaceOrMerge(res *resource.Resource) error {
|
||||
id := res.CurId()
|
||||
matches := m.GetMatchingResourcesByAnyId(id.Equals)
|
||||
|
||||
@@ -26,6 +26,34 @@ import (
|
||||
var depProvider = provider.NewDefaultDepProvider()
|
||||
var rf = depProvider.GetResourceFactory()
|
||||
var rmF = NewFactory(rf)
|
||||
var origin1 = &resource.Origin{
|
||||
Repo: "github.com/myrepo",
|
||||
Ref: "master",
|
||||
ConfiguredIn: "config.yaml",
|
||||
ConfiguredBy: yaml.ResourceIdentifier{
|
||||
TypeMeta: yaml.TypeMeta{
|
||||
APIVersion: "builtin",
|
||||
Kind: "Generator",
|
||||
},
|
||||
NameMeta: yaml.NameMeta{
|
||||
Name: "my-name",
|
||||
Namespace: "my-namespace",
|
||||
},
|
||||
},
|
||||
}
|
||||
var origin2 = &resource.Origin{
|
||||
ConfiguredIn: "../base/config.yaml",
|
||||
ConfiguredBy: yaml.ResourceIdentifier{
|
||||
TypeMeta: yaml.TypeMeta{
|
||||
APIVersion: "builtin",
|
||||
Kind: "Generator",
|
||||
},
|
||||
NameMeta: yaml.NameMeta{
|
||||
Name: "my-name",
|
||||
Namespace: "my-namespace",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func doAppend(t *testing.T, w ResMap, r *resource.Resource) {
|
||||
err := w.Append(r)
|
||||
@@ -1530,6 +1558,67 @@ $patch: delete
|
||||
}
|
||||
}
|
||||
|
||||
func TestOriginAnnotations(t *testing.T) {
|
||||
w := New()
|
||||
for i := 0; i < 3; i++ {
|
||||
assert.NoError(t, w.Append(makeCm(i)))
|
||||
}
|
||||
// this should add an origin annotation to every resource
|
||||
assert.NoError(t, w.AddOriginAnnotation(origin1))
|
||||
resources := w.Resources()
|
||||
for _, res := range resources {
|
||||
or, err := res.GetOrigin()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, origin1, or)
|
||||
}
|
||||
// this should not overwrite the existing origin annotations
|
||||
assert.NoError(t, w.AddOriginAnnotation(origin2))
|
||||
for _, res := range resources {
|
||||
or, err := res.GetOrigin()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, origin1, or)
|
||||
}
|
||||
// this should remove origin annotations from all resources
|
||||
assert.NoError(t, w.RemoveOriginAnnotations())
|
||||
for _, res := range resources {
|
||||
or, err := res.GetOrigin()
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, or)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransformerAnnotations(t *testing.T) {
|
||||
w := New()
|
||||
for i := 0; i < 3; i++ {
|
||||
assert.NoError(t, w.Append(makeCm(i)))
|
||||
}
|
||||
// this should add an origin annotation to every resource
|
||||
assert.NoError(t, w.AddTransformerAnnotation(origin1))
|
||||
resources := w.Resources()
|
||||
for _, res := range resources {
|
||||
or, err := res.GetOrigin()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, origin1, or)
|
||||
}
|
||||
// this should add a transformer annotation to every resource
|
||||
assert.NoError(t, w.AddTransformerAnnotation(origin2))
|
||||
for _, res := range resources {
|
||||
or, err := res.GetOrigin()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, origin1, or)
|
||||
tr, err := res.GetTransformations()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, resource.Transformations{origin2}, tr)
|
||||
}
|
||||
// remove transformer annotations from all resources
|
||||
assert.NoError(t, w.RemoveTransformerAnnotations())
|
||||
for _, res := range resources {
|
||||
tr, err := res.GetTransformations()
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, tr)
|
||||
}
|
||||
}
|
||||
|
||||
// baseResource produces a base object which used to test
|
||||
// patch transformation
|
||||
// Also the structure is matching the Deployment syntax
|
||||
|
||||
Reference in New Issue
Block a user