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
}