Skip to content

Commit f2e1edb

Browse files
committed
Plugin phase 1.5 implementation
Signed-off-by: Adrian Orive <[email protected]>
1 parent faff148 commit f2e1edb

File tree

193 files changed

+4426
-3647
lines changed

Some content is hidden

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

193 files changed

+4426
-3647
lines changed

cmd/main.go

Lines changed: 3 additions & 1 deletion
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
)
@@ -30,13 +31,14 @@ func main() {
3031
c, err := cli.New(
3132
cli.WithCommandName("kubebuilder"),
3233
cli.WithVersion(versionString()),
33-
cli.WithDefaultProjectVersion(cfgv3.Version),
3434
cli.WithPlugins(
3535
&pluginv2.Plugin{},
3636
&pluginv3.Plugin{},
37+
&declarativev1.Plugin{},
3738
),
3839
cli.WithDefaultPlugins(cfgv2.Version, &pluginv2.Plugin{}),
3940
cli.WithDefaultPlugins(cfgv3.Version, &pluginv3.Plugin{}),
41+
cli.WithDefaultProjectVersion(cfgv3.Version),
4042
cli.WithCompletion(),
4143
)
4244
if err != nil {

pkg/cli/api.go

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,77 +14,54 @@ 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+
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+
c.applySubcommandHooks(cmd, subcommands, apiErrorMsg, false)
8465

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)
66+
return cmd
9067
}

0 commit comments

Comments
 (0)