From 9577d61167762082c6577187b0198e0590f58b01 Mon Sep 17 00:00:00 2001 From: natasha41575 Date: Wed, 19 Jan 2022 17:16:43 -0800 Subject: [PATCH] move origin to be an attribute of KustTarget --- api/internal/target/kusttarget.go | 89 +++++++++++-------- .../target/kusttarget_configplugin.go | 20 ++--- api/internal/target/vars_test.go | 7 +- api/types/kustomization.go | 4 +- 4 files changed, 66 insertions(+), 54 deletions(-) diff --git a/api/internal/target/kusttarget.go b/api/internal/target/kusttarget.go index 152862d65..c89e0f799 100644 --- a/api/internal/target/kusttarget.go +++ b/api/internal/target/kusttarget.go @@ -35,6 +35,7 @@ type KustTarget struct { validator ifc.Validator rFactory *resmap.Factory pLdr *loader.Loader + origin *resource.Origin } // NewKustTarget returns a new instance of KustTarget. @@ -122,7 +123,8 @@ func (kt *KustTarget) makeCustomizedResMap() (resmap.ResMap, error) { if len(kt.kustomization.BuildMetadata) != 0 { origin = &resource.Origin{} } - ra, err := kt.AccumulateTarget(origin) + kt.origin = origin + ra, err := kt.AccumulateTarget() if err != nil { return nil, err } @@ -174,20 +176,20 @@ func (kt *KustTarget) addHashesToNames( // through kustomization directories, it updates `origin.path` // accordingly. When a remote base is found, it updates `origin.repo` // and `origin.ref` accordingly. -func (kt *KustTarget) AccumulateTarget(origin *resource.Origin) ( +func (kt *KustTarget) AccumulateTarget() ( ra *accumulator.ResAccumulator, err error) { - return kt.accumulateTarget(accumulator.MakeEmptyAccumulator(), origin) + return kt.accumulateTarget(accumulator.MakeEmptyAccumulator()) } // ra should be empty when this KustTarget is a Kustomization, or the ra of the parent if this KustTarget is a Component // (or empty if the Component does not have a parent). -func (kt *KustTarget) accumulateTarget(ra *accumulator.ResAccumulator, origin *resource.Origin) ( +func (kt *KustTarget) accumulateTarget(ra *accumulator.ResAccumulator) ( resRa *accumulator.ResAccumulator, err error) { - ra, err = kt.accumulateResources(ra, kt.kustomization.Resources, origin) + ra, err = kt.accumulateResources(ra, kt.kustomization.Resources) if err != nil { return nil, errors.Wrap(err, "accumulating resources") } - ra, err = kt.accumulateComponents(ra, kt.kustomization.Components, origin) + ra, err = kt.accumulateComponents(ra, kt.kustomization.Components) if err != nil { return nil, errors.Wrap(err, "accumulating components") } @@ -211,11 +213,11 @@ func (kt *KustTarget) accumulateTarget(ra *accumulator.ResAccumulator, origin *r return nil, errors.Wrapf( err, "merging CRDs %v", crdTc) } - err = kt.runGenerators(ra, origin) + err = kt.runGenerators(ra) if err != nil { return nil, err } - err = kt.runTransformers(ra, origin) + err = kt.runTransformers(ra) if err != nil { return nil, err } @@ -249,15 +251,15 @@ func (kt *KustTarget) IgnoreLocal(ra *accumulator.ResAccumulator) error { } func (kt *KustTarget) runGenerators( - ra *accumulator.ResAccumulator, origin *resource.Origin) error { + ra *accumulator.ResAccumulator) error { var generators []*resmap.GeneratorWithProperties - gs, err := kt.configureBuiltinGenerators(origin) + gs, err := kt.configureBuiltinGenerators() if err != nil { return err } generators = append(generators, gs...) - gs, err = kt.configureExternalGenerators(origin) + gs, err = kt.configureExternalGenerators() if err != nil { return errors.Wrap(err, "loading generator plugins") } @@ -281,7 +283,7 @@ func (kt *KustTarget) runGenerators( return nil } -func (kt *KustTarget) configureExternalGenerators(origin *resource.Origin) ( +func (kt *KustTarget) configureExternalGenerators() ( []*resmap.GeneratorWithProperties, error) { ra := accumulator.MakeEmptyAccumulator() var generatorPaths []string @@ -294,31 +296,31 @@ func (kt *KustTarget) configureExternalGenerators(origin *resource.Origin) ( continue } // inline config, track the origin - if origin != nil { + if kt.origin != nil { resources := rm.Resources() for _, r := range resources { - r.SetOrigin(origin.Append(kt.kustFileName)) + r.SetOrigin(kt.origin.Append(kt.kustFileName)) rm.Replace(r) } } ra.AppendAll(rm) } - ra, err := kt.accumulateResources(ra, generatorPaths, origin) + ra, err := kt.accumulateResources(ra, generatorPaths) if err != nil { return nil, err } return kt.pLdr.LoadGenerators(kt.ldr, kt.validator, ra.ResMap()) } -func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator, origin *resource.Origin) error { +func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error { var r []*resmap.TransformerWithProperties tConfig := ra.GetTransformerConfig() - lts, err := kt.configureBuiltinTransformers(tConfig, origin) + lts, err := kt.configureBuiltinTransformers(tConfig) if err != nil { return err } r = append(r, lts...) - lts, err = kt.configureExternalTransformers(kt.kustomization.Transformers, origin) + lts, err = kt.configureExternalTransformers(kt.kustomization.Transformers) if err != nil { return err } @@ -330,7 +332,7 @@ func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator, origin *re return nil } -func (kt *KustTarget) configureExternalTransformers(transformers []string, origin *resource.Origin) ([]*resmap.TransformerWithProperties, error) { +func (kt *KustTarget) configureExternalTransformers(transformers []string) ([]*resmap.TransformerWithProperties, error) { ra := accumulator.MakeEmptyAccumulator() var transformerPaths []string for _, p := range transformers { @@ -342,16 +344,16 @@ func (kt *KustTarget) configureExternalTransformers(transformers []string, origi continue } // inline config, track the origin - if origin != nil { + if kt.origin != nil { resources := rm.Resources() for _, r := range resources { - r.SetOrigin(origin.Append(kt.kustFileName)) + r.SetOrigin(kt.origin.Append(kt.kustFileName)) rm.Replace(r) } } ra.AppendAll(rm) } - ra, err := kt.accumulateResources(ra, transformerPaths, origin) + ra, err := kt.accumulateResources(ra, transformerPaths) if err != nil { return nil, err } @@ -359,7 +361,7 @@ func (kt *KustTarget) configureExternalTransformers(transformers []string, origi } func (kt *KustTarget) runValidators(ra *accumulator.ResAccumulator) error { - validators, err := kt.configureExternalTransformers(kt.kustomization.Validators, nil) + validators, err := kt.configureExternalTransformers(kt.kustomization.Validators) if err != nil { return err } @@ -399,10 +401,10 @@ func (kt *KustTarget) removeValidatedByLabel(rm resmap.ResMap) error { // accumulateResources fills the given resourceAccumulator // with resources read from the given list of paths. func (kt *KustTarget) accumulateResources( - ra *accumulator.ResAccumulator, paths []string, origin *resource.Origin) (*accumulator.ResAccumulator, error) { + ra *accumulator.ResAccumulator, paths []string) (*accumulator.ResAccumulator, error) { for _, path := range paths { // try loading resource as file then as base (directory or git repository) - if errF := kt.accumulateFile(ra, path, origin); errF != nil { + if errF := kt.accumulateFile(ra, path); errF != nil { // not much we can do if the error is an HTTP error so we bail out if errors.Is(errF, load.ErrorHTTP) { return nil, errF @@ -412,10 +414,15 @@ func (kt *KustTarget) accumulateResources( return nil, errors.Wrapf( err, "accumulation err='%s'", errF.Error()) } - if origin != nil { - ra, err = kt.accumulateDirectory(ra, ldr, origin.Append(path), false) + // store the origin, we'll need it later + origin := kt.origin.Copy() + if kt.origin != nil { + kt.origin = kt.origin.Append(path) + ra, err = kt.accumulateDirectory(ra, ldr, false) + // after we are done recursing through the directory, reset the origin + kt.origin = &origin } else { - ra, err = kt.accumulateDirectory(ra, ldr, nil, false) + ra, err = kt.accumulateDirectory(ra, ldr, false) } if err != nil { return nil, errors.Wrapf( @@ -429,7 +436,7 @@ func (kt *KustTarget) accumulateResources( // accumulateResources fills the given resourceAccumulator // with resources read from the given list of paths. func (kt *KustTarget) accumulateComponents( - ra *accumulator.ResAccumulator, paths []string, origin *resource.Origin) (*accumulator.ResAccumulator, error) { + ra *accumulator.ResAccumulator, paths []string) (*accumulator.ResAccumulator, error) { for _, path := range paths { // Components always refer to directories ldr, errL := kt.ldr.New(path) @@ -437,10 +444,15 @@ func (kt *KustTarget) accumulateComponents( return nil, fmt.Errorf("loader.New %q", errL) } var errD error - if origin != nil { - ra, errD = kt.accumulateDirectory(ra, ldr, origin.Append(path), true) + // store the origin, we'll need it later + origin := kt.origin.Copy() + if kt.origin != nil { + kt.origin = kt.origin.Append(path) + ra, errD = kt.accumulateDirectory(ra, ldr, true) + // after we are done recursing through the directory, reset the origin + kt.origin = &origin } else { - ra, errD = kt.accumulateDirectory(ra, ldr, nil, true) + ra, errD = kt.accumulateDirectory(ra, ldr, true) } if errD != nil { return nil, fmt.Errorf("accumulateDirectory: %q", errD) @@ -450,7 +462,7 @@ func (kt *KustTarget) accumulateComponents( } func (kt *KustTarget) accumulateDirectory( - ra *accumulator.ResAccumulator, ldr ifc.Loader, origin *resource.Origin, isComponent bool) (*accumulator.ResAccumulator, error) { + ra *accumulator.ResAccumulator, ldr ifc.Loader, isComponent bool) (*accumulator.ResAccumulator, error) { defer ldr.Cleanup() subKt := NewKustTarget(ldr, kt.validator, kt.rFactory, kt.pLdr) err := subKt.Load() @@ -459,6 +471,7 @@ func (kt *KustTarget) accumulateDirectory( err, "couldn't make target for path '%s'", ldr.Root()) } subKt.kustomization.BuildMetadata = kt.kustomization.BuildMetadata + subKt.origin = kt.origin var bytes []byte path := ldr.Root() if openApiPath, exists := subKt.Kustomization().OpenAPI["path"]; exists { @@ -482,12 +495,12 @@ func (kt *KustTarget) accumulateDirectory( var subRa *accumulator.ResAccumulator if isComponent { // Components don't create a new accumulator: the kustomization directives are added to the current accumulator - subRa, err = subKt.accumulateTarget(ra, origin) + subRa, err = subKt.accumulateTarget(ra) ra = accumulator.MakeEmptyAccumulator() } else { // Child Kustomizations create a new accumulator which resolves their kustomization directives, which will later // be merged into the current accumulator. - subRa, err = subKt.AccumulateTarget(origin) + subRa, err = subKt.AccumulateTarget() } if err != nil { return nil, errors.Wrapf( @@ -502,13 +515,13 @@ func (kt *KustTarget) accumulateDirectory( } func (kt *KustTarget) accumulateFile( - ra *accumulator.ResAccumulator, path string, origin *resource.Origin) error { + ra *accumulator.ResAccumulator, path string) error { resources, err := kt.rFactory.FromFile(kt.ldr, path) if err != nil { return errors.Wrapf(err, "accumulating resources from '%s'", path) } - if origin != nil { - originAnno, err := origin.Append(path).String() + if kt.origin != nil { + originAnno, err := kt.origin.Append(path).String() if err != nil { return errors.Wrapf(err, "cannot add path annotation for '%s'", path) } diff --git a/api/internal/target/kusttarget_configplugin.go b/api/internal/target/kusttarget_configplugin.go index cdada6295..5ad0fc490 100644 --- a/api/internal/target/kusttarget_configplugin.go +++ b/api/internal/target/kusttarget_configplugin.go @@ -30,7 +30,7 @@ import ( // image tag transforms. In these cases, we'll need // N plugin instances with differing configurations. -func (kt *KustTarget) configureBuiltinGenerators(origin *resource.Origin) ( +func (kt *KustTarget) configureBuiltinGenerators() ( result []*resmap.GeneratorWithProperties, err error) { for _, bpt := range []builtinhelpers.BuiltinPluginType{ builtinhelpers.ConfigMapGenerator, @@ -44,11 +44,11 @@ func (kt *KustTarget) configureBuiltinGenerators(origin *resource.Origin) ( } var generatorOrigin *resource.Origin - if origin != nil { + if kt.origin != nil { generatorOrigin = &resource.Origin{ - Repo: origin.Repo, - Ref: origin.Ref, - ConfiguredIn: filepath.Join(origin.Path, kt.kustFileName), + Repo: kt.origin.Repo, + Ref: kt.origin.Ref, + ConfiguredIn: filepath.Join(kt.origin.Path, kt.kustFileName), ConfiguredBy: yaml.ResourceIdentifier{ TypeMeta: yaml.TypeMeta{ APIVersion: "builtin", @@ -66,7 +66,7 @@ func (kt *KustTarget) configureBuiltinGenerators(origin *resource.Origin) ( } func (kt *KustTarget) configureBuiltinTransformers( - tc *builtinconfig.TransformerConfig, origin *resource.Origin) ( + tc *builtinconfig.TransformerConfig) ( result []*resmap.TransformerWithProperties, err error) { for _, bpt := range []builtinhelpers.BuiltinPluginType{ builtinhelpers.PatchStrategicMergeTransformer, @@ -87,11 +87,11 @@ func (kt *KustTarget) configureBuiltinTransformers( return nil, err } var transformerOrigin *resource.Origin - if origin != nil { + if kt.origin != nil { transformerOrigin = &resource.Origin{ - Repo: origin.Repo, - Ref: origin.Ref, - ConfiguredIn: filepath.Join(origin.Path, kt.kustFileName), + Repo: kt.origin.Repo, + Ref: kt.origin.Ref, + ConfiguredIn: filepath.Join(kt.origin.Path, kt.kustFileName), ConfiguredBy: yaml.ResourceIdentifier{ TypeMeta: yaml.TypeMeta{ APIVersion: "builtin", diff --git a/api/internal/target/vars_test.go b/api/internal/target/vars_test.go index 1fb912471..901dcde90 100644 --- a/api/internal/target/vars_test.go +++ b/api/internal/target/vars_test.go @@ -8,7 +8,6 @@ import ( "strings" "testing" - "sigs.k8s.io/kustomize/api/resource" kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kyaml/resid" @@ -66,7 +65,7 @@ vars: apiVersion: v300 `) ra, err := makeAndLoadKustTarget( - t, th.GetFSys(), "/app").AccumulateTarget(&resource.Origin{}) + t, th.GetFSys(), "/app").AccumulateTarget() if err != nil { t.Fatalf("Err: %v", err) } @@ -121,7 +120,7 @@ resources: `) ra, err := makeAndLoadKustTarget( - t, th.GetFSys(), "/app/overlays/o2").AccumulateTarget(&resource.Origin{}) + t, th.GetFSys(), "/app/overlays/o2").AccumulateTarget() if err != nil { t.Fatalf("Err: %v", err) } @@ -178,7 +177,7 @@ resources: - ../o1 `) _, err := makeAndLoadKustTarget( - t, th.GetFSys(), "/app/overlays/o2").AccumulateTarget(&resource.Origin{}) + t, th.GetFSys(), "/app/overlays/o2").AccumulateTarget() if err == nil { t.Fatalf("expected var collision") } diff --git a/api/types/kustomization.go b/api/types/kustomization.go index 65e1d818d..fb14f191c 100644 --- a/api/types/kustomization.go +++ b/api/types/kustomization.go @@ -18,9 +18,9 @@ const ( ComponentKind = "Component" MetadataNamespacePath = "metadata/namespace" - OriginAnnotations = "originAnnotations" + OriginAnnotations = "originAnnotations" TransformerAnnotations = "transformerAnnotations" - ManagedByLabelOption = "managedByLabel" + ManagedByLabelOption = "managedByLabel" ) // Kustomization holds the information needed to generate customized k8s api resources.