From 9b8952bbe83ba0ce8ab88370187e4d4d00ea896a Mon Sep 17 00:00:00 2001 From: Adam Wolfe Gordon Date: Thu, 13 Oct 2022 16:16:36 -0600 Subject: [PATCH] Set the user-agent for godo Show that requests are coming from the do-operator by setting a custom user-agent for godo. Set the version during the build process so we can include it in the user-agent string. --- Dockerfile | 3 ++- Makefile | 6 ++++-- main.go | 11 ++++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index f15132d0..971b1ff0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,8 @@ COPY controllers/ controllers/ COPY extgodo/ extgodo/ # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go +ARG LDFLAGS +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "${LDFLAGS}" -a -o manager 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 bb031a61..80509cec 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,8 @@ endif SHELL = /usr/bin/env bash -o pipefail .SHELLFLAGS = -ec +LDFLAGS ?= -X main.version=$(IMG_TAG) + .PHONY: all all: build @@ -63,7 +65,7 @@ test: manifests generate fmt vet envtest ## Run tests. .PHONY: build build: generate fmt vet ## Build manager binary. - go build -o bin/manager main.go + go build -ldflags "$(LDFLAGS)" -o bin/manager main.go .PHONY: run run: manifests generate fmt vet ## Run a controller from your host. @@ -71,7 +73,7 @@ run: manifests generate fmt vet ## Run a controller from your host. .PHONY: docker-build docker-build: test ## Build docker image with the manager. - docker build -t ${IMG} . + docker build --build-arg LDFLAGS="$(LDFLAGS)" -t ${IMG} . .PHONY: docker-push docker-push: ## Push docker image with the manager. diff --git a/main.go b/main.go index 02661e52..fa71418d 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,7 @@ import ( var ( scheme = runtime.NewScheme() setupLog = ctrl.Log.WithName("setup") + version string ) func init() { @@ -73,7 +74,11 @@ func main() { opts.BindFlags(flag.CommandLine) flag.Parse() - ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) + if version == "" { + version = "dev" + } + + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)).WithValues("version", version)) mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, @@ -164,7 +169,7 @@ func main() { os.Exit(1) } - setupLog.Info("starting manager") + setupLog.WithValues("version", version).Info("starting manager") if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { setupLog.Error(err, "problem running manager") os.Exit(1) @@ -173,5 +178,5 @@ func main() { func makeGodo(ctx context.Context, token, addr string) (*godo.Client, error) { client := oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})) - return godo.New(client, godo.SetBaseURL(addr)) + return godo.New(client, godo.SetBaseURL(addr), godo.SetUserAgent("do-operator/"+version)) }