[Git][root/cli/mirror-monitor][main] 2 commits: feat: add gracefully exit

Heric Camargo pushed to branch main at Root / CLI / Mirror Monitor Commits: bbf91f31 by Heric Camargo at 2025-08-11T18:39:31-03:00 feat: add gracefully exit - - - - - 97f8a604 by Heric Camargo at 2025-08-11T18:39:53-03:00 feat: implement Kubernetes deployment, service, and ingress for mirror-monitor API - - - - - 8 changed files: - .gitignore - Containerfile - cmd/api/main.go - + deploy/k8s/deployment.yaml - + deploy/k8s/ingress.yaml - + deploy/k8s/service.yaml - deploy/rsyncuptime.service → deploy/mirror-monitor.service - shell.nix Changes: ===================================== .gitignore ===================================== @@ -3,8 +3,7 @@ # # binaries -rsyncuptime-server -rsyncuptime-tui +mirror-monitor-tui bin/ # Binaries for programs and plugins ===================================== Containerfile ===================================== @@ -3,26 +3,39 @@ FROM golang:1.24 AS builder WORKDIR /app -# Copia os arquivos do projeto +# Copia go.mod e go.sum para melhor cache de layers +COPY go.mod go.sum ./ + +# Download das dependências +RUN go mod download + +# Copia o restante dos arquivos do projeto COPY . . # Compilando o binário da API -RUN go build -o rsyncuptime cmd/api/main.go +RUN go build -o mirror-monitor ./cmd/api # Etapa final: FROM debian:bookworm-slim -# Instalando o rsync: +# Instalando o rsync e criando usuário não-root: RUN apt-get update && \ - apt-get install -y rsync && \ - apt-get clean && rm -rf /var/lib/apt/lists/* + apt-get install -y rsync ca-certificates && \ + apt-get clean && rm -rf /var/lib/apt/lists/* && \ + useradd -r -u 1000 -s /bin/false appuser # Copiando o binário compilado: -COPY --from=builder /app/rsyncuptime /usr/local/bin/rsyncuptime +COPY --from=builder /app/mirror-monitor /usr/local/bin/mirror-monitor + +# Tornando o binário executável +RUN chmod +x /usr/local/bin/mirror-monitor + +# Mudando para usuário não-root +USER 1000 # Exponha a porta, se necessário: #Atenção: se você mudar de porta, esse trecho do código deve-se mudar também! EXPOSE 8080 # Definindo o comando de entrada: -CMD ["rsyncuptime"] +CMD ["mirror-monitor"] ===================================== cmd/api/main.go ===================================== @@ -2,6 +2,7 @@ package main import ( "bufio" + "context" "encoding/json" "errors" "fmt" @@ -9,6 +10,7 @@ import ( "net/http" "os" "os/exec" + "os/signal" "regexp" "strconv" "strings" @@ -332,7 +334,21 @@ func main() { }) log.Printf("Starting monitoring server on :%s using rsync URL '%s'", serverPort, rsyncURL) - if err := http.ListenAndServe(":"+serverPort, mux); err != nil { - log.Fatalf("Server failed to start: %s", err) + server := &http.Server{Addr: ":" + serverPort, Handler: mux} + // Graceful shutdown on SIGINT/SIGTERM + stop := make(chan os.Signal, 1) + signal.Notify(stop, os.Interrupt) + go func() { + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalf("Server error: %s", err) + } + }() + <-stop + log.Println("Shutting down server...") + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := server.Shutdown(ctx); err != nil { + log.Fatalf("Server Shutdown Failed:%+v", err) } + log.Println("Server exited gracefully") } ===================================== deploy/k8s/deployment.yaml ===================================== @@ -0,0 +1,66 @@ +# Kubernetes Deployment for the mirror-monitor API service +apiVersion: apps/v1 +kind: Deployment +metadata: + name: mirror-monitor-api + labels: + app: mirror-monitor-api + version: v1 + component: api +spec: + replicas: 1 + selector: + matchLabels: + app: mirror-monitor-api + template: + metadata: + labels: + app: mirror-monitor-api + version: v1 + component: api + spec: + containers: + - name: api + image: mirror-monitor-api:latest + imagePullPolicy: IfNotPresent + ports: + - containerPort: 8080 + env: + - name: RSYNC_URL + value: "rsync://sagres.c3sl.ufpr.br/" + - name: POLLING_INTERVAL_SECONDS + value: "300" + - name: PORT + value: "8080" + resources: + requests: + memory: "64Mi" + cpu: "50m" + limits: + memory: "128Mi" + cpu: "100m" + livenessProbe: + httpGet: + path: /modules + port: 8080 + initialDelaySeconds: 30 + periodSeconds: 30 + timeoutSeconds: 5 + failureThreshold: 3 + readinessProbe: + httpGet: + path: /modules + port: 8080 + initialDelaySeconds: 5 + periodSeconds: 10 + timeoutSeconds: 3 + failureThreshold: 3 + # Security context for better security + securityContext: + allowPrivilegeEscalation: false + runAsNonRoot: true + runAsUser: 1000 + readOnlyRootFilesystem: true + capabilities: + drop: + - ALL ===================================== deploy/k8s/ingress.yaml ===================================== @@ -0,0 +1,20 @@ +# Optional Ingress for exposing the mirror-monitor API externally +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: mirror-monitor-api-ingress + annotations: + # Remove the rewrite rule since we want to preserve the API paths + nginx.ingress.kubernetes.io/ssl-redirect: "false" +spec: + rules: + - host: mirror-monitor.local + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: mirror-monitor-api + port: + number: 80 ===================================== deploy/k8s/service.yaml ===================================== @@ -0,0 +1,13 @@ +# Kubernetes Service for the mirror-monitor API +apiVersion: v1 +kind: Service +metadata: + name: mirror-monitor-api +spec: + type: ClusterIP + selector: + app: mirror-monitor-api + ports: + - name: http + port: 80 + targetPort: 8080 # Match the containerPort in your Deployment ===================================== deploy/rsyncuptime.service → deploy/mirror-monitor.service ===================================== ===================================== shell.nix ===================================== @@ -1,4 +1,4 @@ -# Nix shell para desenvolvimento do servidor web rsyncuptime +# Nix shell para desenvolvimento do servidor web mirror-monitor { pkgs ? import <nixpkgs> {} }: @@ -8,6 +8,11 @@ pkgs.mkShell { pkgs.rsync pkgs.jq pkgs.mage + + # kubernetes + pkgs.kubectl + pkgs.kind + pkgs.docker ]; shellHook = '' View it on GitLab: https://gitlab.c3sl.ufpr.br/root/cli/mirror-monitor/-/compare/6b2a262d3fb5d6... -- View it on GitLab: https://gitlab.c3sl.ufpr.br/root/cli/mirror-monitor/-/compare/6b2a262d3fb5d6... You're receiving this email because of your account on gitlab.c3sl.ufpr.br.
participantes (1)
-
Heric Camargo (@hc20)