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

Add a new configmap for variables from the Lagoon API only #2348

Closed
63 changes: 59 additions & 4 deletions images/kubectl-build-deploy-dind/build-deploy-docker-compose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1003,15 +1003,39 @@ LAGOON_AUTOGENERATED_ROUTES=${AUTOGENERATED_ROUTES}\n\
# Generate a Config Map with project wide env variables
kubectl -n ${NAMESPACE} create configmap lagoon-env -o yaml --dry-run --from-env-file=/kubectl-build-deploy/values.env | kubectl apply -n ${NAMESPACE} -f -

# process the api-configmap template with null data value
# doing this means that the only variables in `lagoon-api-vars` configmap will be ones that come from the lagoon api
# this allows us to remove them from the lagoon api and not leave them hanging around in a configmap
# the lagoon-env configmap can then be left for persistent changes
# mounting `lagoon-api-vars` into a pod after `lagoon-env` means that what is in the api-vars configmap
# should be used instead of whats in lagoon-env, meaning API variables will be used before lagoon-env ones
if ! kubectl --insecure-skip-tls-verify -n ${NAMESPACE} get configmap lagoon-api-vars &> /dev/null; then
# only create the configmap if it doesn't already exist
cat <<EOF | kubectl --insecure-skip-tls-verify apply -n ${NAMESPACE} -f -
kind: ConfigMap
apiVersion: v1
metadata:
name: lagoon-api-vars
data:
EOF
fi

set +x # reduce noise in build logs
# store the existing variables that are in the configmap here
EXISTING_CONFIGMAP_VARS=""
if kubectl --insecure-skip-tls-verify -n ${NAMESPACE} get configmap lagoon-api-vars -o json | jq -r '.data | keys[]' 2> /dev/null; then
EXISTING_CONFIGMAP_VARS=$(kubectl --insecure-skip-tls-verify -n ${NAMESPACE} get configmap lagoon-api-vars -o json | jq -r '.data | keys[]' 2> /dev/null)
fi

# Add environment variables from lagoon API
# patch the `lagoon-api-vars` configmap with what is in actually in the lagoon api
if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then
HAS_PROJECT_RUNTIME_VARS=$(echo $LAGOON_PROJECT_VARIABLES | jq -r 'map( select(.scope == "runtime" or .scope == "global") )')

if [ ! "$HAS_PROJECT_RUNTIME_VARS" = "[]" ]; then
kubectl patch --insecure-skip-tls-verify \
-n ${NAMESPACE} \
configmap lagoon-env \
configmap lagoon-api-vars \
-p "{\"data\":$(echo $LAGOON_PROJECT_VARIABLES | jq -r 'map( select(.scope == "runtime" or .scope == "global") ) | map( { (.name) : .value } ) | add | tostring')}"
fi
fi
Expand All @@ -1021,10 +1045,40 @@ if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then
if [ ! "$HAS_ENVIRONMENT_RUNTIME_VARS" = "[]" ]; then
kubectl patch --insecure-skip-tls-verify \
-n ${NAMESPACE} \
configmap lagoon-env \
configmap lagoon-api-vars \
-p "{\"data\":$(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r 'map( select(.scope == "runtime" or .scope == "global") ) | map( { (.name) : .value } ) | add | tostring')}"
fi
fi

# if there were existing vars in the configmap
# work out which ones no longer exist in the API and run patch op remove on them
if [ ! -z "$EXISTING_CONFIGMAP_VARS" ]; then
# get what is in the configmap now that the patch operations to add what is in the API has been done already
CURRENT_CONFIGMAP_VARS=$(kubectl --insecure-skip-tls-verify -n ${NAMESPACE} get configmap lagoon-api-vars -o json | jq -r '.data | keys[]')
# get the keys of the vars that were added from the api
API_PROJECT_VARS=$(echo $LAGOON_PROJECT_VARIABLES | jq -r 'map( select(.scope == "runtime" or .scope == "global") ) | map( { (.name) : .value } ) | add | keys[]')
API_ENVIRONMENT_VARS=$(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r 'map( select(.scope == "runtime" or .scope == "global") ) | map( { (.name) : .value } ) | add | keys[]')
# get all the unique keys from the API and current configmap
# and remove anything that isn't in the API anymore from the configmap
VARS_TO_REMOVE1=$(comm -23 <(echo $CURRENT_CONFIGMAP_VARS | tr ' ' '\n' | sort) <(echo $API_ENVIRONMENT_VARS | tr ' ' '\n' | sort))
VARS_TO_REMOVE2=$(comm -23 <(echo $VARS_TO_REMOVE1 | tr ' ' '\n' | sort) <(echo $API_PROJECT_VARS | tr ' ' '\n' | sort))

# now work out the patch operations to remove the unneeded keys from the configmap
REMOVE_OPERATION_JSON=""

# if there are vars to remove, then craft the remove operation patch
if [ ! -z "$VARS_TO_REMOVE2" ]; then
for VAR_TO_REMOVE in $VARS_TO_REMOVE2
do
REMOVE_OPERATION_JSON="${REMOVE_OPERATION_JSON:+$REMOVE_OPERATION_JSON, }$(echo -n {\"op\": \"remove\", \"path\": \"/data/$VAR_TO_REMOVE\"})"
done
# then actually apply the patch to remove the vars from the configmap
kubectl patch --insecure-skip-tls-verify \
-n ${NAMESPACE} \
configmap lagoon-api-vars \
--type=json -p "[$REMOVE_OPERATION_JSON]"
fi
fi
set -x

if [ "$BUILD_TYPE" == "pullrequest" ]; then
Expand Down Expand Up @@ -1067,8 +1121,9 @@ done
##############################################
### REDEPLOY DEPLOYMENTS IF CONFIG MAP CHANGES
##############################################

CONFIG_MAP_SHA=$(kubectl --insecure-skip-tls-verify -n ${NAMESPACE} get configmap lagoon-env -o yaml | shyaml get-value data | sha256sum | awk '{print $1}')
VARS_CONFIGMAP_SHA=$(kubectl --insecure-skip-tls-verify -n ${NAMESPACE} get configmap lagoon-api-vars -o yaml | shyaml get-value data "" | sha256sum | awk '{print $1}')
LAGOON_CONFIG_MAP_SHA=$(kubectl --insecure-skip-tls-verify -n ${NAMESPACE} get configmap lagoon-env -o yaml | shyaml get-value data "" | sha256sum | awk '{print $1}')
CONFIG_MAP_SHA=$(echo $VARS_CONFIGMAP_SHA$LAGOON_CONFIG_MAP_SHA | sha256sum | awk '{print $1}')
# write the configmap to the values file so when we `exec-kubectl-resources-with-images.sh` the deployments will get the value of the config map
# which will cause a change in the deployment and trigger a rollout if only the configmap has changed
yq write -i -- /kubectl-build-deploy/values.yaml 'configMapSha' $CONFIG_MAP_SHA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
volumeMounts:
- mountPath: /var/run/secrets/lagoon/sshkey/
name: lagoon-sshkey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
ports:
- name: 5601-tcp
containerPort: 5601
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
restartPolicy: Never
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
ports:
- name: 9600-tcp
containerPort: 9600
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
ports:
- containerPort: 3306
protocol: TCP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
volumeMounts:
- name: {{ include "nginx-php-persistent.persistentStorageName" . }}
mountPath: {{ .Values.persistentStorage.path | quote }}
Expand All @@ -92,6 +94,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
env:
# LAGOON_GIT_SHA is injected directly and not loaded via `lagoon-env` config
# This will cause the pod to redeploy on every deployment, even the files have not changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml .Values.resources.nginx | nindent 12 }}

Expand All @@ -83,6 +85,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
env:
# LAGOON_GIT_SHA is injected directly and not loaded via `lagoon-env` config
# This will cause the pod to redeploy on every deployment, even the files have not changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
volumeMounts:
- name: {{ include "node-persistent.persistentStorageName" . }}
mountPath: {{ .Values.persistentStorage.path | quote }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
restartPolicy: Never
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
volumeMounts:
- name: {{ include "redis-persistent.persistentStorageName" . }}
mountPath: {{ .Values.persistentStorage.path | quote }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
image: imagecache.amazeeio.cloud/library/alpine
imagePullPolicy: Always
name: {{ include "redis-persistent.fullname" . }}-prebackuppod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
restartPolicy: Never
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
ports:
- name: 6379-tcp
containerPort: 6379
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
restartPolicy: Never
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
env:
- name: CRONJOBS
value: {{ .Values.inPodCronjobs | quote }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
image: imagecache.amazeeio.cloud/library/alpine
imagePullPolicy: Always
name: {{ include "solr.fullname" . }}-prebackuppod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
resources:
{{- toYaml $.Values.resources | nindent 16 }}
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
ports:
- name: http
containerPort: 8080
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ spec:
envFrom:
- configMapRef:
name: lagoon-env
- configMapRef:
name: lagoon-api-vars
image: imagecache.amazeeio.cloud/library/alpine
imagePullPolicy: Always
name: {{ include "varnish-persistent.fullname" . }}-prebackuppod
Expand Down
Loading