Commit 49fc0620 authored by Ronny Moreas's avatar Ronny Moreas Committed by Pierre Smeyers
Browse files

feat: add namespace auto-creation support

parent 1b208b7b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -283,6 +283,7 @@ The Helm template uses some global configuration used throughout all jobs.
| `path` / `HELMFILE_PATH` | Path to file or directory to load helmfile config from. If not defined, defaults to `helmfile.yaml`, `helmfile.yaml.gotmpl` or `helmfile.d` (meaning `helmfile.d/*.yaml` or `helmfile.d/*.yaml.gotmpl`) | _none_ |
| `scripts-dir` / `HELMFILE_SCRIPTS_DIR` | The folder where hook scripts are located | `.` _(root project dir)_ |
| `kube-namespace` / `KUBE_NAMESPACE` | The default Kubernetes namespace to use | `"${CI_PROJECT_NAME}-${CI_PROJECT_ID}-${CI_ENVIRONMENT_SLUG}"` ([see GitLab doc](https://docs.gitlab.com/ci/variables/predefined_variables/)) |
| `create-namespace-enabled` / `HELMFILE_CREATE_NAMESPACE_ENABLED` | Set to `true` to automatically create the Kubernetes namespace if it does not exist | `false` |
| `KUBE_CONTEXT`      | Defines the context to be used in `KUBECONFIG`. When using [GitLab agents with the CI/CD workflow](https://docs.gitlab.com/user/clusters/agent/ci_cd_workflow/), the value should be like `path/to/agent/project:agent-name`. To use different agents per environment, define an [environment-scoped CI/CD variable](https://docs.gitlab.com/ci/environments/#limit-the-environment-scope-of-a-cicd-variable) for each agent. | _none_ |
| :lock: `HELMFILE_DEFAULT_KUBE_CONFIG` | The default kubeconfig to use (either content or file variable) | `$KUBECONFIG` (thus supports the [GitLab Kubernetes integration](https://docs.gitlab.com/user/project/clusters/) when enabled) |
| `deploy-args` / `HELMFILE_DEPLOY_ARGS` | The helmfile [command with options](https://helmfile.readthedocs.io/en/latest/#apply) to deploy the application (_without dynamic global parameters such as `helmfile.yaml` path and environment name_) | `apply --wait` |
+7 −0
Original line number Diff line number Diff line
@@ -24,6 +24,13 @@
      "name": "KUBE_NAMESPACE",
      "description": "The default Kubernetes namespace to use. _Leave default if [GitLab Kubernetes integration](https://docs.gitlab.com/user/project/clusters/) is enabled._"
    },
    {
      "name": "HELMFILE_CREATE_NAMESPACE_ENABLED",
      "description": "Set to `true` to automatically create the Kubernetes namespace if it does not exist",
      "type": "boolean",
      "default": "false",
      "advanced": true
    },
    {
      "name": "HELMFILE_DEFAULT_KUBE_CONFIG",
      "description": "The default kubeconfig content to use. Automatically set if GitLab Kubernetes integration is enabled.",
+16 −0
Original line number Diff line number Diff line
@@ -28,6 +28,10 @@ spec:
    kube-namespace:
      description: The default Kubernetes namespace to use. _Leave default if [GitLab Kubernetes integration](https://docs.gitlab.com/user/project/clusters/) is enabled._
      default: ''
    create-namespace-enabled:
      description: Set to `true` to automatically create the Kubernetes namespace if it does not exist
      type: boolean
      default: false
    base-app-name:
      description: Base application name
      default: $CI_PROJECT_NAME
@@ -199,6 +203,7 @@ variables:

  # Will work with gitlab Kubernetes integration (per env variables)
  KUBE_NAMESPACE: $[[ inputs.kube-namespace ]]
  HELMFILE_CREATE_NAMESPACE_ENABLED: $[[ inputs.create-namespace-enabled ]]
  HELMFILE_ENVIRONMENT_URL: $[[ inputs.environment-url ]]
  HELMFILE_IMAGE_PULL_SECRET_NAME: $[[ inputs.image-pull-secret-name ]]
  HELMFILE_LINT_ENABLED: $[[ inputs.lint-enabled ]]
@@ -589,6 +594,17 @@ stages:
    log_info "--- \$environment_name: \\e[33;1m${environment_name}\\e[0m"
    log_info "--- \$environment_hostname: \\e[33;1m${environment_hostname}\\e[0m"

    # Auto-create namespace if required
    if [[ "$HELMFILE_CREATE_NAMESPACE_ENABLED" == "true" ]]; then
        log_info "--- Auto-create namespace: \\e[33;1m${kube_namespace}\\e[0m"        
        if ! kubectl create namespace "${kube_namespace}" --dry-run=client -o yaml | kubectl apply -f -; then
            fail "Could not create/apply namespace '${kube_namespace}'. Likely insufficient permissions."
        fi
        log_info "--- Namespace '${kube_namespace}' ensured."
    else
        log_info "--- Auto-create namespace disabled"
    fi

    kubectl config set-context --current --namespace="$kube_namespace"

    # Auto-create docker-registry secret for GitLab deploy token