Skip to content

Commit 075c9be

Browse files
✨ (API, CLI, go/v4) Add cliVersion field to the PROJECT file configuration to store the CLI binary version used to scaffold projects. (#4621)
Add cliVersion field to PROJECT file This commit adds a new field to the PROJECT file: cliVersion. Additionally, it updates the documentation in the Project Configuration Reference to include the new cliVersion field. It also updates the PROJECT file scaffolded in the docs and in testdata.
1 parent 34e04a0 commit 075c9be

File tree

14 files changed

+58
-2
lines changed

14 files changed

+58
-2
lines changed

cmd/cmd.go

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func Run() {
5555
c, err := cli.New(
5656
cli.WithCommandName("kubebuilder"),
5757
cli.WithVersion(versionString()),
58+
cli.WithCliVersion(GetKubebuilderVersion()),
5859
cli.WithPlugins(
5960
golangv4.Plugin{},
6061
gov4Bundle,

cmd/version.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type version struct {
4545
GoArch string `json:"goArch"`
4646
}
4747

48-
// versionString returns the CLI version
48+
// versionString returns the Full CLI version
4949
func versionString() string {
5050
if kubeBuilderVersion == unknown {
5151
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
@@ -62,3 +62,13 @@ func versionString() string {
6262
goarch,
6363
})
6464
}
65+
66+
// GetKubebuilderVersion returns only the CLI version string
67+
func GetKubebuilderVersion() string {
68+
if kubeBuilderVersion == unknown {
69+
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
70+
kubeBuilderVersion = info.Main.Version
71+
}
72+
}
73+
return kubeBuilderVersion
74+
}

docs/book/src/cronjob-tutorial/testdata/project/PROJECT

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This file is used to track the info used to scaffold your project
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
5+
cliVersion: (devel)
56
domain: tutorial.kubebuilder.io
67
layout:
78
- go.kubebuilder.io/v4

docs/book/src/getting-started/testdata/project/PROJECT

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This file is used to track the info used to scaffold your project
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
5+
cliVersion: (devel)
56
domain: example.com
67
layout:
78
- go.kubebuilder.io/v4

docs/book/src/multiversion-tutorial/testdata/project/PROJECT

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This file is used to track the info used to scaffold your project
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
5+
cliVersion: (devel)
56
domain: tutorial.kubebuilder.io
67
layout:
78
- go.kubebuilder.io/v4

docs/book/src/reference/project-config.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Following is an example of a PROJECT config file which is the result of a projec
1414
# and allow the plugins properly work.
1515
# More info: https://book.kubebuilder.io/reference/project-config.html
1616
domain: testproject.org
17+
cliVersion: v4.6.0
1718
layout:
1819
- go.kubebuilder.io/v4
1920
plugins:
@@ -81,6 +82,7 @@ The `PROJECT` version `3` layout looks like:
8182

8283
```yaml
8384
domain: testproject.org
85+
cliVersion: v4.6.0
8486
layout:
8587
- go.kubebuilder.io/v4
8688
plugins:
@@ -132,6 +134,7 @@ Now let's check its layout fields definition:
132134

133135
| Field | Description |
134136
|-------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
137+
| `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. |
135138
| `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. |
136139
| `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. |
137140
| `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`. |

pkg/cli/cli.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ type CLI struct { //nolint:maligned
4747

4848
// Root command name. It is injected downstream to provide correct help, usage, examples and errors.
4949
commandName string
50-
// CLI version string.
50+
// Full CLI version string.
5151
version string
52+
// CLI version string (just the CLI version number, no extra information).
53+
cliVersion string
5254
// CLI root's command description.
5355
description string
5456
// Plugins registered in the CLI.

pkg/cli/cmd_helpers.go

+8
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func (c *CLI) applySubcommandHooks(
128128
errorMessage: errorMessage,
129129
projectVersion: c.projectVersion,
130130
pluginChain: pluginChain,
131+
cliVersion: c.cliVersion,
131132
}
132133
cmd.PreRunE = factory.preRunEFunc(options, createConfig)
133134
cmd.RunE = factory.runEFunc()
@@ -189,6 +190,8 @@ type executionHooksFactory struct {
189190
projectVersion config.Version
190191
// pluginChain is the plugin chain configured for this project.
191192
pluginChain []string
193+
// cliVersion is the version of the CLI.
194+
cliVersion string
192195
}
193196

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

250+
// Set the CLI version if creating a new project configuration.
251+
if createConfig {
252+
_ = cfg.SetCliVersion(factory.cliVersion)
253+
}
254+
247255
// Set the pluginChain field.
248256
if len(factory.pluginChain) != 0 {
249257
_ = cfg.SetPluginChain(factory.pluginChain)

pkg/cli/options.go

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ func WithVersion(version string) Option {
5757
}
5858
}
5959

60+
// WithCliVersion is an Option that defines only the version string of the CLI (no extra info).
61+
func WithCliVersion(version string) Option {
62+
return func(c *CLI) error {
63+
c.cliVersion = version
64+
return nil
65+
}
66+
}
67+
6068
// WithDescription is an Option that sets the CLI's root description.
6169
func WithDescription(description string) Option {
6270
return func(c *CLI) error {

pkg/config/interface.go

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ type Config interface {
2727
// GetVersion returns the current project version.
2828
GetVersion() Version
2929

30+
// GetCliVersion returns the CLI binary version that was used to scaffold or initialize the project.
31+
GetCliVersion() string
32+
33+
// SetCliVersion sets the binary version used to initialize the project.
34+
SetCliVersion(version string) error
35+
3036
/* String fields */
3137

3238
// GetDomain returns the project domain.

pkg/config/v3/config.go

+12
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type Cfg struct {
6161
Domain string `json:"domain,omitempty"`
6262
Repository string `json:"repo,omitempty"`
6363
Name string `json:"projectName,omitempty"`
64+
CliVersion string `json:"cliVersion,omitempty"`
6465
PluginChain stringSlice `json:"layout,omitempty"`
6566

6667
// Boolean fields
@@ -93,6 +94,17 @@ func (c Cfg) GetVersion() config.Version {
9394
return c.Version
9495
}
9596

97+
// GetCliVersion implements config.Config
98+
func (c Cfg) GetCliVersion() string {
99+
return c.CliVersion
100+
}
101+
102+
// SetCliVersion implements config.Config
103+
func (c *Cfg) SetCliVersion(version string) error {
104+
c.CliVersion = version
105+
return nil
106+
}
107+
96108
// GetDomain implements config.Config
97109
func (c Cfg) GetDomain() string {
98110
return c.Domain

testdata/project-v4-multigroup/PROJECT

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This file is used to track the info used to scaffold your project
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
5+
cliVersion: (devel)
56
domain: testproject.org
67
layout:
78
- go.kubebuilder.io/v4

testdata/project-v4-with-plugins/PROJECT

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This file is used to track the info used to scaffold your project
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
5+
cliVersion: (devel)
56
domain: testproject.org
67
layout:
78
- go.kubebuilder.io/v4

testdata/project-v4/PROJECT

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This file is used to track the info used to scaffold your project
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
5+
cliVersion: (devel)
56
domain: testproject.org
67
layout:
78
- go.kubebuilder.io/v4

0 commit comments

Comments
 (0)