From 82b53e02d62557d6cd120ce649529995c8d635d7 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 6 Apr 2026 16:08:12 +0000 Subject: [PATCH] Add Kubernetes support and modernize build process This change enables the application to run effectively in a Kubernetes environment by: 1. Updating 'helpers/fetch_index.go' to support Kubernetes-style pod names as a source for the application index. 2. Providing a 'k8s-deployment.yaml' manifest with a Deployment and Service to easily deploy the app to a K8s cluster. 3. Modernizing the 'Dockerfile' to use a multi-stage build, eliminating the need for a pre-built binary and reducing the final image size. Co-authored-by: gusraggio <172923+gusraggio@users.noreply.github.com> --- Dockerfile | 18 ++++++++++++++---- helpers/fetch_index.go | 25 +++++++++++++++++++------ k8s-deployment.yaml | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 k8s-deployment.yaml diff --git a/Dockerfile b/Dockerfile index e5b4902..7320376 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,16 @@ -FROM busybox:ubuntu-14.04 +# Build stage +FROM golang:1.24-alpine AS builder -ENTRYPOINT ["/test-app"] +WORKDIR /app +COPY . . +RUN go build -o test-app . -COPY test-app /test-app -RUN chmod a+x /test-app \ No newline at end of file +# Final stage +FROM alpine:latest + +WORKDIR / +COPY --from=builder /app/test-app /test-app + +EXPOSE 8080 + +ENTRYPOINT ["/test-app"] \ No newline at end of file diff --git a/helpers/fetch_index.go b/helpers/fetch_index.go index 0c3ba3d..dac3388 100644 --- a/helpers/fetch_index.go +++ b/helpers/fetch_index.go @@ -3,15 +3,28 @@ package helpers import ( "os" "strconv" + "strings" ) func FetchIndex() (int, error) { - index := "-1" + if val := os.Getenv("CF_INSTANCE_INDEX"); val != "" { + return strconv.Atoi(val) + } + if val := os.Getenv("INSTANCE_INDEX"); val != "" { + return strconv.Atoi(val) + } - if os.Getenv("CF_INSTANCE_INDEX") != "" { - index = os.Getenv("CF_INSTANCE_INDEX") - } else if os.Getenv("INSTANCE_INDEX") != "" { - index = os.Getenv("INSTANCE_INDEX") + // Fallback for Kubernetes (StatefulSets often have pod-0, pod-1) + hostname, _ := os.Hostname() + if val := os.Getenv("POD_NAME"); val != "" { + hostname = val } - return strconv.Atoi(index) + + parts := strings.Split(hostname, "-") + lastPart := parts[len(parts)-1] + if i, err := strconv.Atoi(lastPart); err == nil { + return i, nil + } + + return -1, nil } diff --git a/k8s-deployment.yaml b/k8s-deployment.yaml new file mode 100644 index 0000000..846d3c5 --- /dev/null +++ b/k8s-deployment.yaml @@ -0,0 +1,41 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test-app + labels: + app: test-app +spec: + replicas: 1 + selector: + matchLabels: + app: test-app + template: + metadata: + labels: + app: test-app + spec: + containers: + - name: test-app + image: cloudfoundry/test-app:latest + ports: + - containerPort: 8080 + env: + - name: POD_NAME + valueFrom: + fieldRef: + fieldPath: metadata.name + - name: APP_NAME + value: "test-app" +--- +apiVersion: v1 +kind: Service +metadata: + name: test-app +spec: + selector: + app: test-app + ports: + - protocol: TCP + port: 80 + targetPort: 8080 + type: LoadBalancer