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