From 052286e74ffbc3322dadaad00a172531dff0c3ac Mon Sep 17 00:00:00 2001 From: roi-codefresh Date: Thu, 13 Oct 2022 15:48:18 +0300 Subject: [PATCH] added migration script to add assigned node annotation to local volumes (#14) * added migration script to add assigned node annotation to local volumes * added readme --- .../migrations/add-assigned-node/README.md | 9 ++ .../add-assigned-node/logic/Dockerfile | 14 +++ .../logic/add_assigned_node.sh | 33 +++++++ .../migrations/add-assigned-node/run.sh | 94 +++++++++++++++++++ service.yaml | 2 +- 5 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 local-volumes/migrations/add-assigned-node/README.md create mode 100644 local-volumes/migrations/add-assigned-node/logic/Dockerfile create mode 100644 local-volumes/migrations/add-assigned-node/logic/add_assigned_node.sh create mode 100755 local-volumes/migrations/add-assigned-node/run.sh diff --git a/local-volumes/migrations/add-assigned-node/README.md b/local-volumes/migrations/add-assigned-node/README.md new file mode 100644 index 0000000..86edf34 --- /dev/null +++ b/local-volumes/migrations/add-assigned-node/README.md @@ -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= ./run.sh +``` +The namespace should be the namespace of your codefresh runtime. diff --git a/local-volumes/migrations/add-assigned-node/logic/Dockerfile b/local-volumes/migrations/add-assigned-node/logic/Dockerfile new file mode 100644 index 0000000..2768064 --- /dev/null +++ b/local-volumes/migrations/add-assigned-node/logic/Dockerfile @@ -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"] diff --git a/local-volumes/migrations/add-assigned-node/logic/add_assigned_node.sh b/local-volumes/migrations/add-assigned-node/logic/add_assigned_node.sh new file mode 100644 index 0000000..c311b7b --- /dev/null +++ b/local-volumes/migrations/add-assigned-node/logic/add_assigned_node.sh @@ -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" diff --git a/local-volumes/migrations/add-assigned-node/run.sh b/local-volumes/migrations/add-assigned-node/run.sh new file mode 100755 index 0000000..5d5edc9 --- /dev/null +++ b/local-volumes/migrations/add-assigned-node/run.sh @@ -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!" diff --git a/service.yaml b/service.yaml index 5e57fb8..83cf0d9 100644 --- a/service.yaml +++ b/service.yaml @@ -1 +1 @@ -1.29.0 +1.29.1