Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
# Final stage
FROM alpine:latest

WORKDIR /
COPY --from=builder /app/test-app /test-app

EXPOSE 8080

ENTRYPOINT ["/test-app"]
25 changes: 19 additions & 6 deletions helpers/fetch_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
41 changes: 41 additions & 0 deletions k8s-deployment.yaml
Original file line number Diff line number Diff line change
@@ -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