Skip to content

Commit 18627f0

Browse files
committed
feat: pass config between KB and external plugin
1 parent 32720bc commit 18627f0

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

pkg/plugin/external/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ type PluginRequest struct {
3535
// Universe represents the modified file contents that gets updated over a series of plugin runs
3636
// across the plugin chain. Initially, it starts out as empty.
3737
Universe map[string]string `json:"universe"`
38+
39+
// Config stores the project configuration file.
40+
Config string `json:"config"`
3841
}
3942

4043
// PluginResponse is returned to kubebuilder by the plugin and contains all files
@@ -63,6 +66,9 @@ type PluginResponse struct {
6366
// Flags contains the plugin specific flags that the plugin returns to Kubebuilder when it receives
6467
// a request for a list of supported flags from Kubebuilder
6568
Flags []Flag `json:"flags,omitempty"`
69+
70+
// Config stores the project configuration file.
71+
Config string `json:"config"`
6672
}
6773

6874
// Flag is meant to represent a CLI flag that is used by Kubebuilder to define flags that are parsed

pkg/plugins/external/init.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ limitations under the License.
1717
package external
1818

1919
import (
20+
"fmt"
2021
"github.com/spf13/pflag"
2122

23+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2224
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2325
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
2426
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
27+
"sigs.k8s.io/yaml"
2528
)
2629

2730
var _ plugin.InitSubcommand = &initSubcommand{}
2831

2932
type initSubcommand struct {
30-
Path string
31-
Args []string
33+
Path string
34+
Args []string
35+
config config.Config
3236
}
3337

3438
func (p *initSubcommand) UpdateMetadata(_ plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
@@ -40,16 +44,29 @@ func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {
4044
}
4145

4246
func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
47+
configBytes, err := yaml.Marshal(p.config)
48+
if err != nil {
49+
return err
50+
}
51+
52+
fmt.Println("Scaffolding with config:", string(configBytes))
53+
4354
req := external.PluginRequest{
4455
APIVersion: defaultAPIVersion,
4556
Command: "init",
4657
Args: p.Args,
58+
Config: string(configBytes),
4759
}
4860

49-
err := handlePluginResponse(fs, req, p.Path)
61+
err = handlePluginResponse(fs, req, p.Path)
5062
if err != nil {
5163
return err
5264
}
5365

5466
return nil
5567
}
68+
69+
func (p *initSubcommand) InjectConfig(c config.Config) error {
70+
p.config = c
71+
return nil
72+
}

0 commit comments

Comments
 (0)