Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
koba1t committed Jul 11, 2023
1 parent 8a195ab commit b8f2659
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 25 deletions.
33 changes: 33 additions & 0 deletions api/export/target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package export

import (
"fmt"

"sigs.k8s.io/kustomize/api/internal/accumulator"
"sigs.k8s.io/kustomize/api/internal/target"
"sigs.k8s.io/kustomize/api/resmap"
)

type KustTarget target.KustTarget

func (kt *KustTarget) AccumulateTarget() (*accumulator.ResAccumulator, error) {
internalKt := (*target.KustTarget)(kt)
ra, err := internalKt.AccumulateTarget()
if err != nil {
return nil, fmt.Errorf("%w", err)
}
return ra, nil
}

func (kt *KustTarget) Accumulate() (resmap.ResMap, error) {
internalKt := (*target.KustTarget)(kt)
_, err := internalKt.AccumulateTarget()
if err != nil {
return nil, fmt.Errorf("%w", err)
}
resmap, err := internalKt.MakeCustomizedResMap()
if err != nil {
return nil, fmt.Errorf("%w", err)
}
return resmap, nil
}
31 changes: 25 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.

13 changes: 8 additions & 5 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package target
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"

Expand Down Expand Up @@ -34,7 +35,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 @@ -198,10 +199,10 @@ func (kt *KustTarget) AccumulateTarget() (
// (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")
}
// 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)
Expand Down Expand Up @@ -562,6 +563,8 @@ func (kt *KustTarget) configureBuiltinPlugin(
p resmap.Configurable, c interface{}, bpt builtinhelpers.BuiltinPluginType) (err error) {
var y []byte
if c != nil {
j, err := json.Marshal(c)
log.Printf("config json: %s", j)
y, err = yaml.Marshal(c)
if err != nil {
return errors.WrapPrefixf(
Expand Down
13 changes: 11 additions & 2 deletions api/internal/target/kusttarget_configplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package target

import (
"fmt"
"log"
"path/filepath"

"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
Expand Down Expand Up @@ -109,22 +110,30 @@ 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
ResourceArgs
}
for _, args := range kt.kustomization.Resources {
c.resource = args
c.ResourceArgs.Resource = args
c.ResourceArgs.Kt = kt
p := f()

if err := kt.configureBuiltinPlugin(p, c, bpt); err != nil {
return nil, err
}
result = append(result, p)
log.Printf("configure Generator resource: %s kt: %s", c.Resource, c.Kt.kustFileName)
}
return
},
Expand Down
4 changes: 4 additions & 0 deletions api/resmap/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,7 @@ func (rmF *Factory) NewResMapFromRNodeSlice(s []*yaml.RNode) (ResMap, error) {
}
return newResMapFromResourceSlice(rs)
}

// func (rmF *Factory) FromResourceEntry(loader ifc.Loader, resource string, ktInterface interface{}) (ResMap, error) {
// return nil, nil
// }
31 changes: 25 additions & 6 deletions plugin/builtin/resourcegenerator/ResourceGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,42 @@
package main

import (
"fmt"
"log"

"sigs.k8s.io/kustomize/api/resmap"

"sigs.k8s.io/kustomize/kyaml/yaml"
)

type plugin struct {
h *resmap.PluginHelpers
resource string
resource string `json:"resource,omitempty" yaml:"resource,omitempty"`
kt KustTargetInterface `json:"kusttarget,omitempty" yaml:"kusttarget,omitempty"`
}

type KustTargetInterface interface {
// ab
Accumulate() (resmap.ResMap, error)
}

var KustomizePlugin plugin //nolint:gochecknoglobals

func (p *plugin) Config(h *resmap.PluginHelpers, config []byte) (err error) {
func (p *plugin) Config(h *resmap.PluginHelpers, config []byte) error {
p.h = h
return
if err := yaml.Unmarshal(config, p); err != nil {
return fmt.Errorf("failed to unmarshal ResourceGenerator config: %w", err)
}
log.Printf("generate resource by Config: %s, kt: %s", p.resource, p.kt)
return nil
}

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

resourceBytes := []byte(p.resource) //idiot
return p.h.ResmapFactory().NewResMapFromBytes(resourceBytes)
// return p.h.ResmapFactory().FromResourceEntry(p.h.Loader(), p.resource, p.kt)
log.Printf("generate resource: %s, kt: %s", p.resource, p.kt)
resmap, err := p.kt.Accumulate()
if err != nil {
return nil, fmt.Errorf("failed to Accumulate: %w", err)
}
return resmap, nil
}
36 changes: 31 additions & 5 deletions plugin/builtin/resourcegenerator/ResourceGenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,35 @@

package main_test

import (
"testing"
)
// func TestResourceGenerator(t *testing.T) {
// th := kusttest_test.MakeEnhancedHarness(t).
// PrepBuiltin("ResourceGenerator")
// defer th.Reset()

func TestResourceGenerator(t *testing.T) {
}
// th.WriteF("config.yaml", `
// apiVersion: v1
// kind: ConfigMap
// metadata:
// name: myMap
// data:
// COLOR: red
// FRUIT: apple
// `)
// rm := th.LoadAndRunGenerator(`
// apiVersion: builtin
// kind: ResourceGenerator
// metadata:
// name: myMap
// resource: config.yaml
// `)

// th.AssertActualEqualsExpected(rm, `
// apiVersion: v1
// kind: ConfigMap
// metadata:
// name: myMap
// data:
// COLOR: red
// FRUIT: apple
// `)
// }
2 changes: 1 addition & 1 deletion plugin/builtin/resourcegenerator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/kube-openapi v0.0.0-20230601164746-7562a1006961 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.2 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.2
sigs.k8s.io/yaml v1.3.0 // indirect
)

Expand Down

0 comments on commit b8f2659

Please sign in to comment.