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

chore: Add Makefile target to deploy snapshot #35

Merged
merged 1 commit into from
Apr 8, 2022
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
4 changes: 2 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dockers:
ids:
- execution-controller
image_templates:
- "furikoio/execution-controller:{{ if .IsSnapshot }}v{{ .Version }}{{ else }}{{ .Tag }}{{ end }}"
- "furikoio/execution-controller:{{ if (index .Env \"IMAGE_TAG\") }}{{ index .Env \"IMAGE_TAG\" }}{{ else if .IsSnapshot }}v{{ .Version }}{{ else }}{{ .Tag }}{{ end }}"
- "furikoio/execution-controller:latest"
build_flag_templates:
- "--platform=linux/amd64"
Expand All @@ -61,7 +61,7 @@ dockers:
ids:
- execution-webhook
image_templates:
- "furikoio/execution-webhook:{{ if .IsSnapshot }}v{{ .Version }}{{ else }}{{ .Tag }}{{ end }}"
- "furikoio/execution-webhook:{{ if (index .Env \"IMAGE_TAG\") }}{{ index .Env \"IMAGE_TAG\" }}{{ else if .IsSnapshot }}v{{ .Version }}{{ else }}{{ .Tag }}{{ end }}"
- "furikoio/execution-webhook:latest"
build_flag_templates:
- "--platform=linux/amd64"
Expand Down
46 changes: 20 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ LICENSE_HEADER_GO ?= hack/boilerplate.go.txt
# Set image name prefix. The actual image name and tag will be appended to this.
IMAGE_NAME_PREFIX ?= "docker.io/furikoio"

# Define the image tag to use, otherwise will default to latest.
# The latest tag always refers to the latest stable release.
IMAGE_TAG ?= "latest"

# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
Expand All @@ -52,7 +56,7 @@ all: manifests generate fmt build yaml ## Generate code, build Go binaries and Y

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-24s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development

Expand Down Expand Up @@ -90,36 +94,27 @@ tidy: ## Run go mod tidy.
test: ## Run tests with coverage. Outputs to combined.cov.
./hack/run-tests.sh

##@ Building

.PHONY: build
build: build-execution-controller ## Build all Go binaries.

.PHONY: build-execution-controller
build-execution-controller: ## Build execution-controller.
go build -o build/execution-controller ./cmd/execution-controller

##@ YAML Configuration

## Location to write YAMLs to
YAML_DEST ?= $(shell pwd)/yamls
$(YAML_DEST): ## Ensure that the directory exists
mkdir -p $(YAML_DEST)

KUSTOMIZE_DEST ?= $(shell pwd)/dist/kustomize
$(KUSTOMIZE_DEST):
mkdir -p $(KUSTOMIZE_DEST)

.PHONY: yaml
yaml: yaml-execution ## Build kustomize configs. Outputs to dist folder.

.PHONY: yaml-execution
yaml-execution: manifests kustomize $(YAML_DEST) ## Build furiko-execution.yaml with Kustomize.
{ \
cd config/default ;\
$(KUSTOMIZE) edit set image execution-controller=$(IMAGE_NAME_PREFIX)/execution-controller:$(IMAGE_TAG) ;\
$(KUSTOMIZE) edit set image execution-webhook=$(IMAGE_NAME_PREFIX)/execution-webhook:$(IMAGE_TAG) ;\
}
$(KUSTOMIZE) build config/default -o $(YAML_DEST)/furiko-execution.yaml
yaml-execution: manifests kustomize $(YAML_DEST) $(KUSTOMIZE_DEST) ## Build furiko-execution.yaml with Kustomize.
DEST_DIR=$(KUSTOMIZE_DEST) ./hack/generate-kustomization.sh "$(IMAGE_NAME_PREFIX)" "$(IMAGE_TAG)"
$(KUSTOMIZE) build $(KUSTOMIZE_DEST) -o $(YAML_DEST)/furiko-execution.yaml

.PHONY: manifests
manifests: tidy controller-gen yq ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
manifests: tidy controller-gen yq ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects into the "config" directory.
# Generate CRDs
$(CONTROLLER_GEN) crd paths="./..." output:crd:artifacts:config=config/crd/bases
# Generate webhook manifests
Expand All @@ -137,20 +132,21 @@ ifndef ignore-not-found
endif

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
install: manifests kustomize ## Installs the CRDs into the K8s cluster.
$(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.
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster. 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.
$(KUSTOMIZE) build config/default | kubectl apply -f -
deploy: manifests kustomize $(KUSTOMIZE_DEST) ## Deploys snapshot version of all components to the K8s cluster. Useful for testing current HEAD.
DEST_DIR=$(KUSTOMIZE_DEST) ./hack/generate-kustomization.sh "$(IMAGE_NAME_PREFIX)" "snapshot"
$(KUSTOMIZE) build $(KUSTOMIZE_DEST) | 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 -
undeploy: $(KUSTOMIZE_DEST) ## Undeploy controller from the K8s cluster. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build $(KUSTOMIZE_DEST) | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

##@ Release

Expand All @@ -173,15 +169,13 @@ GOIMPORTS ?= $(LOCALBIN)/goimports
YQ ?= $(LOCALBIN)/yq
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
LICENSE_HEADER_CHECKER ?= $(LOCALBIN)/license-header-checker
GORELEASER ?= $(LOCALBIN)/goreleaser

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.8.0
YQ_VERSION ?= v4.14.1
GOLANGCILINT_VERSION ?= v1.45.2
LICENSEHEADERCHECKER_VERSION ?= v1.3.0
GORELEASER_VERSION ?= v1.7.0

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
Expand Down
15 changes: 4 additions & 11 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ namespace: furiko-system
commonLabels:
app.kubernetes.io/name: furiko

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../common
- ../crd
- ../execution
images:
- name: execution-controller
newName: docker.io/furikoio/execution-controller
- name: execution-webhook
newName: docker.io/furikoio/execution-webhook
bases:
- ../common
- ../crd
- ../execution
71 changes: 71 additions & 0 deletions hack/generate-kustomization.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

#
# Copyright 2022 The Furiko Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -euo pipefail

## Simple script that generates a new kustomization.yaml and adds additional fields needed before building.
## For example, this script adds the image field necessary for substitution in kustomize.
## The purpose of this script is to create a temporary "overlay" config, so that we do not override existing
## kustomization.yaml files checked into source control.

if [ $# -ne 2 ]
then
echo 'Usage:'
echo ' ./generate-kustomization.sh IMAGE_NAME_PREFIX IMAGE_TAG'
echo
echo 'Optional environment variables:'
echo ' DEST_DIR: Path to generate kustomization.yaml to. Default: Current working directory'
echo ' BASE_CONFIG: Path to config path containing base kustomization.yaml. Default: ./config/default'
echo ' KUSTOMIZE: Path to kustomize executable. Default: ./bin/kustomize'
exit 1
fi

# Positional arguments
IMAGE_NAME_PREFIX="$1"
if [[ -z "${IMAGE_NAME_PREFIX}" ]]
then
echo 'Error: IMAGE_NAME_PREFIX cannot be empty'
exit 2
fi

IMAGE_TAG="$2"
if [[ -z "${IMAGE_TAG}" ]]
then
echo 'Error: IMAGE_TAG cannot be empty'
exit 2
fi

# Optional environment variables
DEST_DIR="${DEST_DIR:-$(pwd)}"
KUSTOMIZE="${KUSTOMIZE:-$(pwd)/bin/kustomize}"
BASE_CONFIG="${BASE_CONFIG:-$(realpath --relative-to="${DEST_DIR}" "$(pwd)/config/default")}" # NOTE: Cannot be absolute, otherwise kustomize will complain

# Create new kustomization.yaml to hold our substitution.
cd "${DEST_DIR}"
rm -f kustomization.yaml # Remove if exists
"${KUSTOMIZE}" create --resources "${BASE_CONFIG}"

# Add image fields.
IMAGES=(
'execution-controller'
'execution-webhook'
)
for IMAGE in "${IMAGES[@]}"
do
"${KUSTOMIZE}" edit set image "${IMAGE}=${IMAGE_NAME_PREFIX}/${IMAGE}:${IMAGE_TAG}"
done
18 changes: 16 additions & 2 deletions hack/release-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ set -euo pipefail

## Simple script that builds a snapshot of the current repository and pushes an image to Docker Hub.
## Uses GoReleaser to build artifacts.
## Docker image will be released as using the `<NEXT TAG>-next` image tag.
## Docker images will be pushed to Docker Hub using a snapshot version tag (e.g. `v1.2.3-next`).

# Optionally specify IMAGE_TAG, otherwise will default to `(current tag with patch version incremented)-next`.
# Environment variable is expected to be propagated to GoReleaser.
export IMAGE_TAG

# Build snapshot into dist.
curl -sL https://git.io/goreleaser | bash -s -- release --snapshot --rm-dist
Expand All @@ -35,5 +39,15 @@ REPOS=(
)
for REPO in "${REPOS[@]}"
do
docker push "${REPO}:v${VERSION}"
VERSION_TAG="${IMAGE_TAG:-v${VERSION}}"

# The versioned image tag is like v1.2.3-next, or will default to the IMAGE_TAG environment variable specified.
VERSIONED_IMAGE="${REPO}:${VERSION_TAG}"

# The snapshot image tag always points to the latest snapshot.
SNAPSHOT_IMAGE="${REPO}:snapshot"

docker tag "${VERSIONED_IMAGE}" "${SNAPSHOT_IMAGE}"
docker push "${VERSIONED_IMAGE}"
docker push "${SNAPSHOT_IMAGE}"
done