Skip to content
Merged
Changes from 2 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
0a5c604
:sparkles: Use ErrorIfCRDPathMissing in EnvTest
jamielennox Nov 2, 2020
887d714
:fix: remove unecessary checks adds to the main.go (go/v3-alpha)
camilamacedo86 Dec 16, 2020
b1670d8
Remove misleading help that mentioned dep
justinsb Jan 13, 2021
3ad354c
Merge pull request #1945 from justinsb/no_more_dep
k8s-ci-robot Jan 13, 2021
84b6f53
Build the command and use it to report user errors so that the comman…
Adirio Jan 12, 2021
9fd31f1
pkg/plugins/golang/v3: upgrade kubebuilder-declarative-pattern to k8s…
estroz Jan 13, 2021
1cd45e2
Merge pull request #1946 from estroz/chore/update-dep
k8s-ci-robot Jan 13, 2021
c988208
Add help dialog to makefile template
droctothorpe Jan 14, 2021
a7fc753
Add updated testdata
droctothorpe Jan 14, 2021
20df76b
Make directory-not-empty error more self-explanatory
justinsb Jan 13, 2021
f7a5201
Merge pull request #1947 from droctothorpe/makefile-updates
k8s-ci-robot Jan 14, 2021
1001d36
Merge pull request #1943 from justinsb/directory_not_empty_message
k8s-ci-robot Jan 15, 2021
d1105e2
*: upgrade kube-rbac-proxy to v0.8.0
estroz Jan 16, 2021
c577244
Merge pull request #1937 from Adirio/enhance-root-cmd
k8s-ci-robot Jan 18, 2021
032f0c4
Merge pull request #1910 from camilamacedo86/fix-checks
k8s-ci-robot Jan 18, 2021
0d938e4
upgrade kubebuilder module from v2 to v3
camilamacedo86 Jan 18, 2021
e3e3658
Merge pull request #1958 from camilamacedo86/v3-dep
k8s-ci-robot Jan 19, 2021
8d108aa
test commit
estroz Jan 20, 2021
3af7d23
Merge pull request #1955 from estroz/deps/krp-v0.8.0
k8s-ci-robot Jan 20, 2021
79ecd51
Merge pull request #1765 from jamielennox/error-if-missing
k8s-ci-robot Jan 20, 2021
ea75c59
Update Options and Resource models and implement Config as an interfa…
Adirio Dec 17, 2020
1e78a6f
Merge pull request #1911 from Adirio/update-models
k8s-ci-robot Jan 22, 2021
1a6bb7a
Add --plural flag (go/v3)
Adirio Jan 22, 2021
b290147
Merge pull request #1967 from Adirio/plurals
k8s-ci-robot Jan 22, 2021
45f69ce
Validate the plugin flag
Adirio Jan 23, 2021
7b84f60
Merge pull request #1970 from Adirio/validate-plural
k8s-ci-robot Jan 25, 2021
db966c6
Split resource validation between Options and Resource objects so tha…
Adirio Jan 26, 2021
2aef3ad
Add the rest of the missing fields and stabilize config v3
Adirio Jan 22, 2021
03431ed
(go/v3) change 'runAsUser: 65532', which is redundant, to 'runAsNonRo…
estroz Jan 27, 2021
14b27b4
Merge pull request #1978 from estroz/feature/pod-nonroot
k8s-ci-robot Jan 28, 2021
12de4fb
Remove make calls from post-scaffold hooks
Adirio Jan 29, 2021
4fd2cd7
Tolerate "dot" directories when checking if dir is empty
justinsb Jan 13, 2021
e222f25
Merge pull request #1944 from justinsb/tolerate_git_dir
k8s-ci-robot Jan 29, 2021
71b8578
Merge pull request #1968 from Adirio/config/v3
k8s-ci-robot Jan 29, 2021
386207f
:seedling: sort plugins key for the help
camilamacedo86 Jan 29, 2021
63286ce
Merge pull request #1984 from camilamacedo86/sort-plugins-key
k8s-ci-robot Jan 29, 2021
479367b
Merge pull request #1983 from Adirio/remove-post-scaffold-make
k8s-ci-robot Jan 30, 2021
fa40846
Return a typed error in case DecodePluginConfig was unable to find th…
Adirio Feb 1, 2021
564a6e9
Merge pull request #1985 from Adirio/plugin-config-missing-error
k8s-ci-robot Feb 1, 2021
90344af
Merge pull request #1974 from Adirio/split-resource-validation
k8s-ci-robot Feb 2, 2021
95466c5
Add documentation to v2 controllers
Adirio Feb 3, 2021
fed6174
Go fmt templates
Adirio Feb 3, 2021
63904d2
Merge pull request #1989 from Adirio/v2-controller-doc
k8s-ci-robot Feb 3, 2021
6a676b1
Merge pull request #1987 from Adirio/go-fmt-templates
k8s-ci-robot Feb 3, 2021
6e1be3d
Bump addon *_types.go and controller.go templates with changes that w…
Adirio Feb 3, 2021
4ddc71c
Improve Makefile help
Adirio Jan 29, 2021
3c8e370
Merge pull request #1988 from Adirio/bump-addon
k8s-ci-robot Feb 3, 2021
f1685b8
Fix sorting issue nwith plugin versions and their supported project v…
Adirio Feb 8, 2021
89c42c4
Merge pull request #1995 from Adirio/fix-sort
k8s-ci-robot Feb 8, 2021
fe481ef
Merge pull request #1981 from Adirio/makefile-help
k8s-ci-robot Feb 8, 2021
a9dd9dc
Fix go/v2 with config/v3 resources in config file to store webhook in…
Adirio Feb 9, 2021
72f01e5
Merge pull request #1998 from Adirio/fix-go-v2-config-v3-webhooks
k8s-ci-robot Feb 9, 2021
42091e9
Track resources without an API in the config file (config/v3)
Adirio Feb 10, 2021
05abe25
Merge pull request #2003 from Adirio/track-resources-without-api
k8s-ci-robot Feb 11, 2021
bd00512
:seeding: ignore maligned lint issue for WebhookSuite struct
camilamacedo86 Feb 11, 2021
c82530b
Merge pull request #2009 from camilamacedo86/fix-lint-issue
k8s-ci-robot Feb 11, 2021
3948289
Plugin phase 1.5 EP
Adirio Feb 3, 2021
757cc8a
Merge pull request #1990 from Adirio/plugin-phase-1-5
k8s-ci-robot Feb 12, 2021
8c08161
Tests cleanup
Adirio Jan 8, 2021
ab3bb7f
:book: uprade migration guide for v3
camilamacedo86 Dec 17, 2020
685558d
Merge pull request #1916 from camilamacedo86/migration-doc
k8s-ci-robot Feb 12, 2021
9fafbf9
Accept auto-generated files from GitHub into the allowed set of files
Adirio Feb 13, 2021
46c65ba
Merge pull request #2014 from Adirio/accept-github-fefault-files
k8s-ci-robot Feb 13, 2021
a9c3e50
Add Adirio to aprovers
Adirio Feb 13, 2021
5044d6e
Merge pull request #2013 from Adirio/adirio-aprover
k8s-ci-robot Feb 13, 2021
6224a06
Merge pull request #1933 from Adirio/cleanup-tests
k8s-ci-robot Feb 14, 2021
53b2cf6
:book: proposal for new plugin to generate code
camilamacedo86 Nov 9, 2020
5afa97e
Update book examples to Kubebuilder v3.
HoustonPutman Feb 16, 2021
7adcffd
Merge pull request #1803 from camilamacedo86/code-generate-plugin
k8s-ci-robot Feb 17, 2021
e23cbe4
Fix the bug where an error was being hidden by a potentially valid fl…
Adirio Feb 17, 2021
b46beea
Merge pull request #2023 from Adirio/fix-hidden-error
k8s-ci-robot Feb 17, 2021
2398445
Merge pull request #2021 from HoustonPutman/book-empty-examples-v3
k8s-ci-robot Feb 17, 2021
d71b1a9
Allow cross-package coverage inside the "pkg" directory
Adirio Feb 17, 2021
2d9367e
Merge pull request #2024 from Adirio/increase-coverage
k8s-ci-robot Feb 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 258 additions & 0 deletions designs/extensible-cli-and-scaffolding-plugins-phase-1-5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
# Extensible CLI and Scaffolding Plugins - Phase 1.5

Continuation of [Extensible CLI and Scaffolding Plugins](./extensible-cli-and-scaffolding-plugins-phase-1.md).

## Goal

The goal of this phase is to achieve one of the goals proposed for Phase 2: chaining plugins.
Phase 2 includes several other challenging goals, but being able to chain plugins will be beneficial
for third-party developers that are using kubebuilder as a library.

## Table of contents
- [Goal](#goal)
- [Motivation](#motivation)
- [Proposal](#proposal)
- [Implementation](#implementation)

## Motivation

There are several cases of plugins that want to maintain most of the go plugin functionality and add
certain features on top of it, both inside and outside kubebuilder repository:
- [Addon pattern](../plugins/addon)
- [Operator SDK](https://github.com/operator-framework/operator-sdk/tree/master/internal/plugins/golang)

This behavior fits perfectly under Phase 1.5, where plugins could be chained. However, as this feature is
not available, the adopted temporal solution is to wrap the base go plugin and perform additional actions
after its `Run` method has been executed. This solution faces several issues:

- Wrapper plugins are unable to access the data of the wrapped plugins, as they weren't designed for this
purpose, and therefore, most of its internal data is non-exported. An example of this inaccessible data
would be the `Resource` objects created inside the `create api` and `create webhook` commands.
- Wrapper plugins are dependent on their wrapped plugins, and therefore can't be used for other plugins.
- Under the hood, subcommands implement a second hidden interface: `RunOptions`, which further accentuates
these issues.

Plugin chaining solves the aforementioned problems but the current plugin API, and more specifically the
`Subcommand` interface, does not support plugin chaining.

- The `RunOptions` interface implemented under the hood is not part of the plugin API, and therefore
the cli is not able to run post-scaffold logic (implemented in `RunOptions.PostScaffold` method) after
all the plugins have scaffolded their part.
- `Resource`-related commands can't bind flags like `--group`, `--version` or `--kind` in each plugin,
it must be created outside the plugins and then injected into them similar to the approach followed
currently for `Config` objects.

## Proposal

Design a Plugin API that combines the current [`Subcommand`](../pkg/plugin/interfaces.go) and
[`RunOptions`](../pkg/plugins/internal/cmdutil/cmdutil.go) interfaces and enables plugin-chaining.
The new `Subcommand` methods can be split in two different categories:
- Initialization methods
- Execution methods

Additionally, some of these methods may be optional, in which case a non-implemented method will be skipped
when it should be called and consider it succeeded. This also allows to create some methods specific for
a certain subcommand call (e.g.: `Resource`-related methods for the `edit` subcommand are not needed).

Different ordering guarantees can be considered:
- Method order guarantee: a method for a plugin will be called after its previous methods succeeded.
- Steps order guarantee: methods will be called when all plugins have finished the previous method.
- Plugin order guarantee: same method for each plugin will be called in the order specified
by the plugin position at the plugin chain.

All of the methods will offer plugin order guarantee, as they all modify/update some item so the order
of plugins is important. Execution methods need to guarantee step order, as the items that are being modified
in each step (config, resource, and filesystem) are also needed in the following steps. This is not true for
initialization methods that modify items (metadata and flagset) that are only used in their own methods,
so they only need to guarantee method order.

Execution methods will be able to return an error. A specific error can be returned to specify that
no further methods of this plugin should be called, but that the scaffold process should be continued.
This enables plugins to exit early, e.g., a plugin that scaffolds some files only for cluster-scoped
resources can detect if the resource is cluster-scoped at one of the first execution steps, and
therefore, use this error to tell the CLI that no further execution step should be called for itself.

### Initialization methods

#### Update metadata
This method will be used for two purposes. It provides CLI-related metadata to the Subcommand (e.g.,
command name) and update the subcommands metadata such as the description or examples.

- Required/optional
- [ ] Required
- [x] Optional
- Subcommands
- [x] Init
- [x] Edit
- [x] Create API
- [x] Create webhook

#### Bind flags
This method will allow subcommands to define specific flags.

- Required/optional
- [ ] Required
- [x] Optional
- Subcommands
- [x] Init
- [x] Edit
- [x] Create API
- [x] Create webhook

### Execution methods

#### Inject configuration
This method will be used to inject the `Config` object that the plugin can modify at will.
The CLI will create/load/save this configuration object.

- Required/optional
- [ ] Required
- [x] Optional
- Subcommands
- [x] Init
- [x] Edit
- [x] Create API
- [x] Create webhook

#### Inject resource
This method will be used to inject the `Resource` object.

- Required/optional
- [x] Required
- [ ] Optional
- Subcommands
- [ ] Init
- [ ] Edit
- [x] Create API
- [x] Create webhook

#### Pre-scaffold
This method will be used to take actions before the main scaffolding is performed, e.g. validations.

NOTE: a filesystem abstraction will be passed to this method that must be used for scaffolding.

- Required/optional
- [ ] Required
- [x] Optional
- Subcommands
- [x] Init
- [x] Edit
- [x] Create API
- [x] Create webhook

#### Scaffold
This method will be used to perform the main scaffolding.

NOTE: a filesystem abstraction will be passed to this method that must be used for scaffolding.

- Required/optional
- [x] Required
- [ ] Optional
- Subcommands
- [x] Init
- [x] Edit
- [x] Create API
- [x] Create webhook

#### Post-scaffold
This method will be used to take actions after the main scaffolding is performed, e.g. cleanup.

NOTE: a filesystem abstraction will **NOT** be passed to this method, as post-scaffold task do not require it.
In case some post-scaffold task requires a filesystem abstraction, it could be added.

- Required/optional
- [ ] Required
- [x] Optional
- Subcommands
- [x] Init
- [x] Edit
- [x] Create API
- [x] Create webhook

## Implementation

The following types are used as input/output values of the described methods:
```go
// CLIMetadata is the runtime meta-data of the CLI
type CLIMetadata struct {
// CommandName is the root command name.
CommandName string
}

// SubcommandMetadata is the runtime meta-data for a subcommand
type SubcommandMetadata struct {
// Description is a description of what this subcommand does. It is used to display help.
Description string
// Examples are one or more examples of the command-line usage of this subcommand. It is used to display help.
Examples string
}

type ExitError struct {
Plugin string
Reason string
}

func (e ExitError) Error() string {
return fmt.Sprintf("plugin %s exit early: %s", e.Plugin, e.Reason)
}
```

The described methods are implemented through the use of the following interfaces.
```go
type RequiresCLIMetadata interface {
InjectCLIMetadata(CLIMetadata)
}

type UpdatesSubcommandMetadata interface {
UpdateSubcommandMetadata(*SubcommandMetadata)
}

type HasFlags interface {
BindFlags(*pflag.FlagSet)
}

type RequiresConfig interface {
InjectConfig(config.Config) error
}

type RequiresResource interface {
InjectResource(*resource.Resource) error
}

type HasPreScaffold interface {
PreScaffold(afero.Fs) error
}

type Scaffolder interface {
Scaffold(afero.Fs) error
}

type HasPostScaffold interface {
PostScaffold() error
}
```

Additional interfaces define the required method for each type of plugin:
```go
// InitSubcommand is the specific interface for subcommands returned by init plugins.
type InitSubcommand interface {
Scaffolder
}

// EditSubcommand is the specific interface for subcommands returned by edit plugins.
type EditSubcommand interface {
Scaffolder
}

// CreateAPISubcommand is the specific interface for subcommands returned by create API plugins.
type CreateAPISubcommand interface {
RequiresResource
Scaffolder
}

// CreateWebhookSubcommand is the specific interface for subcommands returned by create webhook plugins.
type CreateWebhookSubcommand interface {
RequiresResource
Scaffolder
}
```