Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (API, CLI, go/v4) Add cliVersion field to the PROJECT file configuration to store the CLI binary version used to scaffold projects. #4621

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func Run() {
c, err := cli.New(
cli.WithCommandName("kubebuilder"),
cli.WithVersion(versionString()),
cli.WithCliVersion(GetKubebuilderVersion()),
cli.WithPlugins(
golangv4.Plugin{},
gov4Bundle,
Expand Down
12 changes: 11 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type version struct {
GoArch string `json:"goArch"`
}

// versionString returns the CLI version
// versionString returns the Full CLI version
func versionString() string {
if kubeBuilderVersion == unknown {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
Expand All @@ -62,3 +62,13 @@ func versionString() string {
goarch,
})
}

// GetKubebuilderVersion returns only the CLI version string
func GetKubebuilderVersion() string {
if kubeBuilderVersion == unknown {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
kubeBuilderVersion = info.Main.Version
}
}
return kubeBuilderVersion
}
1 change: 1 addition & 0 deletions docs/book/src/cronjob-tutorial/testdata/project/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
cliVersion: (devel)
domain: tutorial.kubebuilder.io
layout:
- go.kubebuilder.io/v4
Expand Down
1 change: 1 addition & 0 deletions docs/book/src/getting-started/testdata/project/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
cliVersion: (devel)
domain: example.com
layout:
- go.kubebuilder.io/v4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
cliVersion: (devel)
domain: tutorial.kubebuilder.io
layout:
- go.kubebuilder.io/v4
Expand Down
3 changes: 3 additions & 0 deletions docs/book/src/reference/project-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Following is an example of a PROJECT config file which is the result of a projec
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
cliVersion: v4.6.0
layout:
- go.kubebuilder.io/v4
plugins:
Expand Down Expand Up @@ -81,6 +82,7 @@ The `PROJECT` version `3` layout looks like:

```yaml
domain: testproject.org
cliVersion: v4.6.0
layout:
- go.kubebuilder.io/v4
plugins:
Expand Down Expand Up @@ -132,6 +134,7 @@ Now let's check its layout fields definition:

| Field | Description |
|-------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `cliVersion` | Used to record the specific CLI version used during project scaffolding with `init`. Helps identifying the version of the tooling employed, aiding in troubleshooting and ensuring compatibility with updates. |
| `layout` | Defines the global plugins, e.g. a project `init` with `--plugins="go/v4,deploy-image/v1-alpha"` means that any sub-command used will always call its implementation for both plugins in a chain. |
| `domain` | Store the domain of the project. This information can be provided by the user when the project is generate with the `init` sub-command and the `domain` flag. |
| `plugins` | Defines the plugins used to do custom scaffolding, e.g. to use the optional `deploy-image/v1-alpha` plugin to do scaffolding for just a specific api via the command `kubebuider create api [options] --plugins=deploy-image/v1-alpha`. |
Expand Down
4 changes: 3 additions & 1 deletion pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ type CLI struct { //nolint:maligned

// Root command name. It is injected downstream to provide correct help, usage, examples and errors.
commandName string
// CLI version string.
// Full CLI version string.
version string
// CLI version string (just the CLI version number, no extra information).
cliVersion string
// CLI root's command description.
description string
// Plugins registered in the CLI.
Expand Down
8 changes: 8 additions & 0 deletions pkg/cli/cmd_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (c *CLI) applySubcommandHooks(
errorMessage: errorMessage,
projectVersion: c.projectVersion,
pluginChain: pluginChain,
cliVersion: c.cliVersion,
}
cmd.PreRunE = factory.preRunEFunc(options, createConfig)
cmd.RunE = factory.runEFunc()
Expand Down Expand Up @@ -189,6 +190,8 @@ type executionHooksFactory struct {
projectVersion config.Version
// pluginChain is the plugin chain configured for this project.
pluginChain []string
// cliVersion is the version of the CLI.
cliVersion string
}

func (factory *executionHooksFactory) forEach(cb func(subcommand plugin.Subcommand) error, errorMessage string) error {
Expand Down Expand Up @@ -244,6 +247,11 @@ func (factory *executionHooksFactory) preRunEFunc(
}
cfg := factory.store.Config()

// Set the CLI version if creating a new project configuration.
if createConfig {
_ = cfg.SetCliVersion(factory.cliVersion)
}

// Set the pluginChain field.
if len(factory.pluginChain) != 0 {
_ = cfg.SetPluginChain(factory.pluginChain)
Expand Down
8 changes: 8 additions & 0 deletions pkg/cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func WithVersion(version string) Option {
}
}

// WithCliVersion is an Option that defines only the version string of the CLI (no extra info).
func WithCliVersion(version string) Option {
return func(c *CLI) error {
c.cliVersion = version
return nil
}
}

// WithDescription is an Option that sets the CLI's root description.
func WithDescription(description string) Option {
return func(c *CLI) error {
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ type Config interface {
// GetVersion returns the current project version.
GetVersion() Version

// GetCliVersion returns the CLI binary version that was used to scaffold or initialize the project.
GetCliVersion() string

// SetCliVersion sets the binary version used to initialize the project.
SetCliVersion(version string) error

/* String fields */

// GetDomain returns the project domain.
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/v3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Cfg struct {
Domain string `json:"domain,omitempty"`
Repository string `json:"repo,omitempty"`
Name string `json:"projectName,omitempty"`
CliVersion string `json:"cliVersion,omitempty"`
PluginChain stringSlice `json:"layout,omitempty"`

// Boolean fields
Expand Down Expand Up @@ -93,6 +94,17 @@ func (c Cfg) GetVersion() config.Version {
return c.Version
}

// GetCliVersion implements config.Config
func (c Cfg) GetCliVersion() string {
return c.CliVersion
}

// SetCliVersion implements config.Config
func (c *Cfg) SetCliVersion(version string) error {
c.CliVersion = version
return nil
}

// GetDomain implements config.Config
func (c Cfg) GetDomain() string {
return c.Domain
Expand Down
1 change: 1 addition & 0 deletions testdata/project-v4-multigroup/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
cliVersion: (devel)
domain: testproject.org
layout:
- go.kubebuilder.io/v4
Expand Down
1 change: 1 addition & 0 deletions testdata/project-v4-with-plugins/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
cliVersion: (devel)
domain: testproject.org
layout:
- go.kubebuilder.io/v4
Expand Down
1 change: 1 addition & 0 deletions testdata/project-v4/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
cliVersion: (devel)
domain: testproject.org
layout:
- go.kubebuilder.io/v4
Expand Down
Loading