Skip to content

Commit

Permalink
Dynamically create an overlay for the image tag
Browse files Browse the repository at this point in the history
Avoid editing config/manager/kustomization.yaml to set the image tag.  This
causes git to consider the workarea to be dirty, so two consecutive deploys
from a workarea that has no other changes will result in the second deploy
looking for an image with a "-dirty" tag.

Instead, from the makefile we create a throw-away, and untracked, overlay that
will be used to set the image tag

Signed-off-by: Dean Roehrich <[email protected]>
  • Loading branch information
roehrich-hpe committed Mar 19, 2024
1 parent 22613e5 commit 09b374e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ bin
.vscode

.version
config/begin/*
cover.out

23 changes: 15 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022-2023 Hewlett Packard Enterprise Development LP
# Copyright 2022-2024 Hewlett Packard Enterprise Development LP
# Other additional copyright holders may be indicated within.
#
# The entirety of this work is licensed under the Apache License,
Expand Down Expand Up @@ -29,8 +29,10 @@
# cray.hpe.com/dws-bundle:$VERSION and cray.hpe.com/dws-catalog:$VERSION.
IMAGE_TAG_BASE ?= ghcr.io/dataworkflowservices/dws-test-driver

OVERLAY ?= top

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.26.0
ENVTEST_K8S_VERSION = 1.28.0

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -72,7 +74,8 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
# $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
echo

.PHONY: fmt
fmt: ## Run go fmt against code.
Expand Down Expand Up @@ -145,15 +148,18 @@ ifndef ignore-not-found
ignore-not-found = false
endif

.PHONY: edit-image
edit-image: VERSION ?= $(shell cat .version)
edit-image: .version
$(KUSTOMIZE_IMAGE_TAG) config/begin $(OVERLAY) $(IMAGE_TAG_BASE) $(VERSION)

.PHONY: deploy
deploy: VERSION ?= $(shell cat .version)
deploy: .version kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMAGE_TAG_BASE}:${VERSION}
$(KUSTOMIZE) build config/top | kubectl apply -f -
deploy: kustomize edit-image ## Deploy controller to the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/begin | 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/top | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
$(KUSTOMIZE) build config/$(OVERLAY) | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

# Let .version be phony so that a git update to the workarea can be reflected
# in it each time it's needed.
Expand All @@ -178,6 +184,7 @@ clean-bin:
fi

## Tool Binaries
KUSTOMIZE_IMAGE_TAG ?= ./hack/make-kustomization.sh
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
Expand Down
2 changes: 1 addition & 1 deletion hack/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 Hewlett Packard Enterprise Development LP.
Copyright 2024 Hewlett Packard Enterprise Development LP.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
42 changes: 42 additions & 0 deletions hack/make-kustomization.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# Copyright 2023 Hewlett Packard Enterprise Development LP
# Other additional copyright holders may be indicated within.
#
# The entirety of this work is 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 -e

OVERLAY_DIR=$1
OVERLAY=$2
IMAGE_TAG_BASE=$3
TAG=$4

if [[ ! -d $OVERLAY_DIR ]]
then
mkdir "$OVERLAY_DIR"
fi

cat <<EOF > "$OVERLAY_DIR"/kustomization.yaml
resources:
- ../$OVERLAY
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: $IMAGE_TAG_BASE
newTag: $TAG
EOF

0 comments on commit 09b374e

Please sign in to comment.