Commit 912b9076 authored by Thong Kuah's avatar Thong Kuah
Browse files

create_application_secret and test happy path

parent 8ecee728
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -143,6 +143,24 @@ test-persist-environment-url:
    - persist_environment_url
    - grep review-app.example.com environment_url.txt

test-create-application-secret:
  stage: test
  image: "$BUILD_IMAGE_NAME"
  <<: *k3s-services
  variables:
    KUBE_NAMESPACE: default
    CI_ENVIRONMENT_SLUG: production
    K8S_SECRET_CODE: 12345
  script:
    - source /build/deploy-helpers.sh
    - kubectl config set-cluster k3s --server https://node:some-secret@k3s:6443 --insecure-skip-tls-verify
    - kubectl config set-context k3s --cluster=k3s
    - kubectl config use-context k3s
    - kubectl version
    - create_application_secret "stable"
    - kubectl get secrets -n $KUBE_NAMESPACE
    - kubectl get secrets production-secret -n $KUBE_NAMESPACE

test-deploy-name:
  stage: test
  image: "$BUILD_IMAGE_NAME"
+34 −0
Original line number Diff line number Diff line
@@ -78,6 +78,40 @@ function persist_environment_url() {

## Helper functions

# Extracts variables prefixed with K8S_SECRET_
# and creates a Kubernetes secret.
#
# e.g. If we have the following environment variables:
#   K8S_SECRET_A=value1
#   K8S_SECRET_B=multi\ word\ value
#
# Then we will create a secret with the following key-value pairs:
#   data:
#     A: dmFsdWUxCg==
#     B: bXVsdGkgd29yZCB2YWx1ZQo=
function create_application_secret() {
  track="${1-stable}"
  export APPLICATION_SECRET_NAME=$(application_secret_name "$track")

  env | sed -n "s/^K8S_SECRET_\(.*\)$/\1/p" > k8s_prefixed_variables

  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 -

  export APPLICATION_SECRET_CHECKSUM=$(cat k8s_prefixed_variables | sha256sum | cut -d ' ' -f 1)

  rm k8s_prefixed_variables
}

function application_secret_name() {
  track="${1-stable}"
  name=$(deploy_name "$track")

  echo "${name}-secret"
}

function deploy_name() {
  name="$CI_ENVIRONMENT_SLUG"
  track="${1-stable}"