Skip to content

Commit

Permalink
add version to controller and include it in user-agent for linode api…
Browse files Browse the repository at this point in the history
… requests
  • Loading branch information
eljohnson92 committed Feb 14, 2024
1 parent 13df910 commit 615e383
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
FROM golang:1.21 as builder
ARG TARGETOS
ARG TARGETARCH
ARG VERSION

WORKDIR /workspace
# Copy the Go Modules manifests
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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

Expand Down
7 changes: 5 additions & 2 deletions Tiltfile
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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",
Expand All @@ -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"]
)
4 changes: 4 additions & 0 deletions cloud/scope/common.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -17,5 +19,7 @@ func createLinodeClient(apiKey string) *linodego.Client {
}
linodeClient := linodego.NewClient(oauth2Client)

linodeClient.SetUserAgent(fmt.Sprintf("CAPL/%s", version.GetVersion()))

return &linodeClient
}
5 changes: 4 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 615e383

Please sign in to comment.