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 Oct 19, 2023
1 parent 1e1fa0c commit caac2cb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ bin
testbin/*
nnf-dm
.version
config/begin/*

# Test binary, build with `go test -c`
*.test
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,11 @@ uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified

deploy: VERSION ?= $(shell cat .version)
deploy: .version kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
./deploy.sh deploy $(KUSTOMIZE) $(IMAGE_TAG_BASE):$(VERSION) $(NNFMFU_TAG_BASE):$(NNFMFU_VERSION)
$(KUSTOMIZE_IMAGE_TAG) config/begin default $(IMAGE_TAG_BASE) $(VERSION) $(NNFMFU_TAG_BASE) $(NNFMFU_VERSION)
./deploy.sh deploy $(KUSTOMIZE) config/begin

undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
./deploy.sh undeploy $(KUSTOMIZE)
./deploy.sh undeploy $(KUSTOMIZE) config/default

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

## Tool Binaries
KUSTOMIZE_IMAGE_TAG ?= ./hack/make-kustomization2.sh
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
Expand Down
15 changes: 5 additions & 10 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
usage() {
cat <<EOF
Deploy or Undeploy Data Movement
Usage $0 COMMAND KUSTOMIZE [IMG] [NNFMFU_IMG]
Usage $0 COMMAND KUSTOMIZE <OVERLAY_DIR>
Commands:
deploy Deploy data movement
Expand All @@ -32,32 +32,27 @@ EOF

CMD=$1
KUSTOMIZE=$2
IMG=$3
NNFMFU_IMG=$4
OVERLAY_DIR=$3

case $CMD in
deploy)
(cd config/manager &&
$KUSTOMIZE edit set image controller="$IMG" &&
$KUSTOMIZE edit set image nnf-mfu="$NNFMFU_IMG")

$KUSTOMIZE build config/default | kubectl apply -f - || true
$KUSTOMIZE build $OVERLAY_DIR | kubectl apply -f - || true

# Sometimes the deployment of the DataMovementManager occurs too quickly for k8s to digest the CRD
# Retry the deployment if this is the case. It seems to be fast enough where we can just
# turn around and re-deploy; but this may need to move to a polling loop if that goes away.
echo "Waiting for DataMovementManager resource to become ready"
while :; do
[[ $(kubectl get datamovementmanager -n nnf-dm-system 2>&1) == "No resources found" ]] && sleep 1 && continue
$KUSTOMIZE build config/default | kubectl apply -f -
$KUSTOMIZE build $OVERLAY_DIR | kubectl apply -f -
break
done
;;
undeploy)
# When the DataMovementManager CRD gets deleted all related resource are also
# removed, so the delete will always fail. We ignore all errors at our
# own risk.
$KUSTOMIZE build config/default | kubectl delete --ignore-not-found -f -
$KUSTOMIZE build $OVERLAY_DIR | kubectl delete --ignore-not-found -f -
;;
*)
usage
Expand Down
46 changes: 46 additions & 0 deletions hack/make-kustomization2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/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_1=$3
TAG_1=$4
IMAGE_TAG_BASE_2=$5
TAG_2=$6

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_1
newTag: $TAG_1
- name: $IMAGE_TAG_BASE_2
newTag: $TAG_2
EOF

0 comments on commit caac2cb

Please sign in to comment.