Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
Fix DH deployment update (#65)
Browse files Browse the repository at this point in the history
Do not try to update the deployment if there's no changes.
  • Loading branch information
Roming22 authored Mar 21, 2024
1 parent 9bf43ac commit 9201850
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
6 changes: 5 additions & 1 deletion templates/developer-hub/includes/_configure.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
set -x
{{ end }}

echo -n "Installing utils: "
dnf install -y diffutils > /dev/null 2>/dev/null
echo -n "."

# Installing Helm...
curl --fail --silent --show-error --location \
https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 \
| bash
| bash >/dev/null
echo "OK"

YQ_VERSION="v4.40.5"
curl --fail --location --output "/usr/bin/yq" --silent --show-error "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64"
Expand Down
40 changes: 24 additions & 16 deletions templates/developer-hub/includes/configure/_configure_tls.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ echo "OK"
ORIGNAL_POD=$(kubectl get pods -l app.kubernetes.io/name=developer-hub -o name)

DEPLOYMENT="/tmp/deployment.yaml"
DEPLOYMENT_PATCHED="/tmp/deployment.patched.yaml"
oc get deployment/developer-hub -o yaml >"$DEPLOYMENT"
cp "$DEPLOYMENT" "$DEPLOYMENT_PATCHED"

echo -n "* Configure TLS:"
echo -n "* Configuring TLS:"
# Update env var.
if [ "$(yq '.spec.template.spec.containers[0].env[] | select(.name == "NODE_EXTRA_CA_CERTS") | length' "$DEPLOYMENT")" == "2" ]; then
if [ "$(yq '.spec.template.spec.containers[0].env[] | select(.name == "NODE_EXTRA_CA_CERTS") | length' "$DEPLOYMENT_PATCHED")" == "2" ]; then
YQ_EXPRESSION='
(
.spec.template.spec.containers[].env[] |
Expand All @@ -26,10 +28,10 @@ if [ "$(yq '.spec.template.spec.containers[0].env[] | select(.name == "NODE_EXTR
else
YQ_EXPRESSION='.spec.template.spec.containers[0].env += {"name": "NODE_EXTRA_CA_CERTS", "value": "/ingress-cert/ca.crt"}'
fi
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT"
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT_PATCHED"
echo -n "."
# Update volume mount
if [ "$(yq '.spec.template.spec.containers[0].volumeMounts[] | select(.name == "kube-root-ca") | length' "$DEPLOYMENT")" == "2" ]; then
if [ "$(yq '.spec.template.spec.containers[0].volumeMounts[] | select(.name == "kube-root-ca") | length' "$DEPLOYMENT_PATCHED")" == "2" ]; then
YQ_EXPRESSION='
(
.spec.template.spec.containers[].volumeMounts[] |
Expand All @@ -39,10 +41,10 @@ if [ "$(yq '.spec.template.spec.containers[0].volumeMounts[] | select(.name == "
else
YQ_EXPRESSION='.spec.template.spec.containers[0].volumeMounts += {"name": "kube-root-ca", "mountPath": "/ingress-cert"}'
fi
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT"
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT_PATCHED"
echo -n "."
# Update volume
if [ "$(yq '.spec.template.spec.volumes[] | select(.name == "kube-root-ca") | length' "$DEPLOYMENT")" == "2" ]; then
if [ "$(yq '.spec.template.spec.volumes[] | select(.name == "kube-root-ca") | length' "$DEPLOYMENT_PATCHED")" == "2" ]; then
YQ_EXPRESSION='
(
.spec.template.spec.volumes[] |
Expand All @@ -52,16 +54,22 @@ if [ "$(yq '.spec.template.spec.volumes[] | select(.name == "kube-root-ca") | le
else
YQ_EXPRESSION='.spec.template.spec.volumes += {"name": "kube-root-ca", "configMap": {"name": "kube-root-ca.crt", "defaultMode": 420}}'
fi
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT"
echo -n "."
oc apply -f "$DEPLOYMENT" >/dev/null 2>&1
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT_PATCHED"
echo "OK"

# Wait for the configuration to be deployed
while kubectl get "$ORIGNAL_POD" -o name >/dev/null 2>&1 ; do
echo -n "_"
sleep 2
done
echo -n "."
echo -n "* Updating deployment: "
yq -i 'sort_keys(..)' "$DEPLOYMENT"
yq -i 'sort_keys(..)' "$DEPLOYMENT_PATCHED"
if ! diff --brief "$DEPLOYMENT" "$DEPLOYMENT_PATCHED" >/dev/null; then
oc apply -f "$DEPLOYMENT_PATCHED" >/dev/null 2>&1

echo "OK"
# Wait for the configuration to be deployed
while kubectl get "$ORIGNAL_POD" -o name >/dev/null 2>&1 ; do
echo -n "_"
sleep 2
done
echo -n "OK"
else
echo "Configuration already up to date"
fi
{{ end }}
48 changes: 30 additions & 18 deletions test/data/helm-chart/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,15 @@ spec:
set -o pipefail
echo -n "Installing utils: "
dnf install -y diffutils > /dev/null 2>/dev/null
echo -n "."

# Installing Helm...
curl --fail --silent --show-error --location \
https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 \
| bash
| bash >/dev/null
echo "OK"

YQ_VERSION="v4.40.5"
curl --fail --location --output "/usr/bin/yq" --silent --show-error "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64"
Expand Down Expand Up @@ -427,11 +431,13 @@ spec:
ORIGNAL_POD=$(kubectl get pods -l app.kubernetes.io/name=developer-hub -o name)
DEPLOYMENT="/tmp/deployment.yaml"
DEPLOYMENT_PATCHED="/tmp/deployment.patched.yaml"
oc get deployment/developer-hub -o yaml >"$DEPLOYMENT"
cp "$DEPLOYMENT" "$DEPLOYMENT_PATCHED"
echo -n "* Configure TLS:"
echo -n "* Configuring TLS:"
# Update env var.
if [ "$(yq '.spec.template.spec.containers[0].env[] | select(.name == "NODE_EXTRA_CA_CERTS") | length' "$DEPLOYMENT")" == "2" ]; then
if [ "$(yq '.spec.template.spec.containers[0].env[] | select(.name == "NODE_EXTRA_CA_CERTS") | length' "$DEPLOYMENT_PATCHED")" == "2" ]; then
YQ_EXPRESSION='
(
.spec.template.spec.containers[].env[] |
Expand All @@ -441,10 +447,10 @@ spec:
else
YQ_EXPRESSION='.spec.template.spec.containers[0].env += {"name": "NODE_EXTRA_CA_CERTS", "value": "/ingress-cert/ca.crt"}'
fi
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT"
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT_PATCHED"
echo -n "."
# Update volume mount
if [ "$(yq '.spec.template.spec.containers[0].volumeMounts[] | select(.name == "kube-root-ca") | length' "$DEPLOYMENT")" == "2" ]; then
if [ "$(yq '.spec.template.spec.containers[0].volumeMounts[] | select(.name == "kube-root-ca") | length' "$DEPLOYMENT_PATCHED")" == "2" ]; then
YQ_EXPRESSION='
(
.spec.template.spec.containers[].volumeMounts[] |
Expand All @@ -454,10 +460,10 @@ spec:
else
YQ_EXPRESSION='.spec.template.spec.containers[0].volumeMounts += {"name": "kube-root-ca", "mountPath": "/ingress-cert"}'
fi
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT"
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT_PATCHED"
echo -n "."
# Update volume
if [ "$(yq '.spec.template.spec.volumes[] | select(.name == "kube-root-ca") | length' "$DEPLOYMENT")" == "2" ]; then
if [ "$(yq '.spec.template.spec.volumes[] | select(.name == "kube-root-ca") | length' "$DEPLOYMENT_PATCHED")" == "2" ]; then
YQ_EXPRESSION='
(
.spec.template.spec.volumes[] |
Expand All @@ -467,18 +473,24 @@ spec:
else
YQ_EXPRESSION='.spec.template.spec.volumes += {"name": "kube-root-ca", "configMap": {"name": "kube-root-ca.crt", "defaultMode": 420}}'
fi
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT"
echo -n "."
oc apply -f "$DEPLOYMENT" >/dev/null 2>&1
yq --inplace "$YQ_EXPRESSION" "$DEPLOYMENT_PATCHED"
echo "OK"

# Wait for the configuration to be deployed
while kubectl get "$ORIGNAL_POD" -o name >/dev/null 2>&1 ; do
echo -n "_"
sleep 2
done
echo -n "."
echo -n "* Updating deployment: "
yq -i 'sort_keys(..)' "$DEPLOYMENT"
yq -i 'sort_keys(..)' "$DEPLOYMENT_PATCHED"
if ! diff --brief "$DEPLOYMENT" "$DEPLOYMENT_PATCHED" >/dev/null; then
oc apply -f "$DEPLOYMENT_PATCHED" >/dev/null 2>&1

echo "OK"
# Wait for the configuration to be deployed
while kubectl get "$ORIGNAL_POD" -o name >/dev/null 2>&1 ; do
echo -n "_"
sleep 2
done
echo -n "OK"
else
echo "Configuration already up to date"
fi


echo -n "* Waiting for UI: "
Expand Down Expand Up @@ -1071,7 +1083,7 @@ spec:

for DEV_NAMESPACE in "${DEV_NAMESPACE_LIST[@]}"; do
echo -n "* Creating '$DEV_NAMESPACE' namespace: "
kubectl create namespace "$DEV_NAMESPACE" >/dev/null
kubectl create namespace "$DEV_NAMESPACE" --dry-run=client -o yaml | kubectl apply -f - >/dev/null
echo -n "."
kubectl annotate namespace "$DEV_NAMESPACE" "argocd.argoproj.io/managed-by=$NAMESPACE" >/dev/null
echo "OK"
Expand Down

0 comments on commit 9201850

Please sign in to comment.