Skip to content

Commit

Permalink
change cfg from string to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Eileen-Yu committed Feb 13, 2024
1 parent 45d75bc commit 9ef48b9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
"sigs.k8s.io/yaml"
)

var ApiFlags = []external.Flag{
Expand Down Expand Up @@ -58,40 +57,6 @@ func ApiCmd(pr *external.PluginRequest) external.PluginResponse {
flags.Parse(pr.Args)
number, _ := flags.GetInt("number")

// Update the project config with GVK
cfg := PluginConfig{}
err := yaml.Unmarshal([]byte(pr.Config), &cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}

// Create and append the new config info
newResource := ResourceData{
Group: "group",
Domain: "my.domain",
Version: "v1",
Kind: "Externalpluginsample",
}
cfg.Resources = append(cfg.Resources, newResource)

updatedConfig, err := yaml.Marshal(cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}

// Update the PluginResponse with the modified config string
pluginResponse.Config = string(updatedConfig)

apiFile := api.NewApiFile(api.WithNumber(number))

// Phase 2 Plugins uses the concept of a "universe" to represent the filesystem for a plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ limitations under the License.
package scaffolds

import (
"os"
"path/filepath"

"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/yaml"

"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
"v1/scaffolds/internal/templates"
)
Expand Down Expand Up @@ -61,45 +57,30 @@ func InitCmd(pr *external.PluginRequest) external.PluginResponse {
flags.Parse(pr.Args)
domain, _ := flags.GetString("domain")

// Update the project config with ProjectName
cfg := PluginConfig{}
err := yaml.Unmarshal([]byte(pr.Config), &cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}
// Update the project config
cfg := pr.Config

// Get current directory as the project name
cwd, err := os.Getwd()
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}
pluginChain := cfg.GetPluginChain()
pluginChain = append(pluginChain, pflag.Arg(0))
cfg.SetPluginChain(pluginChain)

dirName := filepath.Base(cwd)

cfg.ProjectName = dirName
if cfg.GetProjectName() == "" {
cfg.SetProjectName("externalplugin")
}

updatedConfig, err := yaml.Marshal(cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
rsc := resource.Resource{
GVK: resource.GVK{
Group: "group",
Version: "v1",
Kind: "Externalpluginsample",
Domain: "my.domain",
},
}

// Update the PluginResponse with the modified config string
pluginResponse.Config = string(updatedConfig)
cfg.UpdateResource(rsc)

// Update the PluginResponse with the modified updated config
pluginResponse.Config = cfg

initFile := templates.NewInitFile(templates.WithDomain(domain))

Expand Down

This file was deleted.

9 changes: 6 additions & 3 deletions pkg/plugin/external/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.

package external

import "sigs.k8s.io/kubebuilder/v3/pkg/plugin"
import (
v3cfg "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
)

// PluginRequest contains all information kubebuilder received from the CLI
// and plugins executed before it.
Expand All @@ -37,7 +40,7 @@ type PluginRequest struct {
Universe map[string]string `json:"universe"`

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

// PluginResponse is returned to kubebuilder by the plugin and contains all files
Expand Down Expand Up @@ -68,7 +71,7 @@ type PluginResponse struct {
Flags []Flag `json:"flags,omitempty"`

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

// Flag is meant to represent a CLI flag that is used by Kubebuilder to define flags that are parsed
Expand Down
4 changes: 4 additions & 0 deletions pkg/plugins/external/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
v3cfg "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
Expand Down Expand Up @@ -52,10 +53,13 @@ func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) {
}

func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
cfg := p.config.(*v3cfg.Cfg)

req := external.PluginRequest{
APIVersion: defaultAPIVersion,
Command: "create api",
Args: p.Args,
Config: *cfg,
}

err := handlePluginResponse(fs, req, p.Path, p)
Expand Down
16 changes: 2 additions & 14 deletions pkg/plugins/external/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"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 outputGetter ExecOutputGetter = &execOutputGetter{}
Expand Down Expand Up @@ -176,20 +175,9 @@ func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, p
return fmt.Errorf("error getting current directory: %v", err)
}

// TODO: for debug only, would delete it
fmt.Println("This is the received config from plugin response!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", res.Config)

// update the config
if res.Config != "" {
updatedConfig := p.GetConfig()

if err := yaml.Unmarshal([]byte(res.Config), updatedConfig); err != nil {
return fmt.Errorf("error unmarshalling the updated config from PluginResponse: %w", err)
}

if err := p.InjectConfig(updatedConfig); err != nil {
return fmt.Errorf("error injecting the updated config from PluginResponse: %w", err)
}
if err := p.InjectConfig(&res.Config); err != nil {
return fmt.Errorf("error injecting the updated config from PluginResponse: %w", err)
}

for filename, data := range res.Universe {
Expand Down
15 changes: 4 additions & 11 deletions pkg/plugins/external/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ limitations under the License.
package external

import (
"fmt"

"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
v3cfg "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"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{}
Expand All @@ -45,21 +43,16 @@ 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))
cfg := p.config.(*v3cfg.Cfg)

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

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

0 comments on commit 9ef48b9

Please sign in to comment.