|
| 1 | +# Plugin and project version resolution |
| 2 | + |
| 3 | +## Old behavior |
| 4 | + |
| 5 | +The same process is followed for all commands, without distinction between `init` and the rest. The only difference is |
| 6 | +that, for `init`, the configuration file will not be present. |
| 7 | + |
| 8 | +1. Obtain the project version from project configuration file's `version` field, `--project-verison` flag, or default |
| 9 | + project version configured during CLI creation (`cli.New(cli.WithDefaultProjectVersion(...))`). |
| 10 | + * In case both a project configuration file and the flag are found, fail if they are different. |
| 11 | +2. Obtain the plugin keys from the project configuration file's `layout` field, `--plugins` flag, or default plugins |
| 12 | + configured during CLI creation for the above project version (`cli.New(cli.WithDefaultPlugins(...))`). |
| 13 | + * In case both a project configuration file and the flag are found, fail if they are different. |
| 14 | +3. In case any of the plugin keys is not fully qualified (full name and version), check if there is only one plugin that |
| 15 | + fits that unqualified plugin key. |
| 16 | +4. Resolve the plugins with the list of qualified keys above. |
| 17 | +5. Verify the plugins support the project version. |
| 18 | + |
| 19 | + |
| 20 | +### Flaws |
| 21 | + |
| 22 | +* Default plugins need to be provided per project version |
| 23 | +* Project version can't be resolved (e.g., when a single project version is supported by the provided plugins). |
| 24 | +* Command calls are not allowed to override plugin keys. |
| 25 | + |
| 26 | +## Proposal |
| 27 | + |
| 28 | +The following plugin resolution algorithm solves the flaws described above: |
| 29 | + |
| 30 | +1. [All commands but Init] Obtain the project version and plugin keys from the project configuration if available. |
| 31 | + * If they are available, plugin keys will be fully qualified. |
| 32 | +2. [Init command only] Obtain the project version from `--project-version` flag. |
| 33 | + * The flag is optional, we will try to resolve project version later in case it is not provided. |
| 34 | +3. Obtain the optional plugin keys from `--plugins` flag. |
| 35 | + * [Init command only] If not present, use default plugins. |
| 36 | + * [All commands but Init] If not present, use plugin keys from configuration file (step 1). |
| 37 | +4. Qualify the unqualified keys from step 3. |
| 38 | + * If we know the project version, use this info to filter the available plugins so that we can be more accurate. |
| 39 | +5. Resolve the plugins with the list of qualified keys above. |
| 40 | +6. [Init command only] Resolve the project version in case it wasn't provided and there is only one project version |
| 41 | + supported by all the resolved plugins. |
| 42 | + |
| 43 | +### Example |
| 44 | + |
| 45 | +Based on the following main.go file: |
| 46 | +```go |
| 47 | +package main |
| 48 | + |
| 49 | +import ( |
| 50 | + "log" |
| 51 | + |
| 52 | + "sigs.k8s.io/kubebuilder/v2/pkg/cli" |
| 53 | + "sigs.k8s.io/kubebuilder/v2/pkg/model/config" |
| 54 | + pluginv2 "sigs.k8s.io/kubebuilder/v2/pkg/plugins/golang/v2" |
| 55 | + pluginv3 "sigs.k8s.io/kubebuilder/v2/pkg/plugins/golang/v3" |
| 56 | +) |
| 57 | + |
| 58 | +func main() { |
| 59 | + c, err := cli.New( |
| 60 | + cli.WithCommandName("kubebuilder"), |
| 61 | + cli.WithVersion(versionString()), |
| 62 | + cli.WithPlugins( |
| 63 | + &pluginv2.Plugin{}, |
| 64 | + &pluginv3.Plugin{}, |
| 65 | + ), |
| 66 | + cli.WithDefaultPlugins(&pluginv3.Plugin{}), |
| 67 | + cli.WithCompletion, |
| 68 | + ) |
| 69 | + if err != nil { |
| 70 | + log.Fatal(err) |
| 71 | + } |
| 72 | + if err := c.Run(); err != nil { |
| 73 | + log.Fatal(err) |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +As the `go.kubebuilder.io/v3` only supports project version "3", the project version can be resolved and doesn't |
| 79 | +need to be specified in the flags. |
| 80 | +``` |
| 81 | +kubebuilder init --plugins=go.kubebuilder.io/v3 |
| 82 | +``` |
| 83 | + |
| 84 | +``` |
| 85 | +kuebebuilder create api --plugins=declarative.kubebuilder.io/v1 ... |
| 86 | +``` |
| 87 | +For this command, the declarative plugin will be used instead of the base plugin, |
| 88 | +but following command calls won't use it unless provided again. |
0 commit comments