Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add commit hash implementation similar to kuadrantctl to authorino #473

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions .github/workflows/build-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ jobs:
id: add-branch-tag
run: |
echo "IMG_TAGS=${GITHUB_REF_NAME/\//-} ${{ env.IMG_TAGS }}" >> $GITHUB_ENV
- name: Set Authorino version
id: authorino-version
- name: Set Authorino build info
id: authorino-build-info
run: |
if [[ ${GITHUB_REF_NAME/\//-} =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-.+)?$ ]]; then
echo "VERSION=${GITHUB_REF_NAME/\//-}" >> $GITHUB_ENV
tag=${GITHUB_REF_NAME/\//-}
echo "version=${tag#v}" >> $GITHUB_ENV
elif [[ ${GITHUB_REF_NAME/\//-} == "main" ]]; then
echo "version=latest" >> $GITHUB_ENV
else
echo "VERSION=${{ github.sha }}" >> $GITHUB_ENV
echo "version=${{ github.ref_name }}" >> $GITHUB_ENV
Comment on lines +37 to +42
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we still have 2 ways to do the same thing more or less. Even though GHA and Makefile both seem to use the git ref, for some reason one does it straightforward and the other uses a file (two files!) in the process.

This GHA workflow (used to build and push the images to registry on pushes to main, releases and for PR testing) uses only the git ref straight away to infer the version info. The Makefile (used locally for dev/testing workflows), on the other hand, reads the version info from build-recent.yaml, while still generating this file (from a build.yaml "template") before every command, based on the git ref.

I see two paths from here to fix this. We need to decide whether build.yaml will be the source for the version info or not.

OPTION A: build.yaml is the source of version info:

  1. The version info should NOT be updated before every command, but simply read from the build.yaml file locally (this is the base line, but keep reading to see the special case)
  2. Updating build.yaml must be a manual step only performed in preparation for releases
  3. We need to start doing release branches for Authorino, so these can contain the version of build.yaml with the altered version info inside (i.e. "X.Y.Z"), unlike all other branches (including main) whose build.yaml will always say "latest"
  4. The GHA must read the version info also from build.yaml
  5. There must be a way for the user to manually override the info in build.yaml without modifying the file. This will be the case mostly of when building from feature branches/PRs, both using the Makefile or via GHA. We probably don't need build-recent.yaml for that.

OPTION B: build.yaml is NOT the source:

  1. version info must be inferred from the git ref directly, both in the GHA and in the make targets:
git ref version info
main latest
feature branch-name
vX.Y.Z X.Y.Z

IMO, OPTION B looks much simpler. On the flip side, it doesn't give us the traceability, along with the code, about which version info would be stamped when building from that source. However, we're also introducing git shas and the dirty flag here. So I tend to think that storing the version info along with the code is not needed.

But please let me know what you think @ehearneRedHat @eguzki @didierofrivia

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guicassolato I think OPTION B looks to be easier but you would lose the traceability as you mentioned, but there would be less files to look to fix.

I will await other responses before implementing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version info in logs gives high level view of the version being used. Useful to know if some feature should be there or not.

For us, developers, version is not accurate enough to debug and investigate issues. We want the git sha revision to know exactly the source code revision being run.

I leave version management up to you guys.

My take? version lives in version.go. In main branch it is vX.Y.Z-dev (where vX.Y.Z has not been released yet). Note that it is not latest or main as it is meaningless. In release branches it is vX.Y.Z. In RELEASE.md file, there is an additional step to update this version.go file. This is the approach we are taking in other kuadrant projects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@didierofrivia what are your thoughts ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going with OPTION B here for simplicity.

fi
echo "git_sha=${{ github.sha }}" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Build Image
Expand All @@ -48,7 +52,9 @@ jobs:
tags: ${{ env.IMG_TAGS }}
platforms: linux/amd64,linux/arm64,linux/s390x,linux/ppc64le
build-args: |
version=${{ env.VERSION }}
git_sha=${{ env.git_sha }}
version=${{ env.version }}
dirty=${{ env.dirty }}
containerfiles: |
./Dockerfile
- name: Push Image
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ vendor
tmp
target
.scannerwork
build-recent.yaml
9 changes: 7 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ FROM registry.access.redhat.com/ubi9/go-toolset:1.21 AS builder
USER root
WORKDIR /usr/src/authorino
COPY ./ ./
ARG version=latest
RUN CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=${version}" -o /usr/bin/authorino main.go
ARG version
ENV version=${version:-unknown}
alexsnaps marked this conversation as resolved.
Show resolved Hide resolved
ARG git_sha
ENV git_sha=${git_sha:-unknown}
ARG dirty
ENV dirty=${dirty:-unknown}
RUN CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=${version} -X main.gitSHA=${git_sha} -X main.dirty=${dirty}" -o /usr/bin/authorino main.go

# Use Red Hat minimal base image to package the binary
# https://catalog.redhat.com/software/containers/ubi9-minimal
Expand Down
47 changes: 29 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
# Use bash as shell
SHELL = /bin/bash

# Authorino version
VERSION = $(shell git rev-parse HEAD)

# Use vi as default editor
EDITOR ?= vi

# 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
# Set version and image tag
ifeq ($(VERSION),)
VERSION = $(shell git rev-parse --abbrev-ref HEAD)
endif
ifeq ($(VERSION),main)
override VERSION = latest
endif
using_semantic_version := $(shell [[ $(VERSION) =~ ^[0-9]+\.[0-9]+\.[0-9]+(-.+)?$$ ]] && echo "true")
ifdef using_semantic_version
IMAGE_TAG=v$(VERSION)
else
GOBIN=$(shell go env GOBIN)
IMAGE_TAG=local
endif
IMAGE_REPO ?= authorino
AUTHORINO_IMAGE ?= $(IMAGE_REPO):$(IMAGE_TAG)

PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
export PATH := $(PROJECT_DIR)/bin:$(PATH)
Expand Down Expand Up @@ -77,6 +83,13 @@ ifeq ($(SED),)
exit 1
endif

# 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
else
GOBIN=$(shell go env GOBIN)
endif

# go-get-tool will 'go install' any package $2 and install it to $1.
define go-get-tool
@[ -f $(1) ] || { \
Expand Down Expand Up @@ -112,22 +125,20 @@ manifests: controller-gen kustomize ## Generates the manifests in $PROJECT_DIR/i
controller-gen crd:crdVersions=v1 rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=install/crd output:rbac:artifacts:config=install/rbac && $(KUSTOMIZE) build install > $(AUTHORINO_MANIFESTS)
$(MAKE) patch-webhook

run:git_sha=$(shell git rev-parse HEAD)
run:dirty=$(shell $(PROJECT_DIR)/hack/check-git-dirty.sh || echo "unknown")
run: generate manifests ## Runs the application against the Kubernetes cluster configured in ~/.kube/config
go run -ldflags "-X main.version=$(VERSION)" ./main.go server
go run -ldflags "-X main.version=$(VERSION) -X main.gitSHA=${git_sha} -X main.dirty=${dirty}" ./main.go server

build:git_sha=$(shell git rev-parse HEAD)
build:dirty=$(shell $(PROJECT_DIR)/hack/check-git-dirty.sh || echo "unknown")
build: generate ## Builds the manager binary
CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=$(VERSION)" -o bin/authorino main.go
CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=$(VERSION) -X main.gitSHA=${git_sha} -X main.dirty=${dirty}" -o bin/authorino main.go

IMAGE_REPO ?= authorino
using_semantic_version := $(shell [[ $(VERSION) =~ ^[0-9]+\.[0-9]+\.[0-9]+(-.+)?$$ ]] && echo "true")
ifdef using_semantic_version
IMAGE_TAG=v$(VERSION)
else
IMAGE_TAG=local
endif
AUTHORINO_IMAGE ?= $(IMAGE_REPO):$(IMAGE_TAG)
docker-build:git_sha=$(shell git rev-parse HEAD)
docker-build:dirty=$(shell $(PROJECT_DIR)/hack/check-git-dirty.sh || echo "unknown")
docker-build: ## Builds an image based on the current branch
docker build --build-arg version=$(VERSION) -t $(AUTHORINO_IMAGE) .
docker build --build-arg version=$(VERSION) --build-arg git_sha=$(git_sha) --build-arg dirty=$(dirty) -t $(AUTHORINO_IMAGE) .

test: generate manifests envtest ## Runs the tests
KUBEBUILDER_ASSETS='$(strip $(shell $(ENVTEST) use -p path 1.21.2 --os linux))' go test ./... -coverprofile cover.out
Expand Down
10 changes: 5 additions & 5 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

## Process

To release a version “v0.X.Y” of Authorino in GitHub and Quay.io, follow these steps:
To release a version “vX.Y.Z” of Authorino in GitHub and Quay.io, follow these steps:

1. Pick a `<git-ref>` (SHA-1) as source.

```shell
git checkout <git-ref>
```

2. Create a new tag and named release `v0.X.Y`. Push the tag to GitHub.
2. Create a new tag and named release `vX.Y.Z`. Push the tag to GitHub.

```shell
git tag -a v0.X.Y -m "v0.X.Y"
git push origin v0.X.Y
git tag -a vX.Y.Z -m "vX.Y.Z"
git push origin vX.Y.Z
```

Then at the GitHub repository, create a new release from the tag you just pushed. One could start autogenerating the
release notes and then write the change notes highlighting all the new features, bug fixes, enhancements, etc.
([example](https://github.com/Kuadrant/authorino/releases/tag/v0.9.0)).

3. Run the GHA ‘Build and push images’ for the `v0.X.Y` tag. This will cause a new image to be built and pushed to quay.io/kuadrant/authorino.
3. Run the GHA ‘Build and push images’ for the `vX.Y.Z` tag. This will cause a new image to be built and pushed to quay.io/kuadrant/authorino.

## Notes on Authorino’s automated builds

Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions hack/check-git-dirty.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

if ! command -v git &>/dev/null
then
echo "git not found..." >&2
exit 1
fi

if output=$(git diff --stat 2>/dev/null)
then
[ -n "$output" ] && echo "true" || echo "false"
else
# Not a git repository
exit 1
fi
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const (
var (
// ldflags
version string
dirty string
ehearneRedHat marked this conversation as resolved.
Show resolved Hide resolved
ehearneRedHat marked this conversation as resolved.
Show resolved Hide resolved
gitSHA string

scheme = runtime.NewScheme()
logger logr.Logger
Expand Down Expand Up @@ -373,7 +375,7 @@ func runWebhookServer(cmd *cobra.Command, _ []string) {
func setup(cmd *cobra.Command, log logOptions, telemetry telemetryOptions) {
setupLogger(log)

logger.Info("booting up authorino", "version", version, "cmd", cmd.Use)
ehearneRedHat marked this conversation as resolved.
Show resolved Hide resolved
logger.Info("build information", "version", version, "commit", gitSHA, "dirty", dirty, "cmd", cmd.Use)

// log the command-line args
if logger.V(1).Enabled() {
Expand Down Expand Up @@ -562,5 +564,9 @@ func timeoutMs(timeout int) time.Duration {
}

func printVersion(_ *cobra.Command, _ []string) {
fmt.Println("Authorino", version)
if dirty == "true" {
ehearneRedHat marked this conversation as resolved.
Show resolved Hide resolved
fmt.Printf("Authorino %s (%s-dirty)\n", version, gitSHA)
guicassolato marked this conversation as resolved.
Show resolved Hide resolved
} else {
fmt.Printf("Authorino %s (%s)\n", version, gitSHA)
}
}
Loading