@@ -8,6 +8,9 @@ The goal of this phase is to achieve one of the goals proposed for Phase 2: chai
8
8
Phase 2 includes several other challenging goals, but being able to chain plugins will be beneficial
9
9
for third-party developers that are using kubebuilder as a library.
10
10
11
+ The other main goal of phase 2, discovering and using external plugins, is out of the scope of this phase,
12
+ and will be tackled when phase 2 is implemented.
13
+
11
14
## Table of contents
12
15
- [ Goal] ( #goal )
13
16
- [ Motivation] ( #motivation )
@@ -46,36 +49,42 @@ Plugin chaining solves the aforementioned problems but the current plugin API, a
46
49
47
50
Design a Plugin API that combines the current [ ` Subcommand ` ] ( ../pkg/plugin/interfaces.go ) and
48
51
[ ` RunOptions ` ] ( ../pkg/plugins/internal/cmdutil/cmdutil.go ) interfaces and enables plugin-chaining.
49
- The new ` Subcommand ` methods can be split in two different categories:
50
- - Initialization methods
51
- - Execution methods
52
+ The new ` Subcommand ` hooks can be split in two different categories:
53
+ - Initialization hooks
54
+ - Execution hooks
55
+
56
+ Initialization hooks are run during the dynamic creation of the CLI, which means that they are able to
57
+ modify the CLI, e.g. providing descriptions and examples for subcommands or binding flags.
58
+ Execution hooks are run after the CLI is created, and therefore cannot modify the CLI. On the other hand,
59
+ as they are run during the CLI execution, they have access to user-provided flag values, project configuration,
60
+ the new API resource or the filesystem abstraction, as opposed to the initialization hooks.
52
61
53
- Additionally, some of these methods may be optional, in which case a non-implemented method will be skipped
54
- when it should be called and consider it succeeded. This also allows to create some methods specific for
55
- a certain subcommand call (e.g.: ` Resource ` -related methods for the ` edit ` subcommand are not needed).
62
+ Additionally, some of these hooks may be optional, in which case a non-implemented hook will be skipped
63
+ when it should be called and consider it succeeded. This also allows to create some hooks specific for
64
+ a certain subcommand call (e.g.: ` Resource ` -related hooks for the ` edit ` subcommand are not needed).
56
65
57
66
Different ordering guarantees can be considered:
58
- - Method order guarantee: a method for a plugin will be called after its previous methods succeeded.
59
- - Steps order guarantee: methods will be called when all plugins have finished the previous method .
60
- - Plugin order guarantee: same method for each plugin will be called in the order specified
67
+ - Hook order guarantee: a hook for a plugin will be called after its previous hooks succeeded.
68
+ - Steps order guarantee: hooks will be called when all plugins have finished the previous hook .
69
+ - Plugin order guarantee: same hook for each plugin will be called in the order specified
61
70
by the plugin position at the plugin chain.
62
71
63
- All of the methods will offer plugin order guarantee, as they all modify/update some item so the order
64
- of plugins is important. Execution methods need to guarantee step order, as the items that are being modified
72
+ All of the hooks will offer plugin order guarantee, as they all modify/update some item so the order
73
+ of plugins is important. Execution hooks need to guarantee step order, as the items that are being modified
65
74
in each step (config, resource, and filesystem) are also needed in the following steps. This is not true for
66
- initialization methods that modify items (metadata and flagset) that are only used in their own methods,
67
- so they only need to guarantee method order.
75
+ initialization hooks that modify items (metadata and flagset) that are only used in their own methods,
76
+ so they only need to guarantee hook order.
68
77
69
- Execution methods will be able to return an error. A specific error can be returned to specify that
70
- no further methods of this plugin should be called, but that the scaffold process should be continued.
78
+ Execution hooks will be able to return an error. A specific error can be returned to specify that
79
+ no further hooks of this plugin should be called, but that the scaffold process should be continued.
71
80
This enables plugins to exit early, e.g., a plugin that scaffolds some files only for cluster-scoped
72
81
resources can detect if the resource is cluster-scoped at one of the first execution steps, and
73
82
therefore, use this error to tell the CLI that no further execution step should be called for itself.
74
83
75
- ### Initialization methods
84
+ ### Initialization hooks
76
85
77
86
#### Update metadata
78
- This method will be used for two purposes. It provides CLI-related metadata to the Subcommand (e.g.,
87
+ This hook will be used for two purposes. It provides CLI-related metadata to the Subcommand (e.g.,
79
88
command name) and update the subcommands metadata such as the description or examples.
80
89
81
90
- Required/optional
@@ -88,7 +97,7 @@ command name) and update the subcommands metadata such as the description or exa
88
97
- [x] Create webhook
89
98
90
99
#### Bind flags
91
- This method will allow subcommands to define specific flags.
100
+ This hook will allow subcommands to define specific flags.
92
101
93
102
- Required/optional
94
103
- [ ] Required
@@ -102,7 +111,7 @@ This method will allow subcommands to define specific flags.
102
111
### Execution methods
103
112
104
113
#### Inject configuration
105
- This method will be used to inject the ` Config ` object that the plugin can modify at will.
114
+ This hook will be used to inject the ` Config ` object that the plugin can modify at will.
106
115
The CLI will create/load/save this configuration object.
107
116
108
117
- Required/optional
@@ -115,7 +124,7 @@ The CLI will create/load/save this configuration object.
115
124
- [x] Create webhook
116
125
117
126
#### Inject resource
118
- This method will be used to inject the ` Resource ` object.
127
+ This hook will be used to inject the ` Resource ` object created by the CLI .
119
128
120
129
- Required/optional
121
130
- [x] Required
@@ -127,9 +136,9 @@ This method will be used to inject the `Resource` object.
127
136
- [x] Create webhook
128
137
129
138
#### Pre-scaffold
130
- This method will be used to take actions before the main scaffolding is performed, e.g. validations.
139
+ This hook will be used to take actions before the main scaffolding is performed, e.g. validations.
131
140
132
- NOTE: a filesystem abstraction will be passed to this method that must be used for scaffolding.
141
+ NOTE: a filesystem abstraction will be passed to this hook, but it should not be used for scaffolding.
133
142
134
143
- Required/optional
135
144
- [ ] Required
@@ -141,9 +150,9 @@ NOTE: a filesystem abstraction will be passed to this method that must be used f
141
150
- [x] Create webhook
142
151
143
152
#### Scaffold
144
- This method will be used to perform the main scaffolding.
153
+ This hook will be used to perform the main scaffolding.
145
154
146
- NOTE: a filesystem abstraction will be passed to this method that must be used for scaffolding.
155
+ NOTE: a filesystem abstraction will be passed to this hook that must be used for scaffolding.
147
156
148
157
- Required/optional
149
158
- [x] Required
@@ -155,11 +164,14 @@ NOTE: a filesystem abstraction will be passed to this method that must be used f
155
164
- [x] Create webhook
156
165
157
166
#### Post-scaffold
158
- This method will be used to take actions after the main scaffolding is performed, e.g. cleanup.
167
+ This hook will be used to take actions after the main scaffolding is performed, e.g. cleanup.
159
168
160
- NOTE: a filesystem abstraction will ** NOT** be passed to this method , as post-scaffold task do not require it.
169
+ NOTE: a filesystem abstraction will ** NOT** be passed to this hook , as post-scaffold task do not require it.
161
170
In case some post-scaffold task requires a filesystem abstraction, it could be added.
162
171
172
+ NOTE 2: the project configuration is saved by the CLI before calling this hook, so changes done to the
173
+ configuration at this hook will not be persisted.
174
+
163
175
- Required/optional
164
176
- [ ] Required
165
177
- [x] Optional
@@ -168,10 +180,67 @@ In case some post-scaffold task requires a filesystem abstraction, it could be a
168
180
- [x] Edit
169
181
- [x] Create API
170
182
- [x] Create webhook
183
+
184
+ ### Override plugins for single subcommand calls
185
+
186
+ Defining plugins at initialization and using them for every command call will solve most of the cases.
187
+ However, there are some cases where a plugin may be wanted just for a certain subcommand call. For
188
+ example, a project with multiple controllers may want to follow the declarative pattern in only one of
189
+ their controllers. The other case is also relevant, a project where most of the controllers follow the
190
+ declarative pattern may need a single controller not to follow it.
191
+
192
+ In order to achieve this, the ` --plugins ` flag will be allowed in every command call, overriding the
193
+ value used in its corresponging project initialization call.
194
+
195
+ ### Plugin chain persistence
196
+
197
+ Currently, the project configuration v3 offers two mechanisms for storing plugin-related information.
198
+
199
+ - A layout field (` string ` ) that is used for plugin resolution on initialized projects.
200
+ - A plugin field (` map[string]interface{} ` ) that is used for plugin configuration raw storage.
201
+
202
+ Plugin resolution uses the ` layout ` field to resolve plugins. In this phase, it has to store a plugin
203
+ chain and not a single plugin. As this value is stored as a string, comma-separated representation can
204
+ be used to represent a chain of plugins instead.
205
+
206
+ NOTE: commas are not allowed in the plugin key.
207
+
208
+ While the ` plugin ` field may seem like a better fit to store the plugin chain, as it can already
209
+ contain multiple values, there are several issues with this alternative approach:
210
+ - A map does not provide any order guarantee, and the plugin chain order is relevant.
211
+ - Some plugins do not store plugin-specific configuration information, e.g. the ` go ` -plugins. So
212
+ the absence of a plugin key doesn't mean that the plugin is not part of the plugin chain.
213
+ - The desire of running a different set of plugins for a single subcommand call has already been
214
+ mentioned. Some of these out-of-chain plugins may need to store plugin-specific configuration,
215
+ so the presence of a plugin doesn't mean that is part of the plugin chain.
216
+
217
+ The next project configuration version could consider this new requirements to define the
218
+ names/types of these two fields.
219
+
220
+ ### Plugin bundle
221
+
222
+ As a side-effect of plugin chaining, the user experience may suffer if they need to provide
223
+ several plugin keys for the ` --plugins ` flag. Additionally, this would also mean a user-facing
224
+ important breaking change.
225
+
226
+ In order to solve this issue, a plugin bundle concept will be introduced. A plugin bundle
227
+ behaves as a plugin:
228
+ - It has a name: provided at creation.
229
+ - It has a version: provided at creation.
230
+ - It has a list of supported project versions: computed from the common supported project
231
+ versions of all the plugins in the bundled.
232
+
233
+ Instead of implementing the optional getter methods that return a subcommand, it offers a way
234
+ to retrieve the list of bundled plugins. This process will be done after plugin resolution.
235
+
236
+ This way, CLIs will be able to define bundles, which will be used in the user-facing API and
237
+ the plugin resolution process, but later they will be treated as separate plugins offering
238
+ the maintainability and separation of concerns advantages that smaller plugins have in
239
+ comparison with bigger monolithic plugins.
171
240
172
241
## Implementation
173
242
174
- The following types are used as input/output values of the described methods :
243
+ The following types are used as input/output values of the described hooks :
175
244
``` go
176
245
// CLIMetadata is the runtime meta-data of the CLI
177
246
type CLIMetadata struct {
@@ -197,7 +266,7 @@ func (e ExitError) Error() string {
197
266
}
198
267
```
199
268
200
- The described methods are implemented through the use of the following interfaces.
269
+ The described hooks are implemented through the use of the following interfaces.
201
270
``` go
202
271
type RequiresCLIMetadata interface {
203
272
InjectCLIMetadata (CLIMetadata)
@@ -220,11 +289,11 @@ type RequiresResource interface {
220
289
}
221
290
222
291
type HasPreScaffold interface {
223
- PreScaffold (afero. Fs ) error
292
+ PreScaffold (machinery. Filesystem ) error
224
293
}
225
294
226
295
type Scaffolder interface {
227
- Scaffold (afero. Fs ) error
296
+ Scaffold (machinery. Filesystem ) error
228
297
}
229
298
230
299
type HasPostScaffold interface {
@@ -256,3 +325,11 @@ type CreateWebhookSubcommand interface {
256
325
Scaffolder
257
326
}
258
327
```
328
+
329
+ An additional interface defines the bundle method to return the wrapped plugins:
330
+ ``` go
331
+ type Bundle interface {
332
+ Plugin
333
+ Plugins () []Plugin
334
+ }
335
+ ```
0 commit comments