mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 18:10:59 +00:00
helper methods, types, and unit tests for transformer annotations
This commit is contained in:
@@ -11,30 +11,31 @@ import (
|
||||
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Origin retains information about where resources in the output
|
||||
// of `kustomize build` originated from
|
||||
// Origin retains information about the origin of resources and transformer configs
|
||||
// that contributed to the output of `kustomize build`
|
||||
type Origin struct {
|
||||
// Path is the path to the resource. If a local resource, this path is
|
||||
// rooted from the directory upon which `kustomize build` was invoked. If a
|
||||
// remote resource, this path is rooted from the root of the remote repo.
|
||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||
|
||||
// Repo is the remote repository that the resource originated from if it is
|
||||
// Repo is the remote repository that the resource or transformer originated from if it is
|
||||
// not from a local file
|
||||
Repo string `json:"repo,omitempty" yaml:"repo,omitempty"`
|
||||
|
||||
// Ref is the ref of the remote repository that the resource originated from
|
||||
// Ref is the ref of the remote repository that the resource or transformer originated from
|
||||
// if it is not from a local file
|
||||
Ref string `json:"ref,omitempty" yaml:"ref,omitempty"`
|
||||
|
||||
// The following fields only apply to resources that have been
|
||||
// generated by fields other than the `resources` field.
|
||||
// generated by fields other than the `resources` field, or to transformer
|
||||
// configs.
|
||||
|
||||
// ConfiguredIn is the file path to the generator config that created the
|
||||
// ConfiguredIn is the file path to the generator or transformer config that created the
|
||||
// resource
|
||||
ConfiguredIn string `json:"configuredIn,omitempty" yaml:"configuredIn,omitempty"`
|
||||
|
||||
// ConfiguredBy is the ObjectReference of the generator config
|
||||
// ConfiguredBy is the ObjectReference of the generator or transformer config
|
||||
ConfiguredBy kyaml.ResourceIdentifier `json:"configuredBy,omitempty" yaml:"configuredBy,omitempty"`
|
||||
}
|
||||
|
||||
@@ -67,6 +68,17 @@ func (origin *Origin) String() (string, error) {
|
||||
return string(anno), err
|
||||
}
|
||||
|
||||
// Transformations is a list of Origin
|
||||
type Transformations []*Origin
|
||||
|
||||
// String returns a string version of Transformations
|
||||
func (transformations *Transformations) String() (string, error) {
|
||||
anno, err := kyaml.Marshal(transformations)
|
||||
return string(anno), err
|
||||
}
|
||||
|
||||
// OriginFromCustomPlugin takes a custom plugin defined as a resource
|
||||
// and returns an origin object to describe it
|
||||
func OriginFromCustomPlugin(res *Resource) (*Origin, error) {
|
||||
origin, err := res.GetOrigin()
|
||||
if err != nil {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
. "sigs.k8s.io/kustomize/api/resource"
|
||||
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
func TestOriginAppend(t *testing.T) {
|
||||
@@ -78,6 +79,82 @@ repo: github.com/kubernetes-sigs/kustomize/examples/multibases/dev/
|
||||
for _, test := range tests {
|
||||
actual, err := test.in.String()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, actual, test.expected)
|
||||
assert.Equal(t, test.expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransformationsString(t *testing.T) {
|
||||
origin1 := &Origin{
|
||||
Repo: "github.com/myrepo",
|
||||
Ref: "master",
|
||||
ConfiguredIn: "config.yaml",
|
||||
ConfiguredBy: kyaml.ResourceIdentifier{
|
||||
TypeMeta: kyaml.TypeMeta{
|
||||
APIVersion: "builtin",
|
||||
Kind: "Generator",
|
||||
},
|
||||
NameMeta: kyaml.NameMeta{
|
||||
Name: "my-name",
|
||||
Namespace: "my-namespace",
|
||||
},
|
||||
},
|
||||
}
|
||||
origin2 := &Origin{
|
||||
ConfiguredIn: "../base/config.yaml",
|
||||
ConfiguredBy: kyaml.ResourceIdentifier{
|
||||
TypeMeta: kyaml.TypeMeta{
|
||||
APIVersion: "builtin",
|
||||
Kind: "Generator",
|
||||
},
|
||||
NameMeta: kyaml.NameMeta{
|
||||
Name: "my-name",
|
||||
Namespace: "my-namespace",
|
||||
},
|
||||
},
|
||||
}
|
||||
tests := []struct {
|
||||
in Transformations
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
in: Transformations{origin1},
|
||||
expected: `- repo: github.com/myrepo
|
||||
ref: master
|
||||
configuredIn: config.yaml
|
||||
configuredBy:
|
||||
apiVersion: builtin
|
||||
kind: Generator
|
||||
name: my-name
|
||||
namespace: my-namespace
|
||||
`,
|
||||
},
|
||||
{
|
||||
in: Transformations{origin1, origin2},
|
||||
expected: `- repo: github.com/myrepo
|
||||
ref: master
|
||||
configuredIn: config.yaml
|
||||
configuredBy:
|
||||
apiVersion: builtin
|
||||
kind: Generator
|
||||
name: my-name
|
||||
namespace: my-namespace
|
||||
- configuredIn: ../base/config.yaml
|
||||
configuredBy:
|
||||
apiVersion: builtin
|
||||
kind: Generator
|
||||
name: my-name
|
||||
namespace: my-namespace
|
||||
`,
|
||||
},
|
||||
{
|
||||
in: Transformations{},
|
||||
expected: `[]
|
||||
`,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
actual, err := test.in.String()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, test.expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ func (r *Resource) SetGvk(gvk resid.Gvk) {
|
||||
|
||||
func (r *Resource) GetOrigin() (*Origin, error) {
|
||||
annotations := r.GetAnnotations()
|
||||
originAnnotations, ok := annotations[utils.OriginAnnotation]
|
||||
originAnnotations, ok := annotations[utils.OriginAnnotationKey]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -82,11 +82,52 @@ func (r *Resource) GetOrigin() (*Origin, error) {
|
||||
|
||||
func (r *Resource) SetOrigin(origin *Origin) error {
|
||||
annotations := r.GetAnnotations()
|
||||
originStr, err := origin.String()
|
||||
if origin == nil {
|
||||
delete(annotations, utils.OriginAnnotationKey)
|
||||
} else {
|
||||
originStr, err := origin.String()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
annotations[utils.OriginAnnotationKey] = originStr
|
||||
}
|
||||
return r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
func (r *Resource) GetTransformations() (Transformations, error) {
|
||||
annotations := r.GetAnnotations()
|
||||
transformerAnnotations, ok := annotations[utils.TransformerAnnotationKey]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
var transformations Transformations
|
||||
if err := yaml.Unmarshal([]byte(transformerAnnotations), &transformations); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformations, nil
|
||||
}
|
||||
|
||||
func (r *Resource) AddTransformation(origin *Origin) error {
|
||||
annotations := r.GetAnnotations()
|
||||
transformations, err := r.GetTransformations()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
annotations[utils.OriginAnnotation] = originStr
|
||||
if transformations == nil {
|
||||
transformations = Transformations{}
|
||||
}
|
||||
transformations = append(transformations, origin)
|
||||
transformationStr, err := transformations.String()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
annotations[utils.TransformerAnnotationKey] = transformationStr
|
||||
return r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
func (r *Resource) ClearTransformations() error {
|
||||
annotations := r.GetAnnotations()
|
||||
delete(annotations, utils.TransformerAnnotationKey)
|
||||
return r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
. "sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
var factory = provider.NewDefaultDepProvider().GetResourceFactory()
|
||||
@@ -1427,3 +1428,129 @@ spec:
|
||||
resid.FromString("knd2.ver2.gr2/name2.ns2"),
|
||||
})
|
||||
}
|
||||
|
||||
func TestOrigin(t *testing.T) {
|
||||
r, err := factory.FromBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: clown
|
||||
spec:
|
||||
numReplicas: 1
|
||||
`))
|
||||
assert.NoError(t, err)
|
||||
origin := &Origin{
|
||||
Path: "deployment.yaml",
|
||||
Repo: "github.com/myrepo",
|
||||
Ref: "master",
|
||||
}
|
||||
assert.NoError(t, r.SetOrigin(origin))
|
||||
assert.Equal(t, `apiVersion: v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: clown
|
||||
annotations:
|
||||
config.kubernetes.io/origin: |
|
||||
path: deployment.yaml
|
||||
repo: github.com/myrepo
|
||||
ref: master
|
||||
spec:
|
||||
numReplicas: 1
|
||||
`, r.MustString())
|
||||
or, err := r.GetOrigin()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, origin, or)
|
||||
}
|
||||
|
||||
func TestTransformations(t *testing.T) {
|
||||
r, err := factory.FromBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: clown
|
||||
spec:
|
||||
numReplicas: 1
|
||||
`))
|
||||
assert.NoError(t, err)
|
||||
origin1 := &Origin{
|
||||
Repo: "github.com/myrepo",
|
||||
Ref: "master",
|
||||
ConfiguredIn: "config.yaml",
|
||||
ConfiguredBy: kyaml.ResourceIdentifier{
|
||||
TypeMeta: kyaml.TypeMeta{
|
||||
APIVersion: "builtin",
|
||||
Kind: "Generator",
|
||||
},
|
||||
NameMeta: kyaml.NameMeta{
|
||||
Name: "my-name",
|
||||
Namespace: "my-namespace",
|
||||
},
|
||||
},
|
||||
}
|
||||
origin2 := &Origin{
|
||||
ConfiguredIn: "../base/config.yaml",
|
||||
ConfiguredBy: kyaml.ResourceIdentifier{
|
||||
TypeMeta: kyaml.TypeMeta{
|
||||
APIVersion: "builtin",
|
||||
Kind: "Generator",
|
||||
},
|
||||
NameMeta: kyaml.NameMeta{
|
||||
Name: "my-name",
|
||||
Namespace: "my-namespace",
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.NoError(t, r.AddTransformation(origin1))
|
||||
assert.Equal(t, `apiVersion: v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: clown
|
||||
annotations:
|
||||
config.kubernetes.io/transformations: |
|
||||
- repo: github.com/myrepo
|
||||
ref: master
|
||||
configuredIn: config.yaml
|
||||
configuredBy:
|
||||
apiVersion: builtin
|
||||
kind: Generator
|
||||
name: my-name
|
||||
namespace: my-namespace
|
||||
spec:
|
||||
numReplicas: 1
|
||||
`, r.MustString())
|
||||
assert.NoError(t, r.AddTransformation(origin2))
|
||||
assert.Equal(t, `apiVersion: v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: clown
|
||||
annotations:
|
||||
config.kubernetes.io/transformations: |
|
||||
- repo: github.com/myrepo
|
||||
ref: master
|
||||
configuredIn: config.yaml
|
||||
configuredBy:
|
||||
apiVersion: builtin
|
||||
kind: Generator
|
||||
name: my-name
|
||||
namespace: my-namespace
|
||||
- configuredIn: ../base/config.yaml
|
||||
configuredBy:
|
||||
apiVersion: builtin
|
||||
kind: Generator
|
||||
name: my-name
|
||||
namespace: my-namespace
|
||||
spec:
|
||||
numReplicas: 1
|
||||
`, r.MustString())
|
||||
transformations, err := r.GetTransformations()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, Transformations{origin1, origin2}, transformations)
|
||||
assert.NoError(t, r.ClearTransformations())
|
||||
assert.Equal(t, `apiVersion: v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: clown
|
||||
spec:
|
||||
numReplicas: 1
|
||||
`, r.MustString())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user