Skip to content

Commit

Permalink
implement passing flags to external plugins
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
  • Loading branch information
everettraven committed Jun 6, 2022
1 parent 479cde1 commit fc7f1e0
Show file tree
Hide file tree
Showing 9 changed files with 461 additions and 3 deletions.
14 changes: 12 additions & 2 deletions pkg/cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,18 @@ func WithCompletion() Option {

// parseExternalPluginArgs returns the program arguments.
func parseExternalPluginArgs() (args []string) {
args = make([]string, len(os.Args)-1)
copy(args, os.Args[1:])
// Loop through os.Args and only get flags and their values that should be passed to the plugins
// this also removes the --plugins flag and its values from the list passed to the external plugin
for i := range os.Args {
if strings.Contains(os.Args[i], "--") && !strings.Contains(os.Args[i], "--plugins") {
args = append(args, os.Args[i])

// Don't go out of bounds and don't append the next value if it is a flag
if i+1 < len(os.Args) && !strings.Contains(os.Args[i+1], "--") {
args = append(args, os.Args[i+1])
}
}
}

return args
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/cli/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,43 @@ var _ = Describe("Discover external plugins", func() {

})
})

Context("parsing flags for external plugins", func() {
It("should only parse flags excluding the `--plugins` flag", func() {
// change the os.Args for this test and set them back after
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = []string{
"kubebuilder",
"init",
"--plugins",
"myexternalplugin/v1",
"--domain",
"example.com",
"--binary-flag",
"--license",
"apache2",
"--another-binary",
}

args := parseExternalPluginArgs()
Expect(args).Should(ContainElements(
"--domain",
"example.com",
"--binary-flag",
"--license",
"apache2",
"--another-binary",
))

Expect(args).ShouldNot(ContainElements(
"kubebuilder",
"init",
"--plugins",
"myexternalplugin/v1",
))
})
})
})

var _ = Describe("CLI options", func() {
Expand Down
25 changes: 25 additions & 0 deletions pkg/plugin/external/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,29 @@ type PluginResponse struct {

// ErrorMsgs contains the specific error messages of the plugin failures.
ErrorMsgs []string `json:"errorMsgs,omitempty"`

// 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"`
}

// Flag is meant to represent a CLI flag that is used by Kubebuilder to define flags that are parsed
// for use with an external plugin
type Flag struct {
// Name is the name that should be used when creating the flag.
// i.e a name of "domain" would become the CLI flag "--domain"
Name string

// Type is the type of flag that should be created. The types that
// Kubebuilder supports are: string, bool, int, and float.
// any value other than the supported will be defaulted to be a string
Type string

// Default is the default value that should be used for a flag.
// Kubebuilder will attempt to convert this value to the defined
// type for this flag.
Default string

// Usage is a description of the flag and when/why/what it is used for.
Usage string
}
6 changes: 6 additions & 0 deletions pkg/plugins/external/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package external

import (
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
Expand All @@ -39,6 +41,10 @@ func (p *createAPISubcommand) InjectResource(*resource.Resource) error {
return nil
}

func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) {
bindExternalPluginFlags(fs, "api", p.Path, p.Args)
}

func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
req := external.PluginRequest{
APIVersion: defaultAPIVersion,
Expand Down
6 changes: 6 additions & 0 deletions pkg/plugins/external/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package external

import (
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
Expand All @@ -29,6 +31,10 @@ type editSubcommand struct {
Args []string
}

func (p *editSubcommand) BindFlags(fs *pflag.FlagSet) {
bindExternalPluginFlags(fs, "edit", p.Path, p.Args)
}

func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
req := external.PluginRequest{
APIVersion: defaultAPIVersion,
Expand Down
Loading

0 comments on commit fc7f1e0

Please sign in to comment.