Skip to content

Commit

Permalink
Add --version flag to cluster-compare and helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Ramsay <[email protected]>
  • Loading branch information
lack committed Jan 7, 2025
1 parent a217c8d commit b059aaf
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 7 deletions.
20 changes: 17 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ GO_BUILD_FLAGS_DARWIN :=-tags 'include_gcs include_oss containers_image_openpgp'
GO_BUILD_FLAGS_WINDOWS :=-tags 'include_gcs include_oss containers_image_openpgp'
GO_BUILD_FLAGS_LINUX_CROSS :=-tags 'include_gcs include_oss containers_image_openpgp'

# Inject a version and date via ldflags for the '--version' flag
# Upstream builds get their ldflags set via goreleaser automatically
ifneq ($(strip $(OS_GIT_VERSION)),)
# Downstream builds should have this set:
# OS_GIT_VERSION=4.19.0-202412190006.p0.ga217c8d.assembly.stream.el9
# So use it verbatim for the version string
BUILD_VERSION ?= $(OS_GIT_VERSION)
else
# For manual builds, use 'git describe' based on the latest tag
BUILD_VERSION ?= $(shell git describe --tag | sed -e 's/^v//')
endif
BUILD_DATE ?= $(shell date --rfc-3339=seconds)
GO_LDFLAGS := -ldflags="-X 'main.version=$(BUILD_VERSION)' -X 'main.date=$(BUILD_DATE)'"

OUTPUT_DIR :=_output
GO_BUILD_BINDIR ?=$(OUTPUT_DIR)/bin
CROSS_BUILD_BINDIR ?=$(OUTPUT_DIR)/bin
Expand Down Expand Up @@ -42,7 +56,7 @@ endif
.PHONY: build
build:
mkdir -p $(GO_BUILD_BINDIR)
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -mod=vendor $(GO_BUILD_FLAGS) -o $(GO_BUILD_BINDIR)/kubectl-cluster_compare ./cmd/kubectl-cluster_compare.go
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -mod=vendor $(GO_BUILD_FLAGS) $(GO_LDFLAGS) -o $(GO_BUILD_BINDIR)/kubectl-cluster_compare ./cmd/kubectl-cluster_compare.go

# Install the plugin and completion script in /usr/local/bin
.PHONY: install
Expand All @@ -63,11 +77,11 @@ test-report-creator:

.PHONY: build-report-creator
build-report-creator:
go build ./addon-tools/report-creator/report-creator.go
go build $(GO_LDFLAGS) ./addon-tools/report-creator/report-creator.go

.PHONY: build-helm-convert
build-helm-convert:
go build ./addon-tools/helm-convert/helm-convert.go
go build $(GO_LDFLAGS) ./addon-tools/helm-convert/helm-convert.go

.PHONY: test-helm-convert
test-helm-convert:
Expand Down
6 changes: 6 additions & 0 deletions addon-tools/helm-convert/helm-convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import (
"github.com/openshift/kube-compare/addon-tools/helm-convert/convert"
)

var (
version = "unreleased"
date = "unknown"
)

func main() {
cmd := convert.NewCmd()
cmd.Version = fmt.Sprintf("%s (%s)", version, date)
if err := cmd.Execute(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "There was an error: '%s'", err)
os.Exit(1)
Expand Down
6 changes: 6 additions & 0 deletions addon-tools/report-creator/report-creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import (
"github.com/openshift/kube-compare/addon-tools/report-creator/report"
)

var (
version = "unreleased"
date = "unknown"
)

func main() {
cmd := report.NewCmd()
cmd.Version = fmt.Sprintf("%s (%s)", version, date)
if err := cmd.Execute(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "There was an error: '%s'", err)
os.Exit(1)
Expand Down
7 changes: 7 additions & 0 deletions cmd/kubectl-cluster_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"fmt"
"os"

"github.com/openshift/kube-compare/pkg/compare"
Expand All @@ -11,11 +12,17 @@ import (
kcmdutil "k8s.io/kubectl/pkg/cmd/util"
)

var (
version = "unreleased"
date = "unknown"
)

func main() {
ioStreams := genericiooptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
configFlags := genericclioptions.NewConfigFlags(true)
f := kcmdutil.NewFactory(configFlags)
compareCmd := compare.NewCmd(f, ioStreams)
compareCmd.Version = fmt.Sprintf("%s (%s)", version, date)
if err := compareCmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func NewCmd(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Comma
}

cmd := &cobra.Command{
Use: "compare -r <Reference File>",
Use: "cluster-compare -r <Reference File>",
DisableFlagsInUseLine: true,
Short: i18n.T("Compare a reference configuration and a set of cluster configuration CRs."),
Long: compareLong,
Expand Down
2 changes: 1 addition & 1 deletion pkg/compare/testdata/NoInput/localerr.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
error: "Reference config file is required"
See 'compare -h' for help and examples
See 'cluster-compare -h' for help and examples
error code:2
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
error: Requested user override generation but no entires for which template to generate overrides for
See 'compare -h' for help and examples
See 'cluster-compare -h' for help and examples
error code:2
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
error: Reason required when generating overrides
See 'compare -h' for help and examples
See 'cluster-compare -h' for help and examples
error code:2

0 comments on commit b059aaf

Please sign in to comment.