Skip to content

Commit

Permalink
convert resources to ResourceGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
koba1t committed Jul 5, 2023
1 parent 9e42f8d commit 8a195ab
Show file tree
Hide file tree
Showing 38 changed files with 290 additions and 43 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
2 changes: 2 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 @@ -30,6 +31,7 @@ type (

var (
NewAnnotationsTransformerPlugin = internal.NewAnnotationsTransformerPlugin
NewResourceGeneratorPlugin = internal.NewResourceGeneratorPlugin
NewConfigMapGeneratorPlugin = internal.NewConfigMapGeneratorPlugin
NewHashTransformerPlugin = internal.NewHashTransformerPlugin
NewHelmChartInflationGeneratorPlugin = internal.NewHelmChartInflationGeneratorPlugin
Expand Down
28 changes: 28 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
44 changes: 23 additions & 21 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,34 +196,33 @@ 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)
func (kt *KustTarget) accumulateTarget(ra *accumulator.ResAccumulator) (resRa *accumulator.ResAccumulator, err error) {
// read `resources`
ra, err = kt.accumulateResources(ra, kt.kustomization.Resources) // it needs to remove
if err != nil {
return nil, errors.WrapPrefixf(err, "accumulating resources")
}

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 +233,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
21 changes: 18 additions & 3 deletions api/internal/target/kusttarget_configplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
func (kt *KustTarget) configureBuiltinGenerators() (
result []*resmap.GeneratorWithProperties, err error) {
for _, bpt := range []builtinhelpers.BuiltinPluginType{
builtinhelpers.ResourceGenerator,
builtinhelpers.ConfigMapGenerator,
builtinhelpers.SecretGenerator,
builtinhelpers.HelmChartInflationGenerator,
Expand Down Expand Up @@ -66,9 +67,7 @@ func (kt *KustTarget) configureBuiltinGenerators() (
return result, nil
}

func (kt *KustTarget) configureBuiltinTransformers(
tc *builtinconfig.TransformerConfig) (
result []*resmap.TransformerWithProperties, err error) {
func (kt *KustTarget) configureBuiltinTransformers(tc *builtinconfig.TransformerConfig) (result []*resmap.TransformerWithProperties, err error) {
for _, bpt := range []builtinhelpers.BuiltinPluginType{
builtinhelpers.PatchStrategicMergeTransformer,
builtinhelpers.PatchTransformer,
Expand Down Expand Up @@ -114,6 +113,22 @@ var generatorConfigurators = map[builtinhelpers.BuiltinPluginType]func(
kt *KustTarget,
bpt builtinhelpers.BuiltinPluginType,
factory gFactory) (result []resmap.Generator, err error){
builtinhelpers.ResourceGenerator: func(kt *KustTarget, bpt builtinhelpers.BuiltinPluginType, f gFactory) (
result []resmap.Generator, err error) {
var c struct {
resource string
}
for _, args := range kt.kustomization.Resources {
c.resource = args
p := f()
if err := kt.configureBuiltinPlugin(p, c, bpt); err != nil {
return nil, err
}
result = append(result, p)
}
return
},

builtinhelpers.SecretGenerator: func(kt *KustTarget, bpt builtinhelpers.BuiltinPluginType, f gFactory) (
result []resmap.Generator, err error) {
var c struct {
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use (
./plugin/builtin/prefixtransformer
./plugin/builtin/replacementtransformer
./plugin/builtin/replicacounttransformer
./plugin/builtin/resourcegenerator
./plugin/builtin/secretgenerator
./plugin/builtin/sortordertransformer
./plugin/builtin/suffixtransformer
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/annotationstransformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/configmapgenerator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/hashtransformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/helmchartinflationgenerator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/imagetagtransformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/labeltransformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/patchjson6902transformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/patchtransformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/prefixtransformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/replacementtransformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/replicacounttransformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions plugin/builtin/resourcegenerator/Makefile
27 changes: 27 additions & 0 deletions plugin/builtin/resourcegenerator/ResourceGenerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

//go:generate pluginator
package main

import (
"sigs.k8s.io/kustomize/api/resmap"
)

type plugin struct {
h *resmap.PluginHelpers
resource string
}

var KustomizePlugin plugin //nolint:gochecknoglobals

func (p *plugin) Config(h *resmap.PluginHelpers, config []byte) (err error) {
p.h = h
return
}

func (p *plugin) Generate() (resmap.ResMap, error) {

resourceBytes := []byte(p.resource) //idiot
return p.h.ResmapFactory().NewResMapFromBytes(resourceBytes)
}
11 changes: 11 additions & 0 deletions plugin/builtin/resourcegenerator/ResourceGenerator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package main_test

import (
"testing"
)

func TestResourceGenerator(t *testing.T) {
}
Loading

0 comments on commit 8a195ab

Please sign in to comment.