Skip to content

Commit dff6ef1

Browse files
committed
Add kubebuilderVersion field to PROJECT file
This commit adds a new field to the PROJECT file: kubebuilderVersion. Additionally, it updates the documentation in the Project Configuration Reference to include the new kubebuilderVersion field. It also updates the PROJECT file scaffolded in the docs and in testdata.
1 parent 3f4d5f4 commit dff6ef1

File tree

14 files changed

+60
-2
lines changed

14 files changed

+60
-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(CliVersionString()),
5859
cli.WithPlugins(
5960
golangv4.Plugin{},
6061
gov4Bundle,

cmd/version.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const unknown = "unknown"
2727
// information in the release process
2828
var (
2929
kubeBuilderVersion = unknown
30-
kubernetesVendorVersion = unknown
30+
kubernetesVendorVersion = "1.32.1"
3131
goos = unknown
3232
goarch = unknown
3333
gitCommit = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
@@ -62,3 +62,13 @@ func versionString() string {
6262
goarch,
6363
})
6464
}
65+
66+
// KubeBuilderVersionString returns just the KubeBuilder version string
67+
func CliVersionString() 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
@@ -3,6 +3,7 @@
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
55
domain: tutorial.kubebuilder.io
6+
kubebuilderVersion: (devel)
67
layout:
78
- go.kubebuilder.io/v4
89
plugins:

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
55
domain: example.com
6+
kubebuilderVersion: (devel)
67
layout:
78
- go.kubebuilder.io/v4
89
plugins:

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
55
domain: tutorial.kubebuilder.io
6+
kubebuilderVersion: (devel)
67
layout:
78
- go.kubebuilder.io/v4
89
plugins:

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+
kubebuilderVersion: 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+
kubebuilderVersion: 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+
| `kubebuilderVersion` | 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+
// Clean 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 kubebuilder 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+
// WithCleanVersion is an Option that defines the clean version string of the CLI.
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

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

30+
// GetCliVersion returns the kubebuilder version used to initialize the project.
31+
// This method was introduced in project version 3.
32+
GetCliVersion() string
33+
34+
// SetCliVersion sets the kubebuilder version used to initialize the project.
35+
//This method was introduced in project version 3.
36+
SetCliVersion(version string) error
37+
3038
/* String fields */
3139

3240
// 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+
KubebuilderVersion string `json:"kubebuilderVersion,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+
// GetKubebuilderVersion implements config.Config
98+
func (c Cfg) GetCliVersion() string {
99+
return c.KubebuilderVersion
100+
}
101+
102+
// SetKubebuilderVersion implements config.Config
103+
func (c *Cfg) SetCliVersion(version string) error {
104+
c.KubebuilderVersion = 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
@@ -3,6 +3,7 @@
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
55
domain: testproject.org
6+
kubebuilderVersion: (devel)
67
layout:
78
- go.kubebuilder.io/v4
89
multigroup: true

testdata/project-v4-with-plugins/PROJECT

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
55
domain: testproject.org
6+
kubebuilderVersion: (devel)
67
layout:
78
- go.kubebuilder.io/v4
89
plugins:

testdata/project-v4/PROJECT

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
55
domain: testproject.org
6+
kubebuilderVersion: (devel)
67
layout:
78
- go.kubebuilder.io/v4
89
projectName: project-v4

0 commit comments

Comments
 (0)