I'm trying to set up a full CI/CD pipeline for a SpringBoot application, starting from a GitLab repository (see https://gitlab.com/pietrom/clock-api) and automatically deploying to a Kubernetes Cluster backed by Google Cloud Platform.
My pipeline works quite fine (the app is built, it is packaged as a Docker image, the image is published on my project registry and containers are started for both staging
and production
environment), except for a detail: Operation/Environments
page shows me both environments, with the following warning:
Kubernetes deployment not found
To see deployment progress for your environments, make sure your deployments are in Kubernetes namespace <projectname>, and annotated with app.gitlab.com/app=$CI_PROJECT_PATH_SLUG and app.gitlab.com/env=$CI_ENVIRONMENT_SLUG.
I googled around a bit but I can't resolve this problem: my deployment.yml
contains requested annotation, both for deployment and pod:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: clock-api-ENVIRONMENT
annotations:
app.gitlab.com/app: "PROJECT_PATH_SLUG"
app.gitlab.com/env: "ENVIRONMENT"
spec:
replicas: 1
template:
metadata:
labels:
app: ENVIRONMENT
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "8080"
prometheus.io/path: "/actuator/prometheus"
app.gitlab.com/app: "PROJECT_PATH_SLUG"
app.gitlab.com/env: "ENVIRONMENT"
spec:
containers:
- name: clock-api-ENVIRONMENT
image: registry.gitlab.com/pietrom/clock-api
imagePullPolicy: Always
ports:
- containerPort: 8080
imagePullSecrets:
- name: registry.gitlab.com
PROJECT_PATH_SLUG
and ENVIRONMENT
placeholder are substituded (using sed
) during pipeline execution with values provided by GitLab infrastructure ($CI_PROJECT_PATH_SLUG
and $CI_ENVIRONMENT_SLUG
, respectively) and I can see the expected values in my GCP console, but GitLab integration does not seem to work.
I'm missing something but I can't figure what differences there are between my deployment setup and the official documentation available here.
Thanks in advance for your help!
This is also an important part:
make sure your deployments are in Kubernetes namespace
GitLab tries to manage namespaces in the attached Kubernetes cluster - creates a new namespace for every new GitLab project. It generates namespace from project name and project id.
Sometimes GitLab fails to create namespace, for example when cluster is added after project is created. It's probably a bug, and here is how they overcome it in AutoDevOps:
function ensure_namespace() {
kubectl get namespace "$KUBE_NAMESPACE" || kubectl create namespace "$KUBE_NAMESPACE"
}
This env var $KUBE_NAMESPACE
- is defined by GitLab automatically, as well as many other Kubernetes-related variables: https://docs.gitlab.com/ee/user/project/clusters/#deployment-variables
Then GitLab relies on this namespace internally and uses for various UI pages of the project, including Operations/Environment. So if you follow their convention, and apply your Kubernetes deployment to this namespace, GitLab will see your application.
In our work we go one step further and at the beginning of a job even set this namespace as the default.
kubectl config set-context --current --namespace="$KUBE_NAMESPACE"
Then all further kubectl
commands will by default use this namespace.