Skip to content

Commit

Permalink
Merge pull request #25 from digitalocean/awg/user-agent
Browse files Browse the repository at this point in the history
Set the user-agent for godo
  • Loading branch information
adamwg authored Oct 14, 2022
2 parents f90cabf + 9b8952b commit 04a1c76
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ endif
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

LDFLAGS ?= -X main.version=$(IMG_TAG)

.PHONY: all
all: build

Expand Down Expand Up @@ -63,15 +65,15 @@ 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.
go run ./main.go ${FLAGS}

.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.
Expand Down
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
version string
)

func init() {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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))
}

0 comments on commit 04a1c76

Please sign in to comment.