-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
64 lines (55 loc) · 2.35 KB
/
deploy.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
#!/bin/bash
# Variables
namespace="burstable-csi"
persistentVolumeClaimTemplate="pvc.yml"
persistentVolumeClaimName="burstable-managed-csi-premium"
storageClassTemplate="sc.yml"
storageClassName="burstable-managed-csi-premium"
podTemplate="pod.yml"
podName="nginx"
# Create the storage class if doesn't already exists in the cluster
result=$(kubectl get sc -o jsonpath="{.items[?(@.metadata.name=='$storageClassName')].metadata.name}")
if [[ -n $result ]]; then
echo "[$storageClassName] storage class already exists in the cluster"
else
echo "[$storageClassName] storage class does not exist in the cluster"
echo "creating [$storageClassName] storage class in the cluster..."
kubectl apply -f $storageClassTemplate
fi
# Create the namespace if it doesn't already exists in the cluster
result=$(kubectl get namespace -o jsonpath="{.items[?(@.metadata.name=='$namespace')].metadata.name}")
if [[ -n $result ]]; then
echo "[$namespace] namespace already exists in the cluster"
else
echo "[$namespace] namespace does not exist in the cluster"
echo "creating [$namespace] namespace in the cluster..."
kubectl create namespace $namespace
fi
# Check if the persistent volume claim already exists
result=$(kubectl get pvc -n $namespace -o json | jq -r '.items[].metadata.name | select(. == "'$persistentVolumeClaimName'")')
if [[ -n $result ]]; then
echo "[$persistentVolumeClaimName] persistent volume claim already exists"
exit
else
# Create the persistent volume claim
echo "[$persistentVolumeClaimName] persistent volume claim does not exist"
echo "Creating [$persistentVolumeClaimName] persistent volume claim..."
cat $persistentVolumeClaimTemplate |
yq "(.metadata.name)|="\""$persistentVolumeClaimName"\" |
yq "(.spec.storageClassName)|="\""$storageClassName"\" |
kubectl apply -n $namespace -f -
fi
# Check if the pod already exists
result=$(kubectl get pod -n $namespace -o json | jq -r '.items[].metadata.name | select(. == "'$podName'")')
if [[ -n $result ]]; then
echo "[$podName] pod already exists"
exit
else
# Create the pod
echo "[$podName] pod does not exist"
echo "Creating [$podName] pod..."
cat $podTemplate |
yq "(.metadata.name)|="\""$podName"\" |
yq "(.spec.volumes[0].persistentVolumeClaim.claimName)|="\""$persistentVolumeClaimName"\" |
kubectl apply -n $namespace -f -
fi