add origin annotation for resources generated by generators (#4341)

* add origin annotation for resources generated by builtin and custom generators

* decouple origin data from generator data and account for inline generators
This commit is contained in:
Natasha Sarkar
2021-12-23 10:40:29 -08:00
committed by GitHub
parent 233f1a3c2a
commit 964bb38ba2
8 changed files with 717 additions and 21 deletions

View File

@@ -14,8 +14,9 @@ import (
// Origin retains information about where resources in the output
// of `kustomize build` originated from
type Origin struct {
// Path is the path to the resource, rooted from the directory upon
// which `kustomize build` was invoked
// 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
@@ -25,6 +26,16 @@ type Origin struct {
// Ref is the ref of the remote repository that the resource 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.
// ConfiguredIn is the file path to the generator config that created the
// resource
ConfiguredIn string `json:"configuredIn,omitempty" yaml:"configuredIn,omitempty"`
// ConfiguredBy is the ObjectReference of the generator config
ConfiguredBy kyaml.ResourceIdentifier `json:"configuredBy,omitempty" yaml:"configuredBy,omitempty"`
}
// Copy returns a copy of origin
@@ -55,3 +66,29 @@ func (origin *Origin) String() (string, error) {
anno, err := kyaml.Marshal(origin)
return string(anno), err
}
func OriginFromCustomPlugin(res *Resource) (*Origin, error) {
origin, err := res.GetOrigin()
if err != nil {
return nil, err
}
var result *Origin
if origin != nil {
result = &Origin{
Repo: origin.Repo,
Ref: origin.Ref,
ConfiguredIn: origin.Path,
ConfiguredBy: kyaml.ResourceIdentifier{
TypeMeta: kyaml.TypeMeta{
APIVersion: res.GetApiVersion(),
Kind: res.GetKind(),
},
NameMeta: kyaml.NameMeta{
Name: res.GetName(),
Namespace: res.GetNamespace(),
},
},
}
}
return result, nil
}

View File

@@ -67,6 +67,29 @@ func (r *Resource) SetGvk(gvk resid.Gvk) {
r.SetApiVersion(gvk.ApiVersion())
}
func (r *Resource) GetOrigin() (*Origin, error) {
annotations := r.GetAnnotations()
originAnnotations, ok := annotations[utils.OriginAnnotation]
if !ok {
return nil, nil
}
var origin Origin
if err := yaml.Unmarshal([]byte(originAnnotations), &origin); err != nil {
return nil, err
}
return &origin, nil
}
func (r *Resource) SetOrigin(origin *Origin) error {
annotations := r.GetAnnotations()
originStr, err := origin.String()
if err != nil {
return err
}
annotations[utils.OriginAnnotation] = originStr
return r.SetAnnotations(annotations)
}
// ResCtx is an interface describing the contextual added
// kept kustomize in the context of each Resource object.
// Currently mainly the name prefix and name suffix are added.