From b00b5f4370d1439b3bff38e334639f8edfc26017 Mon Sep 17 00:00:00 2001 From: Yaroslav Borbat Date: Thu, 8 Feb 2024 13:52:03 +0300 Subject: [PATCH 1/2] init Signed-off-by: Yaroslav Borbat --- .../Taskfile.dist.yaml | 20 ++++++++ .../hack/mirrord-config.json | 12 +++++ .../virtualization-controller/hack/mirrord.sh | 50 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 images/virtualization-controller/hack/mirrord-config.json create mode 100755 images/virtualization-controller/hack/mirrord.sh diff --git a/images/virtualization-controller/Taskfile.dist.yaml b/images/virtualization-controller/Taskfile.dist.yaml index 93dfce12e..464d226a1 100644 --- a/images/virtualization-controller/Taskfile.dist.yaml +++ b/images/virtualization-controller/Taskfile.dist.yaml @@ -617,6 +617,17 @@ tasks: - | golangci-lint run --sort-results + mirrord:run: + desc: "Run local virtualization-controller in cluster using a mirrord" + deps: + - _ensure:mirrord + cmd: ./hack/mirrord.sh run + mirrord:wipe: + desc: "wipe up Mirrord's trash" + deps: + - _ensure:mirrord + cmd: ./hack/mirrord.sh wipe + _copy_d8_registry_secret: internal: true vars: @@ -752,3 +763,12 @@ tasks: - exit 1 status: - which virtctl >/dev/null + _ensure:mirrord: + desc: "Ensure mirrord tool is installed" + internal: true + cmds: + - echo -e >&2 "Please install mirrord https://mirrord.dev/docs/overview/quick-start/#cli-tool" + - exit 1 + status: + - which mirrord >/dev/null + diff --git a/images/virtualization-controller/hack/mirrord-config.json b/images/virtualization-controller/hack/mirrord-config.json new file mode 100644 index 000000000..fadc11c61 --- /dev/null +++ b/images/virtualization-controller/hack/mirrord-config.json @@ -0,0 +1,12 @@ +{ + "feature": { + "fs": { + "mode": "write", + "read_write": ".*" + }, + "network": { + "incoming": "steal" + } + } +} + diff --git a/images/virtualization-controller/hack/mirrord.sh b/images/virtualization-controller/hack/mirrord.sh new file mode 100755 index 000000000..a883c2dde --- /dev/null +++ b/images/virtualization-controller/hack/mirrord.sh @@ -0,0 +1,50 @@ +#!/bin/bash +set -eo pipefail + +function usage { echo "Usage: $0 [run/wipe]" ; exit 1; } + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" +BIN_DIR="${SCRIPT_DIR}/../bin" +APP="${SCRIPT_DIR}/../cmd/virtualization-controller/main.go" +CONFIG_MIRRORD="${SCRIPT_DIR}/mirrord-config.json" +BINARY="virtualization-controller" +NAMESPACE="d8-virtualization" +DEPLOYMENT="virtualization-controller" +NEW_NAME="mirrord-copy-${DEPLOYMENT}" +export NEW_NAME + +if [[ -z $1 ]]; then + usage +elif [[ $1 == "wipe" ]]; then + echo "Stopping mirror..." + echo "Delete deployment ${NAMESPACE}/${NEW_NAME}" + kubectl -n "${NAMESPACE}" delete deployment/"${NEW_NAME}" + kubectl -n "${NAMESPACE}" scale deployment "${DEPLOYMENT}" --replicas 1 + exit 0 +elif [[ $1 == "run" ]]; then + echo "Starting mirror..." +else + usage +fi + + +if [ ! -d "${BIN_DIR}" ]; then + mkdir "${BIN_DIR}" +fi + +go build -ldflags='-linkmode external' -o "${BIN_DIR}/${BINARY}" "${APP}" +chmod +x "${BIN_DIR}/${BINARY}" + +if ! kubectl -n "${NAMESPACE}" get deployment/"${NEW_NAME}" &>/dev/null; then + kubectl -n "${NAMESPACE}" get deployment/"${DEPLOYMENT}" -ojson | \ + jq '.metadata.name = env.NEW_NAME | + .spec.template.spec.containers[0].command = [ "/bin/bash", "-c", "--" ] | + .spec.template.spec.containers[0].args = [ "while true; do sleep 60; done;" ] | + .spec.replicas = 1 | + .spec.template.metadata.labels.mirror = "true"' | \ + kubectl create -f - +fi + +kubectl wait pod --for=jsonpath='{.status.phase}'=Running -l mirror=true,app="${DEPLOYMENT}" --timeout 60s +kubectl -n "${NAMESPACE}" scale deployment "${DEPLOYMENT}" --replicas 0 +mirrord exec --config-file "${CONFIG_MIRRORD}" --target "deployment/${NEW_NAME}" --target-namespace "${NAMESPACE}" "${BIN_DIR}/${BINARY}" From 928949c490fd28c042c119c89079b51f3f5ba5af Mon Sep 17 00:00:00 2001 From: Yaroslav Borbat Date: Fri, 9 Feb 2024 11:04:46 +0300 Subject: [PATCH 2/2] fix Signed-off-by: Yaroslav Borbat --- images/virtualization-controller/hack/mirrord.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/images/virtualization-controller/hack/mirrord.sh b/images/virtualization-controller/hack/mirrord.sh index a883c2dde..a0d59667d 100755 --- a/images/virtualization-controller/hack/mirrord.sh +++ b/images/virtualization-controller/hack/mirrord.sh @@ -45,6 +45,6 @@ if ! kubectl -n "${NAMESPACE}" get deployment/"${NEW_NAME}" &>/dev/null; then kubectl create -f - fi -kubectl wait pod --for=jsonpath='{.status.phase}'=Running -l mirror=true,app="${DEPLOYMENT}" --timeout 60s +kubectl -n "${NAMESPACE}" wait pod --for=jsonpath='{.status.phase}'=Running -l mirror=true,app="${DEPLOYMENT}" --timeout 60s kubectl -n "${NAMESPACE}" scale deployment "${DEPLOYMENT}" --replicas 0 mirrord exec --config-file "${CONFIG_MIRRORD}" --target "deployment/${NEW_NAME}" --target-namespace "${NAMESPACE}" "${BIN_DIR}/${BINARY}"