Skip to content

Commit f17304e

Browse files
committed
implement ResourceGenerator and change call the accumulateResources function to ResourceGenerator
1 parent 358babe commit f17304e

File tree

16 files changed

+169
-83
lines changed

16 files changed

+169
-83
lines changed

api/builtins/builtins.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type (
2929
ValueAddTransformerPlugin = internal.ValueAddTransformerPlugin
3030
)
3131

32+
//nolint:gochecknoglobals
3233
var (
3334
NewAnnotationsTransformerPlugin = internal.NewAnnotationsTransformerPlugin
3435
NewResourceGeneratorPlugin = internal.NewResourceGeneratorPlugin

api/internal/accumulator/resaccumulator.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import (
1414
"sigs.k8s.io/kustomize/kyaml/resid"
1515
)
1616

17-
// ResAccumulator accumulates resources and the rules
18-
// used to customize those resources. It's a ResMap
19-
// plus stuff needed to modify the ResMap.
17+
// ResAccumulator accumulates resources and the rules used to customize those resources.
18+
// It's a ResMap plus stuff needed to modify the ResMap.
2019
type ResAccumulator struct {
2120
resMap resmap.ResMap
2221
tConfig *builtinconfig.TransformerConfig

api/internal/builtins/ResourceGenerator.go

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/internal/plugins/execplugin/execplugin_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ func TestExecPluginConfig(t *testing.T) {
2525
err := fSys.WriteFile("sed-input.txt", []byte(`
2626
s/$FOO/foo/g
2727
s/$BAR/bar baz/g
28-
\ \ \
29-
`))
28+
\ \ \ `))
3029
require.NoError(t, err)
3130
ldr, err := fLdr.NewLoader(
3231
fLdr.RestrictionRootOnly, filesys.Separator, fSys)
@@ -65,8 +64,7 @@ s/$BAR/bar baz/g
6564
t.Fatalf("unexpected err: %v", err)
6665
}
6766
err = p.Config(
68-
resmap.NewPluginHelpers(ldr, pvd.GetFieldValidator(), rf, pc),
69-
yaml)
67+
resmap.NewPluginHelpers(ldr, pvd.GetFieldValidator(), rf, pc), yaml)
7068
require.NoError(t, err)
7169

7270
expected := "someteam.example.com/v1/sedtransformer/SedTransformer"

api/internal/target/kusttarget.go

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type KustTarget struct {
3434
ldr ifc.Loader
3535
validator ifc.Validator
3636
rFactory *resmap.Factory
37-
pLdr *loader.Loader
37+
pLdr *loader.Loader // plugin loader
3838
origin *resource.Origin
3939
}
4040

@@ -176,6 +176,15 @@ func (kt *KustTarget) addHashesToNames(
176176
return ra.Transform(p)
177177
}
178178

179+
// AccumulateResource fills the given resourceAccumulator with resources read from the given path from external package.
180+
func (kt *KustTarget) AccumulateResource(path string) (rm resmap.ResMap, err error) {
181+
ra := accumulator.MakeEmptyAccumulator()
182+
if err := kt.accumulateResource(ra, path); err != nil {
183+
return nil, fmt.Errorf("failed to accumulateResource: %w", err)
184+
}
185+
return ra.ResMap(), nil
186+
}
187+
179188
// AccumulateTarget returns a new ResAccumulator,
180189
// holding customized resources and the data/rules used
181190
// to do so. The name back references and vars are
@@ -197,12 +206,6 @@ func (kt *KustTarget) AccumulateTarget() (
197206
// ra should be empty when this KustTarget is a Kustomization, or the ra of the parent if this KustTarget is a Component
198207
// (or empty if the Component does not have a parent).
199208
func (kt *KustTarget) accumulateTarget(ra *accumulator.ResAccumulator) (resRa *accumulator.ResAccumulator, err error) {
200-
// read `resources`
201-
ra, err = kt.accumulateResources(ra, kt.kustomization.Resources) // it needs to remove
202-
if err != nil {
203-
return nil, errors.WrapPrefixf(err, "accumulating resources")
204-
}
205-
206209
tConfig, err := builtinconfig.MakeTransformerConfig(
207210
kt.ldr, kt.kustomization.Configurations)
208211
if err != nil {
@@ -419,40 +422,50 @@ func (kt *KustTarget) removeValidatedByLabel(rm resmap.ResMap) error {
419422
func (kt *KustTarget) accumulateResources(
420423
ra *accumulator.ResAccumulator, paths []string) (*accumulator.ResAccumulator, error) {
421424
for _, path := range paths {
422-
// try loading resource as file then as base (directory or git repository)
423-
if errF := kt.accumulateFile(ra, path); errF != nil {
424-
// not much we can do if the error is an HTTP error so we bail out
425-
if errors.Is(errF, load.ErrHTTP) {
426-
return nil, errF
427-
}
428-
ldr, err := kt.ldr.New(path)
429-
if err != nil {
430-
if kusterr.IsMalformedYAMLError(errF) { // Some error occurred while tyring to decode YAML file
431-
return nil, errF
432-
}
433-
return nil, errors.WrapPrefixf(
434-
err, "accumulation err='%s'", errF.Error())
435-
}
436-
// store the origin, we'll need it later
437-
origin := kt.origin.Copy()
438-
if kt.origin != nil {
439-
kt.origin = kt.origin.Append(path)
440-
ra, err = kt.accumulateDirectory(ra, ldr, false)
441-
// after we are done recursing through the directory, reset the origin
442-
kt.origin = &origin
443-
} else {
444-
ra, err = kt.accumulateDirectory(ra, ldr, false)
425+
if err := kt.accumulateResource(ra, path); err != nil {
426+
return nil, err
427+
}
428+
}
429+
return ra, nil
430+
}
431+
432+
// accumulateResource fills the given resourceAccumulator with resources read from the given path.
433+
func (kt *KustTarget) accumulateResource(ra *accumulator.ResAccumulator, path string) error {
434+
// try loading resource as file then as base (directory or git repository)
435+
if errF := kt.accumulateFile(ra, path); errF != nil { //nolint:nestif
436+
// not much we can do if the error is an HTTP error so we bail out
437+
if errors.Is(errF, load.ErrHTTP) {
438+
return errF
439+
}
440+
ldr, err := kt.ldr.New(path)
441+
if err != nil {
442+
// Some error occurred while tyring to decode YAML file
443+
if kusterr.IsMalformedYAMLError(errF) {
444+
return errF
445445
}
446-
if err != nil {
447-
if kusterr.IsMalformedYAMLError(errF) { // Some error occurred while tyring to decode YAML file
448-
return nil, errF
449-
}
450-
return nil, errors.WrapPrefixf(
451-
err, "accumulation err='%s'", errF.Error())
446+
return errors.WrapPrefixf(
447+
err, "accumulation err='%s'", errF.Error())
448+
}
449+
// store the origin, we'll need it later
450+
origin := kt.origin.Copy()
451+
if kt.origin != nil {
452+
kt.origin = kt.origin.Append(path)
453+
_, err = kt.accumulateDirectory(ra, ldr, false)
454+
// after we are done recursing through the directory, reset the origin
455+
kt.origin = &origin
456+
} else {
457+
_, err = kt.accumulateDirectory(ra, ldr, false)
458+
}
459+
if err != nil {
460+
// Some error occurred while tyring to decode YAML file
461+
if kusterr.IsMalformedYAMLError(errF) {
462+
return errF
452463
}
464+
return errors.WrapPrefixf(
465+
err, "accumulation err='%s'", errF.Error())
453466
}
454467
}
455-
return ra, nil
468+
return nil
456469
}
457470

458471
// accumulateResources fills the given resourceAccumulator
@@ -527,8 +540,7 @@ func (kt *KustTarget) accumulateDirectory(
527540
return nil, errors.WrapPrefixf(
528541
err, "recursed accumulation of path '%s'", ldr.Root())
529542
}
530-
err = ra.MergeAccumulator(subRa)
531-
if err != nil {
543+
if err := ra.MergeAccumulator(subRa); err != nil {
532544
return nil, errors.WrapPrefixf(
533545
err, "recursed merging from path '%s'", ldr.Root())
534546
}
@@ -569,8 +581,8 @@ func (kt *KustTarget) configureBuiltinPlugin(
569581
}
570582
}
571583
err = p.Config(
572-
resmap.NewPluginHelpers(
573-
kt.ldr, kt.validator, kt.rFactory, kt.pLdr.Config()),
584+
resmap.NewPluginHelpersWithKt(
585+
kt.ldr, kt.validator, kt.rFactory, kt.pLdr.Config(), kt),
574586
y)
575587
if err != nil {
576588
return errors.WrapPrefixf(

api/internal/target/kusttarget_configplugin.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,24 @@ func (kt *KustTarget) configureBuiltinTransformers(tc *builtinconfig.Transformer
109109

110110
type gFactory func() resmap.GeneratorPlugin
111111

112+
type ResourceArgs struct {
113+
Resource string `json:"resource,omitempty" yaml:"resource,omitempty"`
114+
Kt *KustTarget `json:"kusttarget,omitempty" yaml:"kusttarget,omitempty"`
115+
}
116+
112117
var generatorConfigurators = map[builtinhelpers.BuiltinPluginType]func(
113118
kt *KustTarget,
114119
bpt builtinhelpers.BuiltinPluginType,
115120
factory gFactory) (result []resmap.Generator, err error){
116121
builtinhelpers.ResourceGenerator: func(kt *KustTarget, bpt builtinhelpers.BuiltinPluginType, f gFactory) (
117122
result []resmap.Generator, err error) {
118123
var c struct {
119-
resource string
124+
Resource string `json:"resource" yaml:"resource"`
120125
}
121126
for _, args := range kt.kustomization.Resources {
122-
c.resource = args
127+
c.Resource = args
123128
p := f()
129+
124130
if err := kt.configureBuiltinPlugin(p, c, bpt); err != nil {
125131
return nil, err
126132
}

api/krusty/accumulation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ resources:
200200
}
201201
buildError := func(tc testcase) string {
202202
const (
203-
prefix = "accumulating resources"
203+
prefix = "failed to accumulateResource"
204204
filePrefixf = "accumulating resources from '%s'"
205205
fileWrapperIfDirf = "accumulation err='%s'"
206206
separator = ": "

api/resmap/factory.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,7 @@ func (rmF *Factory) NewResMapFromRNodeSlice(s []*yaml.RNode) (ResMap, error) {
143143
}
144144
return newResMapFromResourceSlice(rs)
145145
}
146+
147+
// func (rmF *Factory) FromResourceEntry(loader ifc.Loader, resource string, ktInterface interface{}) (ResMap, error) {
148+
// return nil, nil
149+
// }

api/resmap/resmap.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package resmap
77

88
import (
9+
"fmt"
10+
911
"sigs.k8s.io/kustomize/api/ifc"
1012
"sigs.k8s.io/kustomize/api/resource"
1113
"sigs.k8s.io/kustomize/api/types"
@@ -49,9 +51,16 @@ type Configurable interface {
4951

5052
// NewPluginHelpers makes an instance of PluginHelpers.
5153
func NewPluginHelpers(
52-
ldr ifc.Loader, v ifc.Validator, rf *Factory,
53-
pc *types.PluginConfig) *PluginHelpers {
54-
return &PluginHelpers{ldr: ldr, v: v, rf: rf, pc: pc}
54+
ldr ifc.Loader, v ifc.Validator, rf *Factory, pc *types.PluginConfig,
55+
) *PluginHelpers {
56+
return &PluginHelpers{ldr: ldr, v: v, rf: rf, pc: pc, kt: nil} // TODO(koba1t): check to influence of kt is null
57+
}
58+
59+
// NewPluginHelpersWithKt makes an instance of PluginHelpers additional kt with args.
60+
func NewPluginHelpersWithKt(
61+
ldr ifc.Loader, v ifc.Validator, rf *Factory, pc *types.PluginConfig, kt KustTargetInterface,
62+
) *PluginHelpers {
63+
return &PluginHelpers{ldr: ldr, v: v, rf: rf, pc: pc, kt: kt}
5564
}
5665

5766
// PluginHelpers holds things that any or all plugins might need.
@@ -62,6 +71,21 @@ type PluginHelpers struct {
6271
v ifc.Validator
6372
rf *Factory
6473
pc *types.PluginConfig
74+
kt KustTargetInterface
75+
}
76+
77+
// KustTargetInterface is the interface for exec accumulate functions from external packages.
78+
type KustTargetInterface interface {
79+
AccumulateResource(path string) (ResMap, error)
80+
}
81+
82+
// AccumulateResources exec target.(*KustTarget).AccumulateResource()
83+
func (c *PluginHelpers) AccumulateResource(path string) (ResMap, error) {
84+
resmap, err := c.kt.AccumulateResource(path)
85+
if err != nil {
86+
return nil, fmt.Errorf("failed to AccumulateResource in internal `target` pkg: %w", err)
87+
}
88+
return resmap, nil
6589
}
6690

6791
func (c *PluginHelpers) GeneralConfig() *types.PluginConfig {

cmd/pluginator/internal/krmfunction/funcwrappersrc/go.mod.src

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.20
44

55
require (
66
github.com/spf13/cobra v1.4.0
7-
sigs.k8s.io/kustomize/api v0.12.1
8-
sigs.k8s.io/kustomize/kyaml v0.13.9
7+
sigs.k8s.io/kustomize/api v0.14.0
8+
sigs.k8s.io/kustomize/kyaml v0.14.3
99
sigs.k8s.io/yaml v1.2.0
1010
)

cmd/pluginator/internal/krmfunction/funcwrappersrc/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ import (
1515
"sigs.k8s.io/kustomize/kyaml/fn/framework"
1616
)
1717

18-
//nolint
1918
func main() {
2019
var plugin resmap.Configurable
2120
p := provider.NewDefaultDepProvider()
2221
resmapFactory := resmap.NewFactory(p.GetResourceFactory())
2322
pluginHelpers := resmap.NewPluginHelpers(
24-
nil, p.GetFieldValidator(), resmapFactory, types.DisabledPluginConfig())
23+
nil, p.GetFieldValidator(), resmapFactory, types.DisabledPluginConfig()) // TODO(koba1t): check to influence of kt is null
2524

2625
processor := framework.ResourceListProcessorFunc(func(resourceList *framework.ResourceList) error {
2726
resMap, err := resmapFactory.NewResMapFromRNodeSlice(resourceList.Items)

hack/for-each-module.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fi
1515

1616
cmd=$1
1717
skip_pattern="${2-}"
18-
expected_module_count=${3:-45}
18+
expected_module_count=${3:-46}
1919

2020
seen=()
2121
# Hack scripts must be run from the root of the repository.
@@ -25,11 +25,11 @@ export KUSTOMIZE_ROOT
2525
# verify all modules pass validation
2626
for i in $(find . -name go.mod -not -path "./site/*" -not -path "$skip_pattern"); do
2727
pushd .
28-
cd $(dirname $i);
28+
cd $(dirname "$i");
2929

3030
set +x
3131
dir=$(pwd)
32-
module="${dir#$KUSTOMIZE_ROOT}"
32+
module="${dir#"$KUSTOMIZE_ROOT"}"
3333
echo -e "\n----------------------------------------------------------"
3434
echo "Running command in $module"
3535
echo -e "----------------------------------------------------------"

hack/generateBuiltinKrmFunctions.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ builtinPlugins=(AnnotationsTransformer \
1515
PrefixSuffixTransformer \
1616
PrefixTransformer \
1717
SuffixTransformer \
18+
ResourceGenerator \
1819
ReplicaCountTransformer \
1920
SecretGenerator \
2021
ValueAddTransformer \
@@ -29,14 +30,14 @@ fi
2930

3031

3132
# Install pluginator
32-
pushd ../cmd/pluginator
33+
pushd ../cmd/pluginator || exit
3334
make install
34-
popd
35+
popd || exit
3536

3637

3738
for pluginName in ${builtinPlugins[@]}; do
38-
dirName=$(echo $pluginName | tr '[:upper:]' '[:lower:]')
39+
dirName=$(echo "$pluginName" | tr '[:upper:]' '[:lower:]')
3940
srcPath="$builtinPluginDir/$dirName/$pluginName.go"
4041
dstPath="$KRM_FUNCTION_DIR/$dirName"
41-
pluginator krm -i $srcPath -o $dstPath
42+
pluginator krm -i "$srcPath" -o "$dstPath"
4243
done

0 commit comments

Comments
 (0)