Skip to content

Commit

Permalink
collect origin data only when the option is set
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Dec 7, 2021
1 parent bc3b249 commit 40f4a1e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
16 changes: 10 additions & 6 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) {
}

func (kt *KustTarget) makeCustomizedResMap() (resmap.ResMap, error) {
ra, err := kt.AccumulateTarget(&resource.Origin{})
var origin *resource.Origin
if utils.StringSliceContains(kt.kustomization.BuildMetadata, "originAnnotations") {
origin = &resource.Origin{}
}
ra, err := kt.AccumulateTarget(origin)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -392,7 +396,7 @@ func (kt *KustTarget) accumulateComponents(
return nil, fmt.Errorf("loader.New %q", errL)
}
var errD error
origin.Path = filepath.Join(origin.Path, path)
origin = origin.Append(path)
ra, errD = kt.accumulateDirectory(ra, ldr, origin, true)
if errD != nil {
return nil, fmt.Errorf("accumulateDirectory: %q", errD)
Expand Down Expand Up @@ -459,10 +463,10 @@ func (kt *KustTarget) accumulateFile(
if err != nil {
return errors.Wrapf(err, "accumulating resources from '%s'", path)
}
if utils.StringSliceContains(kt.kustomization.BuildMetadata, "originAnnotations") {
origin = origin.Append(path)
err = resources.AnnotateAll(utils.OriginAnnotation, origin.String())
if err != nil {
if origin != nil {
originAnno := origin.Append(path).String()
err = resources.AnnotateAll(utils.OriginAnnotation, originAnno)
if err != nil || originAnno == "" {
return errors.Wrapf(err, "cannot add path annotation for '%s'", path)
}
}
Expand Down
24 changes: 12 additions & 12 deletions api/resource/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,38 @@ import (
"strings"

"sigs.k8s.io/kustomize/api/internal/git"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
)

// 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 string
Path string `json:"path,omitempty" yaml:"path,omitempty"`

// Repo is the remote repository that the resource originated from if it is
// not from a local file
Repo string
Repo string `json:"repo,omitempty" yaml:"repo,omitempty"`

// Ref is the ref of the remote repository that the resource originated from
// if it is not from a local file
Ref string
Ref string `json:"ref,omitempty" yaml:"ref,omitempty"`
}

// Copy returns a copy of origin
func (origin *Origin) Copy() Origin {
if origin == nil {
return Origin{}
}
return *origin
}

// Append returns a copy of origin with a path appended to it
func (origin *Origin) Append(path string) *Origin {
if origin == nil {
return origin
}
originCopy := origin.Copy()
repoSpec, err := git.NewRepoSpecFromUrl(path)
if err == nil {
Expand All @@ -48,13 +55,6 @@ func (origin *Origin) Append(path string) *Origin {

// String returns a string version of origin
func (origin *Origin) String() string {
var anno string
anno = anno + "path: " + origin.Path + "\n"
if origin.Repo != "" {
anno = anno + "repo: " + origin.Repo + "\n"
}
if origin.Ref != "" {
anno = anno + "ref: " + origin.Ref + "\n"
}
return anno
anno, _ := kyaml.Marshal(origin)
return string(anno)
}

0 comments on commit 40f4a1e

Please sign in to comment.