Skip to content

Instantly share code, notes, and snippets.

@etoews etoews/Dockerfile
Last active Sep 21, 2018

Embed
What would you like to do?
#!/bin/sh
set -euo pipefail
# gracefully handle the TERM signal sent when deleting the daemonset
trap 'exit' TERM
# do the work
# the /etc/hosts file of the node is mapped to /node/etc/hosts
cat /node/etc/hosts
# let the monitoring script know we're done'
echo "done"
# this is a workaround to prevent the container from exiting
# and k8s restarting the daemonset pod
while true; do sleep 1; done
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: cat-etc-hosts
spec:
template:
metadata:
labels:
run-once-daemonset: cat-etc-hosts
spec:
containers:
- name: cat-etc-hosts
image: etoews/cat-etc-hosts
imagePullPolicy: Always
volumeMounts:
- name: etc-hosts
mountPath: /node/etc/hosts
readOnly: true
volumes:
- name: etc-hosts
hostPath:
path: /etc/hosts
FROM alpine:3.5
COPY cat-etc-hosts.sh .
RUN chmod u+x cat-etc-hosts.sh
CMD ["./cat-etc-hosts.sh"]
#!/bin/bash
set -euo pipefail
function main() {
kubectl apply -f daemonset.yaml
wait_for_pods cat-etc-hosts
wait_for_cats cat-etc-hosts
kubectl delete -f daemonset.yaml
}
function wait_for_pods() {
echo -n "waiting for $1 pods to run"
PODS=$(kubectl get pods | grep $1 | awk '{print $1}')
for POD in ${PODS}; do
while [[ $(kubectl get pod ${POD} -o go-template --template "{{.status.phase}}") != "Running" ]]; do
sleep 1
echo -n "."
done
done
echo
}
function wait_for_cats() {
echo -n "waiting for $1 daemonset to complete"
PODS=$(kubectl get pods | grep $1 | awk '{print $1}')
for POD in ${PODS}; do
while [[ $(kubectl logs ${POD} --tail 1) != "done" ]]; do
sleep 1
echo -n "."
done
# at this point you could take the output of kubectl logs and do something with it
done
echo
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.