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