Skip to content

Commit

Permalink
feat: pass config between KB and external plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Eileen-Yu committed Aug 10, 2023
1 parent 32720bc commit 18627f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pkg/plugin/external/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type PluginRequest struct {
// Universe represents the modified file contents that gets updated over a series of plugin runs
// across the plugin chain. Initially, it starts out as empty.
Universe map[string]string `json:"universe"`

// Config stores the project configuration file.
Config string `json:"config"`
}

// PluginResponse is returned to kubebuilder by the plugin and contains all files
Expand Down Expand Up @@ -63,6 +66,9 @@ type PluginResponse struct {
// Flags contains the plugin specific flags that the plugin returns to Kubebuilder when it receives
// a request for a list of supported flags from Kubebuilder
Flags []Flag `json:"flags,omitempty"`

// Config stores the project configuration file.
Config string `json:"config"`
}

// Flag is meant to represent a CLI flag that is used by Kubebuilder to define flags that are parsed
Expand Down
23 changes: 20 additions & 3 deletions pkg/plugins/external/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ limitations under the License.
package external

import (
"fmt"

Check failure on line 20 in pkg/plugins/external/init.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `goimports`-ed (goimports)
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
"sigs.k8s.io/yaml"
)

var _ plugin.InitSubcommand = &initSubcommand{}

type initSubcommand struct {
Path string
Args []string
Path string
Args []string
config config.Config
}

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

func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
configBytes, err := yaml.Marshal(p.config)
if err != nil {
return err
}

fmt.Println("Scaffolding with config:", string(configBytes))

req := external.PluginRequest{
APIVersion: defaultAPIVersion,
Command: "init",
Args: p.Args,
Config: string(configBytes),
}

err := handlePluginResponse(fs, req, p.Path)
err = handlePluginResponse(fs, req, p.Path)
if err != nil {
return err
}

return nil
}

func (p *initSubcommand) InjectConfig(c config.Config) error {
p.config = c
return nil
}

0 comments on commit 18627f0

Please sign in to comment.