Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert resources to ResourceGenerator #5228

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile-plugins.mk
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ _builtinplugins = \
PatchTransformer.go \
PrefixTransformer.go \
SuffixTransformer.go \
ResourceGenerator.go \
ReplacementTransformer.go \
ReplicaCountTransformer.go \
SecretGenerator.go \
Expand Down Expand Up @@ -70,6 +71,7 @@ $(pGen)/PatchStrategicMergeTransformer.go: $(pSrc)/patchstrategicmergetransforme
$(pGen)/PatchTransformer.go: $(pSrc)/patchtransformer/PatchTransformer.go
$(pGen)/PrefixTransformer.go: $(pSrc)/prefixtransformer/PrefixTransformer.go
$(pGen)/SuffixTransformer.go: $(pSrc)/suffixtransformer/SuffixTransformer.go
$(pGen)/ResourceGenerator.go: $(pSrc)/resourcegenerator/ResourceGenerator.go
$(pGen)/ReplacementTransformer.go: $(pSrc)/replacementtransformer/ReplacementTransformer.go
$(pGen)/ReplicaCountTransformer.go: $(pSrc)/replicacounttransformer/ReplicaCountTransformer.go
$(pGen)/SecretGenerator.go: $(pSrc)/secretgenerator/SecretGenerator.go
Expand Down
3 changes: 3 additions & 0 deletions api/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type (
AnnotationsTransformerPlugin = internal.AnnotationsTransformerPlugin
ResourceGeneratorPlugin = internal.ResourceGeneratorPlugin
ConfigMapGeneratorPlugin = internal.ConfigMapGeneratorPlugin
HashTransformerPlugin = internal.HashTransformerPlugin
HelmChartInflationGeneratorPlugin = internal.HelmChartInflationGeneratorPlugin
Expand All @@ -28,8 +29,10 @@ type (
ValueAddTransformerPlugin = internal.ValueAddTransformerPlugin
)

//nolint:gochecknoglobals
var (
NewAnnotationsTransformerPlugin = internal.NewAnnotationsTransformerPlugin
NewResourceGeneratorPlugin = internal.NewResourceGeneratorPlugin
NewConfigMapGeneratorPlugin = internal.NewConfigMapGeneratorPlugin
NewHashTransformerPlugin = internal.NewHashTransformerPlugin
NewHelmChartInflationGeneratorPlugin = internal.NewHelmChartInflationGeneratorPlugin
Expand Down
5 changes: 2 additions & 3 deletions api/internal/accumulator/resaccumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
"sigs.k8s.io/kustomize/kyaml/resid"
)

// ResAccumulator accumulates resources and the rules
// used to customize those resources. It's a ResMap
// plus stuff needed to modify the ResMap.
// ResAccumulator accumulates resources and the rules used to customize those resources.
// It's a ResMap plus stuff needed to modify the ResMap.
type ResAccumulator struct {
resMap resmap.ResMap
tConfig *builtinconfig.TransformerConfig
Expand Down
36 changes: 36 additions & 0 deletions api/internal/builtins/ResourceGenerator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 20 additions & 19 deletions api/internal/plugins/builtinhelpers/builtinplugintype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/internal/plugins/builtinhelpers/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type BuiltinPluginType int
const (
Unknown BuiltinPluginType = iota
AnnotationsTransformer
ResourceGenerator
ConfigMapGenerator
IAMPolicyGenerator
HashTransformer
Expand Down Expand Up @@ -59,6 +60,7 @@ func GetBuiltinPluginType(n string) BuiltinPluginType {
}

var GeneratorFactories = map[BuiltinPluginType]func() resmap.GeneratorPlugin{
ResourceGenerator: builtins.NewResourceGeneratorPlugin,
ConfigMapGenerator: builtins.NewConfigMapGeneratorPlugin,
IAMPolicyGenerator: builtins.NewIAMPolicyGeneratorPlugin,
SecretGenerator: builtins.NewSecretGeneratorPlugin,
Expand Down
6 changes: 2 additions & 4 deletions api/internal/plugins/execplugin/execplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func TestExecPluginConfig(t *testing.T) {
err := fSys.WriteFile("sed-input.txt", []byte(`
s/$FOO/foo/g
s/$BAR/bar baz/g
\ \ \
`))
\ \ \ `))
require.NoError(t, err)
ldr, err := fLdr.NewLoader(
fLdr.RestrictionRootOnly, filesys.Separator, fSys)
Expand Down Expand Up @@ -65,8 +64,7 @@ s/$BAR/bar baz/g
t.Fatalf("unexpected err: %v", err)
}
err = p.Config(
resmap.NewPluginHelpers(ldr, pvd.GetFieldValidator(), rf, pc),
yaml)
resmap.NewPluginHelpers(ldr, pvd.GetFieldValidator(), rf, pc), yaml)
require.NoError(t, err)

expected := "someteam.example.com/v1/sedtransformer/SedTransformer"
Expand Down
132 changes: 73 additions & 59 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type KustTarget struct {
ldr ifc.Loader
validator ifc.Validator
rFactory *resmap.Factory
pLdr *loader.Loader
pLdr *loader.Loader // plugin loader
origin *resource.Origin
}

Expand Down Expand Up @@ -176,6 +176,15 @@ func (kt *KustTarget) addHashesToNames(
return ra.Transform(p)
}

// AccumulateResource fills the given resourceAccumulator with resources read from the given path from external package.
func (kt *KustTarget) AccumulateResource(path string) (rm resmap.ResMap, err error) {
ra := accumulator.MakeEmptyAccumulator()
if err := kt.accumulateResource(ra, path); err != nil {
return nil, fmt.Errorf("failed to accumulateResource: %w", err)
}
return ra.ResMap(), nil
}

// AccumulateTarget returns a new ResAccumulator,
// holding customized resources and the data/rules used
// to do so. The name back references and vars are
Expand All @@ -196,34 +205,27 @@ func (kt *KustTarget) AccumulateTarget() (

// 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) (
resRa *accumulator.ResAccumulator, err error) {
ra, err = kt.accumulateResources(ra, kt.kustomization.Resources)
if err != nil {
return nil, errors.WrapPrefixf(err, "accumulating resources")
}
func (kt *KustTarget) accumulateTarget(ra *accumulator.ResAccumulator) (resRa *accumulator.ResAccumulator, err error) {
tConfig, err := builtinconfig.MakeTransformerConfig(
kt.ldr, kt.kustomization.Configurations)
if err != nil {
return nil, err
}
err = ra.MergeConfig(tConfig)
if err != nil {
return nil, errors.WrapPrefixf(
err, "merging config %v", tConfig)
if err := ra.MergeConfig(tConfig); err != nil {
return nil, errors.WrapPrefixf(err, "merging config %v", tConfig)
}

// load CRDs schemas
crdTc, err := accumulator.LoadConfigFromCRDs(kt.ldr, kt.kustomization.Crds)
if err != nil {
return nil, errors.WrapPrefixf(
err, "loading CRDs %v", kt.kustomization.Crds)
return nil, errors.WrapPrefixf(err, "loading CRDs %v", kt.kustomization.Crds)
}
err = ra.MergeConfig(crdTc)
if err != nil {
return nil, errors.WrapPrefixf(
err, "merging CRDs %v", crdTc)
if err := ra.MergeConfig(crdTc); err != nil {
return nil, errors.WrapPrefixf(err, "merging CRDs %v", crdTc)
}
err = kt.runGenerators(ra)
if err != nil {

// exec Generators
if err := kt.runGenerators(ra); err != nil {
return nil, err
}

Expand All @@ -234,16 +236,19 @@ func (kt *KustTarget) accumulateTarget(ra *accumulator.ResAccumulator) (
return nil, errors.WrapPrefixf(err, "accumulating components")
}

err = kt.runTransformers(ra)
if err != nil {
// exec Transformers
if err := kt.runTransformers(ra); err != nil {
return nil, err
}
err = kt.runValidators(ra)
if err != nil {

// exec Validators
if err := kt.runValidators(ra); err != nil {
return nil, err
}
err = ra.MergeVars(kt.kustomization.Vars)
if err != nil {

// merging `vars`
//nolint:staticcheck
if err := ra.MergeVars(kt.kustomization.Vars); err != nil {
return nil, errors.WrapPrefixf(
err, "merging vars %v", kt.kustomization.Vars)
}
Expand Down Expand Up @@ -417,40 +422,50 @@ func (kt *KustTarget) removeValidatedByLabel(rm resmap.ResMap) error {
func (kt *KustTarget) accumulateResources(
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); errF != nil {
// not much we can do if the error is an HTTP error so we bail out
if errors.Is(errF, load.ErrHTTP) {
return nil, errF
}
ldr, err := kt.ldr.New(path)
if err != nil {
if kusterr.IsMalformedYAMLError(errF) { // Some error occurred while tyring to decode YAML file
return nil, errF
}
return nil, errors.WrapPrefixf(
err, "accumulation err='%s'", errF.Error())
}
// 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, false)
if err := kt.accumulateResource(ra, path); err != nil {
return nil, err
}
}
return ra, nil
}

// accumulateResource fills the given resourceAccumulator with resources read from the given path.
func (kt *KustTarget) accumulateResource(ra *accumulator.ResAccumulator, path string) error {
// try loading resource as file then as base (directory or git repository)
if errF := kt.accumulateFile(ra, path); errF != nil { //nolint:nestif
// not much we can do if the error is an HTTP error so we bail out
if errors.Is(errF, load.ErrHTTP) {
return errF
}
ldr, err := kt.ldr.New(path)
if err != nil {
// Some error occurred while tyring to decode YAML file
if kusterr.IsMalformedYAMLError(errF) {
return errF
}
if err != nil {
if kusterr.IsMalformedYAMLError(errF) { // Some error occurred while tyring to decode YAML file
return nil, errF
}
return nil, errors.WrapPrefixf(
err, "accumulation err='%s'", errF.Error())
return errors.WrapPrefixf(
err, "accumulation err='%s'", errF.Error())
}
// store the origin, we'll need it later
origin := kt.origin.Copy()
if kt.origin != nil {
kt.origin = kt.origin.Append(path)
_, err = kt.accumulateDirectory(ra, ldr, false)
// after we are done recursing through the directory, reset the origin
kt.origin = &origin
} else {
_, err = kt.accumulateDirectory(ra, ldr, false)
}
if err != nil {
// Some error occurred while tyring to decode YAML file
if kusterr.IsMalformedYAMLError(errF) {
return errF
}
return errors.WrapPrefixf(
err, "accumulation err='%s'", errF.Error())
}
}
return ra, nil
return nil
}

// accumulateResources fills the given resourceAccumulator
Expand Down Expand Up @@ -525,8 +540,7 @@ func (kt *KustTarget) accumulateDirectory(
return nil, errors.WrapPrefixf(
err, "recursed accumulation of path '%s'", ldr.Root())
}
err = ra.MergeAccumulator(subRa)
if err != nil {
if err := ra.MergeAccumulator(subRa); err != nil {
return nil, errors.WrapPrefixf(
err, "recursed merging from path '%s'", ldr.Root())
}
Expand Down Expand Up @@ -567,8 +581,8 @@ func (kt *KustTarget) configureBuiltinPlugin(
}
}
err = p.Config(
resmap.NewPluginHelpers(
kt.ldr, kt.validator, kt.rFactory, kt.pLdr.Config()),
resmap.NewPluginHelpersWithKt(
kt.ldr, kt.validator, kt.rFactory, kt.pLdr.Config(), kt),
y)
if err != nil {
return errors.WrapPrefixf(
Expand Down
Loading