version/verflag: make program name variable

Instead of hard-coding `Kubernetes` in the output generated when
executing a binary using the `version/verflag` feature using
`--version`, use a variable `programName` which can be overwritten using
the `-X` linker flag, e.g.:

```
go build -ldflags "-X k8s.io/component-base/version/verflag.programName=my-tool" ...
```

This allows to use the package in non-Kubernetes applications.

Signed-off-by: Nicolas Trangez <ikke@nicolast.be>
See: https://github.com/kubernetes/kubernetes/issues/90138
This commit is contained in:
Nicolas Trangez 2020-04-14 17:52:44 +02:00
parent 250884c9c1
commit dd934c9ebd

View File

@ -89,6 +89,7 @@ const versionFlagName = "version"
var (
versionFlag = Version(versionFlagName, VersionFalse, "Print version information and quit")
programName = "Kubernetes"
)
// AddFlags registers this package's flags on arbitrary FlagSets, such that they point to the
@ -104,7 +105,7 @@ func PrintAndExitIfRequested() {
fmt.Printf("%#v\n", version.Get())
os.Exit(0)
} else if *versionFlag == VersionTrue {
fmt.Printf("Kubernetes %s\n", version.Get())
fmt.Printf("%s %s\n", programName, version.Get())
os.Exit(0)
}
}