Skip to content

Commit

Permalink
WIP: Configurable version and image
Browse files Browse the repository at this point in the history
Replace the hard coded version and image name using git describe.

When building from tag (e.g. v0.5.1) the version (0.5.1) and the image
(quay.io/nirsof/gather:0.5.1) are injected into the executable and used
to report the version and for the --remote option.

When building without a tag, the version includes the number of commits
since the last tag, and a short git commit hash (v0.5.1-1-gcf79160).

The executable will use the image with the same version from the
registry, so we need to push the image to the registry for testing the
--remote option.

A new --version option added, reporting gather version.

A new --image option added for setting the image for the --remote
option. This is useful for testing the latest release image without
pushing the image to the registry, for testing and image from your
registry, or for using custom image on multiple clusters.

The gather script use the version option to generate
/must-gather/version file instead of the hard-coded version.

The build command is complicated now, so we use make to build the
releases. This ensures also that local builds are builds exactly like
release builds and makes it easier to debug release issues.
  • Loading branch information
nirs committed Aug 24, 2024
1 parent cf79160 commit cd8406a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ permissions:
contents: write
packages: write

env:
CGO_ENABLED: "0"

jobs:
release-matrix:
name: Release binaries
Expand All @@ -35,3 +32,4 @@ jobs:
goarch: ${{ matrix.goarch }}
md5sum: FALSE
compress_assets: OFF
build_command: make
3 changes: 2 additions & 1 deletion Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 13 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
4 changes: 1 addition & 3 deletions cmd/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"time"
)

const remoteDefaultImage = "quay.io/nirsof/gather:0.5.1"

func remoteGather(clusters []*clusterConfig) {
start := time.Now()

Expand Down Expand Up @@ -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,
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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",
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions gather
Original file line number Diff line number Diff line change
Expand Up @@ -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" "$@"
4 changes: 4 additions & 0 deletions pkg/gather/gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd8406a

Please sign in to comment.