-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger.sh
More file actions
executable file
·57 lines (47 loc) · 2.06 KB
/
trigger.sh
File metadata and controls
executable file
·57 lines (47 loc) · 2.06 KB
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
#!/bin/bash
set -e
# Get the list of all deployments in the same namespace. If we ever move this into a controller
# we should use a ConfigMapRefIndex to index the cache so we won't end up requeueing configs that
# are copies.
deployments=$(oc get deployment -n $1 | awk '{print $1}')
triggered=()
for d in $deployments
do
if [ "$d" == "NAME" ]
then
continue
fi
triggeredby="$(oc get deployment "${d}" -n $1 -o go-template='{{index .metadata.annotations "deployment.kubernetes.io/triggered-by"}}')"
IFS='/' read resource name <<< "$triggeredby"
if [ "$2" == "$name" ]
then
triggered+=($d)
fi
done
# If nothing is triggered then exit.
if [ ${#triggered[@]} == 0 ]
then
exit 0
fi
# Create a copy of configmap/$2. This should be mounted on every deployment that has a
# configmap triggered-by annotation.
# The deployment controller should add owner references to the copies of the configmap
# in the replica sets it creates so that garbage collection can be facilitated.
oc get configmap $2 -n $1 -o yaml --export > /tmp/$2.data
cmName=$2-$(md5sum /tmp/$2.data | head -c8)
echo "Creating a copy for configmap \""${2}"\": "${cmName}""
cat /tmp/$2.data | sed "s/name: $2/name: $cmName/" | oc create -f -
for d in $triggered
do
echo "Triggering a new rollout for deployment \""${d}"\" based on configmap \""${2}"\" update..."
# The controller will need to identify which volume has to be updated. It should
# be given a list of container names[0], look into those containers and identify which
# one is using the volume we are interested in.
#
# [0] Maybe via another annotation? It seems we need a generic trigger object that would
# hold an object reference, a slice of container names, and maybe a way to disable the
# trigger, similar to DeploymentTriggerImageChangeParams in OpenShift.
# https://github.com/openshift/origin/blob/master/pkg/deploy/api/types.go#L378
volumeName=$(oc get deployment/$d -n $1 -o jsonpath='{.spec.template.spec.volumes[0].name}')
oc set volume deployment/$d -n $1 --add --overwrite --name=$volumeName -t configmap --configmap-name=$cmName
done