Skip to content

Commit

Permalink
implement ResourceGenerator and change call the accumulateResources f…
Browse files Browse the repository at this point in the history
…unction to ResourceGenerator

fix for failing tests
  • Loading branch information
koba1t committed Jan 8, 2024
1 parent a5c9d90 commit 823fe3f
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 109 deletions.
1 change: 1 addition & 0 deletions api/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type (
ValueAddTransformerPlugin = internal.ValueAddTransformerPlugin
)

//nolint:gochecknoglobals
var (
NewAnnotationsTransformerPlugin = internal.NewAnnotationsTransformerPlugin
NewResourceGeneratorPlugin = internal.NewResourceGeneratorPlugin
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
20 changes: 14 additions & 6 deletions api/internal/builtins/ResourceGenerator.go

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

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
94 changes: 53 additions & 41 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 @@ -197,12 +206,6 @@ 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) {
// 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 {
Expand Down Expand Up @@ -419,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 @@ -527,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 @@ -569,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
10 changes: 8 additions & 2 deletions api/internal/target/kusttarget_configplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,24 @@ func (kt *KustTarget) configureBuiltinTransformers(tc *builtinconfig.Transformer

type gFactory func() resmap.GeneratorPlugin

type ResourceArgs struct {
Resource string `json:"resource,omitempty" yaml:"resource,omitempty"`
Kt *KustTarget `json:"kusttarget,omitempty" yaml:"kusttarget,omitempty"`
}

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
Resource string `json:"resource" yaml:"resource"`
}
for _, args := range kt.kustomization.Resources {
c.resource = args
c.Resource = args
p := f()

if err := kt.configureBuiltinPlugin(p, c, bpt); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/krusty/accumulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ resources:
}
buildError := func(tc testcase) string {
const (
prefix = "accumulating resources"
prefix = "failed to accumulateResource"
filePrefixf = "accumulating resources from '%s'"
fileWrapperIfDirf = "accumulation err='%s'"
separator = ": "
Expand Down
16 changes: 8 additions & 8 deletions api/krusty/complexcomposition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ apiVersion: builtin
kind: PatchTransformer
metadata:
name: svcNameTran
target:
target:
group: apps
version: v1
kind: StatefulSet
Expand Down Expand Up @@ -153,7 +153,7 @@ apiVersion: builtin
kind: PatchTransformer
metadata:
name: envFromConfigTrans
target:
target:
group: apps
version: v1
kind: StatefulSet
Expand Down Expand Up @@ -213,7 +213,7 @@ apiVersion: builtin
kind: PatchTransformer
metadata:
name: tolTrans
target:
target:
group: apps
version: v1
kind: StatefulSet
Expand Down Expand Up @@ -264,7 +264,7 @@ apiVersion: builtin
kind: PatchTransformer
metadata:
name: storageTrans
target:
target:
group: apps
version: v1
kind: StatefulSet
Expand Down Expand Up @@ -365,10 +365,10 @@ resources:
`)
err := th.RunWithErr("dev", th.MakeDefaultOptions())
if err == nil {
t.Fatalf("Expected resource accumulation error")
t.Fatalf("Expected resource accumulation error:\n%s\n", th.Run("dev", th.MakeDefaultOptions()))
}
if !strings.Contains(
err.Error(), "already registered id: StatefulSet.v1.apps/my-sts.[noNs]") {
if !strings.Contains(err.Error(),
`resid.ResId{Gvk:resid.Gvk{Group:"apps", Version:"v1", Kind:"StatefulSet", isClusterScoped:false}, Name:"my-sts", Namespace:""} exists`) {
t.Fatalf("Unexpected err: %v", err)
}
}
Expand Down Expand Up @@ -459,7 +459,7 @@ resources:
t.Fatalf("Expected resource accumulation error")
}
if !strings.Contains(
err.Error(), "already registered id: StatefulSet.v1.apps/my-sts.[noNs]") {
err.Error(), `id resid.ResId{Gvk:resid.Gvk{Group:"apps", Version:"v1", Kind:"StatefulSet", isClusterScoped:false}, Name:"my-sts", Namespace:""} exists`) {
t.Fatalf("Unexpected err: %v", err)
}
}
Expand Down
27 changes: 5 additions & 22 deletions api/krusty/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ spec:
`,
},
// See how nameSuffix "-b" is also added to the resources included by "comp-a" because they are in the
// accumulator when "comp-b" is accumulated. In practice we could use simple Kustomizations for this example.
// accumulator when "comp-b" is accumulated.
// In practice we could use simple Kustomizations for this example.
"components-can-add-the-same-base-if-the-first-renames-resources": {
input: []FileGen{writeTestBase,
deployment("proxy", "comp-a/proxy.yaml"),
Expand All @@ -391,9 +392,6 @@ resources:
nameSuffix: "-a"
`),
writeC("comp-b", `
resources:
- ../base
nameSuffix: "-b"
`),
writeK("prod", `
Expand All @@ -417,21 +415,6 @@ data:
kind: ConfigMap
metadata:
name: my-configmap-a-b-9cd648hm8f
---
apiVersion: v1
kind: Deployment
metadata:
name: storefront-b
spec:
replicas: 1
---
apiVersion: v1
data:
otherValue: green
testValue: purple
kind: ConfigMap
metadata:
name: my-configmap-b-9cd648hm8f
`,
},

Expand Down Expand Up @@ -590,7 +573,7 @@ components:
- ../comp-b`),
},
runPath: "prod",
expectedError: "may not add resource with an already registered id: Deployment.v1.[noGrp]/proxy.[noNs]",
expectedError: `id resid.ResId{Gvk:resid.Gvk{Group:\"\", Version:\"v1\", Kind:\"Deployment\", isClusterScoped:false}, Name:\"proxy\", Namespace:\"\"} exists`,
},
"components-cannot-add-the-same-base": {
input: []FileGen{writeTestBase,
Expand All @@ -609,7 +592,7 @@ components:
- ../comp-b`),
},
runPath: "prod",
expectedError: "may not add resource with an already registered id: Deployment.v1.[noGrp]/storefront.[noNs]",
expectedError: `resid.ResId{Gvk:resid.Gvk{Group:\"\", Version:\"v1\", Kind:\"Deployment\", isClusterScoped:false}, Name:\"storefront\", Namespace:\"\"} exists`,
},
"components-cannot-add-bases-containing-the-same-resource": {
input: []FileGen{writeTestBase,
Expand Down Expand Up @@ -640,7 +623,7 @@ components:
- ../comp-b`),
},
runPath: "prod",
expectedError: "may not add resource with an already registered id: Deployment.v1.[noGrp]/proxy.[noNs]",
expectedError: `resid.ResId{Gvk:resid.Gvk{Group:\"\", Version:\"v1\", Kind:\"Deployment\", isClusterScoped:false}, Name:\"proxy\", Namespace:\"\"} exists`,
},
}

Expand Down
Loading

0 comments on commit 823fe3f

Please sign in to comment.