From 23b6a2aea833e8b8484b51a82a310597711bbedf Mon Sep 17 00:00:00 2001 From: Thong Kuah Date: Thu, 3 Jan 2019 14:50:53 +1300 Subject: Simplify bash function Use --from-env-file with bash process substitution We still need bash as process substition (`<()`) is not available in sh --- lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml | 38 +++++++---------------- 1 file changed, 11 insertions(+), 27 deletions(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml b/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml index 4623636144a..222856f9e14 100644 --- a/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml @@ -598,40 +598,24 @@ rollout 100%: # Extracts variables prefixed with K8S_SECRET_ # and creates a Kubernetes secret. # - # e.g. if we have the following vars + # e.g. If we have the following environment variables: # K8S_SECRET_A=value1 # K8S_SECRET_B=multi\ word\ value # - # Then we get: - # --from-literal K8S_SECRET_A=value1 --from-literal 'K8S_SECRET_B=multi word value' - # - # NOTE: We set IFS as we need to split by newline so that we can loop through - # multi word variables correctly. + # Then we will create a secret with the following key-value pairs: + # data: + # A: dmFsdWUxCg== + # B: bXVsdGkgd29yZCB2YWx1ZQo= function create_application_secret() { bash -c ' - function extract_prefixed_variables() { - prefix="K8S_SECRET_" - k8s_variables=$(env | (grep "^${prefix}" || [[ $? == 1 ]])) - - export K8S_VARIABLES=$k8s_variables - } - - function create_secret() { - local IFS=$(echo -en "\n\b") - for k8s_variable in $K8S_VARIABLES; do - param="${k8s_variable#K8S_SECRET_}" - - fromLiteralArgs+=("--from-literal") - fromLiteralArgs+=("${param}") - done - - kubectl create secret \ - -n "$KUBE_NAMESPACE" generic "$APPLICATION_SECRET_NAME" ${fromLiteralArgs[@]} -o yaml \ - --dry-run | kubectl replace -n "$KUBE_NAMESPACE" --force -f - + function k8s_prefixed_variables() { + env | sed -n "s/^K8S_SECRET_\(.*\)$/\1/p" } - extract_prefixed_variables - create_secret + kubectl create secret \ + -n "$KUBE_NAMESPACE" generic "$APPLICATION_SECRET_NAME" \ + --from-env-file <(k8s_prefixed_variables) -o yaml --dry-run | + kubectl replace -n "$KUBE_NAMESPACE" --force -f - ' } -- cgit v1.2.3