---
apiVersion: v1
kind: Namespace
metadata:
  name: ${{ values.namespace }}
---
apiVersion: v1
kind: Service
metadata:
  name: petclinic
  namespace: ${{ values.namespace }}
  labels:
    app: petclinic
spec:
  type: ClusterIP
  ports:
    - port: 8080
      targetPort: http
      name: http
  selector:
    app: petclinic
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ${{ values.namespace }}-petclinic
  namespace: ${{ values.namespace }}
spec:
  ingressClassName: nginx
  rules:
    - host: ${{ values.namespace }}.
      http:
        paths:
          - backend:
              service:
                name: petclinic
                port:
                  name: http
            path: /
            pathType: Prefix
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: petclinic
  namespace: ${{ values.namespace }}
  labels:
    app: petclinic
spec:
  replicas: 1
  selector:
    matchLabels:
      app: petclinic
  template:
    metadata:
      labels:
        app: petclinic
    spec:
      containers:
        - name: workload
          image: gitea./giteaadmin/${{ values.name }}
          env:
            - name: SPRING_PROFILES_ACTIVE
              value: postgres
            - name: POSTGRES_URL
              value: jdbc:postgresql://demo-db/petclinic
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: demo-db
                  key: username
            - name: POSTGRES_PASS
              valueFrom:
                secretKeyRef:
                  name: demo-db
                  key: password
            - name: SERVICE_BINDING_ROOT
              value: /bindings
            - name: SPRING_APPLICATION_JSON
              value: |
                {
                  "management.endpoint.health.probes.add-additional-paths": true
                }
          ports:
            - name: http
              containerPort: 8080
          livenessProbe:
            httpGet:
              path: /livez
              port: http
          readinessProbe:
            httpGet:
              path: /readyz
              port: http
          volumeMounts:
            - mountPath: /bindings/secret
              name: binding
              readOnly: true
      volumes:
        - name: binding
          projected:
            sources:
              - secret:
                  name: demo-db

---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: ${{ values.namespace }}-petclinic-monitor
  namespace: monitoring # Namespace where Prometheus is running
  labels:
    release: kube-prometheus-stack # this must match Prometheus' service monitor matching
spec:
  selector:
    matchLabels:
      app: petclinic # Match your application's labels
  namespaceSelector:
    matchNames:
      - ${{ values.namespace }} # Namespace where your app is running
  endpoints:
    - port: http # Name of the port in your Service
      path: /actuator/prometheus # Path to your metrics endpoint
      interval: 15s # How often to scrape metrics

