Skip to content

Instantly share code, notes, and snippets.

@stephanGarland
Created January 24, 2022 00:18
Show Gist options
  • Save stephanGarland/0d25f23d215a511f7b339a1685d6ad7b to your computer and use it in GitHub Desktop.
Save stephanGarland/0d25f23d215a511f7b339a1685d6ad7b to your computer and use it in GitHub Desktop.
Creates a Pod, mounts a StatefulSet's PV to the pod, copies config files to it, deletes the pod, and releases the claim so the StatefulSet can regain its use.
#!/usr/bin/env bash
remove_claim() {
pvc=$(export name="$1" && kubectl get pv --no-headers | awk -v name="$name" '$0~name {print $1}')
kubectl patch pv $pvc --type json -p '[{"op": "remove", "path": "/spec/claimRef"}]'
}
if [ -n $2 ]; then
namespace=$2
else
namespace=$1
fi
cat << EOF > $1.yaml
apiVersion: v1
kind: Pod
metadata:
name: dataaccess
namespace: $namespace
spec:
containers:
- name: alpine
image: alpine:latest
imagePullPolicy: IfNotPresent
command: ['sleep', 'infinity']
volumeMounts:
- name: $1-config-pvc
mountPath: /config
volumes:
- name: $1-config-pvc
persistentVolumeClaim:
claimName: $1-config-$1-0
EOF
echo "Scaling $1 down..."
kubectl scale statefulset -n $namespace $1 --replicas=0
echo "Removing claimRef from PVC..."
remove_claim $1
echo "Sleeping 10 seconds..."
sleep 10
echo "Creating a temporary pod to copy to $1..."
kubectl apply -f $1.yaml
echo "Sleeping 20 seconds to allow the temporary pod to come up..."
sleep 20
echo "Copying files to $1..."
ssh dkr "./kubectl cp --kubeconfig ./config ~/@data/$1/config $namespace/dataaccess:/"
echo "Deleting temporary pod..."
kubectl delete -f $1.yaml
rm $1.yaml
echo "Removing claimRef from PVC..."
remove_claim $1
echo "Sleeping 10 seconds..."
sleep 10
echo "Scaling $1 back up..."
kubectl scale statefulset -n $namespace $1 --replicas=1
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment