Skip to content

Commit

Permalink
convert resources to ResourceGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleChimera committed Sep 28, 2024
1 parent 01cce4f commit b9e9e65
Show file tree
Hide file tree
Showing 30 changed files with 470 additions and 166 deletions.
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 @@ -31,8 +31,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 @@ -74,8 +73,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
23 changes: 12 additions & 11 deletions api/internal/plugins/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func (l *Loader) Config() *types.PluginConfig {
}

func (l *Loader) LoadGenerators(
ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap) (
ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap, kt resmap.KustTargetInterface) (
result []*resmap.GeneratorWithProperties, err error) {
for _, res := range rm.Resources() {
g, err := l.LoadGenerator(ldr, v, res)
g, err := l.LoadGenerator(ldr, v, res, kt)
if err != nil {
return nil, fmt.Errorf("failed to load generator: %w", err)
}
Expand All @@ -74,8 +74,8 @@ func (l *Loader) LoadGenerators(
}

func (l *Loader) LoadGenerator(
ldr ifc.Loader, v ifc.Validator, res *resource.Resource) (resmap.Generator, error) {
c, err := l.loadAndConfigurePlugin(ldr, v, res)
ldr ifc.Loader, v ifc.Validator, res *resource.Resource, kt resmap.KustTargetInterface) (resmap.Generator, error) {
c, err := l.loadAndConfigurePlugin(ldr, v, res, kt)
if err != nil {
return nil, err
}
Expand All @@ -87,10 +87,10 @@ func (l *Loader) LoadGenerator(
}

func (l *Loader) LoadTransformers(
ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap) ([]*resmap.TransformerWithProperties, error) {
ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap, kt resmap.KustTargetInterface) ([]*resmap.TransformerWithProperties, error) {
var result []*resmap.TransformerWithProperties
for _, res := range rm.Resources() {
t, err := l.LoadTransformer(ldr, v, res)
t, err := l.LoadTransformer(ldr, v, res, kt)
if err != nil {
return nil, err
}
Expand All @@ -104,8 +104,8 @@ func (l *Loader) LoadTransformers(
}

func (l *Loader) LoadTransformer(
ldr ifc.Loader, v ifc.Validator, res *resource.Resource) (*resmap.TransformerWithProperties, error) {
c, err := l.loadAndConfigurePlugin(ldr, v, res)
ldr ifc.Loader, v ifc.Validator, res *resource.Resource, kt resmap.KustTargetInterface) (*resmap.TransformerWithProperties, error) {
c, err := l.loadAndConfigurePlugin(ldr, v, res, kt)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -179,7 +179,9 @@ func isBuiltinPlugin(res *resource.Resource) bool {
func (l *Loader) loadAndConfigurePlugin(
ldr ifc.Loader,
v ifc.Validator,
res *resource.Resource) (c resmap.Configurable, err error) {
res *resource.Resource,
kt resmap.KustTargetInterface,
) (c resmap.Configurable, err error) {
if isBuiltinPlugin(res) {
switch l.pc.BpLoadingOptions {
case types.BploLoadFromFileSys:
Expand Down Expand Up @@ -214,7 +216,7 @@ func (l *Loader) loadAndConfigurePlugin(
if err != nil {
return nil, errors.WrapPrefixf(err, "marshalling yaml from res %s", res.OrgId())
}
err = c.Config(resmap.NewPluginHelpers(ldr, v, l.rf, l.pc), yaml)
err = c.Config(resmap.NewPluginHelpersWithKt(ldr, v, l.rf, l.pc, kt), yaml)
if err != nil {
return nil, errors.WrapPrefixf(
err, "plugin %s fails configuration", res.OrgId())
Expand Down Expand Up @@ -282,4 +284,3 @@ func (l *Loader) loadExecOrGoPlugin(resId resid.ResId) (resmap.Configurable, err
}
return c, nil
}

2 changes: 1 addition & 1 deletion api/internal/plugins/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestLoader(t *testing.T) {
t.Fatal("expect non-nil loader")
}
_, err = pLdr.LoadGenerators(
fLdr, valtest_test.MakeFakeValidator(), generatorConfigs)
fLdr, valtest_test.MakeFakeValidator(), generatorConfigs, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit b9e9e65

Please sign in to comment.