Skip to content

Commit d0a8ac6

Browse files
authored
Merge pull request #2693 from everettraven/feature/p2p-flags
✨ external plugins(plugin phase 2): add support to pass CLI flags
2 parents 8b0f136 + fc7f1e0 commit d0a8ac6

File tree

9 files changed

+461
-3
lines changed

9 files changed

+461
-3
lines changed

pkg/cli/options.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,18 @@ func WithCompletion() Option {
155155

156156
// parseExternalPluginArgs returns the program arguments.
157157
func parseExternalPluginArgs() (args []string) {
158-
args = make([]string, len(os.Args)-1)
159-
copy(args, os.Args[1:])
158+
// Loop through os.Args and only get flags and their values that should be passed to the plugins
159+
// this also removes the --plugins flag and its values from the list passed to the external plugin
160+
for i := range os.Args {
161+
if strings.Contains(os.Args[i], "--") && !strings.Contains(os.Args[i], "--plugins") {
162+
args = append(args, os.Args[i])
163+
164+
// Don't go out of bounds and don't append the next value if it is a flag
165+
if i+1 < len(os.Args) && !strings.Contains(os.Args[i+1], "--") {
166+
args = append(args, os.Args[i+1])
167+
}
168+
}
169+
}
160170

161171
return args
162172
}

pkg/cli/options_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,43 @@ var _ = Describe("Discover external plugins", func() {
283283

284284
})
285285
})
286+
287+
Context("parsing flags for external plugins", func() {
288+
It("should only parse flags excluding the `--plugins` flag", func() {
289+
// change the os.Args for this test and set them back after
290+
oldArgs := os.Args
291+
defer func() { os.Args = oldArgs }()
292+
os.Args = []string{
293+
"kubebuilder",
294+
"init",
295+
"--plugins",
296+
"myexternalplugin/v1",
297+
"--domain",
298+
"example.com",
299+
"--binary-flag",
300+
"--license",
301+
"apache2",
302+
"--another-binary",
303+
}
304+
305+
args := parseExternalPluginArgs()
306+
Expect(args).Should(ContainElements(
307+
"--domain",
308+
"example.com",
309+
"--binary-flag",
310+
"--license",
311+
"apache2",
312+
"--another-binary",
313+
))
314+
315+
Expect(args).ShouldNot(ContainElements(
316+
"kubebuilder",
317+
"init",
318+
"--plugins",
319+
"myexternalplugin/v1",
320+
))
321+
})
322+
})
286323
})
287324

288325
var _ = Describe("CLI options", func() {

pkg/plugin/external/types.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,29 @@ type PluginResponse struct {
5757

5858
// ErrorMsgs contains the specific error messages of the plugin failures.
5959
ErrorMsgs []string `json:"errorMsgs,omitempty"`
60+
61+
// Flags contains the plugin specific flags that the plugin returns to Kubebuilder when it receives
62+
// a request for a list of supported flags from Kubebuilder
63+
Flags []Flag `json:"flags,omitempty"`
64+
}
65+
66+
// Flag is meant to represent a CLI flag that is used by Kubebuilder to define flags that are parsed
67+
// for use with an external plugin
68+
type Flag struct {
69+
// Name is the name that should be used when creating the flag.
70+
// i.e a name of "domain" would become the CLI flag "--domain"
71+
Name string
72+
73+
// Type is the type of flag that should be created. The types that
74+
// Kubebuilder supports are: string, bool, int, and float.
75+
// any value other than the supported will be defaulted to be a string
76+
Type string
77+
78+
// Default is the default value that should be used for a flag.
79+
// Kubebuilder will attempt to convert this value to the defined
80+
// type for this flag.
81+
Default string
82+
83+
// Usage is a description of the flag and when/why/what it is used for.
84+
Usage string
6085
}

pkg/plugins/external/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package external
1818

1919
import (
20+
"github.com/spf13/pflag"
21+
2022
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2123
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
2224
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
@@ -39,6 +41,10 @@ func (p *createAPISubcommand) InjectResource(*resource.Resource) error {
3941
return nil
4042
}
4143

44+
func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) {
45+
bindExternalPluginFlags(fs, "api", p.Path, p.Args)
46+
}
47+
4248
func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
4349
req := external.PluginRequest{
4450
APIVersion: defaultAPIVersion,

pkg/plugins/external/edit.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package external
1818

1919
import (
20+
"github.com/spf13/pflag"
21+
2022
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2123
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
2224
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
@@ -29,6 +31,10 @@ type editSubcommand struct {
2931
Args []string
3032
}
3133

34+
func (p *editSubcommand) BindFlags(fs *pflag.FlagSet) {
35+
bindExternalPluginFlags(fs, "edit", p.Path, p.Args)
36+
}
37+
3238
func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
3339
req := external.PluginRequest{
3440
APIVersion: defaultAPIVersion,

0 commit comments

Comments
 (0)