Skip to content

Commit 48ea4cb

Browse files
authored
Merge pull request #1250 from joelanford/extensible-cli-design
designs: extensible CLI and scaffolding plugins
2 parents f2ebe36 + 5187a15 commit 48ea4cb

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# Extensible CLI and Scaffolding Plugins
2+
3+
## Overview
4+
5+
I would like for Kubebuilder to become more extensible, such that it could be imported and used as a library in other projects. Specifically, I'm looking for a way to use Kubebuilder's existing CLI and scaffolding for Go projects, but to also be able to augment the Kubebuilder project structure with other custom project types so that I can support the Kubebuilder workflow with non-Go operators (e.g. operator-sdk's Ansible and Helm-based operators).
6+
7+
The idea is for Kubebuilder to define one or more plugin interfaces that can be used to drive what the `init`, `create api` and `create webhooks` subcommands do and to add a new `cli` package that other projects can use to integrate out-of-tree plugins with the Kubebuilder CLI in their own projects.
8+
9+
## Related issues and PRs
10+
11+
* [#1148](https://github.com/kubernetes-sigs/kubebuilder/pull/1148)
12+
* [#1171](https://github.com/kubernetes-sigs/kubebuilder/pull/1171)
13+
* Possibly [#1218](https://github.com/kubernetes-sigs/kubebuilder/issues/1218)
14+
15+
## Prototype implementation
16+
17+
Barebones plugin refactor: https://github.com/joelanford/kubebuilder-exp
18+
Kubebuilder feature branch: https://github.com/kubernetes-sigs/kubebuilder/tree/feature/plugins-part-2-electric-boogaloo
19+
20+
## Plugin interfaces
21+
22+
### Required
23+
24+
Each plugin would minimally be required to implement the `Plugin` interface.
25+
26+
```go
27+
type Plugin interface {
28+
// Version returns the plugin's semantic version, ex. "v1.2.3".
29+
//
30+
// Note: this version is different from config version.
31+
Version() string
32+
// Name returns a DNS1123 label string defining the plugin type.
33+
// For example, Kubebuilder's main plugin would return "go".
34+
//
35+
// Plugin names can be fully-qualified, and non-fully-qualified names are
36+
// prepended to ".kubebuilder.io" to prevent conflicts.
37+
Name() string
38+
// SupportedProjectVersions lists all project configuration versions this
39+
// plugin supports, ex. []string{"2", "3"}. The returned slice cannot be empty.
40+
SupportedProjectVersions() []string
41+
}
42+
```
43+
44+
#### Plugin naming
45+
46+
Plugin names (returned by `Name()`) must be DNS1123 labels. The returned name
47+
may be fully qualified (fq), ex. `go.kubebuilder.io`, or not but internally will
48+
always be fq by either appending `.kubebuilder.io` to the name or using an
49+
existing qualifier defined by the plugin. FQ names prevent conflicts between
50+
plugin names; the plugin runner will ask the user to add a name qualifier to
51+
a conflicting plugin.
52+
53+
#### Project file plugin `layout`
54+
55+
The `PROJECT` file will specify what base plugin generated the project under
56+
a `layout` key. `layout` will have the format: `Plugin.Name() + "/" + Plugin.Version()`.
57+
`version` and `layout` have versions with different meanings: `version` is the
58+
project config version, while `layout`'s version is the plugin semantic version.
59+
The value in `version` will determine that in `layout` by a plugin's supported
60+
project versions (via `SupportedProjectVersions()`).
61+
62+
Example `PROJECT` file:
63+
64+
```yaml
65+
version: "3-alpha"
66+
layout: go/v1.0.0
67+
domain: testproject.org
68+
repo: github.com/test-inc/testproject
69+
resources:
70+
- group: crew
71+
kind: Captain
72+
version: v1
73+
```
74+
75+
### Optional
76+
77+
Next, a plugin could optionally implement further interfaces to declare its support for specific Kubebuilder subcommands. For example:
78+
* `InitPlugin` - to initialize new projects
79+
* `CreateAPIPlugin` - to create APIs (and possibly controllers) for existing projects
80+
* `CreateWebhookPlugin` - to create webhooks for existing projects
81+
82+
Each of these interfaces would follow the same pattern (see the `InitPlugin` interface example below).
83+
84+
```go
85+
type InitPluginGetter interface {
86+
Plugin
87+
// GetInitPlugin returns the underlying InitPlugin interface.
88+
GetInitPlugin() InitPlugin
89+
}
90+
91+
type InitPlugin interface {
92+
GenericSubcommand
93+
}
94+
```
95+
96+
Each specialized plugin interface can leverage a generic subcommand interface, which prevents duplication of methods while permitting type checking and interface flexibility. A plugin context can be used to preserve default help text in case a plugin does not implement its own.
97+
98+
```go
99+
type GenericSubcommand interface {
100+
// UpdateContext updates a PluginContext with command-specific help text, like description and examples.
101+
// Can be a no-op if default help text is desired.
102+
UpdateContext(*PluginContext)
103+
// BindFlags binds the plugin's flags to the CLI. This allows each plugin to define its own
104+
// command line flags for the kubebuilder subcommand.
105+
BindFlags(fs *pflag.FlagSet)
106+
// Run runs the subcommand.
107+
Run() error
108+
}
109+
110+
type PluginContext struct {
111+
// Description is a description of what this subcommand does. It is used to display help.
112+
Description string
113+
// Examples are one or more examples of the command-line usage
114+
// of this plugin's project subcommand support. It is used to display help.
115+
Examples string
116+
}
117+
```
118+
119+
#### Deprecated Plugins
120+
121+
To generically support deprecated project versions, we could also add a `Deprecated` interface that the CLI could use to decide when to print deprecation warnings:
122+
123+
```go
124+
// Deprecated is an interface that, if implemented, informs the CLI
125+
// that the plugin is deprecated. The CLI uses this to print deprecation
126+
// warnings when the plugin is in use.
127+
type Deprecated interface {
128+
// DeprecationWarning returns a deprecation message that callers
129+
// can use to warn users of deprecations
130+
DeprecationWarning() string
131+
}
132+
```
133+
134+
## CLI
135+
136+
To make the above plugin system extensible and usable by other projects, we could add a new CLI package that Kubebuilder (and other projects) could use as their entrypoint.
137+
138+
Example Kubebuilder main.go:
139+
140+
```go
141+
func main() {
142+
c, err := cli.New(
143+
cli.WithPlugins(
144+
&golangv1.Plugin{},
145+
&golangv2.Plugin{},
146+
),
147+
)
148+
if err != nil {
149+
log.Fatal(err)
150+
}
151+
if err := c.Run(); err != nil {
152+
log.Fatal(err)
153+
}
154+
}
155+
```
156+
157+
Example Operator SDK main.go:
158+
159+
```go
160+
func main() {
161+
c, err := cli.New(
162+
cli.WithCommandName("operator-sdk"),
163+
cli.WithDefaultProjectVersion("2"),
164+
cli.WithExtraCommands(newCustomCobraCmd()),
165+
cli.WithPlugins(
166+
&golangv1.Plugin{},
167+
&golangv2.Plugin{},
168+
&helmv1.Plugin{},
169+
&ansiblev1.Plugin{},
170+
),
171+
)
172+
if err != nil {
173+
log.Fatal(err)
174+
}
175+
if err := c.Run(); err != nil {
176+
log.Fatal(err)
177+
}
178+
}
179+
```
180+
181+
## Comments & Questions
182+
183+
### Cobra Commands
184+
185+
**RESOLUTION:** `cobra` will be used directly in Phase 1 since it is a widely used, feature-rich CLI package. This, however unlikely, may change in future phases.
186+
187+
As discussed earlier as part of [#1148](https://github.com/kubernetes-sigs/kubebuilder/pull/1148), one goal is to eliminate the use of `cobra.Command` in the exported API of Kubebuilder since that is considered an internal implementation detail.
188+
189+
However, at some point, projects that make use of this extensibility will likely want to integrate their own subcommands. In this proposal, `cli.WithExtraCommands()` _DOES_ expose `cobra.Command` to allow callers to pass their own subcommands to the CLI.
190+
191+
In [#1148](https://github.com/kubernetes-sigs/kubebuilder/pull/1148), callers would use Kubebuilder's cobra commands to build their CLI. Here, control of the CLI is retained by Kubebuilder, and callers pass their subcommands to Kubebuilder. This has several benefits:
192+
1. Kubebuilder's CLI subcommands are never exposed except via the explicit plugin interface. This allows the Kubebuilder project to re-implement its subcommand internals without worrying about backwards compatibility of consumers of Kubebuilder's CLI.
193+
2. If desired, Kubebuilder could ensure that extra subcommands do not overwrite/reuse the existing Kubebuilder subcommand names. For example, only Kubebuilder gets to define the `init` subcommand
194+
3. The overall binary's help handling is self-contained in Kubebuilder's CLI. Callers don't have to figure out how to have a cohesive help output between the Kubebuilder CLI and their own custom subcommands.
195+
196+
With all of that said, even this exposure of `cobra.Command` could be problematic. If Kubebuilder decides in the future to transition to a different CLI framework (or to roll its own) it has to either continue maintaining support for these extra cobra commands passed into it, or it was to break the CLI API.
197+
198+
Are there other ideas for how to handle the following requirements?
199+
* Eliminate use of cobra in CLI interface
200+
* Allow other projects to have custom subcommands
201+
* Support cohesive help output
202+
203+
### Other
204+
1. ~Should the `InitPlugin` interface methods be required of all plugins?~ No
205+
2. ~Any other approaches or ideas?~
206+
3. ~Anything I didn't cover that could use more explanation?~

0 commit comments

Comments
 (0)