From 739f70b440ee835685cf692bb38d6b87bf34b41a Mon Sep 17 00:00:00 2001 From: ITO Mac Date: Mon, 4 May 2026 16:10:31 +0200 Subject: [PATCH] zadaca --- Dockerfile | 18 +++++++ app/main.py | 123 +++++++++++++++++++++++++++++++++++++++++++ app/requirements.txt | 2 + k8s/configmap.yaml | 9 ++++ k8s/deployment.yaml | 37 +++++++++++++ k8s/ingress.yaml | 19 +++++++ k8s/service.yaml | 13 +++++ 7 files changed, 221 insertions(+) create mode 100644 Dockerfile create mode 100644 app/main.py create mode 100644 app/requirements.txt create mode 100644 k8s/configmap.yaml create mode 100644 k8s/deployment.yaml create mode 100644 k8s/ingress.yaml create mode 100644 k8s/service.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..dbd6e2c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM python:3.12-slim AS builder + +WORKDIR /builder +COPY app/requirements.txt . +RUN pip install --no-cache-dir --prefix=/install -r requirements.txt + +FROM python:3.12-slim +RUN adduser --disabled-password --no-create-home appuser +WORKDIR /app +COPY --from=builder /install /usr/local +COPY app/ . + +USER appuser +EXPOSE 8000 + +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] + + diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..634e363 --- /dev/null +++ b/app/main.py @@ -0,0 +1,123 @@ +import os +import datetime +from fastapi import FastAPI +from fastapi.responses import HTMLResponse + +app = FastAPI(title="Distribuirani Sustavi - FPMOZ") + +SERVICE_NAME = os.getenv("SERVICE_NAME", "distribuirani-service") +LOG_LEVEL = os.getenv("LOG_LEVEL", "info") +WELCOME_MSG = os.getenv("WELCOME_MSG", "Pozdrav iz FPMOZ k3s clustera!") + + +@app.get("/", response_class=HTMLResponse) +def root(): + return f""" + + + + + {SERVICE_NAME} + + + +
+

Distribuirani Sustavi

+

{WELCOME_MSG}

+
+
+ Servis + {SERVICE_NAME} +
+
+ Hostname + {os.getenv("HOSTNAME", "local")} +
+
+ Log Level + {LOG_LEVEL} +
+
+ Vrijeme + {datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")} +
+
+ FPMOZ K3s Cluster • GitOps CI/CD +
+ +""" + + +@app.get("/health") +def health(): + return {"status": "ok"} + + +@app.get("/info") +def info(): + return { + "service": SERVICE_NAME, + "hostname": os.getenv("HOSTNAME", "local"), + "log_level": LOG_LEVEL, + "welcome": WELCOME_MSG, + "timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(), + } \ No newline at end of file diff --git a/app/requirements.txt b/app/requirements.txt new file mode 100644 index 0000000..6b9a502 --- /dev/null +++ b/app/requirements.txt @@ -0,0 +1,2 @@ +fastapi==0.115.6 +uvicorn[standard]==0.34.0 \ No newline at end of file diff --git a/k8s/configmap.yaml b/k8s/configmap.yaml new file mode 100644 index 0000000..09cfbe7 --- /dev/null +++ b/k8s/configmap.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: zadaca-config + namespace: student-blazp04 +data: + SERVICE_NAME: "distribuirani-service" + LOG_LEVEL: "info" + WELCOME_MSG: "Zadaca 1" diff --git a/k8s/deployment.yaml b/k8s/deployment.yaml new file mode 100644 index 0000000..6ef34eb --- /dev/null +++ b/k8s/deployment.yaml @@ -0,0 +1,37 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: zadaca-deployment + namespace: student-blazp04 +spec: + replicas: 1 + selector: + matchLabels: + app: zadaca + template: + metadata: + labels: + app: zadaca + spec: + containers: + - name: zadaca + image: git.fpmoz.sum.ba/blazp04/zadaca:latest + ports: + - containerPort: 8000 + envFrom: + - configMapRef: + name: zadaca-config + readinessProbe: + httpGet: + path: /health + port: 8000 + initialDelaySeconds: 3 + periodSeconds: 5 + resources: + requests: + memory: "64Mi" + cpu: "50m" + limits: + memory: "128Mi" + cpu: "200m" diff --git a/k8s/ingress.yaml b/k8s/ingress.yaml new file mode 100644 index 0000000..b7845b2 --- /dev/null +++ b/k8s/ingress.yaml @@ -0,0 +1,19 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: zadaca-ingress + namespace: student-blazp04 + annotations: + traefik.ingress.kubernetes.io/router.entrypoints: web +spec: + rules: + - host: blazp04-zadaca.argocd.fpmoz.sum.ba + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: zadaca-service + port: + number: 80 diff --git a/k8s/service.yaml b/k8s/service.yaml new file mode 100644 index 0000000..920ab47 --- /dev/null +++ b/k8s/service.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: zadaca-service + namespace: student-blazp04 +spec: + selector: + app: zadaca + ports: + - port: 80 + targetPort: 8000 + protocol: TCP + type: ClusterIP