Skip to content
Snippets Groups Projects
Commit db300308 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch 'create-namespace' into 'master'

Feature: Create create namespace automatically, if it doesn't exist

See merge request to-be-continuous/kubernetes!87
parents 042f506e 266bee92
No related branches found
No related tags found
No related merge requests found
...@@ -395,6 +395,7 @@ The Kubernetes template uses some global configuration used throughout all jobs. ...@@ -395,6 +395,7 @@ The Kubernetes template uses some global configuration used throughout all jobs.
| `scripts-dir` / `K8S_SCRIPTS_DIR` | directory where k8s scripts (hook scripts) are located | `.` _(root project dir)_ | | `scripts-dir` / `K8S_SCRIPTS_DIR` | directory where k8s scripts (hook scripts) are located | `.` _(root project dir)_ |
| `kustomize-enabled` / `K8S_KUSTOMIZE_ENABLED` | Set to `true` to force using [Kustomize](https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/) | _none_ (disabled) | | `kustomize-enabled` / `K8S_KUSTOMIZE_ENABLED` | Set to `true` to force using [Kustomize](https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/) | _none_ (disabled) |
| `kustomize-args` / `K8S_KUSTOMIZE_ARGS` | Additional [`kubectl kustomize` options](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#kustomize)<br/>_for example: `--enable-helm`_ | _none_ | | `kustomize-args` / `K8S_KUSTOMIZE_ARGS` | Additional [`kubectl kustomize` options](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#kustomize)<br/>_for example: `--enable-helm`_ | _none_ |
| `create-namespace` / `K8S_CREATE_NAMESPACE_ENABLED` | Set to `true` to enable automatic namespace creation | `false` |
| `DOCKER_CONTAINER_STABLE_IMAGE` | Docker image name to use for staging/prod | **has to be defined when not chaining execution from Docker template** | | `DOCKER_CONTAINER_STABLE_IMAGE` | Docker image name to use for staging/prod | **has to be defined when not chaining execution from Docker template** |
| `DOCKER_CONTAINER_UNSTABLE_IMAGE` | Docker image name to use for review | **has to be defined when not chaining execution from Docker template** | | `DOCKER_CONTAINER_UNSTABLE_IMAGE` | Docker image name to use for review | **has to be defined when not chaining execution from Docker template** |
......
...@@ -58,6 +58,12 @@ ...@@ -58,6 +58,12 @@
"name": "K8S_KUSTOMIZE_ARGS", "name": "K8S_KUSTOMIZE_ARGS",
"description": "Additional [`kubectl kustomize` options](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#kustomize)\n\n_For example: `--enable-helm`_", "description": "Additional [`kubectl kustomize` options](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#kustomize)\n\n_For example: `--enable-helm`_",
"advanced": true "advanced": true
},
{
"name": "K8S_CREATE_NAMESPACE_ENABLED",
"description": "Set to `true` to enable automatic namespace creation",
"type": "boolean",
"advanced": true
} }
], ],
"features": [ "features": [
......
...@@ -43,6 +43,10 @@ spec: ...@@ -43,6 +43,10 @@ spec:
_For example: `--enable-helm`_ _For example: `--enable-helm`_
default: '' default: ''
create-namespace-enabled:
description: Creates the namespace, if it doesn't exist
type: boolean
default: false
score-disabled: score-disabled:
description: Disable kube-score description: Disable kube-score
type: boolean type: boolean
...@@ -181,6 +185,7 @@ variables: ...@@ -181,6 +185,7 @@ variables:
K8S_ENVIRONMENT_URL: $[[ inputs.environment-url ]] K8S_ENVIRONMENT_URL: $[[ inputs.environment-url ]]
K8S_KUSTOMIZE_ENABLED: $[[ inputs.kustomize-enabled ]] K8S_KUSTOMIZE_ENABLED: $[[ inputs.kustomize-enabled ]]
K8S_KUSTOMIZE_ARGS: $[[ inputs.kustomize-args ]] K8S_KUSTOMIZE_ARGS: $[[ inputs.kustomize-args ]]
K8S_CREATE_NAMESPACE_ENABLED: $[[ inputs.create-namespace-enabled ]]
K8S_SCORE_DISABLED: $[[ inputs.score-disabled ]] K8S_SCORE_DISABLED: $[[ inputs.score-disabled ]]
K8S_SCORE_EXTRA_OPTS: $[[ inputs.score-extra-opts ]] K8S_SCORE_EXTRA_OPTS: $[[ inputs.score-extra-opts ]]
K8S_REVIEW_SPACE: $[[ inputs.review-space ]] K8S_REVIEW_SPACE: $[[ inputs.review-space ]]
...@@ -490,6 +495,16 @@ stages: ...@@ -490,6 +495,16 @@ stages:
kubectl config use-context gitlab-k8s-cluster kubectl config use-context gitlab-k8s-cluster
fi fi
if ! kubectl get namespace "$k8s_namespace" > /dev/null 2>&1; then
if [[ "${K8S_CREATE_NAMESPACE_ENABLED}" == "true" ]]
then
log_info "--- Namespace \\e[33;1m${k8s_namespace}\\e[0m does not exist: create..."
kubectl create namespace "$k8s_namespace" --save-config
else
log_warn "--- Namespace \\e[33;1m${k8s_namespace}\\e[0m does not exist: set K8S_CREATE_NAMESPACE_ENABLED=true to enable automatic namespace creation"
fi
fi
kubectl config set-context --current --namespace="$k8s_namespace" kubectl config set-context --current --namespace="$k8s_namespace"
log_info "--- \\e[32mlogin\\e[0m" log_info "--- \\e[32mlogin\\e[0m"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment