Kuadrant / authorino-operator

Kubernetes Operator to manage Authorino instances
Apache License 2.0
9 stars 21 forks source link

Embed Authorino Operator version info into the binary #92

Closed guicassolato closed 2 years ago

guicassolato commented 2 years ago

The Authorino Operator version info can be specified by setting an ldflag main.version in the go build command. E.g.

go build -ldflags "-X main.version=0.4.0" -o bin/manager main.go

When using the Makefile:

make build VERSION=0.4.0
  1. If the makefile parameter VERSION is not specified, it defaults to the Git ref (SHA-1) of HEAD;
  2. The image tag (IMAGE_TAG) is inferred from the value of VERSION (user-defined or set to default):
    • When it matches ^[0-9]+\.[0-9]+\.[0-9]+(-.+)?$ (e.g. '0.10.0-pre'), the tag is set to v$VERSION (e.g. 'v0.10.0-pre');
    • otherwise, the tag is set to fixed value 'latest'.

To build a container image:

  1. Pass the build argument VERSION. E.g.
    docker build --build-args version=0.4.0-pre -t registry/org/repo:v0.4.0-pre .

    -- OR --

  2. Use the Makefile:
    make docker-build VERSION=0.4.0-pre REGISTRY=registry ORG=org REPO=repo -t

In CI, the VERSION (build argument) is inferred based on GITHUB_REF_NAME:

Verification steps

Setup a cluster to target the operator to:

kind create cluster
kubectl create namespace authorino-operator
make install

For each scenario below, build and run the operator. In all the cases, you should spot a message 'booting up authorino operator' in the Authorino Operator logs, with a label version set accordingly.

Scenario: Build and run the binary locally

Run the binary locally:

make run
[…]
go run -ldflags "-X main.version=fff12b68926ddf72646a7ba9653cd7ed426c0816" ./main.go
1.6615074731805818e+09  INFO    setup   botting up authorino operator   {"version": "fff12b68926ddf72646a7ba9653cd7ed426c0816"}

Try with a version number:

make run VERSION=0.4.0-pre
[…]
go run -ldflags "-X main.version=0.4.0-pre" ./main.go
1.661507509064471e+09   INFO    setup   botting up authorino operator   {"version": "0.4.0-pre"}

Scenario: Build and run container image in docker

Setup a cluster to target the operator to:

kind create cluster
kubectl create namespace authorino-operator
make install

Build and run container image in docker:

make docker-build
docker run \
  -v $HOME/.kube/config:/home/.kube/config \
  --env KUBECONFIG=/home/.kube/config \
  --network=host \
  quay.io/kuadrant/authorino-operator:latest

Try with a version number:

make docker-build VERSION=0.4.0-pre
docker run \
  -v $HOME/.kube/config:/home/.kube/config \
  --env KUBECONFIG=/home/.kube/config \
  --network=host \
  quay.io/kuadrant/authorino-operator:v0.4.0-pre