diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 5cd3838..1ced73c 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -12,9 +12,6 @@ permissions: contents: write packages: write -env: - CGO_ENABLED: "0" - jobs: release-matrix: name: Release binaries @@ -35,3 +32,4 @@ jobs: goarch: ${{ matrix.goarch }} md5sum: FALSE compress_assets: OFF + build_command: make diff --git a/Containerfile b/Containerfile index a4270ac..a97f6ce 100644 --- a/Containerfile +++ b/Containerfile @@ -14,10 +14,11 @@ RUN go mod download COPY cmd cmd COPY pkg pkg COPY main.go main.go +ARG ldflags # Disable CGO to avoid dependencies on libc. Built image can be built on latest # Fedora and run on old RHEL. -RUN CGO_ENABLED=0 go build +RUN CGO_ENABLED=0 go build -ldflags "${ldflags}" FROM docker.io/library/alpine:latest diff --git a/Makefile b/Makefile index 50e9b47..2f75854 100644 --- a/Makefile +++ b/Makefile @@ -4,19 +4,28 @@ REGISTRY ?= quay.io REPO ?= nirsof IMAGE ?= gather -TAG ?= 0.5.1 -image := $(REGISTRY)/$(REPO)/$(IMAGE):$(TAG) +pkg_path := github.com/nirs/kubectl-gather/pkg/gather + +# 0.5.1 when building from tag (release) +# 0.5.1-1-gcf79160 when building without tag (development) +version := $(shell git describe --tags | sed -e 's/^v//') + +# The container image uses the same version. For testing the --remote option +# you need to push the container to the registry. +image := $(REGISTRY)/$(REPO)/$(IMAGE):$(version) + +ldflags := -X '$(pkg_path).Version=$(version)' -X '$(pkg_path).Image=$(image)' .PHONY: all kubectl-gather all: kubectl-gather container: - podman build -t $(image) . + podman build --tag $(image) --build-arg ldflags="$(ldflags)" . container-push: container podman push $(image) kubectl-gather: - CGO_ENABLED=0 go build + CGO_ENABLED=0 go build -ldflags "$(ldflags)" diff --git a/cmd/remote.go b/cmd/remote.go index ae34451..3dbdc5e 100644 --- a/cmd/remote.go +++ b/cmd/remote.go @@ -14,8 +14,6 @@ import ( "time" ) -const remoteDefaultImage = "quay.io/nirsof/gather:0.5.1" - func remoteGather(clusters []*clusterConfig) { start := time.Now() @@ -87,7 +85,7 @@ func mustGatherCommand(context string, directory string) *exec.Cmd { args := []string{ "adm", "must-gather", - "--image=" + remoteDefaultImage, + "--image=" + remoteImage, "--context=" + context, "--dest-dir=" + directory, } diff --git a/cmd/root.go b/cmd/root.go index 45da2a9..1aaa96c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -25,6 +25,7 @@ var contexts []string var namespaces []string var addons []string var remote bool +var remoteImage string var verbose bool var log *zap.SugaredLogger @@ -51,6 +52,7 @@ var example = ` # Gather data from all namespaces in current context in my-kube var rootCmd = &cobra.Command{ Use: "kubectl-gather", Short: "Gather data from clusters", + Version: gather.Version, Example: example, Annotations: map[string]string{ cobra.CommandDisplayNameAnnotation: "kubectl gather", @@ -84,8 +86,13 @@ func init() { availableAddons())) rootCmd.Flags().BoolVarP(&remote, "remote", "r", false, "run on the remote clusters (requires the \"oc\" command)") + rootCmd.Flags().StringVar(&remoteImage, "remote-image", gather.Image, + "image to use when running on remote clusters") rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "be more verbose") + + // Use plain machine friendly version string. + rootCmd.SetVersionTemplate("{{.Version}}\n") } func runGather(cmd *cobra.Command, args []string) { diff --git a/gather b/gather index df9ce1e..1e78efc 100755 --- a/gather +++ b/gather @@ -6,6 +6,7 @@ base="/must-gather" mkdir -p "$base" -printf "gather\n0.1.0\n" > "$base/version" -/usr/bin/kubectl-gather --directory "$base" "$@" +printf "gather\n$(kubectl-gather --version)\n" > "$base/version" + +kubectl-gather --directory "$base" "$@" diff --git a/pkg/gather/gather.go b/pkg/gather/gather.go index 5c1b8e1..88a7a89 100644 --- a/pkg/gather/gather.go +++ b/pkg/gather/gather.go @@ -32,6 +32,10 @@ import ( // TODO: Needs more testing to find the optimal value. const listResourcesLimit = 100 +// Replaced during build with actual values. +var Version = "latest" +var Image = "quay.io/nirsof/gather:latest" + type Options struct { Kubeconfig string Context string