From a74007ae4ac6d8b19acf18e9e64044af0858d279 Mon Sep 17 00:00:00 2001 From: Amine Date: Tue, 24 Sep 2024 08:21:40 -0700 Subject: [PATCH] refactor: update build and release process (#28) 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. --- Dockerfile | 11 ----- Makefile | 107 ++++++++++++++++++++++++----------------------- helm/Chart.yaml | 4 +- helm/values.yaml | 2 +- 4 files changed, 57 insertions(+), 67 deletions(-) delete mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index fe3486ae..00000000 --- a/Dockerfile +++ /dev/null @@ -1,11 +0,0 @@ -# Use distroless as minimal base image to package the manager binary -# Refer to https://github.com/GoogleContainerTools/distroless for more details -FROM gcr.io/distroless/static:nonroot - -WORKDIR / - -COPY symphony-controller . - -USER 65532:65532 - -ENTRYPOINT ["/symphony-controller"] diff --git a/Makefile b/Makefile index d0c55cba..82b8462c 100644 --- a/Makefile +++ b/Makefile @@ -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)) @@ -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. @@ -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. @@ -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 @@ -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. @@ -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 @@ -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=> 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 @@ -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 @@ -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 - \ No newline at end of file +.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 \ No newline at end of file diff --git a/helm/Chart.yaml b/helm/Chart.yaml index 61a4ce23..d3398a60 100644 --- a/helm/Chart.yaml +++ b/helm/Chart.yaml @@ -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" \ No newline at end of file +version: 0.1.0-rc.5 +appVersion: "0.1.0-rc.5" \ No newline at end of file diff --git a/helm/values.yaml b/helm/values.yaml index 4f49ef51..712e1450 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -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: ""