Skip to content

Instantly share code, notes, and snippets.

@leoh0
Created September 15, 2018 12:11
Show Gist options
  • Select an option

  • Save leoh0/418a4598e7ea8541eddead5a9f87622e to your computer and use it in GitHub Desktop.

Select an option

Save leoh0/418a4598e7ea8541eddead5a9f87622e to your computer and use it in GitHub Desktop.
bash csi-migration.sh $PV
#!/usr/bin/env bash
CEPH_RBD=${CEPH_RBD:-ceph-rbd}
read POOL MONITOR ID SECRET SECRET_NS <<< \
$(kubectl get storageclass ${CEPH_RBD} \
-o go-template='{{ .parameters.pool }}
{{ .parameters.monitors }}
{{ .parameters.adminId }}
{{ .parameters.adminSecretName }}
{{ .parameters.adminSecretNamespace }}')
KEY=$(kubectl get secret -n ${SECRET_NS} ${SECRET} \
-o go-template='{{ .data.key }}' | base64 -d)
PV=$1
kubectl get pv ${PV} -o yaml | \
sed 's/persistentVolumeReclaimPolicy: Delete/persistentVolumeReclaimPolicy: Retain/g' | \
kubectl replace -f -
RBDIMAGENAME=$(kubectl get pv ${PV} -o go-template='{{ .spec.rbd.image }}')
NEWPVNAME=$(echo "pvc-$(echo $PV | cut -d'-' -f2,3,4 | sed 's/-//g')")
read NAMESPACE PVCNAME <<< \
$(kubectl get pv $PV \
-o go-template='{{ .spec.claimRef.namespace }}
{{ .spec.claimRef.name }}')
PODNAME=$(kubectl get pods -n $NAMESPACE -o go-template="""
{{- range .items -}}
{{\$metadata:=.metadata}}
{{- range .spec.volumes -}}
{{- if .persistentVolumeClaim -}}
{{- if eq .persistentVolumeClaim.claimName \"$PVCNAME\" -}}
{{- printf \"%s\\n\" \$metadata.name -}}
{{- end }}
{{- end }}
{{- end }}
{{- end }}""")
if [ ! -f csi-pv.py ]; then
curl -sO https://gist.githubusercontent.com/leoh0/03ca401d690579fdf65c222fa5bfccef/raw/c5439a500c46803b599a0bccfc0f36d11664bd3a/csi-pv.py
fi
python csi-pv.py ${PV}
if [ ! -f csi-pvc.py ]; then
curl -sO https://gist.githubusercontent.com/leoh0/cd17492cc5274ae7e41fdf56967e0177/raw/33269b4584a02caefc8640100592484f34494fe6/csi-pvc.py
fi
python csi-pvc.py ${PVCNAME} -n ${NAMESPACE}
kubectl delete pvc -n ${NAMESPACE} ${PVCNAME}
# start disconnect
kubectl delete pods -n ${NAMESPACE} ${PODNAME}
kubectl delete pv ${PV}
while true; do
rbd --pool ${POOL} --id ${ID} -m ${MONITOR} --key=${KEY} \
status ${RBDIMAGENAME} 2>/dev/null | grep -q 'watcher=' || \
rbd --pool ${POOL} --id ${ID} -m ${MONITOR} --key=${KEY} \
mv "${POOL}/${RBDIMAGENAME}" "${POOL}/${NEWPVNAME}" 2>/dev/null && break
done
while true; do
kubectl create -f csi-pv-${NEWPVNAME}.yaml && break
done
while true; do
kubectl create -f csi-pvc-${NEWPVNAME}.yaml -n ${NAMESPACE} && break
done
NEWPODNAME=$(kubectl get pods --field-selector status.phase!=Terminating -n $NAMESPACE -o go-template="""
{{- range .items -}}
{{\$metadata:=.metadata}}
{{- if ne .metadata.name \"$PODNAME\" -}}
{{- range .spec.volumes -}}
{{- if .persistentVolumeClaim -}}
{{- if eq .persistentVolumeClaim.claimName \"$PVCNAME\" -}}
{{- printf \"%s\\n\" \$metadata.name -}}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}""")
kubectl get pods -n ${NAMESPACE} ${NEWPODNAME}
kubectl get pv ${NEWPVNAME} -o yaml | \
sed 's/persistentVolumeReclaimPolicy: Retain/persistentVolumeReclaimPolicy: Delete/g' | \
kubectl replace -f -
echo "kubectl get pods -n ${NAMESPACE} ${NEWPODNAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment