From 98358cec5a8217aa7e15074ea46b14567c2dec12 Mon Sep 17 00:00:00 2001 From: Fabio Falzoi Date: Fri, 19 May 2023 15:09:23 +0200 Subject: [PATCH] cli: Export Version var to be set externally When building an extended version of cilium-cli with hooks, it is not possible to set Version var with -ldflags because it is located in an internal package. This commit exports the Version var in the cli package to allow setting it both here and in the extended cilium-cli binaries. Signed-off-by: Fabio Falzoi --- Makefile | 4 ++-- cli/cmd.go | 7 +++++++ internal/cli/cmd/cmd.go | 8 ++++++++ internal/cli/cmd/connectivity.go | 2 +- internal/cli/cmd/install.go | 4 ++-- internal/cli/cmd/sysdump.go | 2 +- internal/cli/cmd/version.go | 8 +------- 7 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 8d23b3721a..f266608b48 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ GOLANGCILINT_VERSION = $(shell golangci-lint version 2>/dev/null) $(TARGET): $(GO_BUILD) $(if $(GO_TAGS),-tags $(GO_TAGS)) \ -ldflags "-w -s \ - -X 'github.com/cilium/cilium-cli/internal/cli/cmd.Version=${VERSION}'" \ + -X 'github.com/cilium/cilium-cli/cli.Version=${VERSION}'" \ -o $(TARGET) \ ./cmd/cilium @@ -54,7 +54,7 @@ local-release: clean echo Building release binary for $$OS/$$ARCH...; \ test -d release/$$OS/$$ARCH|| mkdir -p release/$$OS/$$ARCH; \ env GOOS=$$OS GOARCH=$$ARCH $(GO_BUILD) $(if $(GO_TAGS),-tags $(GO_TAGS)) \ - -ldflags "-w -s -X 'github.com/cilium/cilium-cli/internal/cli/cmd.Version=${VERSION}'" \ + -ldflags "-w -s -X 'github.com/cilium/cilium-cli/cli.Version=${VERSION}'" \ -o release/$$OS/$$ARCH/$(TARGET)$$EXT ./cmd/cilium; \ tar -czf release/$(TARGET)-$$OS-$$ARCH.tar.gz -C release/$$OS/$$ARCH $(TARGET)$$EXT; \ (cd release && sha256sum $(TARGET)-$$OS-$$ARCH.tar.gz > $(TARGET)-$$OS-$$ARCH.tar.gz.sha256sum); \ diff --git a/cli/cmd.go b/cli/cmd.go index 6227025b95..3ab63fe7a9 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -12,6 +12,12 @@ import ( "github.com/cilium/cilium-cli/sysdump" ) +// The following variables are set at compile time via LDFLAGS. +var ( + // Version is the software version. + Version string +) + // NewDefaultCiliumCommand returns a new "cilium" cli cobra command without any additional hooks. func NewDefaultCiliumCommand() *cobra.Command { return NewCiliumCommand(&NopHooks{}) @@ -19,6 +25,7 @@ func NewDefaultCiliumCommand() *cobra.Command { // NewCiliumCommand returns a new "cilium" cli cobra command registering all the additional input hooks. func NewCiliumCommand(hooks Hooks) *cobra.Command { + cmd.SetVersion(Version) return cmd.NewCiliumCommand(hooks) } diff --git a/internal/cli/cmd/cmd.go b/internal/cli/cmd/cmd.go index 1db3c09f2b..e3c74337d1 100644 --- a/internal/cli/cmd/cmd.go +++ b/internal/cli/cmd/cmd.go @@ -18,8 +18,16 @@ var ( namespace string k8sClient *k8s.Client + + // version is the version string of the cilium-cli itself + version string ) +// SetVersion sets the version string for the cilium command +func SetVersion(v string) { + version = v +} + func NewCiliumCommand(hooks Hooks) *cobra.Command { cmd := &cobra.Command{ PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { diff --git a/internal/cli/cmd/connectivity.go b/internal/cli/cmd/connectivity.go index bc6c6777fe..c5fa9e1f23 100644 --- a/internal/cli/cmd/connectivity.go +++ b/internal/cli/cmd/connectivity.go @@ -71,7 +71,7 @@ func newCmdConnectivityTest(hooks Hooks) *cobra.Command { } // Instantiate the test harness. - cc, err := check.NewConnectivityTest(k8sClient, params, Version) + cc, err := check.NewConnectivityTest(k8sClient, params, version) if err != nil { return err } diff --git a/internal/cli/cmd/install.go b/internal/cli/cmd/install.go index ab418c0e38..55b8fdd2d3 100644 --- a/internal/cli/cmd/install.go +++ b/internal/cli/cmd/install.go @@ -144,7 +144,7 @@ func newCmdUninstall() *cobra.Command { TestNamespace: params.TestNamespace, FlowValidation: check.FlowValidationModeDisabled, Writer: os.Stdout, - }, Version) + }, version) if err != nil { fmt.Printf("⚠ ️ Failed to initialize connectivity test uninstaller: %s", err) } else { @@ -311,7 +311,7 @@ func newCmdUninstallWithHelm() *cobra.Command { TestNamespace: params.TestNamespace, FlowValidation: check.FlowValidationModeDisabled, Writer: os.Stdout, - }, Version) + }, version) if err != nil { fmt.Printf("⚠ ️ Failed to initialize connectivity test uninstaller: %s", err) } else { diff --git a/internal/cli/cmd/sysdump.go b/internal/cli/cmd/sysdump.go index 2fdc5d5d94..1d2685c2bc 100644 --- a/internal/cli/cmd/sysdump.go +++ b/internal/cli/cmd/sysdump.go @@ -36,7 +36,7 @@ func newCmdSysdump(hooks SysdumpHooks) *cobra.Command { // Silence klog to avoid displaying "throttling" messages - those are expected. klog.SetOutput(io.Discard) // Collect the sysdump. - collector, err := sysdump.NewCollector(k8sClient, sysdumpOptions, time.Now(), Version) + collector, err := sysdump.NewCollector(k8sClient, sysdumpOptions, time.Now(), version) if err != nil { return fmt.Errorf("failed to create sysdump collector: %w", err) } diff --git a/internal/cli/cmd/version.go b/internal/cli/cmd/version.go index b51cacd7a7..8d0cf77848 100644 --- a/internal/cli/cmd/version.go +++ b/internal/cli/cmd/version.go @@ -16,12 +16,6 @@ import ( "github.com/cilium/cilium-cli/defaults" ) -// The following variables are set at compile time via LDFLAGS. -var ( - // Version is the software version. - Version string -) - func getLatestStableVersion() string { resp, err := http.Get("https://raw.githubusercontent.com/cilium/cilium/main/stable.txt") if err != nil { @@ -44,7 +38,7 @@ func newCmdVersion() *cobra.Command { Short: "Display detailed version information", Long: `Displays information about the version of this software.`, RunE: func(cmd *cobra.Command, args []string) error { - fmt.Printf("cilium-cli: %s compiled with %v on %v/%v\n", Version, runtime.Version(), runtime.GOOS, runtime.GOARCH) + fmt.Printf("cilium-cli: %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH) fmt.Printf("cilium image (default): %s\n", defaults.Version) fmt.Printf("cilium image (stable): %s\n", getLatestStableVersion()) if clientOnly {