Skip to content

Commit

Permalink
refs platform/#2550: add template job for GCP CDN on L7 and deploy fu…
Browse files Browse the repository at this point in the history
…nctions refactoring
  • Loading branch information
Monska85 committed Jan 4, 2024
1 parent fd8eed5 commit 70287b4
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 145 deletions.
37 changes: 19 additions & 18 deletions scripts/deploy
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DEPLOY_ROOT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
DEPLOY_ROOT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
export DEPLOY_ROOT_DIR

# shellcheck disable=SC1091
source "$DEPLOY_ROOT_DIR/src/common.bash"

# shellcheck disable=SC1091
source "$DEPLOY_ROOT_DIR/src/functions.bash"

CI_ENVIRONMENT_HOSTNAME="${CI_ENVIRONMENT_URL}"
CI_ENVIRONMENT_HOSTNAME="${CI_ENVIRONMENT_HOSTNAME/http:\/\//}"
CI_ENVIRONMENT_HOSTNAME="${CI_ENVIRONMENT_HOSTNAME/https:\/\//}"
Expand All @@ -29,38 +34,35 @@ ensure_deploy_variables

# Create standard k8s configurations.
function init() {
cat <<EOF | kubectl apply -f -
cat <<EOF | kubectl apply -f -
kind: Namespace
apiVersion: v1
metadata:
name: $KUBE_NAMESPACE
EOF

if [ "${CREATE_BASIC_AUTH}" = 1 ]; then
kubectl create secret generic ingress-basic-auth --from-file=/scripts/src/auth --namespace="${KUBE_NAMESPACE}" || true
fi
if [ "${CREATE_BASIC_AUTH}" = 1 ]; then
kubectl create secret generic ingress-basic-auth --from-file=/scripts/src/auth --namespace="${KUBE_NAMESPACE}" || true
fi
}

# Materialize and run k8s configurations.
function configure() {
mkdir -p k8s
for conf in ${DEPLOY_CONF_DIR}/*
do
if [ -d ${conf} ]
then
continue;
for conf in ${DEPLOY_CONF_DIR}/*; do
if [ -d ${conf} ]; then
continue
fi
filename=$(basename "${conf}")
echo "Processing ${filename}"
envsubst < "${conf}" > "k8s/${filename}"
kubectl apply -n "${KUBE_NAMESPACE}" -f - < "k8s/${filename}"
envsubst <"${conf}" >"k8s/${filename}"
kubectl apply -n "${KUBE_NAMESPACE}" -f - <"k8s/${filename}"
done
}

# Run deploy scripts from subfolders.
function configureSubfolders() {
for conf in ${DEPLOY_CONF_DIR}/*
do
for conf in ${DEPLOY_CONF_DIR}/*; do
if [ -d ${conf} ]; then
if [ -f ${conf}/deploy ]; then
echo "Deploying subfolder ${conf}"
Expand All @@ -78,12 +80,11 @@ export STATUS_TIMEOUT
function status() {
SKIP="HorizontalPodAutoscaler"
echo "Waiting for deployment app...."
for file in k8s/*
do
for file in k8s/*; do
if [ -d $file ]; then
continue
fi
if grep -q "kind: Deployment" < "$file" && ! grep -q "$SKIP" < "$file"; then
if grep -q "kind: Deployment" <"$file" && ! grep -q "$SKIP" <"$file"; then
echo "kubectl rollout status -n ${KUBE_NAMESPACE} -R -f ${file}"
if ! timeout ${STATUS_TIMEOUT} kubectl rollout status -n "${KUBE_NAMESPACE}" -R -f "$file"; then
echo "Deployment of ${file} is failed, exiting".
Expand Down
10 changes: 8 additions & 2 deletions scripts/destroy
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
export DEPLOY_ROOT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
DEPLOY_ROOT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
export DEPLOY_ROOT_DIR

# shellcheck disable=SC1091
source "$DEPLOY_ROOT_DIR/src/common.bash"

# shellcheck disable=SC1091
source "$DEPLOY_ROOT_DIR/src/functions.bash"

create_kubeconfig

echo "Removing all pods..."
Expand Down
75 changes: 9 additions & 66 deletions scripts/helm-init
Original file line number Diff line number Diff line change
@@ -1,76 +1,19 @@
#!/bin/bash
#!/usr/bin/env bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DEPLOY_ROOT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
export DEPLOY_ROOT_DIR
source "$DEPLOY_ROOT_DIR/src/common.bash"

prepare-namespace() {
if [ -z "${KUBE_NAMESPACE}" ]; then
echo "KUBE_NAMESPACE is missing."
exit 1
fi
echo "Current KUBE_NAMESPACE=${KUBE_NAMESPACE}"
kubectl create ns "$KUBE_NAMESPACE" || true
}

create-ns-and-developer-role-bindings() {
prepare-namespace
if [ -z "${CI_COMMIT_REF_SLUG}" ]; then
echo "CI_COMMIT_REF_SLUG is missing."
exit 1
fi
ALLOWED_PATTERN=${ALLOWED_PATTERN_OVERRIDE:-'^(dev|develop|(review-.*))$'}
if ! [[ ${CI_COMMIT_REF_SLUG} =~ $ALLOWED_PATTERN ]]; then
echo "Not in Dev/Review branch: not handling team access via RBAC"
echo "Used pattern is: ${ALLOWED_PATTERN}"
return 0
fi
if [ -z "${CI_PROJECT_ID}" ]; then
echo "CI_PROJECT_ID is missing."
exit 1
fi
VIEWER_RB=$(PROJECT_ROLE=viewer envsubst <"$DEPLOY_ROOT_DIR/templates/rbac/rolebinding.yaml")
DEVELOPER_RB=$(PROJECT_ROLE=developer envsubst <"$DEPLOY_ROOT_DIR/templates/rbac/rolebinding.yaml")
IFS=',' read -r -a VIEWER_U <<<${DEV_VIEWER_USERS}
IFS=',' read -r -a VIEWER_G <<<${DEV_VIEWER_GROUPS}
IFS=',' read -r -a DEVELOPER_U <<<${DEV_DEVELOPER_USERS}
IFS=',' read -r -a DEVELOPER_G <<<${DEV_DEVELOPER_GROUPS}
for SUBJECT in "${VIEWER_U[@]}"; do
VIEWER_RB+=$'\n'$(SUBJECT_TYPE=User SUBJECT_NAME=${SUBJECT} envsubst <"$DEPLOY_ROOT_DIR/templates/rbac/rolebinding-subject.yaml")
done
for SUBJECT in "${VIEWER_G[@]}"; do
VIEWER_RB+=$'\n'$(SUBJECT_TYPE="Group" SUBJECT_NAME="${SUBJECT}" envsubst <"$DEPLOY_ROOT_DIR/templates/rbac/rolebinding-subject.yaml")
done
for SUBJECT in "${DEVELOPER_U[@]}"; do
DEVELOPER_RB+=$'\n'$(SUBJECT_TYPE=User SUBJECT_NAME=${SUBJECT} envsubst <"$DEPLOY_ROOT_DIR/templates/rbac/rolebinding-subject.yaml")
done
for SUBJECT in "${DEVELOPER_G[@]}"; do
DEVELOPER_RB+=$'\n'$(SUBJECT_TYPE="Group" SUBJECT_NAME="${SUBJECT}" envsubst <"$DEPLOY_ROOT_DIR/templates/rbac/rolebinding-subject.yaml")
done
echo "$VIEWER_RB"
echo "$VIEWER_RB" | kubectl apply -f -
echo "$DEVELOPER_RB"
echo "$DEVELOPER_RB" | kubectl apply -f -
}

helm-init() {
helm repo add "stable" "https://charts.helm.sh/stable"
helm repo add "sparkfabrik" "${SPARKFABRIK_CHART_REPO_URL:-https://storage.googleapis.com/spark-helm-charts}"
helm repo update
}
# shellcheck disable=SC1091
source "$DEPLOY_ROOT_DIR/src/common.bash"

setup-gitlab-agent() {
if [ -n "${GITLAB_AGENT_PROJECT:-}" ] && [ -n "${GITLAB_AGENT_ID:-}" ] && [ "${DISABLE_GITLAB_AGENT:-0}" != "1" ]; then
echo "The deployment will use the GitLab Agent."
echo "Switching Kubernetes context to use the context provided by the GitLab Agent."
kubectl config use-context "${GITLAB_AGENT_PROJECT}:${GITLAB_AGENT_ID}"
fi
}
# shellcheck disable=SC1091
source "$DEPLOY_ROOT_DIR/src/functions.bash"

# Switch to the GitLab Agent context if needed.
# This must be done before any other step to execute
Expand Down
9 changes: 7 additions & 2 deletions scripts/kubectl
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DEPLOY_ROOT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
DEPLOY_ROOT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
export DEPLOY_ROOT_DIR

# shellcheck disable=SC1091
source "$DEPLOY_ROOT_DIR/src/common.bash"

# shellcheck disable=SC1091
source "$DEPLOY_ROOT_DIR/src/functions.bash"

CI_ENVIRONMENT_HOSTNAME="${CI_ENVIRONMENT_URL}"
CI_ENVIRONMENT_HOSTNAME="${CI_ENVIRONMENT_HOSTNAME/http:\/\//}"
CI_ENVIRONMENT_HOSTNAME="${CI_ENVIRONMENT_HOSTNAME/https:\/\//}"
Expand Down
58 changes: 1 addition & 57 deletions scripts/src/common.bash
Original file line number Diff line number Diff line change
@@ -1,63 +1,7 @@
#!/usr/bin/env bash
set -eo pipefail

[[ "$TRACE" ]] && set -x

export CI_CONTAINER_NAME="ci_job_build_$CI_BUILD_ID"
export CI_REGISTRY_TAG="$CI_BUILD_REF_NAME"

create_kubeconfig() {
echo "Generating kubeconfig..."
export KUBECONFIG="$(pwd)/kubeconfig"
export KUBE_CLUSTER_OPTIONS=
if [[ -n "$KUBE_CA_PEM" ]]; then
echo "Using KUBE_CA_PEM..."
echo "$KUBE_CA_PEM" > "$(pwd)/kube.ca.pem"
export KUBE_CLUSTER_OPTIONS=--certificate-authority="$(pwd)/kube.ca.pem"
fi
kubectl config set-cluster gitlab-deploy --server="$KUBE_URL" \
$KUBE_CLUSTER_OPTIONS
kubectl config set-credentials gitlab-deploy --token="$KUBE_TOKEN" \
$KUBE_CLUSTER_OPTIONS
kubectl config set-context gitlab-deploy \
--cluster=gitlab-deploy --user=gitlab-deploy \
--namespace="$KUBE_NAMESPACE"
kubectl config use-context gitlab-deploy
echo ""
}

ensure_deploy_variables() {
if [[ -z "$KUBE_URL" ]]; then
echo "Missing KUBE_URL."
exit 1
fi

if [[ -z "$KUBE_TOKEN" ]]; then
echo "Missing KUBE_TOKEN."
exit 1
fi

if [[ -z "$KUBE_NAMESPACE" ]]; then
echo "Missing KUBE_NAMESPACE."
exit 1
fi

if [[ -z "$CI_ENVIRONMENT_SLUG" ]]; then
echo "Missing CI_ENVIRONMENT_SLUG."
exit 1
fi

if [[ -z "$CI_ENVIRONMENT_URL" ]]; then
echo "Missing CI_ENVIRONMENT_URL."
exit 1
fi
}

ping_kube() {
if kubectl version > /dev/null; then
echo "Kubernetes is online!"
echo ""
else
echo "Cannot connect to Kubernetes."
return 1
fi
}
Loading

0 comments on commit 70287b4

Please sign in to comment.