From 6e56b1f38a342f668bbc1994ddf7db37ff3c4377 Mon Sep 17 00:00:00 2001 From: Evan Johnson Date: Wed, 14 Feb 2024 11:21:39 -0500 Subject: [PATCH] add version to controller and include it in user-agent for linode api requests --- Dockerfile | 4 +++- Makefile | 9 +++++---- Tiltfile | 7 +++++-- cloud/scope/common.go | 4 ++++ cmd/main.go | 5 ++++- version/version.go | 12 ++++++++++++ 6 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 version/version.go diff --git a/Dockerfile b/Dockerfile index acde2d27e..ccb01a2b3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,7 @@ FROM golang:1.22 as builder ARG TARGETOS ARG TARGETARCH +ARG VERSION WORKDIR /workspace # Copy the Go Modules manifests @@ -17,13 +18,14 @@ COPY api/ api/ COPY controller/ controller/ COPY cloud/ cloud/ COPY util/ util/ +COPY version/ version/ # Build # the GOARCH has not a default value to allow the binary be built according to the host where the command # was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO # the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, # by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. -RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags="-X github.com/linode/cluster-api-provider-linode/version.version=$VERSION" -a -o manager cmd/main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details diff --git a/Makefile b/Makefile index 73054b785..625ae2053 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,8 @@ ARCH_SHORT := amd64 else ifeq ($(ARCH_SHORT),aarch64) ARCH_SHORT := arm64 endif - +VERSION ?= $(shell git describe --tags --dirty=-dev) +BUILD_ARGS := --build-arg VERSION=$(VERSION) # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) ifeq (,$(shell go env GOBIN)) GOBIN=$(shell go env GOPATH)/bin @@ -104,7 +105,7 @@ _e2etest: manifests generate envsubst _e2etest-infra .PHONY: build build: manifests generate fmt vet ## Build manager binary. - go build -o bin/manager cmd/main.go + go build -ldflags="-X github.com/linode/cluster-api-provider-linode/version.version=$(VERSION)" -o bin/manager cmd/main.go .PHONY: run run: manifests generate fmt vet ## Run a controller from your host. @@ -115,7 +116,7 @@ run: manifests generate fmt vet ## Run a controller from your host. # More info: https://docs.docker.com/develop/develop-images/build_enhancements/ .PHONY: docker-build docker-build: ## Build docker image with the manager. - $(CONTAINER_TOOL) build -t ${IMG} . + $(CONTAINER_TOOL) build $(BUILD_ARGS) -t ${IMG} . .PHONY: docker-push docker-push: ## Push docker image with the manager. @@ -134,7 +135,7 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross - $(CONTAINER_TOOL) buildx create --name project-v3-builder $(CONTAINER_TOOL) buildx use project-v3-builder - - $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross . + - $(CONTAINER_TOOL) buildx build $(BUILD_ARGS) --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross . - $(CONTAINER_TOOL) buildx rm project-v3-builder rm Dockerfile.cross diff --git a/Tiltfile b/Tiltfile index 6415c9a37..367f7a5f6 100644 --- a/Tiltfile +++ b/Tiltfile @@ -1,6 +1,6 @@ load("ext://k8s_attach", "k8s_attach") -docker_build("controller", ".", only=("Dockerfile", "Makefile", "vendor","go.mod", "go.sum", "./api", "./cloud","./cmd", "./controller", "./util")) +docker_build("controller", ".", only=("Dockerfile", "Makefile", "vendor","go.mod", "go.sum", "./api", "./cloud","./cmd", "./controller", "./util", "./version"), build_args={'VERSION': os.getenv("VERSION","")}) local_resource( 'capi-controller-manager', @@ -18,6 +18,9 @@ k8s_resource( "cluster-api-provider-linode-system:namespace", "linodeclusters.infrastructure.cluster.x-k8s.io:customresourcedefinition", "linodemachines.infrastructure.cluster.x-k8s.io:customresourcedefinition", + "linodeclustertemplates.infrastructure.cluster.x-k8s.io:customresourcedefinition", + "linodemachinetemplates.infrastructure.cluster.x-k8s.io:customresourcedefinition", + "linodevpcs.infrastructure.cluster.x-k8s.io:customresourcedefinition", "cluster-api-provider-linode-controller-manager:serviceaccount", "cluster-api-provider-linode-leader-election-role:role", "cluster-api-provider-linode-manager-role:clusterrole", @@ -38,5 +41,5 @@ k8s_resource( objects=[ "cilium:helmchartproxy" ], - resource_deps=["capi-controller-manager", "cluster-api-provider-linode-controller-manager", "caaph-controller-manager"] + resource_deps=["capi-controller-manager", "caaph-controller-manager"] ) diff --git a/cloud/scope/common.go b/cloud/scope/common.go index 518edc5a8..6d223ac52 100644 --- a/cloud/scope/common.go +++ b/cloud/scope/common.go @@ -1,8 +1,10 @@ package scope import ( + "fmt" "net/http" + "github.com/linode/cluster-api-provider-linode/version" "github.com/linode/linodego" "golang.org/x/oauth2" ) @@ -17,5 +19,7 @@ func createLinodeClient(apiKey string) *linodego.Client { } linodeClient := linodego.NewClient(oauth2Client) + linodeClient.SetUserAgent(fmt.Sprintf("CAPL/%s", version.GetVersion())) + return &linodeClient } diff --git a/cmd/main.go b/cmd/main.go index 5b44407d2..86e20a308 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,8 +19,11 @@ package main import ( "errors" "flag" + "fmt" "os" + "github.com/linode/cluster-api-provider-linode/version" + _ "go.uber.org/automaxprocs" controller2 "github.com/linode/cluster-api-provider-linode/controller" @@ -79,7 +82,7 @@ func main() { flag.Parse() ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) - + setupLog.Info(fmt.Sprintf("CAPL version: %s", version.GetVersion())) // Check environment variables if linodeToken == "" { setupLog.Error(errors.New("failed to get LINODE_TOKEN environment variable"), "unable to start operator") diff --git a/version/version.go b/version/version.go new file mode 100644 index 000000000..314caf78e --- /dev/null +++ b/version/version.go @@ -0,0 +1,12 @@ +package version + +// version is overridden by build time flags +var version string + +func GetVersion() string { + if version == "" { + return "dev" + } + + return version +}