Skip to content

Commit

Permalink
refactor: update build and release process (#28)
Browse files Browse the repository at this point in the history
This patch removes the `Dockerfile` and updates the Makefile with new targets:

- `build-image`: builds the Symphony controller image using `ko build`
- `publish-image`: publishes the controller image to ECR using `ko publish`
- `package-helm`: packages the helm chart, updating versions in values.yaml/Chart.yaml
- `publish-helm`: publishes the helm chart to an ECR OCI repository using `helm push`
- `release`: runs all the above

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
a-hilaly authored Sep 24, 2024
1 parent cfba7b7 commit a74007a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 67 deletions.
11 changes: 0 additions & 11 deletions Dockerfile

This file was deleted.

107 changes: 54 additions & 53 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.31.0
AWS_ACCOUNT_ID ?= $(shell aws sts get-caller-identity --query Account --output text)
AWS_REGION ?= us-west-2
RELEASE_VERSION ?= dev-$(shell git rev-parse --short HEAD)
ECR_REPO ?= ${AWS_ACCOUNT_ID}.dkr.ecr.us-west-2.amazonaws.com

CONTROLLER_IMAGE ?= ${ECR_REPO}/symphony:${RELEASE_VERSION}
HELM_IMAGE ?= ${ECR_REPO}/symphony-chart:${RELEASE_VERSION}
DOCS_IMAGE ?= ${ECR_REPO}/symphony-docs:${RELEASE_VERSION}

KO_DOCKER_REPO ?= ${ECR_REPO}/symphony
KOCACHE ?= ~/.ko
KO_PUSH ?= true

WITH_GOFLAGS = GOFLAGS="$(GOFLAGS)"

HELM_DIR = ./helm

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand All @@ -14,8 +26,8 @@ endif
# CONTAINER_TOOL defines the container tool to be used for building images.
# Be aware that the target commands are only tested with Docker which is
# scaffolded by default. However, you might want to replace it to use other
# tools. (i.e. podman)
CONTAINER_TOOL ?= docker
# tools. (i.e. finch)
CONTAINER_TOOL ?= finch

# Setting SHELL to bash allows bash commands to be executed by recipes.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
Expand Down Expand Up @@ -53,7 +65,7 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

tt:
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./internal/resourcegroup"
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./internal/controller/resourcegroup"

.PHONY: fmt
fmt: ## Run go fmt against code.
Expand All @@ -64,8 +76,8 @@ vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
test: manifests generate fmt vet ## Run tests.
go test ./... -coverprofile cover.out

GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.54.2
Expand All @@ -86,8 +98,8 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
##@ Build

.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager ./cmd/controller/main.go
build: manifests generate fmt vet ## Build controller binary.
go build -o bin/controller ./cmd/controller/main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
Expand All @@ -96,12 +108,12 @@ run: manifests generate fmt vet ## Run a controller from your host.
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: ## Build docker image with the manager.
.PHONY: image-build
image-build: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
.PHONY: image-push
image-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) push ${IMG}

# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
Expand All @@ -110,9 +122,9 @@ docker-push: ## Push docker image with the manager.
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
PLATFORMS ?= linux/arm64,linux/amd64
.PHONY: docker-buildx
docker-buildx: ## Build and push docker image for the manager for cross-platform support
image-buildx: ## Build and push docker image for the manager for cross-platform support
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
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
Expand All @@ -121,29 +133,6 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross

##@ Deployment

ifndef ignore-not-found
ignore-not-found = false
endif

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -

.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -

.PHONY: undeploy
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -

##@ Build Dependencies

## Location to install dependencies to
Expand Down Expand Up @@ -176,16 +165,28 @@ $(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

release-controller:
GOARCH=amd64 GOOS=linux go build -o symphony-controller ./cmd/controller/main.go
finch build -t 095708837592.dkr.ecr.us-west-2.amazonaws.com/symphony:${VERSION} .
finch push 095708837592.dkr.ecr.us-west-2.amazonaws.com/symphony:${VERSION}

release-helm:
helm package helm/symphony

.PHONY: image
build-image: ## Build the Symphony controller images using ko build
$(WITH_GOFLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO="095708837592.dkr.ecr.us-west-2.amazonaws.com/symphony" \
ko build --bare github.com/aws-controllers-k8s/symphony/cmd/controller \
--push=false --tags ${RELEASE_VERSION} --sbom=none

.PHONY: publish
publish-image: ## Publish the Symphony controller images to ECR
$(WITH_GOFLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO="095708837592.dkr.ecr.us-west-2.amazonaws.com/symphony" \
ko publish --bare github.com/aws-controllers-k8s/symphony/cmd/controller \
--tags ${RELEASE_VERSION} --sbom=none

.PHONY: package-helm
package-helm: ## Package Helm chart
@sed -i '' 's/tag: .*/tag: "$(RELEASE_VERSION)"/' helm/values.yaml
@sed -i '' 's/version: .*/version: $(RELEASE_VERSION)/' helm/Chart.yaml
@sed -i '' 's/appVersion: .*/appVersion: "$(RELEASE_VERSION)"/' helm/Chart.yaml
helm package helm

.PHONY: publish-helm
publish-helm: ## Helm publish
helm push ./symphony-chart-${RELEASE_VERSION}.tgz oci://${ECR_REPO}

.PHONY:
release: build-image publish-image package-helm publish-helm
4 changes: 2 additions & 2 deletions helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: symphony-chart
description: A Helm chart for Symphony
type: application
version: 0.1.0-rc.3
appVersion: "0.1.0-rc.3"
version: 0.1.0-rc.5
appVersion: "0.1.0-rc.5"
2 changes: 1 addition & 1 deletion helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ image:
repository: 095708837592.dkr.ecr.us-west-2.amazonaws.com/symphony
pullPolicy: Always
# Overrides the image tag whose default is the chart appVersion.
tag: "v0.1.0-rc.3"
tag: "0.1.0-rc.5"

imagePullSecrets: []
nameOverride: ""
Expand Down

0 comments on commit a74007a

Please sign in to comment.