mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-09-15 12:18:57 +00:00
Merge pull request #4332 from natasha41575/OriginCollection
collect origin data only when the option is set
This commit is contained in:
@@ -113,7 +113,11 @@ func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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, types.OriginAnnotations) {
|
||||||
|
origin = &resource.Origin{}
|
||||||
|
}
|
||||||
|
ra, err := kt.AccumulateTarget(origin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -371,7 +375,11 @@ func (kt *KustTarget) accumulateResources(
|
|||||||
return nil, errors.Wrapf(
|
return nil, errors.Wrapf(
|
||||||
err, "accumulation err='%s'", errF.Error())
|
err, "accumulation err='%s'", errF.Error())
|
||||||
}
|
}
|
||||||
ra, err = kt.accumulateDirectory(ra, ldr, origin.Append(path), false)
|
if origin != nil {
|
||||||
|
ra, err = kt.accumulateDirectory(ra, ldr, origin.Append(path), false)
|
||||||
|
} else {
|
||||||
|
ra, err = kt.accumulateDirectory(ra, ldr, nil, false)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(
|
return nil, errors.Wrapf(
|
||||||
err, "accumulation err='%s'", errF.Error())
|
err, "accumulation err='%s'", errF.Error())
|
||||||
@@ -392,8 +400,11 @@ func (kt *KustTarget) accumulateComponents(
|
|||||||
return nil, fmt.Errorf("loader.New %q", errL)
|
return nil, fmt.Errorf("loader.New %q", errL)
|
||||||
}
|
}
|
||||||
var errD error
|
var errD error
|
||||||
origin.Path = filepath.Join(origin.Path, path)
|
if origin != nil {
|
||||||
ra, errD = kt.accumulateDirectory(ra, ldr, origin, true)
|
ra, errD = kt.accumulateDirectory(ra, ldr, origin.Append(path), true)
|
||||||
|
} else {
|
||||||
|
ra, errD = kt.accumulateDirectory(ra, ldr, nil, true)
|
||||||
|
}
|
||||||
if errD != nil {
|
if errD != nil {
|
||||||
return nil, fmt.Errorf("accumulateDirectory: %q", errD)
|
return nil, fmt.Errorf("accumulateDirectory: %q", errD)
|
||||||
}
|
}
|
||||||
@@ -459,12 +470,15 @@ func (kt *KustTarget) accumulateFile(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "accumulating resources from '%s'", path)
|
return errors.Wrapf(err, "accumulating resources from '%s'", path)
|
||||||
}
|
}
|
||||||
if utils.StringSliceContains(kt.kustomization.BuildMetadata, "originAnnotations") {
|
if origin != nil {
|
||||||
origin = origin.Append(path)
|
originAnno, err := origin.Append(path).String()
|
||||||
err = resources.AnnotateAll(utils.OriginAnnotation, origin.String())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "cannot add path annotation for '%s'", path)
|
return errors.Wrapf(err, "cannot add path annotation for '%s'", path)
|
||||||
}
|
}
|
||||||
|
err = resources.AnnotateAll(utils.OriginAnnotation, originAnno)
|
||||||
|
if err != nil || originAnno == "" {
|
||||||
|
return errors.Wrapf(err, "cannot add path annotation for '%s'", path)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err = ra.AppendAll(resources)
|
err = ra.AppendAll(resources)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/internal/git"
|
"sigs.k8s.io/kustomize/api/internal/git"
|
||||||
|
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Origin retains information about where resources in the output
|
// Origin retains information about where resources in the output
|
||||||
@@ -15,19 +16,22 @@ import (
|
|||||||
type Origin struct {
|
type Origin struct {
|
||||||
// Path is the path to the resource, rooted from the directory upon
|
// Path is the path to the resource, rooted from the directory upon
|
||||||
// which `kustomize build` was invoked
|
// 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
|
// Repo is the remote repository that the resource originated from if it is
|
||||||
// not from a local file
|
// 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
|
// Ref is the ref of the remote repository that the resource originated from
|
||||||
// if it is not from a local file
|
// if it is not from a local file
|
||||||
Ref string
|
Ref string `json:"ref,omitempty" yaml:"ref,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy returns a copy of origin
|
// Copy returns a copy of origin
|
||||||
func (origin *Origin) Copy() Origin {
|
func (origin *Origin) Copy() Origin {
|
||||||
|
if origin == nil {
|
||||||
|
return Origin{}
|
||||||
|
}
|
||||||
return *origin
|
return *origin
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,14 +51,7 @@ func (origin *Origin) Append(path string) *Origin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// String returns a string version of origin
|
// String returns a string version of origin
|
||||||
func (origin *Origin) String() string {
|
func (origin *Origin) String() (string, error) {
|
||||||
var anno string
|
anno, err := kyaml.Marshal(origin)
|
||||||
anno = anno + "path: " + origin.Path + "\n"
|
return string(anno), err
|
||||||
if origin.Repo != "" {
|
|
||||||
anno = anno + "repo: " + origin.Repo + "\n"
|
|
||||||
}
|
|
||||||
if origin.Ref != "" {
|
|
||||||
anno = anno + "ref: " + origin.Ref + "\n"
|
|
||||||
}
|
|
||||||
return anno
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package resource_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
. "sigs.k8s.io/kustomize/api/resource"
|
. "sigs.k8s.io/kustomize/api/resource"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,10 +35,9 @@ repo: https://github.com/kubernetes-sigs/kustomize
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
actual := test.in.Append(test.path).String()
|
actual, err := test.in.Append(test.path).String()
|
||||||
if actual != test.expected {
|
assert.NoError(t, err)
|
||||||
t.Fatalf("Expected %v, but got %v\n", test.expected, actual)
|
assert.Equal(t, actual, test.expected)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,8 +76,8 @@ repo: github.com/kubernetes-sigs/kustomize/examples/multibases/dev/
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
if test.in.String() != test.expected {
|
actual, err := test.in.String()
|
||||||
t.Fatalf("Expected %v, but got %v\n", test.expected, test.in.String())
|
assert.NoError(t, err)
|
||||||
}
|
assert.Equal(t, actual, test.expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ const (
|
|||||||
ComponentVersion = "kustomize.config.k8s.io/v1alpha1"
|
ComponentVersion = "kustomize.config.k8s.io/v1alpha1"
|
||||||
ComponentKind = "Component"
|
ComponentKind = "Component"
|
||||||
MetadataNamespacePath = "metadata/namespace"
|
MetadataNamespacePath = "metadata/namespace"
|
||||||
|
OriginAnnotations = "originAnnotations"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Kustomization holds the information needed to generate customized k8s api resources.
|
// Kustomization holds the information needed to generate customized k8s api resources.
|
||||||
|
|||||||
Reference in New Issue
Block a user