Skip to content

Conversation

@estroz
Copy link
Contributor

@estroz estroz commented Mar 3, 2020

Description of Change:

  • pkg/cli: --plugins allows a binary invoker to select which plugins known to a CLI to run; this flag can be set with a shortened (non-fully-qualified) name. Either a plugin name passed to --plugins or the name set in an initialized project's config layout, in order of precedence, is resolved to a known plugin.
  • pkg/model/config: add project file layout key, containing the Init plugin key that scaffolded a project
  • pkg/{cli,plugin}: add plugin name validation
  • pkg/plugin/v2: set layout key in config

Motivation for Change: finishes up phase 1 of plugin implementation. The layout key determines which plugin (name and version) initialized a project, useful for a plugin implementation to deduce which post-init plugins should be called. --plugins/layout, in combination with a default plugin, are necessary to select which plugin to use if there are multiple viable plugins per project version.

TODO:

  • instead of getting first *Getter type, infer by config layout or default (in the case of Init)
  • unit tests in pkg/cli

/cc @Adirio @hasbro17 @camilamacedo86
/kind feature

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 3, 2020
@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 3, 2020
@estroz estroz force-pushed the feature/plugins-config-layout branch from 7b3628b to 98b7b5f Compare March 4, 2020 07:51
@estroz estroz changed the title plugins: config layout and plugin name validation [wip] plugins: config layout and plugin name validation Mar 4, 2020
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 4, 2020
Copy link
Contributor

@Adirio Adirio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to move to scaffold v3 (or v3-alpha) to implement the plugins system. We definetely will need to move to kubebuilder v3.y.z. This will influence some of the following comments.

In order to keep it in v2 we need to make it backwards compatible. Providing a default value for layout in case the project file doesn't have it has already be done for the version field, that defaults to v1 at read time as v1 projects didn't have that field. In this case it will only be needed to set the layout field to the default go plugin when reading a config file that has version: "2" but no layout field.

cmd/edit.go Outdated

func (o *editOptions) Validate(c *config.Config) error {
if !c.IsV2() {
if c.IsV1() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Remember I consider we should not use v3-alpha)

Right now they are functionally equivalents but they have different semantics. !c.IsV2() makes no assumption about how following versions will work while c.IsV1() makes the sumption that following versions will implement the multi-group flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is more for convenience, but I'll explicitly check v2 and v3 if we decide to stick with v3.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// Default version if flag not provided
DefaultVersion = config.Version2
DefaultVersion = config.Version3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Remember I consider we should not use v3-alpha)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg/cli/cli.go Outdated
Comment on lines 86 to 87
pluginsFromOptions: map[string][]plugin.Base{},
cliPluginKeys: map[string]string{},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that linters prefer the following:

Suggested change
pluginsFromOptions: map[string][]plugin.Base{},
cliPluginKeys: map[string]string{},
pluginsFromOptions: make(map[string][]plugin.Base),
cliPluginKeys: make(map[string]string),

Comment on lines 124 to 128
// Only V3 projects get a layout.
if p.config.IsV3() {
// Init plugins set the config layout.
p.config.Layout = plugin.Key(pluginName, pluginVersion)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Remember I consider we should not use v3-alpha)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pluginVersion = "v2.0.0"
)

var supportedProjectVersions = []string{"2", "3-alpha"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Remember I consider we should not use v3-alpha)

@kubernetes-sigs kubernetes-sigs deleted a comment from Adirio Mar 4, 2020
@estroz
Copy link
Contributor Author

estroz commented Mar 4, 2020

I'm fine with keeping config version 2, although the layout key represents a breaking change to a config's structure. This should reasonably be handled by kubebuilder version though.

@DirectXMan12 @mengqiy @joelanford thoughts on changing config version to 3-alpha vs going ahead with 2?

Copy link
Contributor

@Adirio Adirio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some additional questions and thoughts

@estroz estroz force-pushed the feature/plugins-config-layout branch from f7d3815 to 75e1ce8 Compare March 5, 2020 19:45
@estroz estroz force-pushed the feature/plugins-config-layout branch 4 times, most recently from 8a654d2 to 23415cf Compare March 20, 2020 21:52
@estroz estroz changed the title [wip] plugins: config layout and plugin name validation feature/plugins: config layout, --plugins flag, and plugin name validation Mar 20, 2020
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 20, 2020
@estroz estroz changed the title feature/plugins: config layout, --plugins flag, and plugin name validation [wip] feature/plugins: config layout, --plugins flag, and plugin name validation Mar 20, 2020
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 20, 2020
@estroz estroz force-pushed the feature/plugins-config-layout branch 3 times, most recently from 234e8ad to 1151b04 Compare March 21, 2020 06:00
@estroz estroz changed the title [wip] feature/plugins: config layout, --plugins flag, and plugin name validation feature/plugins: config layout, --plugins flag, and plugin name validation Mar 21, 2020
Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not againt the master so:

/lgtm
/approve

We will come back to re-check all plugin impl in the PR against master which will be easier to verify all.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 23, 2020
@estroz
Copy link
Contributor Author

estroz commented Mar 23, 2020

/assign @mengqiy

@camilamacedo86
Copy link
Member

/approve

@estroz estroz requested a review from camilamacedo86 March 24, 2020 03:14
@camilamacedo86
Copy link
Member

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: camilamacedo86, estroz

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 24, 2020
@estroz estroz force-pushed the feature/plugins-config-layout branch from da1069b to 2a165d8 Compare March 24, 2020 16:23
@k8s-ci-robot k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Mar 24, 2020
@estroz estroz force-pushed the feature/plugins-config-layout branch from 2a165d8 to 6997218 Compare March 24, 2020 16:35
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Mar 24, 2020
@camilamacedo86
Copy link
Member

camilamacedo86 commented Mar 24, 2020

Hi @estroz,

It is not passing now, after the rebase, in the testdata check. You need run make generate locally to update the testdata dir with your changes.

should run for a subcommand. This option is global. Refactoring of
getBaseFlags -> parseBaseFlags allows multiple global flags to be
parsed for a cli struct. plugin filtering considers short names,
ex. --plugins="go" rather than --plugins="go.kubebuilder.io/v2.0.0".
WithDefaultPlugins sets the default plugins a CLi should use in case
--plugins or the config 'layout' are not set

pkg/model/config: Config support the 'layout' key which indicates the
plugin that scaffolded a project, by plugin key

pkg/plugin: SplitKey, KeyFor, Key, GetShortName are utility functions
for plugin keys

* pkg/{cli,plugin}: add plugin name validation

* pkg/plugin/{v1,v2}: set layout key in config

 Please enter the commit message for your changes. Lines starting
@estroz estroz force-pushed the feature/plugins-config-layout branch from 6997218 to f460cc7 Compare March 24, 2020 16:56
@camilamacedo86
Copy link
Member

Hi @estroz,

The GH did not get the Travis result. If you close and re-open this one the tests will be re-trigged.

@estroz estroz closed this Mar 25, 2020
@estroz estroz reopened this Mar 25, 2020
Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we spoke:

/lgtm

Since it is to merge in the kubernetes-sigs:feature/plugins-part-2-electric-boogaloo and not master.
And then we can check the tests in a follow-up pr.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 25, 2020
@k8s-ci-robot k8s-ci-robot merged commit 505e23d into kubernetes-sigs:feature/plugins-part-2-electric-boogaloo Mar 25, 2020
@estroz estroz deleted the feature/plugins-config-layout branch March 25, 2020 01:57

```yaml
version: "3-alpha"
version: "2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DirectXMan12
I understand that the version here will still be 2 since we have not broken changes in the layout. However, kb tag will need to be bumped to 3.0.0. Am I right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was pushback against upgrading to "3-alpha". Regardless of whether we bump I figured it was better to make the change in a follow-up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's ok, but I think if we're gonna make a field mandatory that's a change in version.

func (c *cli) initialize() error {
// Initialize cli with globally-relevant flags or flags that determine
// certain plugin type's configuration.
if err := c.parseBaseFlags(); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this globally? isn't this just needed on the init invocation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CLI needs to pre-parse the --plugins global flag for each command, so I figured wrapping all flag pre-parsing in one method made sense. The cli.doGenericHelp flag is set by cli.parseBaseFlags so init can still print generic help if --project-version is not set.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CLI needs to pre-parse the --plugins global flag for each command,

wait, does it? I thought we figured out that it didn't...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of discussion: having a global --plugins flag is not in scope for phase 1 since the type of plugin used in init solely determines what plugin is used for subsequent commands (the layout config key). A global --plugins flag will be discussed in phase 2: #1378.

case len(c.cliPluginKeys) != 0:
// Filter plugins by keys passed in CLI.
filterKeys = c.cliPluginKeys
case c.configured && !projectConfig.IsV1():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this break current v2 projects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will, and probably a good reason to upgrade to project version "3-alpha".

if err != nil {
return err
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if filterKeys is not set here? (i.e. if there's no plugins set for options)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(if we check elsewhere, add a comment here)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(it's also maybe a good indication that we're failing at the principle of "parse instead of validate")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cli.resolvedPlugins defaults to the default plugins for a project version if filterKeys is empty. Default plugins are ensured to exist by validating them here. Does this still fail parse instead of validate")? I'll add a comment describing this scenario if not.

Looks like the case when c.configured && projectConfig.IsV1() == true is not validated however. Making a follow-up now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants