Commit 568bd7bb authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

refactor: use $environment_type & $environment_name

Deprecated $appname and $env dynamic variables with $environment_name & $environment_type
With backward compatibility (legacy vars still usable)
parent 62bfa866
Loading
Loading
Loading
Loading
+13 −13
Original line number Diff line number Diff line
@@ -199,11 +199,11 @@ In this mode, you only have to provide a shell script that fully implements the

The deployment script is searched as follows:

1. look for a specific `k8s-deploy-$env.sh` in the `$K8S_SCRIPTS_DIR` directory in your project (e.g. `k8s-deploy-staging.sh` for staging environment),
1. look for a specific `k8s-deploy-$environment_type.sh` in the `$K8S_SCRIPTS_DIR` directory in your project (e.g. `k8s-deploy-staging.sh` for staging environment),
2. if not found: look for a default `k8s-deploy.sh` in the `$K8S_SCRIPTS_DIR` directory in your project,
3. if not found: the GitLab CI template assumes you're using the template-based deployment policy.

:warning: `k8s-deploy-$env.sh` or `k8s-deploy.sh` needs to be executable, you can add flag execution with:  `git update-index --chmod=+x k8s-deploy.sh`
:warning: `k8s-deploy-$environment_type.sh` or `k8s-deploy.sh` needs to be executable, you can add flag execution with:  `git update-index --chmod=+x k8s-deploy.sh`

Your script(s) may use [dynamic variables](#dynamic-variables).

@@ -216,7 +216,7 @@ The template processes the following steps:

1. _optionally_ executes the `k8s-pre-apply.sh` script in your project to perform specific environment pre-initialization (for e.g. create required services),
2. looks for your Kubernetes deployment file, substitutes [dynamic variables](#dynamic-variables) and [`kubectl apply`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) it,
    1. look for a specific `deployment-$env.yml` in your project (e.g. `deployment-staging.yml` for staging environment),
    1. look for a specific `deployment-$environment_type.yml` in your project (e.g. `deployment-staging.yml` for staging environment),
    2. fallbacks to default `deployment.yml`.
3. _optionally_ executes the `k8s-post-apply.sh` script in your project to perform specific environment post-initialization stuff,

@@ -233,7 +233,7 @@ The template processes the following steps:

1. _optionally_ executes the `k8s-pre-apply.sh` script in your project to perform specific environment pre-initialization (for e.g. create required services),
2. looks for your Kustomization file and [`kubectl apply`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) it,
    1. looks for an environment-specific [overlay](https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#overlay) file `./$env/kustomization.yaml` (e.g. `./staging/kustomization.yaml ` for staging environment),
    1. looks for an environment-specific [overlay](https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#overlay) file `./$environment_type/kustomization.yaml` (e.g. `./staging/kustomization.yaml ` for staging environment),
    2. fallbacks to default `kustomization.yaml`.
3. _optionally_ executes the `k8s-post-apply.sh` script in your project to perform specific environment post-initialization stuff,

@@ -265,17 +265,17 @@ In this mode, you only have to provide a shell script that fully implements the

The a deployment script is searched as follows:

1. look for a specific `k8s-cleanup-$env.sh` in the `$K8S_SCRIPTS_DIR` directory in your project (e.g. `k8s-cleanup-staging.sh` for staging environment),
1. look for a specific `k8s-cleanup-$environment_type.sh` in the `$K8S_SCRIPTS_DIR` directory in your project (e.g. `k8s-cleanup-staging.sh` for staging environment),
2. if not found: look for a default `k8s-cleanup.sh` in the `$K8S_SCRIPTS_DIR` directory in your project,
3. if not found: the GitLab CI template assumes you're using the template-based cleanup policy.

:warning: `k8s-cleanup-$env.sh` or `k8s-cleanup.sh` needs to be executable, you can add flag execution with:  `git update-index --chmod=+x k8s-cleanup.sh`
:warning: `k8s-cleanup-$environment_type.sh` or `k8s-cleanup.sh` needs to be executable, you can add flag execution with:  `git update-index --chmod=+x k8s-cleanup.sh`

Your script(s) may use [dynamic variables](#dynamic-variables).

> TIP: a nice way to implement environment cleanup is to declare the label `app=${appname}` on every Kubernetes
> TIP: a nice way to implement environment cleanup is to declare the label `app=${environment_name}` on every Kubernetes
> object associated to your environment.
> Then environment cleanup can be implemented very easily with command `kubectl delete all,pvc,secret,ingress -l "app=${appname}"`
> Then environment cleanup can be implemented very easily with command `kubectl delete all,pvc,secret,ingress -l "app=${environment_name}"`

#### 2: template-based cleanup

@@ -285,7 +285,7 @@ The template processes the following steps:

1. _optionally_ executes the `k8s-pre-cleanup.sh` script in your project to perform specific environment pre-cleanup stuff,
2. looks for your Kubernetes deployment file, substitutes [dynamic variables](#dynamic-variables) and [`kubectl delete`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) it,
    1. look for a specific `deployment-$env.yml` in your project (e.g. `deployment-staging.yml` for staging environment),
    1. look for a specific `deployment-$environment_type.yml` in your project (e.g. `deployment-staging.yml` for staging environment),
    2. fallbacks to default `deployment.yml`.
3. _optionally_ executes the `k8s-post-cleanup.sh` script in your project to perform specific environment post-cleanup (for e.g. delete bound services).

@@ -301,7 +301,7 @@ The template processes the following steps:

1. _optionally_ executes the `k8s-pre-cleanup.sh` script in your project to perform specific environment pre-cleanup stuff,
2. looks for your Kustomization file and [`kubectl delete`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) it,
    1. looks for an environment-specific [overlay](https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#overlay) file `./$env/kustomization.yaml` (e.g. `./staging/kustomization.yaml ` for staging environment),
    1. looks for an environment-specific [overlay](https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#overlay) file `./$environment_type/kustomization.yaml` (e.g. `./staging/kustomization.yaml ` for staging environment),
    2. fallbacks to default `kustomization.yaml`.
3. _optionally_ executes the `k8s-post-cleanup.sh` script in your project to perform specific environment post-cleanup (for e.g. delete bound services).

@@ -336,10 +336,10 @@ In order to be able to implement some **genericity** in your scripts and templat
2. any [custom variable](https://docs.gitlab.com/ee/ci/variables/#custom-environment-variables)
    (ex: `${SECRET_TOKEN}` that you have set in your project CI/CD variables)
3. **dynamic variables** set by the template:
    * `${appname}`: the application target name to use in this environment (ex: `myproject-review-fix-bug-12` or `myproject-staging`)
    * `${appname_ssc}`: the application target name in [SCREAMING_SNAKE_CASE](https://en.wikipedia.org/wiki/Snake_case) format
    * `${environment_name}`: the application target name to use in this environment (ex: `myproject-review-fix-bug-12` or `myproject-staging`)
    * `${environment_name_ssc}`: the application target name in [SCREAMING_SNAKE_CASE](https://en.wikipedia.org/wiki/Snake_case) format
       (ex: `MYPROJECT_REVIEW_FIX_BUG_12` or `MYPROJECT_STAGING`)
    * `${env}`: the environment type (`review`, `integration`, `staging` or `production`)
    * `${environment_type}`: the environment type (`review`, `integration`, `staging` or `production`)
    * `${hostname}`: the environment hostname, extracted from `${CI_ENVIRONMENT_URL}` (has to be explicitly declared as [`environment:url`](https://docs.gitlab.com/ee/ci/yaml/#environmenturl) in your `.gitlab-ci.yml` file)
    * any [GitLab CI variable](https://docs.gitlab.com/ee/ci/variables/#predefined-environment-variables)
      (ex: `${CI_ENVIRONMENT_URL}` to retrieve the actual environment exposed route)
+61 −57
Original line number Diff line number Diff line
@@ -281,7 +281,7 @@ stages:
    fi
  }
  function login() {
    env=$1
    environment_type=$1
    url=$2
    cacert=$3
    token=$4
@@ -295,7 +295,7 @@ stages:
    elif [ -n "$KUBECONFIG" ]; then
      log_info "--- \\e[32mlogin\\e[0m using kubeconfig file provided by GitLab"
    else
      log_info "--- \\e[32mlogin\\e[0m using exploded kubeconfig parameters (env: \\e[33;1m${env}\\e[0m, url: \\e[33;1m${url}\\e[0m, namespace: \\e[33;1m${namespace}\\e[0m)"
      log_info "--- \\e[32mlogin\\e[0m using exploded kubeconfig parameters (env: \\e[33;1m${environment_type}\\e[0m, url: \\e[33;1m${url}\\e[0m, namespace: \\e[33;1m${namespace}\\e[0m)"

      assert_defined "${url}" "Missing required Kubernetes URL. Provide a kubeconfig file or \$K8S_*_URL"
      assert_defined "${token}" "Missing required Kubernetes Token. Provide a kubeconfig file or \$K8S_*_TOKEN"
@@ -333,10 +333,10 @@ stages:

    if [[ "${K8S_KUSTOMIZE_ENABLED}" == "true" ]]
    then
      kustofile=$(ls -1 "$K8S_SCRIPTS_DIR/${env}/kustomization.yaml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/kustomization.yaml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/${env}/kustomization.yml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/kustomization.yml" 2>/dev/null || echo "")
      kustofile=$(ls -1 "$K8S_SCRIPTS_DIR/${environment_type}/kustomization.yaml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/kustomization.yaml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/${environment_type}/kustomization.yml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/kustomization.yml" 2>/dev/null || echo "")
      if [[ -z "$kustofile" ]]
      then
        log_error "--- kustomize enabled but nor ${env}/kustomization.yaml neither kustomization.yaml was found"
        log_error "--- kustomize enabled but nor ${environment_type}/kustomization.yaml neither kustomization.yaml was found"
        exit 1
      fi
      deploymentdir=$(dirname "$kustofile")
@@ -347,7 +347,7 @@ stages:
    else

      # find deployment file
      deploymentfile=$(ls -1 "$K8S_SCRIPTS_DIR/deployment-${env}.yml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/deployment.yml" 2>/dev/null || echo "")
      deploymentfile=$(ls -1 "$K8S_SCRIPTS_DIR/deployment-${environment_type}.yml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/deployment.yml" 2>/dev/null || echo "")
      if [[ -z "$deploymentfile" ]]
      then
        log_error "--- deployment file not found"
@@ -364,30 +364,34 @@ stages:
  }

  function deploy() {
    export env=$1
    export environment_type=$1
    export environment_name=$2
    # also export environment_name in SCREAMING_SNAKE_CASE format (may be useful with Kubernetes env variables)
    environment_name_ssc=$(to_ssc "$2")
    export environment_name_ssc

    # for backward compatibility
    export stage=$1
    export appname=$2
    # also export appname in SCREAMING_SNAKE_CASE format (may be useful with Kubernetes env variables)
    appname_ssc=$(to_ssc "$2")
    export appname_ssc
    export stage=$environment_type
    export env=$environment_type
    export appname=$environment_name
    export appname_ssc=$environment_name_ssc

    # extract hostname from $CI_ENVIRONMENT_URL
    hostname=$(echo "$CI_ENVIRONMENT_URL" | awk -F[/:] '{print $4}')
    export hostname

    # for backward compatibility
    export K8S_APP_NAME=$appname
    export K8S_ENV=$env
    log_info "--- \\e[32mdeploy\\e[0m"
    log_info "--- \$environment_type: \\e[33;1m${environment_type}\\e[0m"
    log_info "--- \$environment_name: \\e[33;1m${environment_name}\\e[0m"
    log_info "--- \$environment_name_ssc: \\e[33;1m${environment_name_ssc}\\e[0m"
    log_info "--- \$hostname: \\e[33;1m${hostname}\\e[0m"

    log_info "--- \\e[32mdeploy\\e[0m (env: \\e[33;1m${env}\\e[0m)"
    log_info "--- looking for Kubernetes scripts in directory: \\e[33;1m${K8S_SCRIPTS_DIR}\\e[0m"
    log_info "--- appname: \\e[33;1m${appname}\\e[0m"
    log_info "--- appname_ssc: \\e[33;1m${appname_ssc}\\e[0m"
    log_info "--- env: \\e[33;1m${env}\\e[0m"
    log_info "--- hostname: \\e[33;1m${hostname}\\e[0m"
    # unset any upstream deployment env & artifacts
    rm -f kubernetes.env
    rm -f environment_url.txt

    # maybe execute deploy script
    deployscript=$(ls -1 "$K8S_SCRIPTS_DIR/k8s-deploy-${env}.sh" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/k8s-deploy.sh" 2>/dev/null || echo "")
    deployscript=$(ls -1 "$K8S_SCRIPTS_DIR/k8s-deploy-${environment_type}.sh" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/k8s-deploy.sh" 2>/dev/null || echo "")
    if [[ -f "$deployscript" ]]
    then
      log_info "--- deploy script (\\e[33;1m${deployscript}\\e[0m) found: execute"
@@ -418,7 +422,7 @@ stages:

    # persist environment url
    echo "$CI_ENVIRONMENT_URL" > environment_url.txt
    echo -e "environment_type=$env\\nenvironment_name=$appname\\nenvironment_url=$CI_ENVIRONMENT_URL" > kubernetes.env
    echo -e "environment_type=$environment_type\\nenvironment_name=$environment_name\\nenvironment_url=$CI_ENVIRONMENT_URL" > kubernetes.env

    # maybe execute readiness check script
    readycheck="$K8S_SCRIPTS_DIR/k8s-readiness-check.sh"
@@ -431,27 +435,27 @@ stages:
  }

  function rollback() {
    export env=$1
    export environment_type=$1
    export environment_name=$2
    # also export environment_name in SCREAMING_SNAKE_CASE format (may be useful with Kubernetes env variables)
    environment_name_ssc=$(to_ssc "$2")
    export environment_name_ssc

    # for backward compatibility
    export stage=$1
    export appname=$2
    # also export appname in SCREAMING_SNAKE_CASE format (may be useful with Kubernetes env variables)
    appname_ssc=$(to_ssc "$2")
    export appname_ssc
    export stage=$environment_type
    export env=$environment_type
    export appname=$environment_name
    export appname_ssc=$environment_name_ssc

    # extract hostname from $CI_ENVIRONMENT_URL
    hostname=$(echo "$CI_ENVIRONMENT_URL" | awk -F[/:] '{print $4}')
    export hostname

    # for backward compatibility
    export K8S_APP_NAME=$appname
    export K8S_ENV=$env

    log_info "--- \\e[32mrollback\\e[0m (env: \\e[33;1m${env}\\e[0m)"
    log_info "--- looking for Kubernetes scripts in directory: \\e[33;1m${K8S_SCRIPTS_DIR}\\e[0m)"
    log_info "--- appname: \\e[33;1m${appname}\\e[0m"
    log_info "--- appname_ssc: \\e[33;1m${appname_ssc}\\e[0m"
    log_info "--- env: \\e[33;1m${env}\\e[0m"
    log_info "--- hostname: \\e[33;1m${hostname}\\e[0m"
    log_info "--- \\e[32mrollback\\e[0m"
    log_info "--- \$environment_type: \\e[33;1m${environment_type}\\e[0m"
    log_info "--- \$environment_name: \\e[33;1m${environment_name}\\e[0m"
    log_info "--- \$environment_name_ssc: \\e[33;1m${environment_name_ssc}\\e[0m"
    log_info "--- \$hostname: \\e[33;1m${hostname}\\e[0m"

    # maybe execute pre cleanup script
    prescript="$K8S_SCRIPTS_DIR/k8s-pre-rollback.sh"
@@ -465,7 +469,7 @@ stages:
    # rollback app
    log_info "--- \\e[32mkubectl rollout undo\\e[0m"

    kubectl ${TRACE+-v=5} rollout undo deployment "$appname-$env"
    kubectl ${TRACE+-v=5} rollout undo deployment "$environment_name-$environment_type"

    # maybe execute post cleanup script
    postscript="$K8S_SCRIPTS_DIR/k8s-post-rollback.sh"
@@ -478,25 +482,25 @@ stages:
  }

  function cleanup() {
    export env=$1
    # for backward compatibility
    export stage=$1
    export appname=$2
    # also export appname in SCREAMING_SNAKE_CASE format (may be useful with Kubernetes env variables)
    appname_ssc=$(to_ssc "$2")
    export appname_ssc
    export environment_type=$1
    export environment_name=$2
    # also export environment_name in SCREAMING_SNAKE_CASE format (may be useful with Kubernetes env variables)
    environment_name_ssc=$(to_ssc "$2")
    export environment_name_ssc

    # for backward compatibility
    export K8S_APP_NAME=$appname
    export K8S_ENV=$env
    export stage=$environment_type
    export env=$environment_type
    export appname=$environment_name
    export appname_ssc=$environment_name_ssc

    log_info "--- \\e[32mcleanup\\e[0m (env: ${env}, appname: ${appname})"
    log_info "--- appname: \\e[33;1m${appname}\\e[0m"
    log_info "--- appname_ssc: \\e[33;1m${appname_ssc}\\e[0m"
    log_info "--- env: \\e[33;1m${env}\\e[0m"
    log_info "--- \\e[32mcleanup\\e[0m"
    log_info "--- \$environment_type: \\e[33;1m${environment_type}\\e[0m"
    log_info "--- \$environment_name: \\e[33;1m${environment_name}\\e[0m"
    log_info "--- \$environment_name_ssc: \\e[33;1m${environment_name_ssc}\\e[0m"

    # maybe execute cleanup script
    cleanupscript=$(ls -1 "$K8S_SCRIPTS_DIR/k8s-cleanup-${env}.sh" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/k8s-cleanup.sh" 2>/dev/null || echo "")
    cleanupscript=$(ls -1 "$K8S_SCRIPTS_DIR/k8s-cleanup-${environment_type}.sh" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/k8s-cleanup.sh" 2>/dev/null || echo "")
    if [[ -f "$cleanupscript" ]]
    then
      log_info "--- cleanup script (\\e[33;1m${cleanupscript}\\e[0m) found: execute"
@@ -530,13 +534,13 @@ stages:
  }

  function score() {
    export env=$1
    export environment_type=$1
    if [[ "$K8S_KUSTOMIZE_ENABLED" == "true" ]]
    then
      kustofile=$(ls -1 "$K8S_SCRIPTS_DIR/${env}/kustomization.yaml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/kustomization.yaml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/${env}/kustomization.yml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/kustomization.yml" 2>/dev/null || echo "")
      kustofile=$(ls -1 "$K8S_SCRIPTS_DIR/${environment_type}/kustomization.yaml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/kustomization.yaml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/${environment_type}/kustomization.yml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/kustomization.yml" 2>/dev/null || echo "")
      if [[ -z "$kustofile" ]]
      then
        log_error "--- kustomize enabled but nor ${env}/kustomization.yaml neither kustomization.yaml was found"
        log_error "--- kustomize enabled but nor ${environment_type}/kustomization.yaml neither kustomization.yaml was found"
        exit 1
      fi
      deploymentdir=$(dirname "$kustofile")
@@ -545,7 +549,7 @@ stages:
      kustomize build "${deploymentdir}" | /usr/bin/kube-score score $K8S_SCORE_EXTRA_OPTS -
    else
      # find deployment file
      deploymentfile=$(ls -1 "$K8S_SCRIPTS_DIR/deployment-${env}.yml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/deployment.yml" 2>/dev/null || echo "")
      deploymentfile=$(ls -1 "$K8S_SCRIPTS_DIR/deployment-${environment_type}.yml" 2>/dev/null || ls -1 "$K8S_SCRIPTS_DIR/deployment.yml" 2>/dev/null || echo "")
      if [[ -z "$deploymentfile" ]]
      then
        log_error "--- deployment file not found"