|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/spf13/pflag" |
| 5 | + "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external" |
| 6 | +) |
| 7 | + |
| 8 | +// flagsCmd handles all the logic for the `flags` subcommand of the sample external plugin. |
| 9 | +// In Kubebuilder's Phase 2 Plugins the `flags` subcommand is an optional subcommand for |
| 10 | +// external plugins to support. The `flags` subcommand allows for an external plugin |
| 11 | +// to provide Kubebuilder with a list of flags that the `init`, `create api`, `create webhook`, |
| 12 | +// and `edit` subcommands allow. This allows Kubebuilder to give an external plugin the ability |
| 13 | +// to feel like a native Kubebuilder plugin to a Kubebuilder user by only binding the supported |
| 14 | +// flags and failing early if an unknown flag is provided. |
| 15 | +func flagsCmd(pr *external.PluginRequest) external.PluginResponse { |
| 16 | + pluginResponse := external.PluginResponse{ |
| 17 | + APIVersion: "v1alpha1", |
| 18 | + Command: "flags", |
| 19 | + Universe: pr.Universe, |
| 20 | + Flags: []external.Flag{}, |
| 21 | + } |
| 22 | + |
| 23 | + // Here is an example of parsing multiple flags from a Kubebuilder external plugin request |
| 24 | + flagsToParse := pflag.NewFlagSet("flagsFlags", pflag.ContinueOnError) |
| 25 | + flagsToParse.Bool("init", false, "sets the init flag to true") |
| 26 | + flagsToParse.Bool("api", false, "sets the api flag to true") |
| 27 | + flagsToParse.Bool("webhook", false, "sets the webhook flag to true") |
| 28 | + |
| 29 | + flagsToParse.Parse(pr.Args) |
| 30 | + |
| 31 | + initFlag, _ := flagsToParse.GetBool("init") |
| 32 | + apiFlag, _ := flagsToParse.GetBool("api") |
| 33 | + webhookFlag, _ := flagsToParse.GetBool("webhook") |
| 34 | + |
| 35 | + // The Phase 2 Plugins implementation will only ever pass a single boolean flag |
| 36 | + // argument in the JSON request `args` field. The flag will be `--init` if it is |
| 37 | + // attempting to get the flags for the `init` subcommand, `--api` for `create api`, |
| 38 | + // `--webhook` for `create webhook`, and `--edit` for `edit` |
| 39 | + if initFlag { |
| 40 | + // Add a flag to the JSON response `flags` field that Kubebuilder reads |
| 41 | + // to ensure it binds to the flags given in the response. |
| 42 | + pluginResponse.Flags = append(pluginResponse.Flags, external.Flag{ |
| 43 | + Name: "domain", |
| 44 | + Type: "string", |
| 45 | + Default: "example.domain.com", |
| 46 | + Usage: "sets the domain added in the scaffolded initFile.txt", |
| 47 | + }) |
| 48 | + } else if apiFlag { |
| 49 | + pluginResponse.Flags = append(pluginResponse.Flags, external.Flag{ |
| 50 | + Name: "number", |
| 51 | + Type: "int", |
| 52 | + Default: "1", |
| 53 | + Usage: "set a number to be added in the scaffolded apiFile.txt", |
| 54 | + }) |
| 55 | + } else if webhookFlag { |
| 56 | + pluginResponse.Flags = append(pluginResponse.Flags, external.Flag{ |
| 57 | + Name: "hooked", |
| 58 | + Type: "bool", |
| 59 | + Default: "false", |
| 60 | + Usage: "add the word `hooked` to the end of the scaffolded webhookFile.txt", |
| 61 | + }) |
| 62 | + } else { |
| 63 | + pluginResponse.Error = true |
| 64 | + pluginResponse.ErrorMsgs = []string{ |
| 65 | + "unrecognized flag", |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return pluginResponse |
| 70 | +} |
0 commit comments