commit 7f031a784873245dbde8aa3c7dedac7d08502b71 Author: rod Date: Sun May 17 11:59:53 2026 +0200 Initial scaffold: Go service, Helm chart, Kustomize overlays diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..56cc09a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +hello-svc diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..575bd05 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.23-alpine AS builder +WORKDIR /app +COPY go.mod go.sum ./ +RUN go mod download +COPY . . +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o hello-svc . + +FROM gcr.io/distroless/static-debian12 +COPY --from=builder /app/hello-svc /hello-svc +EXPOSE 8080 +ENTRYPOINT ["/hello-svc"] diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3cf8350 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module git.samidare.dev/rod/hello-svc + +go 1.23 + +require github.com/prometheus/client_golang v1.22.0 + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/prometheus/client_model v0.6.1 // indirect + github.com/prometheus/common v0.62.0 // indirect + github.com/prometheus/procfs v0.15.1 // indirect + golang.org/x/sys v0.30.0 // indirect + google.golang.org/protobuf v1.36.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e81255c --- /dev/null +++ b/go.sum @@ -0,0 +1,32 @@ +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.22.0 h1:rb93p9lokFEsctTys46VnV1kLCDpVZ0a/Y92Vm0Zc6Q= +github.com/prometheus/client_golang v1.22.0/go.mod h1:R7ljNsLXhuQXYZYtw6GAE9AZg8Y7vEW5scdCXrWRXC0= +github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= +github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= +github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io= +github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I= +github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= +github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= +google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/k8s/base/kustomization.yaml b/k8s/base/kustomization.yaml new file mode 100644 index 0000000..96c5256 --- /dev/null +++ b/k8s/base/kustomization.yaml @@ -0,0 +1,8 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization + +helmCharts: + - name: hello-svc + releaseName: hello-svc + localChartPath: ../../helm/hello-svc + valuesFile: values.yaml diff --git a/k8s/base/values.yaml b/k8s/base/values.yaml new file mode 100644 index 0000000..887f1ca --- /dev/null +++ b/k8s/base/values.yaml @@ -0,0 +1,24 @@ +# Base Helm values — overlays patch on top of this via strategic merge. +replicaCount: 1 + +image: + repository: git.samidare.dev/rod/hello-svc + pullPolicy: Always + tag: "latest" + +env: + APP_ENV: production + LOG_LEVEL: info + APP_VERSION: unknown + +resources: + requests: + cpu: 100m + memory: 64Mi + limits: + cpu: 500m + memory: 128Mi + +serviceMonitor: + enabled: true + interval: 30s diff --git a/k8s/overlays/dev/deployment-patch.yaml b/k8s/overlays/dev/deployment-patch.yaml new file mode 100644 index 0000000..97a1897 --- /dev/null +++ b/k8s/overlays/dev/deployment-patch.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: hello-svc +spec: + replicas: 1 + template: + spec: + containers: + - name: hello-svc + env: + - name: APP_ENV + value: dev + - name: LOG_LEVEL + value: debug + resources: + requests: + cpu: 50m + memory: 32Mi + limits: + cpu: 100m + memory: 64Mi diff --git a/k8s/overlays/dev/kustomization.yaml b/k8s/overlays/dev/kustomization.yaml new file mode 100644 index 0000000..66ac191 --- /dev/null +++ b/k8s/overlays/dev/kustomization.yaml @@ -0,0 +1,11 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization + +namespace: dev + +resources: + - ../../base + - namespace.yaml + +patches: + - path: deployment-patch.yaml diff --git a/k8s/overlays/dev/namespace.yaml b/k8s/overlays/dev/namespace.yaml new file mode 100644 index 0000000..8cab297 --- /dev/null +++ b/k8s/overlays/dev/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: dev diff --git a/k8s/overlays/prod/deployment-patch.yaml b/k8s/overlays/prod/deployment-patch.yaml new file mode 100644 index 0000000..74cfd57 --- /dev/null +++ b/k8s/overlays/prod/deployment-patch.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: hello-svc +spec: + replicas: 3 + template: + spec: + containers: + - name: hello-svc + env: + - name: APP_ENV + value: prod + - name: LOG_LEVEL + value: info + resources: + requests: + cpu: 100m + memory: 64Mi + limits: + cpu: 500m + memory: 128Mi diff --git a/k8s/overlays/prod/kustomization.yaml b/k8s/overlays/prod/kustomization.yaml new file mode 100644 index 0000000..9dff308 --- /dev/null +++ b/k8s/overlays/prod/kustomization.yaml @@ -0,0 +1,11 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization + +namespace: prod + +resources: + - ../../base + - namespace.yaml + +patches: + - path: deployment-patch.yaml diff --git a/k8s/overlays/prod/namespace.yaml b/k8s/overlays/prod/namespace.yaml new file mode 100644 index 0000000..5f325a9 --- /dev/null +++ b/k8s/overlays/prod/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: prod diff --git a/main.go b/main.go new file mode 100644 index 0000000..f01fef6 --- /dev/null +++ b/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "encoding/json" + "log/slog" + "net/http" + "os" + "strconv" + "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +var ( + reqTotal = promauto.NewCounterVec(prometheus.CounterOpts{ + Name: "http_requests_total", + Help: "Total number of HTTP requests.", + }, []string{"method", "path", "status"}) + + reqDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ + Name: "http_request_duration_seconds", + Help: "HTTP request latency in seconds.", + Buckets: prometheus.DefBuckets, + }, []string{"method", "path"}) +) + +func main() { + env := getenv("APP_ENV", "dev") + version := getenv("APP_VERSION", "unknown") + port := getenv("PORT", "8080") + + logLevel := slog.LevelInfo + if getenv("LOG_LEVEL", "info") == "debug" { + logLevel = slog.LevelDebug + } + slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel}))) + + mux := http.NewServeMux() + mux.Handle("/metrics", promhttp.Handler()) + mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + mux.HandleFunc("/", instrument(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]string{ + "env": env, + "version": version, + }) + })) + + slog.Info("listening", "port", port, "env", env, "version", version) + if err := http.ListenAndServe(":"+port, mux); err != nil { + slog.Error("server failed", "err", err) + os.Exit(1) + } +} + +func instrument(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + rw := &statusWriter{ResponseWriter: w, status: http.StatusOK} + start := time.Now() + next(rw, r) + dur := time.Since(start).Seconds() + status := strconv.Itoa(rw.status) + reqTotal.WithLabelValues(r.Method, r.URL.Path, status).Inc() + reqDuration.WithLabelValues(r.Method, r.URL.Path).Observe(dur) + slog.Debug("request", "method", r.Method, "path", r.URL.Path, "status", rw.status, "duration", dur) + } +} + +type statusWriter struct { + http.ResponseWriter + status int +} + +func (sw *statusWriter) WriteHeader(code int) { + sw.status = code + sw.ResponseWriter.WriteHeader(code) +} + +func getenv(key, fallback string) string { + if v := os.Getenv(key); v != "" { + return v + } + return fallback +}