Skip to content

Commit fba78bc

Browse files
committed
K8S-16 gitlab integration (improve)
1 parent 9a54fd4 commit fba78bc

File tree

1 file changed

+61
-25
lines changed

1 file changed

+61
-25
lines changed

manifest.jps

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -560,56 +560,92 @@ addons:
560560
actions:
561561
addon-gitlab-config:
562562
- log: '${settings.envlist}'
563-
- log: '${user.email}'
563+
- cmd[${nodes.k8sm.master.id}]: kubectl get configmaps -n gitlab-managed-apps gitlab-configuration &>/dev/null && echo "true" || echo "false"
564+
- setGlobals:
565+
gitlab_installed: ${response.out}
566+
- if ('${globals.gitlab_installed}' == 'true'):
567+
return:
568+
type: info
569+
message: This cluster already has GitLab integration!
570+
- cmd[${nodes.k8sm.master.id}]: kubectl apply -f ${baseUrl}/addons/gitlab-service-account.yaml
571+
- cmd[${nodes.k8sm.master.id}]: kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep gitlab-admin | awk '{print $1}') | grep '^token:' | awk '{print $2}'
572+
- setGlobals:
573+
gitlab_token: ${response.out}
574+
- cmd[${nodes.k8sm.master.id}]: kubectl cluster-info | sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" | grep 'Kubernetes master' | awk '/http/ {print $NF}'
575+
- setGlobals:
576+
gitlab_api_url: ${response.out}
577+
- cmd[${nodes.k8sm.master.id}]: kubectl get secret $(kubectl get secrets | grep default-token | cut -d " " -f 1) -o jsonpath="{['data']['ca\.crt']}" | base64 --decode
578+
- setGlobals:
579+
gitlab_ca_cert: ${response.out}
564580
- cmd[${nodes.k8sm.master.id}]: |-
565-
api_url=$(kubectl cluster-info | sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" | grep 'Kubernetes master' | awk '/http/ {print $NF}')
566-
ca_cert=$(kubectl get secret $(kubectl get secrets | grep default-token | cut -d " " -f 1) -o jsonpath="{['data']['ca\.crt']}" | base64 --decode)
567-
kubectl apply -f ${baseUrl}/addons/gitlab-service-account.yaml
568-
gitlab_token=$(kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep gitlab-admin | awk '{print $1}') | grep '^token:' | awk '{print $2}')
581+
# Login
569582
page_content=$(curl -sk -c gitlab-jar.txt 'https://${settings.envlist}:4848/users/sign_in')
570583
csrf_token=$(echo ${page_content} | perl -ne 'print "$1\n" if /new_user.*?authenticity_token"[[:blank:]]value="(.+?)"/' | sed -n 1p)
571584
echo "Sign-in CSRF token: ${csrf_token}"
572585
[ -n "${csrf_token}" ] || exit 1
573-
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -d "user[login]=root&user[password]=${settings.gitlab_pass}" --data-urlencode "authenticity_token=${csrf_token}" 'https://${settings.envlist}:4848/users/sign_in' || exit 1
586+
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -d "user[login]=root&user[password]=${settings.gitlab_pass}" --data-urlencode "authenticity_token=${csrf_token}" 'https://${settings.envlist}:4848/users/sign_in'
587+
- cmd[${nodes.k8sm.master.id}]: |-
588+
# Settings
574589
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt 'https://${settings.envlist}:4848/admin/application_settings/network')
575590
csrf_token=$(echo ${page_content} | perl -ne 'print "$1\n" if /meta[[:blank:]]name="csrf-token"[[:blank:]]content="(.+?)"/' | sed -n 1p)
576591
echo "Application settings CSRF token: ${csrf_token}"
577592
[ -n "${csrf_token}" ] || exit 2
578593
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -d "_method=patch&application_setting[allow_local_requests_from_web_hooks_and_services]=1&application_setting[allow_local_requests_from_system_hooks]=1&application_setting[dns_rebinding_protection_enabled]=1" --data-urlencode "authenticity_token=${csrf_token}" 'https://${settings.envlist}:4848/admin/application_settings/network' || exit 2
579-
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt 'https://${settings.envlist}:4848/admin/clusters/new')
580-
csrf_token=$(echo ${page_content} | perl -ne 'print "$1\n" if /new_cluster.*?authenticity_token"[[:blank:]]value="(.+?)"/' | sed -n 1p)
581-
echo "Add cluster CSRF token: ${csrf_token}"
582-
[ -n "${csrf_token}" ] || exit 3
583-
page_content=$(curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -d "cluster[name]=Kubernetes&cluster[platform_kubernetes_attributes][authorization_type]=rbac&cluster[managed]=1" --data-urlencode "cluster[platform_kubernetes_attributes][api_url]=${api_url}" --data-urlencode "cluster[platform_kubernetes_attributes][ca_cert]=${ca_cert}" --data-urlencode "cluster[platform_kubernetes_attributes][token]=${gitlab_token}" --data-urlencode "authenticity_token=${csrf_token}" 'https://${settings.envlist}:4848/admin/clusters/create_user')
594+
curl -sk -b gitlab-jar.txt -c gitlab-jar.txt -f 'https://${settings.envlist}:4848/admin/application_settings/network' &>/dev/null
595+
- cmd[${nodes.k8sm.master.id}]: |-
596+
# Cluster
597+
for i in {1..5}; do
598+
sleep 10
599+
echo "Attempt ${i} of GitLab Kubernetes cluster creation"
600+
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt 'https://${settings.envlist}:4848/admin/clusters/new')
601+
csrf_token=$(echo ${page_content} | perl -ne 'print "$1\n" if /new_cluster.*?authenticity_token"[[:blank:]]value="(.+?)"/' | sed -n 1p)
602+
echo "Add cluster CSRF token: ${csrf_token}"
603+
[ -n "${csrf_token}" ] || continue
604+
page_content=$(curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -d "cluster[name]=Kubernetes&cluster[platform_kubernetes_attributes][authorization_type]=rbac&cluster[managed]=1" --data-urlencode "cluster[platform_kubernetes_attributes][api_url]=${globals.gitlab_api_url}" --data-urlencode "cluster[platform_kubernetes_attributes][ca_cert]=${globals.gitlab_ca_cert}" --data-urlencode "cluster[platform_kubernetes_attributes][token]=${globals.gitlab_token}" --data-urlencode "authenticity_token=${csrf_token}" 'https://${settings.envlist}:4848/admin/clusters/create_user')
605+
cluster_url=$(echo ${page_content} | perl -ne 'print "$1\n" if /You[[:blank:]]are[[:blank:]]being[[:blank:]]\<a[[:blank:]]href="(.+?)"\>redirected/' | sed -n 1p)
606+
echo "Kubernetes GitLab cluster: ${cluster_url}"
607+
[ -n "${cluster_url}" ] && break || continue
608+
done
584609
echo "${page_content}"
585-
cluster_url=$(echo ${page_content} | perl -ne 'print "$1\n" if /You[[:blank:]]are[[:blank:]]being[[:blank:]]\<a[[:blank:]]href="(.+?)"\>redirected/' | sed -n 1p)
586-
echo "Kubernetes GitLab cluster: ${cluster_url}"
587610
[ -n "${cluster_url}" ] || exit 4
611+
kubectl create ns gitlab-managed-apps
612+
kubectl create configmap gitlab-configuration --from-literal cluster_url="${cluster_url}" -n gitlab-managed-apps
613+
- cmd[${nodes.k8sm.master.id}]: kubectl get configmaps -n gitlab-managed-apps gitlab-configuration -o jsonpath='{.data.cluster_url}'
614+
- setGlobals:
615+
gitlab_cluster_url: ${response.out}
616+
- cmd[${nodes.k8sm.master.id}]: |-
617+
# Helm
588618
sleep 5
589-
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt "${cluster_url}")
619+
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt "${globals.gitlab_cluster_url}")
590620
csrf_token=$(echo ${page_content} | perl -ne 'print "$1\n" if /csrf-param.*?csrf-token"[[:blank:]]content="(.+?)"/' | sed -n 1p)
591621
echo "Helm CSRF token: ${csrf_token}"
592622
[ -n "${csrf_token}" ] || exit 5
593-
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -H "X-CSRF-Token: ${csrf_token}" "${cluster_url}/applications/helm" || exit 5
594-
/root/wait-deployment.sh tiller-deploy gitlab-managed-apps 1 720
595-
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt "${cluster_url}")
623+
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -H "X-CSRF-Token: ${csrf_token}" "${globals.gitlab_cluster_url}/applications/helm" || exit 5
624+
wait-deployment.sh tiller-deploy gitlab-managed-apps 1 720
625+
- cmd[${nodes.k8sm.master.id}]: |-
626+
# Ingress
627+
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt "${globals.gitlab_cluster_url}")
596628
csrf_token=$(echo ${page_content} | perl -ne 'print "$1\n" if /csrf-param.*?csrf-token"[[:blank:]]content="(.+?)"/' | sed -n 1p)
597629
echo "Ingress CSRF token: ${csrf_token}"
598630
[ -n "${csrf_token}" ] || exit 5
599-
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -H "X-CSRF-Token: ${csrf_token}" "${cluster_url}/applications/ingress" || exit 5
600-
/root/wait-deployment.sh ingress-nginx-ingress-controller gitlab-managed-apps 1 720
601-
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt "${cluster_url}")
631+
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -H "X-CSRF-Token: ${csrf_token}" "${globals.gitlab_cluster_url}/applications/ingress" || exit 5
632+
wait-deployment.sh ingress-nginx-ingress-controller gitlab-managed-apps 1 720
633+
- cmd[${nodes.k8sm.master.id}]: |-
634+
# Cert manager
635+
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt "${globals.gitlab_cluster_url}")
602636
csrf_token=$(echo ${page_content} | perl -ne 'print "$1\n" if /csrf-param.*?csrf-token"[[:blank:]]content="(.+?)"/' | sed -n 1p)
603637
echo "Cert-manager CSRF token: ${csrf_token}"
604638
[ -n "${csrf_token}" ] || exit 5
605-
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -H "Content-Type: application/json" -H "X-CSRF-Token: ${csrf_token}" -d '{"email":"${user.email}"}' "${cluster_url}/applications/cert_manager" || exit 5
606-
/root/wait-deployment.sh certmanager-webhook gitlab-managed-apps 1 720
607-
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt "${cluster_url}")
639+
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -H "Content-Type: application/json" -H "X-CSRF-Token: ${csrf_token}" -d '{"email":"${user.email}"}' "${globals.gitlab_cluster_url}/applications/cert_manager" || exit 5
640+
wait-deployment.sh certmanager-webhook gitlab-managed-apps 1 720
641+
- cmd[${nodes.k8sm.master.id}]: |-
642+
# Runner
643+
page_content=$(curl -sk -b gitlab-jar.txt -c gitlab-jar.txt "${globals.gitlab_cluster_url}")
608644
csrf_token=$(echo ${page_content} | perl -ne 'print "$1\n" if /csrf-param.*?csrf-token"[[:blank:]]content="(.+?)"/' | sed -n 1p)
609645
echo "Runner CSRF token: ${csrf_token}"
610646
[ -n "${csrf_token}" ] || exit 5
611-
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -H "X-CSRF-Token: ${csrf_token}" "${cluster_url}/applications/runner" || exit 5
612-
/root/wait-deployment.sh runner-gitlab-runner gitlab-managed-apps 1 720
647+
curl -X POST -sk -b gitlab-jar.txt -c gitlab-jar.txt -f -H "X-CSRF-Token: ${csrf_token}" "${globals.gitlab_cluster_url}/applications/runner" || exit 5
648+
wait-deployment.sh runner-gitlab-runner gitlab-managed-apps 1 720
613649

614650
success: |
615651
${globals.default_success:}

0 commit comments

Comments
 (0)