Skip to content

Commit d433c9e

Browse files
committed
feat: update config on KB side
1 parent 91afc94 commit d433c9e

File tree

5 files changed

+69
-13
lines changed

5 files changed

+69
-13
lines changed

pkg/plugins/external/api.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package external
1919
import (
2020
"github.com/spf13/pflag"
2121

22+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2223
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2324
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
2425
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
@@ -32,8 +33,9 @@ const (
3233
)
3334

3435
type createAPISubcommand struct {
35-
Path string
36-
Args []string
36+
Path string
37+
Args []string
38+
config config.Config
3739
}
3840

3941
func (p *createAPISubcommand) InjectResource(*resource.Resource) error {
@@ -56,10 +58,19 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
5658
Args: p.Args,
5759
}
5860

59-
err := handlePluginResponse(fs, req, p.Path)
61+
err := handlePluginResponse(fs, req, p.Path, p)
6062
if err != nil {
6163
return err
6264
}
6365

6466
return nil
6567
}
68+
69+
func (p *createAPISubcommand) InjectConfig(c config.Config) error {
70+
p.config = c
71+
return nil
72+
}
73+
74+
func (p *createAPISubcommand) GetConfig() config.Config {
75+
return p.config
76+
}

pkg/plugins/external/edit.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package external
1919
import (
2020
"github.com/spf13/pflag"
2121

22+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2223
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2324
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
2425
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
@@ -27,8 +28,9 @@ import (
2728
var _ plugin.EditSubcommand = &editSubcommand{}
2829

2930
type editSubcommand struct {
30-
Path string
31-
Args []string
31+
Path string
32+
Args []string
33+
config config.Config
3234
}
3335

3436
func (p *editSubcommand) UpdateMetadata(_ plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
@@ -46,10 +48,19 @@ func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
4648
Args: p.Args,
4749
}
4850

49-
err := handlePluginResponse(fs, req, p.Path)
51+
err := handlePluginResponse(fs, req, p.Path, p)
5052
if err != nil {
5153
return err
5254
}
5355

5456
return nil
5557
}
58+
59+
func (p *editSubcommand) InjectConfig(c config.Config) error {
60+
p.config = c
61+
return nil
62+
}
63+
64+
func (p *editSubcommand) GetConfig() config.Config {
65+
return p.config
66+
}

pkg/plugins/external/helpers.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ import (
3030

3131
"github.com/spf13/afero"
3232
"github.com/spf13/pflag"
33+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
3334
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
3435
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
3536
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
37+
"sigs.k8s.io/yaml"
3638
)
3739

3840
var outputGetter ExecOutputGetter = &execOutputGetter{}
@@ -51,6 +53,12 @@ type ExecOutputGetter interface {
5153

5254
type execOutputGetter struct{}
5355

56+
// PluginConfigHandler is an interface to update the config modified by external plugin.
57+
type PluginConfigHandler interface {
58+
GetConfig() config.Config
59+
InjectConfig(config.Config) error
60+
}
61+
5462
func (e *execOutputGetter) GetExecOutput(request []byte, path string) ([]byte, error) {
5563
cmd := exec.Command(path) //nolint:gosec
5664
cmd.Stdin = bytes.NewBuffer(request)
@@ -149,7 +157,7 @@ func getUniverseMap(fs machinery.Filesystem) (map[string]string, error) {
149157
return universe, nil
150158
}
151159

152-
func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, path string) error {
160+
func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, path string, p PluginConfigHandler) error {
153161
var err error
154162

155163
req.Universe, err = getUniverseMap(fs)
@@ -167,8 +175,19 @@ func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, p
167175
return fmt.Errorf("error getting current directory: %v", err)
168176
}
169177

170-
// TODO: should handle updated PluginResponse.Config shown as below
171-
fmt.Println(res.Config)
178+
// TODO: for debug only, would delete it
179+
fmt.Println("This is the received config from plugin response!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", res.Config)
180+
181+
// update the config
182+
if res.Config != "" {
183+
updatedConfig := p.GetConfig()
184+
185+
if err := yaml.Unmarshal([]byte(res.Config), updatedConfig); err != nil {
186+
return fmt.Errorf("error unmarshalling the updated config from PluginResponse: %w", err)
187+
}
188+
189+
p.InjectConfig(updatedConfig)
190+
}
172191

173192
for filename, data := range res.Universe {
174193
path := filepath.Join(currentDir, filename)

pkg/plugins/external/init.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
5858
Config: string(configBytes),
5959
}
6060

61-
err = handlePluginResponse(fs, req, p.Path)
61+
err = handlePluginResponse(fs, req, p.Path, p)
6262
if err != nil {
6363
return err
6464
}
@@ -70,3 +70,7 @@ func (p *initSubcommand) InjectConfig(c config.Config) error {
7070
p.config = c
7171
return nil
7272
}
73+
74+
func (p *initSubcommand) GetConfig() config.Config {
75+
return p.config
76+
}

pkg/plugins/external/webhook.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package external
1919
import (
2020
"github.com/spf13/pflag"
2121

22+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2223
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2324
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
2425
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
@@ -28,8 +29,9 @@ import (
2829
var _ plugin.CreateWebhookSubcommand = &createWebhookSubcommand{}
2930

3031
type createWebhookSubcommand struct {
31-
Path string
32-
Args []string
32+
Path string
33+
Args []string
34+
config config.Config
3335
}
3436

3537
func (p *createWebhookSubcommand) InjectResource(*resource.Resource) error {
@@ -52,10 +54,19 @@ func (p *createWebhookSubcommand) Scaffold(fs machinery.Filesystem) error {
5254
Args: p.Args,
5355
}
5456

55-
err := handlePluginResponse(fs, req, p.Path)
57+
err := handlePluginResponse(fs, req, p.Path, p)
5658
if err != nil {
5759
return err
5860
}
5961

6062
return nil
6163
}
64+
65+
func (p *createWebhookSubcommand) InjectConfig(c config.Config) error {
66+
p.config = c
67+
return nil
68+
}
69+
70+
func (p *createWebhookSubcommand) GetConfig() config.Config {
71+
return p.config
72+
}

0 commit comments

Comments
 (0)