Skip to content

Commit 705ef0a

Browse files
committed
Implement plugin chaining (plugin phase 1.5)
Declarative plugin created to replace the outdated addon pattern Signed-off-by: Adrian Orive <[email protected]>
1 parent 4690584 commit 705ef0a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2049
-1476
lines changed

cmd/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sigs.k8s.io/kubebuilder/v3/pkg/cli"
2323
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
2424
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
25+
declarativev1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/declarative/v1"
2526
pluginv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
2627
pluginv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
2728
)
@@ -34,6 +35,7 @@ func main() {
3435
cli.WithPlugins(
3536
&pluginv2.Plugin{},
3637
&pluginv3.Plugin{},
38+
&declarativev1.Plugin{},
3739
),
3840
cli.WithDefaultPlugins(cfgv2.Version, &pluginv2.Plugin{}),
3941
cli.WithDefaultPlugins(cfgv3.Version, &pluginv3.Plugin{}),

pkg/cli/api.go

+31-50
Original file line numberDiff line numberDiff line change
@@ -14,77 +14,58 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package cli // nolint:dupl
17+
package cli //nolint:dupl
1818

1919
import (
2020
"fmt"
2121

2222
"github.com/spf13/cobra"
2323

24-
yamlstore "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
2524
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
2625
)
2726

27+
const apiErrorMsg = "failed to create API"
28+
2829
func (c CLI) newCreateAPICmd() *cobra.Command {
29-
ctx := c.newAPIContext()
3030
cmd := &cobra.Command{
31-
Use: "api",
32-
Short: "Scaffold a Kubernetes API",
33-
Long: ctx.Description,
34-
Example: ctx.Examples,
31+
Use: "api",
32+
Short: "Scaffold a Kubernetes API",
33+
Long: `Scaffold a Kubernetes API.
34+
`,
3535
RunE: errCmdFunc(
3636
fmt.Errorf("api subcommand requires an existing project"),
3737
),
3838
}
3939

40-
// Lookup the plugin for projectVersion and bind it to the command.
41-
c.bindCreateAPI(ctx, cmd)
42-
return cmd
43-
}
44-
45-
func (c CLI) newAPIContext() plugin.Context {
46-
return plugin.Context{
47-
CommandName: c.commandName,
48-
Description: `Scaffold a Kubernetes API.
49-
`,
50-
}
51-
}
52-
53-
// nolint:dupl
54-
func (c CLI) bindCreateAPI(ctx plugin.Context, cmd *cobra.Command) {
40+
// In case no plugin was resolved, instead of failing the construction of the CLI, fail the execution of
41+
// this subcommand. This allows the use of subcommands that do not require resolved plugins like help.
5542
if len(c.resolvedPlugins) == 0 {
56-
cmdErr(cmd, fmt.Errorf(noPluginError))
57-
return
43+
cmdErr(cmd, noResolvedPluginError{})
44+
return cmd
5845
}
5946

60-
var createAPIPlugin plugin.CreateAPI
61-
for _, p := range c.resolvedPlugins {
62-
tmpPlugin, isValid := p.(plugin.CreateAPI)
63-
if isValid {
64-
if createAPIPlugin != nil {
65-
err := fmt.Errorf("duplicate API creation plugins (%s, %s), use a more specific plugin key",
66-
plugin.KeyFor(createAPIPlugin), plugin.KeyFor(p))
67-
cmdErr(cmd, err)
68-
return
69-
}
70-
createAPIPlugin = tmpPlugin
71-
}
72-
}
47+
// Obtain the plugin keys and subcommands from the plugins that implement plugin.CreateAPI.
48+
pluginKeys, subcommands := c.filterSubcommands(
49+
func(p plugin.Plugin) bool {
50+
_, isValid := p.(plugin.CreateAPI)
51+
return isValid
52+
},
53+
func(p plugin.Plugin) plugin.Subcommand {
54+
return p.(plugin.CreateAPI).GetCreateAPISubcommand()
55+
},
56+
)
7357

74-
if createAPIPlugin == nil {
75-
cmdErr(cmd, fmt.Errorf("resolved plugins do not provide an API creation plugin: %v", c.pluginKeys))
76-
return
58+
// Verify that there is at least one remaining plugin.
59+
if len(*subcommands) == 0 {
60+
cmdErr(cmd, noAvailablePluginError{"API creation"})
61+
return cmd
7762
}
7863

79-
subcommand := createAPIPlugin.GetCreateAPISubcommand()
80-
subcommand.BindFlags(cmd.Flags())
81-
subcommand.UpdateContext(&ctx)
82-
cmd.Long = ctx.Description
83-
cmd.Example = ctx.Examples
64+
// Initialization methods.
65+
options := c.initializationMethods(cmd, subcommands)
66+
67+
// Execution methods.
68+
cmd.PreRunE, cmd.RunE, cmd.PostRunE = c.executionMethodsFuncs(pluginKeys, subcommands, options, apiErrorMsg)
8469

85-
cfg := yamlstore.New(c.fs)
86-
msg := fmt.Sprintf("failed to create API with %q", plugin.KeyFor(createAPIPlugin))
87-
cmd.PreRunE = preRunECmdFunc(subcommand, cfg, msg)
88-
cmd.RunE = runECmdFunc(c.fs, subcommand, msg)
89-
cmd.PostRunE = postRunECmdFunc(cfg, msg)
70+
return cmd
9071
}

pkg/cli/cli.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ const (
3838

3939
projectVersionFlag = "project-version"
4040
pluginsFlag = "plugins"
41-
42-
noPluginError = "invalid config file please verify that the version and layout fields are set and valid"
4341
)
4442

4543
// equalStringSlice checks if two string slices are equal.
@@ -477,6 +475,13 @@ func (c CLI) printDeprecationWarnings() {
477475
}
478476
}
479477

478+
// metadata returns CLI's metadata.
479+
func (c CLI) metadata() plugin.CLIMetadata {
480+
return plugin.CLIMetadata{
481+
CommandName: c.commandName,
482+
}
483+
}
484+
480485
// Run executes the CLI utility.
481486
//
482487
// If an error is found, command help and examples will be printed.

0 commit comments

Comments
 (0)