From 2bc13e6d618ceafed44b01757e446a06fac68eb6 Mon Sep 17 00:00:00 2001 From: Neil MacDougall Date: Fri, 6 Mar 2020 09:28:46 +0000 Subject: [PATCH 1/2] Fix Jenkins unit tests --- deploy/ci/automation/helmtest.sh | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/deploy/ci/automation/helmtest.sh b/deploy/ci/automation/helmtest.sh index e5fd7abd75..9f4ca0077b 100755 --- a/deploy/ci/automation/helmtest.sh +++ b/deploy/ci/automation/helmtest.sh @@ -1,9 +1,5 @@ #!/bin/bash -echo "=================" -echo "Stratos Helm Test" -echo "=================" - DIRPATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" STRATOS="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd ../../.. && pwd)" @@ -12,6 +8,12 @@ YELLOW="\033[93m" RESET="\033[0m" BOLD="\033[1m" +echo -e "${YELLOW}${BOLD}" +echo "=================" +echo "Stratos Helm Test" +echo "=================" +echo -e "${RESET}" + set -e # We should be running in the Stratos GitHub folder @@ -55,9 +57,10 @@ function waitForHelmRelease { TOTAL=$(($READY + $COMPLETED)) EXPECTED=$(($COUNT - 1)) if [ $TOTAL -eq $EXPECTED ]; then - READY1=$(kubectl get po --namespace=${NAMESPACE} | grep "3/3" | wc -l) - READY2=$(kubectl get po --namespace=${NAMESPACE} | grep "1/1" | wc -l) - READY=$(($READY1 + $READY2)) + READY3=$(kubectl get po --namespace=${NAMESPACE} | grep "3/3" | wc -l) + READY2=$(kubectl get po --namespace=${NAMESPACE} | grep "2/2" | wc -l) + READY1=$(kubectl get po --namespace=${NAMESPACE} | grep "1/1" | wc -l) + READY=$(($READY1 + $READY2 + $READY3)) if [ $READY -eq 2 ]; then DONE="true" fi @@ -65,7 +68,7 @@ function waitForHelmRelease { fi if [ "$DONE" != "true" ]; then echo "Waiting for Stratos Helm release to be ready..." - sleep 5 + sleep 10 TIMEOUT=$((TIMEOUT+1)) if [ ${TIMEOUT} -gt 60 ]; then echo "Timed out waiting for Helm release to be ready" @@ -196,4 +199,4 @@ checkVersion console-0.2.0 # All okay deleteRelease -log "All checks completed" +log "All checks completed" \ No newline at end of file From 78ec1e2fb426fa1908dd3f2937e340c99d4a144f Mon Sep 17 00:00:00 2001 From: Neil MacDougall Date: Fri, 6 Mar 2020 12:24:45 +0000 Subject: [PATCH 2/2] Helm Upgrade fixes --- deploy/ci/automation/helmtest.sh | 61 +++++++++++-------- .../tasks/dev-releases/create-chart-helper.sh | 5 +- deploy/kubernetes/build.sh | 26 +++++++- .../kubernetes/console/templates/secrets.yaml | 12 ++-- 4 files changed, 72 insertions(+), 32 deletions(-) diff --git a/deploy/ci/automation/helmtest.sh b/deploy/ci/automation/helmtest.sh index 9f4ca0077b..27040121a2 100755 --- a/deploy/ci/automation/helmtest.sh +++ b/deploy/ci/automation/helmtest.sh @@ -9,9 +9,9 @@ RESET="\033[0m" BOLD="\033[1m" echo -e "${YELLOW}${BOLD}" -echo "=================" -echo "Stratos Helm Test" -echo "=================" +echo "=========================================================================================================" +echo "===== Stratos Helm Tests =====" +echo "=========================================================================================================" echo -e "${RESET}" set -e @@ -82,7 +82,7 @@ function checkVersion { VERS=$1 STATUS=$(helm list ${NAME} | grep ${NAME}) STATUS=$(echo $STATUS | awk '{$1=$1};1') - local HELM_STATUS_REGEX='^([a-z\-]*) ([0-9]*) ([A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]) ([A-Z]*) ([0-9\.a-z\-]*) ([0-9\.]*) ([a-z\-]*)' + local HELM_STATUS_REGEX='^([a-z\-]*) ([0-9]*) ([A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]) ([A-Z]*) ([0-9\.a-z\-]*) ([0-9\.a-z\-]*) ([a-z\-]*)' echo -e $STATUS if [[ "${STATUS}" =~ ${HELM_STATUS_REGEX} ]]; then # 6 is version @@ -94,11 +94,17 @@ function checkVersion { echo "Helm status parsing failed" exit 1 fi + + echo "Helm release version ok (${BASH_REMATCH[5]})" } function log { MSG=$1 - echo -e "${CYAN}${BOLD}${MSG}${RESET}" + echo -e "${CYAN}${BOLD}" + echo "=========================================================================================================" + echo "==>> ${MSG}" + echo "=========================================================================================================" + echo -e "${RESET}" } log "Performing checks..." @@ -117,6 +123,14 @@ if [ "$EXISTING" -ne 1 ]; then helm repo add ${HELM_REPO_NAME} ${HELM_REPO} fi +KUBE_FOLDER="${STRATOS}/deploy/kubernetes" +HELM_TMP="${STRATOS}/deploy/kubernetes/helm-chart" +echo $HELM_TMP +echo "${KUBE_FOLDER}" +pushd "${KUBE_FOLDER}" > /dev/null +rm -rf *.tgz +popd > /dev/null + # Update repos helm repo update @@ -133,37 +147,36 @@ waitForHelmRelease # Try and upgrade to the latest Chart # Copy the helm chart folder to a temp location -TMP_DIR=${TMP_DIR:-/tmp} -HELM_TMP=${TMP_DIR}/stratos_helm_test + +# Use the buil script to build the Helm Chart +KUBE_FOLDER="${STRATOS}/deploy/kubernetes" +HELM_TMP="${STRATOS}/deploy/kubernetes/helm-chart" echo $HELM_TMP -rm -rf ${HELM_TMP} -mkdir -p ${HELM_TMP} -cp -R "${STRATOS}/deploy/kubernetes/console" ${HELM_TMP} +echo "${KUBE_FOLDER}" +ls -al "${KUBE_FOLDER}" +rm -rf "${KUBE_FOLDER}/*.tgz" -pushd ${HELM_TMP} > /dev/null -# Make sure we can package the chart -helm package ${HELM_TMP}/console +# Build the chart with the specific version for images +"${STRATOS}/deploy/kubernetes/build.sh" -z -c -n -t ${DEV_IMAGE_VERSION} -CHART_FILE=$(ls ${HELM_TMP}/*.tgz) +CHART_FILE=$(ls ${KUBE_FOLDER}/*.tgz) CHART_FILE=$(printf %q "${CHART_FILE}") echo "Chart file path: ${CHART_FILE}" -popd > /dev/null - log "Upgrading using latest Helm Chart" helm upgrade ${NAME} ${CHART_FILE} --recreate-pods --debug --set consoleVersion=${DEV_IMAGE_VERSION} --set imagePullPolicy=Always -checkVersion console-0.1.0 +checkVersion console-${DEV_IMAGE_VERSION} waitForHelmRelease # Change just the chart version and try to upgrade -sed -i.bak -e 's/version: 0.1.0/version: 0.2.0/g' ${HELM_TMP}/console/Chart.yaml -sed -i.bak -e 's/appVersion: 0.1.0/appVersion: 0.2.0/g' ${HELM_TMP}/console/Chart.yaml -cat ${HELM_TMP}/console/Chart.yaml +sed -i.bak -e 's/version: '"${DEV_IMAGE_VERSION}"'/version: 0.2.0/g' ${HELM_TMP}/Chart.yaml +sed -i.bak -e 's/appVersion: '"${DEV_IMAGE_VERSION}"'/appVersion: 0.2.0/g' ${HELM_TMP}/Chart.yaml +cat ${HELM_TMP}/Chart.yaml log "Upgrading using latest Helm Chart (checking chart upgrade)" -helm upgrade ${NAME} ${HELM_TMP}/console --recreate-pods --debug --set consoleVersion=${DEV_IMAGE_VERSION} --set imagePullPolicy=Always +helm upgrade ${NAME} ${HELM_TMP} --recreate-pods --debug --set imagePullPolicy=Always waitForHelmRelease checkVersion console-0.2.0 @@ -172,10 +185,10 @@ checkVersion console-0.2.0 deleteRelease log "Installing using latest Helm Chart" -helm install ${CHART_FILE} --name ${NAME} --namespace ${NAMESPACE} --set consoleVersion=${DEV_IMAGE_VERSION} --set imagePullPolicy=Always +helm install ${CHART_FILE} --name ${NAME} --namespace ${NAMESPACE} --set imagePullPolicy=Always waitForHelmRelease -checkVersion console-0.1.0 +checkVersion console-${DEV_IMAGE_VERSION} deleteRelease @@ -190,7 +203,7 @@ helm install ${HELM_REPO_NAME}/console --name ${NAME} --namespace ${NAMESPACE} waitForHelmRelease log "Upgrading using --reuse-values" -helm upgrade ${NAME} ${HELM_TMP}/console --recreate-pods --debug --reuse-values +helm upgrade ${NAME} ${HELM_TMP} --recreate-pods --debug --reuse-values waitForHelmRelease # Should have used same values as before diff --git a/deploy/ci/tasks/dev-releases/create-chart-helper.sh b/deploy/ci/tasks/dev-releases/create-chart-helper.sh index 8db92c215a..2079cf4544 100644 --- a/deploy/ci/tasks/dev-releases/create-chart-helper.sh +++ b/deploy/ci/tasks/dev-releases/create-chart-helper.sh @@ -15,7 +15,10 @@ patchHelmChart () { sed -i -e 's/appVersion: 0.1.0/appVersion: '"${APP_VERSION}"'/g' ${CHART_PATH}/Chart.yaml # Patch the console image tag in place - otherwise --reuse-values won't work with helm upgrade - sed -i -e 's/{{.Values.consoleVersion}}/'"${TAG}"'/g' ${CHART_PATH}/templates/deployment.yaml + # Make sure we patch this in all files in templates where it occurs + pushd ${CHART_PATH}/templates > dev/null + find . -type f -name '*.yaml' | xargs sed -i -e 's/{{.Values.consoleVersion}}/'"${TAG}"'/g' + popd > /dev/null } patchHelmChartDev () { diff --git a/deploy/kubernetes/build.sh b/deploy/kubernetes/build.sh index 5697be80a8..8c5a76a347 100755 --- a/deploy/kubernetes/build.sh +++ b/deploy/kubernetes/build.sh @@ -29,8 +29,9 @@ DOCKER_REG_DEFAULTS="true" CHART_ONLY="false" ADD_GITHASH_TO_TAG="true" HAS_CUSTOM_BUILD="false" +PACKAGE_CHART="false" -while getopts ":ho:r:t:Tclb:Opcn" opt; do +while getopts ":ho:r:t:Tclb:Opcnz" opt; do case $opt in h) echo @@ -73,6 +74,9 @@ while getopts ":ho:r:t:Tclb:Opcn" opt; do n) ADD_GITHASH_TO_TAG="false" ;; + z) + PACKAGE_CHART="true" + ;; \?) echo "Invalid option: -${OPTARG}" >&2 exit 1 @@ -247,6 +251,14 @@ sed -i.bak -e 's/hostname: docker.io/hostname: '"${DOCKER_REGISTRY}"'/g' values. sed -i.bak -e 's/version: 0.1.0/version: '"${TAG}"'/g' Chart.yaml sed -i.bak -e 's/appVersion: 0.1.0/appVersion: '"${TAG}"'/g' Chart.yaml + +# Patch the console image tag in place - otherwise --reuse-values won't work with helm upgrade +# Make sure we patch all files that have this reference +cd templates +find . -type f -name '*.yaml' | xargs sed -i.bak -e 's/{{.Values.consoleVersion}}/'"${TAG}"'/g' +find . -type f -name "*.bak" -delete +cd .. + rm -rf *.bak # Generate image list @@ -255,6 +267,18 @@ ${STRATOS_PATH}/deploy/kubernetes/imagelist-gen.sh . popd > /dev/null +if [ "${PACKAGE_CHART}" == "true" ]; then + echo "Packaging Helm Chart" + pushd ${STRATOS_PATH}/deploy/kubernetes > /dev/null + PKG_DIST_FOLDER="dist/${TAG}/console" + rm -rf ${PKG_DIST_FOLDER} + mkdir -p ${PKG_DIST_FOLDER} + cp -R ${DEST_HELM_CHART_PATH}/* ${PKG_DIST_FOLDER} + helm package ${PKG_DIST_FOLDER} + rm -rf ${PKG_DIST_FOLDER} + popd > /dev/null +fi + set +e printf "${BOLD}${YELLOW}" diff --git a/deploy/kubernetes/console/templates/secrets.yaml b/deploy/kubernetes/console/templates/secrets.yaml index 544108e376..6e0830dcf4 100644 --- a/deploy/kubernetes/console/templates/secrets.yaml +++ b/deploy/kubernetes/console/templates/secrets.yaml @@ -29,18 +29,18 @@ metadata: helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}" type: Opaque data: - {{- if .Values.mariadb.rootPassword }} - rootPassword: {{ .Values.mariadb.rootPassword | b64enc | quote }} + {{- if or .Values.mariadb.rootPassword .Values.mariadb.mariadbRootPassword }} + rootPassword: {{ (default .Values.mariadb.rootPassword .Values.mariadb.mariadbRootPassword) | b64enc | quote }} {{- else }} rootPassword: {{ randAlphaNum 32 | b64enc | quote }} {{- end }} - {{- if .Values.mariadb.userPassword }} - password: {{ .Values.mariadb.userPassword | b64enc | quote }} + {{- if or .Values.mariadb.userPassword .Values.mariadb.mariadbPassword }} + password: {{ (default .Values.mariadb.userPassword .Values.mariadb.mariadbPassword) | b64enc | quote }} {{- else }} password: {{ randAlphaNum 32 | b64enc | quote }} {{- end }} - user: {{ .Values.mariadb.user | b64enc | quote }} - database: {{ .Values.mariadb.database | b64enc | quote }} + user: {{ default .Values.mariadb.user .Values.mariadb.mariadbUser | b64enc | quote }} + database: {{ default .Values.mariadb.database .Values.mariadb.mariadbDatabase | b64enc | quote }} {{- if .Values.console.sessionStoreSecret }} sessionStoreSecret: {{ .Values.console.sessionStoreSecret | b64enc | quote }} {{- else }}