From 32664b15d2fadcc80a105e870075277c4233f724 Mon Sep 17 00:00:00 2001 From: chahatsagarmain Date: Sun, 3 Nov 2024 11:33:58 +0530 Subject: [PATCH 1/6] k8s integration Signed-off-by: chahatsagarmain --- .github/workflows/ci-docker-hotrod.yml | 10 ++++++ scripts/build-hotrod-image.sh | 47 ++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci-docker-hotrod.yml b/.github/workflows/ci-docker-hotrod.yml index 5f105d32539..74de37eeed6 100644 --- a/.github/workflows/ci-docker-hotrod.yml +++ b/.github/workflows/ci-docker-hotrod.yml @@ -56,6 +56,16 @@ jobs: echo "BUILD_FLAGS=" >> ${GITHUB_ENV} ;; esac + - name: Install kubectl + uses: azure/setup-kubectl@v3 + with: + version: 'latest' + + - name: Install Kustomize + uses: imranismail/setup-kustomize@v2 + + - name: Create k8s Kind Cluster + uses: helm/kind-action@v1 - name: Build, test, and publish hotrod image run: bash scripts/build-hotrod-image.sh ${{ env.BUILD_FLAGS }} -v ${{ matrix.jaeger-version }} diff --git a/scripts/build-hotrod-image.sh b/scripts/build-hotrod-image.sh index 3a1fe3e7184..177a9e1f0b5 100755 --- a/scripts/build-hotrod-image.sh +++ b/scripts/build-hotrod-image.sh @@ -12,6 +12,7 @@ print_help() { echo "-o: overwrite image in the target remote repository even if the semver tag already exists" echo "-p: Comma-separated list of platforms to build for (default: all supported)" echo "-v: Jaeger version to use for hotrod image (v1 or v2, default: v1)" + echo "-r: Runtime to test with (docker|k8s, default: docker)" exit 1 } @@ -22,6 +23,7 @@ jaeger_version="v1" binary="all-in-one" FLAGS=() success="false" +runtime="k8s" while getopts "hlop:v:" opt; do case "${opt}" in @@ -38,6 +40,12 @@ while getopts "hlop:v:" opt; do v) jaeger_version=${OPTARG} ;; + r) + case "${OPTARG}" in + docker|k8s) runtime="${OPTARG}" ;; + *) echo "Invalid runtime: ${OPTARG}. Use 'docker' or 'k8s'" >&2; exit 1 ;; + esac + ;; *) print_help ;; @@ -70,10 +78,20 @@ dump_logs() { teardown() { echo "Tearing down..." - if [[ "$success" == "false" ]]; then - dump_logs "${docker_compose_file}" + if [[ "${runtime}" == "k8s" ]]; then + if [[ -n "${HOTROD_PORT_FWD_PID:-}" ]]; then + kill "$HOTROD_PORT_FWD_PID" || true + fi + if [[ -n "${JAEGER_PORT_FWD_PID:-}" ]]; then + kill "$JAEGER_PORT_FWD_PID" || true + fi + kubectl delete namespace example-hotrod --ignore-not-found=true + else + if [[ "$success" == "false" ]]; then + dump_logs "${docker_compose_file}" + fi + docker compose -f "$docker_compose_file" down fi - docker compose -f "$docker_compose_file" down } trap teardown EXIT @@ -98,9 +116,26 @@ bash scripts/build-upload-a-docker-image.sh -l -c example-hotrod -d examples/hot make build-${binary} bash scripts/build-upload-a-docker-image.sh -l -b -c "${binary}" -d cmd/"${binary}" -p "${current_platform}" -t release -echo '::group:: docker compose' -JAEGER_VERSION=$GITHUB_SHA REGISTRY="localhost:5000/" docker compose -f "$docker_compose_file" up -d -echo '::endgroup::' +if [[ "${runtime}" == "k8s" ]]; then + if ! kubectl cluster-info >/dev/null 2>&1; then + echo "Error: Cannot connect to Kubernetes cluster" + exit 1 + fi + + kustomize build ./examples/hotrod/kubernetes | kubectl apply -n example-hotrod -f - + kubectl wait --for=condition=available --timeout=180s -n example-hotrod deployment/example-hotrod + + kubectl port-forward -n example-hotrod service/example-hotrod 8080:frontend & + HOTROD_PORT_FWD_PID=$! + kubectl port-forward -n example-hotrod service/jaeger 16686:frontend & + JAEGER_PORT_FWD_PID=$! + + sleep 5 +else + echo '::group:: docker compose' + JAEGER_VERSION=$GITHUB_SHA REGISTRY="localhost:5000/" docker compose -f "$docker_compose_file" up -d + echo '::endgroup::' +fi i=0 while [[ "$(curl -s -o /dev/null -w '%{http_code}' localhost:8080)" != "200" && $i -lt 30 ]]; do From 30e0b0f935324f26ad85f7d271b6f20ff57af4a1 Mon Sep 17 00:00:00 2001 From: chahatsagarmain Date: Sun, 3 Nov 2024 11:38:12 +0530 Subject: [PATCH 2/6] change default Signed-off-by: chahatsagarmain --- scripts/build-hotrod-image.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build-hotrod-image.sh b/scripts/build-hotrod-image.sh index 177a9e1f0b5..0b82522f03f 100755 --- a/scripts/build-hotrod-image.sh +++ b/scripts/build-hotrod-image.sh @@ -23,7 +23,7 @@ jaeger_version="v1" binary="all-in-one" FLAGS=() success="false" -runtime="k8s" +runtime="docker" while getopts "hlop:v:" opt; do case "${opt}" in From f09759d24d1718b229ac1d874adf8fb5ea51c762 Mon Sep 17 00:00:00 2001 From: chahatsagarmain Date: Sun, 3 Nov 2024 11:41:52 +0530 Subject: [PATCH 3/6] lint fix Signed-off-by: chahatsagarmain --- scripts/build-hotrod-image.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build-hotrod-image.sh b/scripts/build-hotrod-image.sh index 0b82522f03f..0cfe766a97b 100755 --- a/scripts/build-hotrod-image.sh +++ b/scripts/build-hotrod-image.sh @@ -25,7 +25,7 @@ FLAGS=() success="false" runtime="docker" -while getopts "hlop:v:" opt; do +while getopts "hlop:v:r" opt; do case "${opt}" in l) # in the local-only mode the images will only be pushed to local registry From 8fd66ba136b19bce1a158fbe360f738da7e2a07b Mon Sep 17 00:00:00 2001 From: chahatsagarmain Date: Mon, 4 Nov 2024 11:39:56 +0530 Subject: [PATCH 4/6] minor changes in script and github workflow Signed-off-by: chahatsagarmain --- .github/workflows/ci-docker-hotrod.yml | 6 +++++- scripts/build-hotrod-image.sh | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci-docker-hotrod.yml b/.github/workflows/ci-docker-hotrod.yml index 74de37eeed6..85ad806d0ac 100644 --- a/.github/workflows/ci-docker-hotrod.yml +++ b/.github/workflows/ci-docker-hotrod.yml @@ -21,6 +21,7 @@ jobs: strategy: fail-fast: false matrix: + runtime: [docker, k8s] jaeger-version: [v1, v2] steps: @@ -57,18 +58,21 @@ jobs: ;; esac - name: Install kubectl + if: matrix.runtime == 'k8s' uses: azure/setup-kubectl@v3 with: version: 'latest' - name: Install Kustomize + if: matrix.runtime == 'k8s' uses: imranismail/setup-kustomize@v2 - name: Create k8s Kind Cluster + if: matrix.runtime == 'k8s' uses: helm/kind-action@v1 - name: Build, test, and publish hotrod image - run: bash scripts/build-hotrod-image.sh ${{ env.BUILD_FLAGS }} -v ${{ matrix.jaeger-version }} + run: bash scripts/build-hotrod-image.sh ${{ env.BUILD_FLAGS }} -v ${{ matrix.jaeger-version }} -r ${{ matrix.runtime }} env: DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }} diff --git a/scripts/build-hotrod-image.sh b/scripts/build-hotrod-image.sh index 0cfe766a97b..da91265f481 100755 --- a/scripts/build-hotrod-image.sh +++ b/scripts/build-hotrod-image.sh @@ -25,7 +25,7 @@ FLAGS=() success="false" runtime="docker" -while getopts "hlop:v:r" opt; do +while getopts "hlop:v:r:" opt; do case "${opt}" in l) # in the local-only mode the images will only be pushed to local registry @@ -77,7 +77,7 @@ dump_logs() { } teardown() { - echo "Tearing down..." + echo "::group::Tearing down..." if [[ "${runtime}" == "k8s" ]]; then if [[ -n "${HOTROD_PORT_FWD_PID:-}" ]]; then kill "$HOTROD_PORT_FWD_PID" || true @@ -87,11 +87,12 @@ teardown() { fi kubectl delete namespace example-hotrod --ignore-not-found=true else - if [[ "$success" == "false" ]]; then - dump_logs "${docker_compose_file}" - fi docker compose -f "$docker_compose_file" down fi + if [[ "$success" == "false" ]]; then + dump_logs "${docker_compose_file}" + fi + echo "::endgroup::" } trap teardown EXIT @@ -121,7 +122,8 @@ if [[ "${runtime}" == "k8s" ]]; then echo "Error: Cannot connect to Kubernetes cluster" exit 1 fi - + + echo '::group:: run on Kubernetes' kustomize build ./examples/hotrod/kubernetes | kubectl apply -n example-hotrod -f - kubectl wait --for=condition=available --timeout=180s -n example-hotrod deployment/example-hotrod @@ -129,8 +131,8 @@ if [[ "${runtime}" == "k8s" ]]; then HOTROD_PORT_FWD_PID=$! kubectl port-forward -n example-hotrod service/jaeger 16686:frontend & JAEGER_PORT_FWD_PID=$! - - sleep 5 + echo '::endgroup::' + else echo '::group:: docker compose' JAEGER_VERSION=$GITHUB_SHA REGISTRY="localhost:5000/" docker compose -f "$docker_compose_file" up -d From 59dec64e2cc39e68621d33453f47ab322b4cac98 Mon Sep 17 00:00:00 2001 From: chahatsagarmain Date: Sat, 16 Nov 2024 00:32:22 +0530 Subject: [PATCH 5/6] dump logs changes Signed-off-by: chahatsagarmain --- scripts/build-hotrod-image.sh | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/scripts/build-hotrod-image.sh b/scripts/build-hotrod-image.sh index 8c62e042a61..d014eff9953 100755 --- a/scripts/build-hotrod-image.sh +++ b/scripts/build-hotrod-image.sh @@ -70,14 +70,24 @@ esac set -x dump_logs() { - local compose_file=$1 - echo "::group:: Hotrod logs" - docker compose -f "${compose_file}" logs + local runtime=$1 + local compose_file=$2 + + echo "::group:: Logs" + if [ "$runtime" == "k8s" ]; then + kubectl logs -n example-hotrod -l app=example-hotrod + kubectl logs -n example-hotrod -l app=jaeger + else + docker compose -f "$compose_file" logs + fi echo "::endgroup::" } teardown() { echo "::group::Tearing down..." + if [[ "$success" == "false" ]]; then + dump_logs "${runtime}" "${docker_compose_file}" + fi if [[ "${runtime}" == "k8s" ]]; then if [[ -n "${HOTROD_PORT_FWD_PID:-}" ]]; then kill "$HOTROD_PORT_FWD_PID" || true @@ -89,9 +99,7 @@ teardown() { else docker compose -f "$docker_compose_file" down fi - if [[ "$success" == "false" ]]; then - dump_logs "${docker_compose_file}" - fi + echo "::endgroup::" } trap teardown EXIT From 8b2dff8bc59875e540e21cdc48b3bd0f74b188d1 Mon Sep 17 00:00:00 2001 From: chahatsagarmain Date: Sat, 16 Nov 2024 00:54:09 +0530 Subject: [PATCH 6/6] fix Signed-off-by: chahatsagarmain --- scripts/build-hotrod-image.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build-hotrod-image.sh b/scripts/build-hotrod-image.sh index d014eff9953..08608f47110 100755 --- a/scripts/build-hotrod-image.sh +++ b/scripts/build-hotrod-image.sh @@ -143,7 +143,7 @@ if [[ "${runtime}" == "k8s" ]]; then else echo '::group:: docker compose' - JAEGER_VERSION=$GITHUB_SHA REGISTRY="localhost:5000/" docker compose -f "$docker_compose_file" up -d + JAEGER_VERSION=$GITHUB_SHA HOTROD_VERSION=$GITHUB_SHA REGISTRY="localhost:5000/" docker compose -f "$docker_compose_file" up -d echo '::endgroup::' fi