forked from MrCoffey/drone-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.sh
executable file
·127 lines (110 loc) · 4.01 KB
/
update.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash
set -e
if [ -z ${PLUGIN_NAMESPACE} ]; then
PLUGIN_NAMESPACE="default"
echo "WARNING: NAMESPACE not defined or empty - using default"
fi
if [ -z ${PLUGIN_KUBERNETES_USER} ]; then
PLUGIN_KUBERNETES_USER="default"
echo "WARNING: KUBERNETES_USER not defined or empty - using default"
fi
if [ -z ${PLUGIN_WAIT_TIMEOUT} ]; then
PLUGIN_WAIT_TIMEOUT=30s
fi
if [ -z ${PLUGIN_DEPLOYMENT} ]; then
if [ -z ${PLUGIN_STATEFULSET} ]; then
echo "ERROR: DEPLOYMENT or STATEFULSET variable not defined or empty"
exit 1
fi
fi
if [ -z ${PLUGIN_CONTAINER} ]; then
echo "ERROR: CONTAINER variable not defined or empty"
exit 1
fi
if [ -z ${PLUGIN_REPO} ]; then
echo "ERROR: REPO variable not defined or empty"
exit 1
fi
if [ -z ${PLUGIN_TAG} ]; then
echo "ERROR: TAG variable not defined or empty"
exit 1
fi
if [ ! -z ${PLUGIN_KUBERNETES_TOKEN} ]; then
KUBERNETES_TOKEN=$PLUGIN_KUBERNETES_TOKEN
else
echo "ERROR: KUBERNETES_TOKEN variable not defined or empty"
exit 1
fi
if [ ! -z ${PLUGIN_KUBERNETES_SERVER} ]; then
KUBERNETES_SERVER=$PLUGIN_KUBERNETES_SERVER
else
echo "WARNING: KUBERNETES_SERVER not defined or empty - using default conf from kubectl"
fi
if [ ! -z ${PLUGIN_KUBERNETES_CERT} ]; then
KUBERNETES_CERT=${PLUGIN_KUBERNETES_CERT}
fi
kubectl config set-credentials ${PLUGIN_KUBERNETES_USER} --token=$(echo ${KUBERNETES_TOKEN} | base64 -d)
if [ ! -z ${KUBERNETES_CERT} ]; then
echo ${KUBERNETES_CERT} | base64 -d > ca.crt
kubectl config set-cluster default --server=${KUBERNETES_SERVER} --certificate-authority=ca.crt
else
echo "WARNING: Using insecure connection to cluster, please define CERT"
kubectl config set-cluster default --server=${KUBERNETES_SERVER} --insecure-skip-tls-verify=true
fi
kubectl config set-context default --cluster=default --user=${PLUGIN_KUBERNETES_USER}
kubectl config use-context default
echo "INFO: Starting Kubernetes resources update"
if [ ! -z ${PLUGIN_USE_STATEFULSET} ]; then
echo "INFO: Statefulset mode enabled"
IFS=',' read -r -a STATEFULSETS <<< "${PLUGIN_STATEFULSET}"
IFS=',' read -r -a CONTAINERS <<< "${PLUGIN_CONTAINER}"
for DEPLOY in ${STATEFULSETS[@]}; do
echo "INFO: Updating $DEPLOY on $KUBERNETES_SERVER"
for CONTAINER in ${CONTAINERS[@]}; do
echo "INFO: Updating $CONTAINER"
kubectl -n ${PLUGIN_NAMESPACE} set image statefulset/${DEPLOY} \
${CONTAINER}=${PLUGIN_REPO}:${PLUGIN_TAG}
done
if [[ ! -z ${PLUGIN_FORCE} ]]; then
echo "INFO: Forcing redeployment"
kubectl -n ${PLUGIN_NAMESPACE} rollout restart statefulset/${DEPLOY}
fi
done
else
echo "INFO: Deployment mode enabled"
IFS=',' read -r -a DEPLOYMENTS <<< "${PLUGIN_DEPLOYMENT}"
IFS=',' read -r -a CONTAINERS <<< "${PLUGIN_CONTAINER}"
for DEPLOY in ${DEPLOYMENTS[@]}; do
echo "INFO: Updating $DEPLOY on $KUBERNETES_SERVER"
for CONTAINER in ${CONTAINERS[@]}; do
echo "INFO: Updating $CONTAINER"
kubectl -n ${PLUGIN_NAMESPACE} set image deployment/${DEPLOY} \
${CONTAINER}=${PLUGIN_REPO}:${PLUGIN_TAG}
done
if [[ ! -z ${PLUGIN_FORCE} ]]; then
echo "INFO: Forcing redeployment"
kubectl -n ${PLUGIN_NAMESPACE} rollout restart deployment/${DEPLOY}
fi
done
fi
if [ ! -z ${PLUGIN_WAIT} ]; then
echo "INFO: Waiting for changes to be performed"
if [ ! -z ${PLUGIN_USE_STATEFULSET} ]; then
IFS=',' read -r -a STATEFULSETS <<< "${PLUGIN_STATEFULSET}"
IFS=',' read -r -a CONTAINERS <<< "${PLUGIN_CONTAINER}"
for DEPLOY in ${STATEFULSETS[@]}; do
echo "INFO: Waiting for $DEPLOY"
kubectl -n ${PLUGIN_NAMESPACE} rollout status \
--timeout=${PLUGIN_WAIT_TIMEOUT} statefulset/${DEPLOY}
done
else
IFS=',' read -r -a DEPLOYMENTS <<< "${PLUGIN_DEPLOYMENT}"
IFS=',' read -r -a CONTAINERS <<< "${PLUGIN_CONTAINER}"
for DEPLOY in ${DEPLOYMENTS[@]}; do
echo "INFO: Waiting for $DEPLOY"
kubectl -n ${PLUGIN_NAMESPACE} rollout status \
--timeout=${PLUGIN_WAIT_TIMEOUT} deployment/${DEPLOY}
done
fi
fi
echo "INFO: Kubernetes resources update done"