-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebs-params.sh
executable file
·202 lines (167 loc) · 6.63 KB
/
ebs-params.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env bash
source /shell_lib.sh
EBS_VOL_MODS=''
EBS_VOLS_JSON=''
function __config__() {
cat <<EOF
configVersion: v1
kubernetes:
- apiVersion: v1
name: pvcs
kind: PersistentVolumeClaim
executeHookOnEvent: [ "Added", "Modified" ]
includeSnapshotsFrom: ["pvs"]
- apiVersion: v1
name: pvs
kind: PersistentVolume
executeHookOnSynchronization: false
executeHookOnEvent: []
schedule:
- name: cron
crontab: "*/5 * * * *"
includeSnapshotsFrom: ["pvcs", "pvs"]
EOF
}
function __on_kubernetes::pvcs::synchronization() {
echo "Sync context: ${BINDING_CONTEXT_CURRENT_BINDING}[${BINDING_CONTEXT_CURRENT_INDEX}]"
PVCS_LENGTH=$(context::jq -r '.objects|length')
echo "Received ${PVCS_LENGTH} PVC objects"
for i in $(seq 0 $((PVCS_LENGTH - 1))); do
PVCS_CURRENT_INDEX="${i}"
PVCS_CURRENT_JSON=$(context::jq '.objects['"${PVCS_CURRENT_INDEX}"'].object')
pvc::process
done
}
function __on_kubernetes::pvcs::added_or_modified() {
echo "Added or modified context: ${BINDING_CONTEXT_CURRENT_BINDING}[${BINDING_CONTEXT_CURRENT_INDEX}]"
PVCS_CURRENT_JSON=$(context::jq '.object')
pvc::process
}
function __on_schedule::cron() {
echo "Cron context: ${BINDING_CONTEXT_CURRENT_BINDING}[${BINDING_CONTEXT_CURRENT_INDEX}]"
PVCS_LENGTH=$(context::jq -r '.snapshots.pvcs|length')
echo "Received ${PVCS_LENGTH} PVC objects"
for i in $(seq 0 $((PVCS_LENGTH - 1))); do
PVCS_CURRENT_INDEX="${i}"
PVCS_CURRENT_JSON=$(context::jq '.snapshots.pvcs['"${PVCS_CURRENT_INDEX}"'].object')
pvc::process
done
}
function pvc::jq() {
echo "$PVCS_CURRENT_JSON" | jq "$@"
}
function pvc::process() {
echo "Processing..."
pvc::jq -r '.metadata.name'
if [[ $(pvc::jq -r '.status.phase') != "Bound" ]]; then
echo "Not bound"
return 0
fi
VOLUME_NAME=$(pvc::jq -r '.spec.volumeName')
if [[ "$VOLUME_NAME" == 'null' || "$VOLUME_NAME" == '' ]]; then
echo "No volume name"
return 0
fi
pvc::jq -r '.spec.volumeName'
PV_JSON=$(context::jq '.snapshots.pvs[]|select(.object.metadata.name=="'"$VOLUME_NAME"'").object')
EBS_VOL_ID=$(echo "$PV_JSON" | jq -r '.spec.csi.volumeHandle')
if [[ "$EBS_VOL_ID" == 'null' ]]; then
# No volume id found assuming CSI schema, try the legacy in-tree syntax
set +e
EBS_VOL_ID=$(echo "$PV_JSON" | jq -r '.spec.awsElasticBlockStore.volumeID' | grep -o 'vol-[0-9a-f][0-9a-f]*')
set -e
if [[ -z $EBS_VOL_ID ]]; then
echo "No VolumeID found in PV spec (not a EBS volume?), skipping"
return 0
fi
fi
echo "$EBS_VOL_ID"
if [[ -z $EBS_VOLS_JSON ]]; then
EBS_VOLS_JSON="$(aws ec2 describe-volumes)"
fi
EBS_VOL_JSON="$(echo "$EBS_VOLS_JSON" | jq -c '.Volumes[]|select(.VolumeId=="'"$EBS_VOL_ID"'")')"
if [[ -z $EBS_VOL_JSON ]]; then
echo "No volume $EBS_VOL_ID found in AWS, skipping"
return 0
fi
if [[ -z $EBS_VOL_MODS ]]; then
EBS_VOL_MODS="$(aws ec2 describe-volumes-modifications)"
fi
EBS_VOL_IOPS="$(echo "$EBS_VOL_JSON" | jq -r '.Iops')"
EBS_VOL_TP="$(echo "$EBS_VOL_JSON" | jq -r '.Throughput')"
EBS_VOL_TYPE="$(echo "$EBS_VOL_JSON" | jq -r '.VolumeType')"
EBS_VOL_MOD_STATE="$(echo "$EBS_VOL_MODS" | jq -r '.VolumesModifications[]|select(.VolumeId=="'"$EBS_VOL_ID"'").ModificationState')"
EBS_VOL_MOD_START_TIME="$(echo "$EBS_VOL_MODS" | jq -r '.VolumesModifications[]|select(.VolumeId=="'"$EBS_VOL_ID"'").StartTime')"
EBS_VOL_MOD_END_TIME="$(echo "$EBS_VOL_MODS" | jq -r '.VolumesModifications[]|select(.VolumeId=="'"$EBS_VOL_ID"'").EndTime')"
if [[ "$EBS_VOL_MOD_END_TIME" == 'null' ]]; then
EBS_VOL_MOD_END_TIME=''
fi
if [[ -n $EBS_VOL_MOD_END_TIME || -z $EBS_VOL_MOD_START_TIME ]]; then
EBS_VOL_MOD_ARGS=''
SPEC_IOPS="$(pvc::jq -r '.metadata.annotations."spec.epc.altinity.com/iops"')"
if [[ "$SPEC_IOPS" == 'null' ]]; then
SPEC_IOPS=''
fi
if [[ -n $SPEC_IOPS && "$SPEC_IOPS" != "$EBS_VOL_IOPS" ]]; then
EBS_VOL_MOD_ARGS+=' --iops='"$SPEC_IOPS"
fi
SPEC_TP="$(pvc::jq -r '.metadata.annotations."spec.epc.altinity.com/throughput"')"
if [[ "$SPEC_TP" == 'null' ]]; then
SPEC_TP=''
fi
if [[ -n $SPEC_TP && "$SPEC_TP" != "$EBS_VOL_TP" ]]; then
EBS_VOL_MOD_ARGS+=' --throughput='"$SPEC_TP"
fi
SPEC_TYPE="$(pvc::jq -r '.metadata.annotations."spec.epc.altinity.com/type"')"
if [[ "$SPEC_TYPE" == 'null' ]]; then
SPEC_TYPE=''
fi
if [[ -n $SPEC_TYPE && "$SPEC_TYPE" != "$EBS_VOL_TYPE" ]]; then
EBS_VOL_MOD_ARGS+=' --volume-type='"$SPEC_TYPE"
fi
if [[ -n $EBS_VOL_MOD_ARGS ]]; then
set +e
MOD_JSON="$(aws ec2 modify-volume --volume-id="${EBS_VOL_ID}" ${EBS_VOL_MOD_ARGS})"
RET=$?
set -e
if [[ $RET -ne 0 ]]; then
echo "Aws CLI failed with exit code ${RET}"
return 0
fi
EBS_VOL_MOD_STATE="$(echo "$MOD_JSON" | jq -r '.VolumeModification.ModificationState')"
EBS_VOL_MOD_START_TIME="$(echo "$MOD_JSON" | jq -r '.VolumeModification.StartTime')"
EBS_VOL_MOD_END_TIME=''
fi
fi
JQFILTER=''
if [[ "$EBS_VOL_IOPS" != "$(pvc::jq -r '.metadata.annotations."status.epc.altinity.com/iops"')" ]]; then
JQFILTER+="${JQFILTER:+|}"'.metadata.annotations."status.epc.altinity.com/iops"="'"$EBS_VOL_IOPS"'"'
fi
if [[ "$EBS_VOL_TP" != "$(pvc::jq -r '.metadata.annotations."status.epc.altinity.com/throughput"')" ]]; then
JQFILTER+="${JQFILTER:+|}"'.metadata.annotations."status.epc.altinity.com/throughput"="'"$EBS_VOL_TP"'"'
fi
if [[ "$EBS_VOL_TYPE" != "$(pvc::jq -r '.metadata.annotations."status.epc.altinity.com/type"')" ]]; then
JQFILTER+="${JQFILTER:+|}"'.metadata.annotations."status.epc.altinity.com/type"="'"$EBS_VOL_TYPE"'"'
fi
if [[ "$EBS_VOL_MOD_STATE" != "$(pvc::jq -r '.metadata.annotations."status.epc.altinity.com/mod-state"')" ]]; then
JQFILTER+="${JQFILTER:+|}"'.metadata.annotations."status.epc.altinity.com/mod-state"="'"$EBS_VOL_MOD_STATE"'"'
fi
if [[ "$EBS_VOL_MOD_START_TIME" != "$(pvc::jq -r '.metadata.annotations."status.epc.altinity.com/mod-start-time"')" ]]; then
JQFILTER+="${JQFILTER:+|}"'.metadata.annotations."status.epc.altinity.com/mod-start-time"="'"$EBS_VOL_MOD_START_TIME"'"'
fi
if [[ "$EBS_VOL_MOD_END_TIME" != "$(pvc::jq -r '.metadata.annotations."status.epc.altinity.com/mod-end-time"')" ]]; then
JQFILTER+="${JQFILTER:+|}"'.metadata.annotations."status.epc.altinity.com/mod-end-time"="'"$EBS_VOL_MOD_END_TIME"'"'
fi
if [[ -n "${JQFILTER}" ]]; then
echo "Applying jqFilter ${JQFILTER}"
cat <<EOF >>"$KUBERNETES_PATCH_PATH"
---
operation: JQPatch
kind: PersistentVolumeClaim
namespace: $(pvc::jq -r '.metadata.namespace')
name: $(pvc::jq -r '.metadata.name')
jqFilter: '${JQFILTER}'
EOF
fi
}
hook::run "$@"