Skip to content

Commit edb23a1

Browse files
committed
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 3f4d5f4 commit edb23a1

File tree

16 files changed

+67
-9
lines changed

16 files changed

+67
-9
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ help: ## Display this help
4646
##@ Build
4747

4848
LD_FLAGS=-ldflags " \
49-
-X sigs.k8s.io/kubebuilder/v4/cmd.kubeBuilderVersion=$(shell git describe --tags --dirty --broken) \
49+
-X sigs.k8s.io/kubebuilder/v4/cmd.cliVersion=$(shell git describe --tags --dirty --broken) \
5050
-X sigs.k8s.io/kubebuilder/v4/cmd.goos=$(shell go env GOOS) \
5151
-X sigs.k8s.io/kubebuilder/v4/cmd.goarch=$(shell go env GOARCH) \
5252
-X sigs.k8s.io/kubebuilder/v4/cmd.gitCommit=$(shell git rev-parse HEAD) \

Diff for: build/.goreleaser.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ builds:
3232
binary: kubebuilder
3333
mod_timestamp: "{{ .CommitTimestamp }}"
3434
ldflags:
35-
- -X sigs.k8s.io/kubebuilder/v4/cmd.kubeBuilderVersion={{ .Version }}
35+
- -X sigs.k8s.io/kubebuilder/v4/cmd.cliVersion={{ .Version }}
3636
- -X sigs.k8s.io/kubebuilder/v4/cmd.goos={{ .Os }}
3737
- -X sigs.k8s.io/kubebuilder/v4/cmd.goarch={{ .Arch }}
3838
- -X sigs.k8s.io/kubebuilder/v4/cmd.gitCommit={{ .Commit }}

Diff for: 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,

Diff for: cmd/version.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const unknown = "unknown"
2626
// var needs to be used instead of const as ldflags is used to fill this
2727
// information in the release process
2828
var (
29-
kubeBuilderVersion = unknown
29+
cliVersion = unknown
3030
kubernetesVendorVersion = unknown
3131
goos = unknown
3232
goarch = unknown
@@ -37,28 +37,38 @@ var (
3737

3838
// version contains all the information related to the CLI version
3939
type version struct {
40-
KubeBuilderVersion string `json:"kubeBuilderVersion"`
40+
CliVersion string `json:"cliVersion"`
4141
KubernetesVendor string `json:"kubernetesVendor"`
4242
GitCommit string `json:"gitCommit"`
4343
BuildDate string `json:"buildDate"`
4444
GoOs string `json:"goOs"`
4545
GoArch string `json:"goArch"`
4646
}
4747

48-
// versionString returns the CLI version
48+
// versionString returns the Full CLI version
4949
func versionString() string {
50-
if kubeBuilderVersion == unknown {
50+
if cliVersion == unknown {
5151
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
52-
kubeBuilderVersion = info.Main.Version
52+
cliVersion = info.Main.Version
5353
}
5454
}
5555

5656
return fmt.Sprintf("Version: %#v", version{
57-
kubeBuilderVersion,
57+
cliVersion,
5858
kubernetesVendorVersion,
5959
gitCommit,
6060
buildDate,
6161
goos,
6262
goarch,
6363
})
6464
}
65+
66+
// CliVersionString returns only the CLI version string
67+
func CliVersionString() string {
68+
if cliVersion == unknown {
69+
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
70+
cliVersion = info.Main.Version
71+
}
72+
}
73+
return cliVersion
74+
}

Diff for: 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

Diff for: 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

Diff for: 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

Diff for: 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`. |

Diff for: 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+
// CLIi 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.

Diff for: 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)

Diff for: 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 {

Diff for: 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.

Diff for: 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

Diff for: 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

Diff for: 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

Diff for: 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)