Skip to content

Commit

Permalink
added migration script to add assigned node annotation to local volum…
Browse files Browse the repository at this point in the history
…es (#14)

* added migration script to add assigned node annotation to local volumes

* added readme
  • Loading branch information
roi-codefresh authored Oct 13, 2022
1 parent 0d4779d commit 052286e
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 1 deletion.
9 changes: 9 additions & 0 deletions local-volumes/migrations/add-assigned-node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# What is this script doing?
This migration script will run the migration logic on each of the cluster nodes and add the `codefresh.io/assignedNode` annotation to the `PersistentVolume` entities that are of type local volume. After running this migration script the codefresh volume provisioner would be able to cleanup the local-volumes from the nodes when the cleanup job decides their `PersistentVolume` should be deleted.

# How to run?
To run the migration script you need to have `kubectl` binary installed. Make sure the current kubernetes context is the cluster you want and run the script like this:
```
NAMESPACE=<codefresh-ns> ./run.sh
```
The namespace should be the namespace of your codefresh runtime.
14 changes: 14 additions & 0 deletions local-volumes/migrations/add-assigned-node/logic/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ARG ARCH=amd64

FROM alpine:3.15

ENV KUBECTL_VERSION="v1.24.0"

RUN apk add --update curl bash coreutils \
&& export ARCH=$([[ "$(uname -m)" == "aarch64" ]] && echo "arm64" || echo "amd64") \
&& curl -L https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/${ARCH}/kubectl -o /usr/local/bin/kubectl \
&& chmod +x /usr/local/bin/kubectl

ADD add_assigned_node.sh /add_assigned_node.sh

CMD ["/bin/bash", "add_assigned_node.sh"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

if [[ -z "${VOLUME_PARENT_DIR}" ]]; then
echo "Error: missing dir path, export VOLUME_PARENT_DIR variable"
exit 1
fi

if [[ -z "${NODE_NAME}" ]]; then
echo "Error: missing node name, export NODE_NAME variable"
exit 1
fi

MIGRATED_COUNTER=0

for vol_dir in $(find "${VOLUME_PARENT_DIR}" -mindepth 1 -maxdepth 1 -type d -name "vol-*")
do
PV_NAME=$(cat ${vol_dir}/pv)
PATCH="{\"metadata\":{\"annotations\":{\"codefresh.io/assignedNode\":\"$NODE_NAME\"}}}"
RES=$(kubectl patch pv $PV_NAME -p $PATCH 2>&1)
EXIT_CODE="$?"

if echo $RES | grep -q '(NotFound)'; then
continue
fi

echo $RES

if [[ "0" -eq "$EXIT_CODE" ]]; then
((MIGRATED_COUNTER++))
fi
done

echo "Successfully migrated $MIGRATED_COUNTER PVs"
94 changes: 94 additions & 0 deletions local-volumes/migrations/add-assigned-node/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/bash

VOLUME_PARENT_DIR="/var/lib/codefresh/dind-volumes"

if [[ -z "${MIGRATOR_IMAGE}" ]]; then
MIGRATOR_IMAGE="quay.io/codefresh/lv-pv-migrator:latest"
fi

if [[ -z "${NAMESPACE}" ]]; then
echo "Error: missing namespace, export NAMESPACE variable"
exit 1
fi

echo "Using kube context: '$(kubectl config current-context)'"
echo "Using node label selector: '${NODE_LABEL_SELECTOR}'"
echo "Using namespace: '${NAMESPACE}'"
echo "Using migrator image: '${MIGRATOR_IMAGE}'"

echo "==="

run_on_node() {
local node_name="$1"
echo "running on node $node_name"

local res=$(echo "
apiVersion: v1
kind: Pod
metadata:
generateName: lv-pv-migrator-
namespace: $NAMESPACE
labels:
app.kubernetes.io/name: lv-pv-migrator
spec:
containers:
- name: pv-migrator
image: $MIGRATOR_IMAGE
imagePullPolicy: Always
env:
- name: VOLUME_PARENT_DIR
value: ${VOLUME_PARENT_DIR}
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
volumeMounts:
- mountPath: ${VOLUME_PARENT_DIR}
name: dind-volume-dir
serviceAccountName: volume-provisioner-runner
restartPolicy: Never
nodeName: $node_name
volumes:
- hostPath:
path: ${VOLUME_PARENT_DIR}
type: ""
name: dind-volume-dir
" | kubectl create -f -)

local pod_name=$(echo "$res" | sed s@pod/@@ | sed s@\ created@@)
echo "created pod: $pod_name, waiting for logs..."

for (( i=1; i<=15; i++ ))
do
if eval kubectl logs -f -n $NAMESPACE $pod_name > /dev/null 2>&1; then
break
fi
sleep 1
done

echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
kubectl logs -f -n $NAMESPACE $pod_name
echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
}

# Get nodes
if [[ -z "${NODE_LABEL_SELECTOR}" ]]; then
RES=`kubectl get nodes 2>&1`
else
RES=`kubectl get nodes -l ${NODE_LABEL_SELECTOR} 2>&1`
fi

if [[ "$RES" == "No resources found" ]]; then
echo "Error: No nodes found"
exit 1
fi

NODE_COUNTER=0
NODES=$(echo "$RES" | awk 'NR!=1 {print $1}')
for node in $NODES
do
run_on_node $node
((NODE_COUNTER++))
done

echo "Done migrating local volume PVs on ${NODE_COUNTER} nodes!"
2 changes: 1 addition & 1 deletion service.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.29.0
1.29.1

0 comments on commit 052286e

Please sign in to comment.