apiVersion: apps/v1
kind: Deployment
metadata:
  name: traefik
  labels:
    app: vibecarbon-traefik
    component: ingress
  # The CLI's deploy owns this Deployment: {{TRAEFIK_IMAGE}} is rendered from
  # the version set, and Flux drops it from its build (flux/clusters/*).
  # prune: disabled keeps Flux's garbage collection off it too.
  annotations:
    kustomize.toolkit.fluxcd.io/prune: disabled
spec:
  replicas: 1
  selector:
    matchLabels:
      app: vibecarbon-traefik
  template:
    metadata:
      labels:
        app: vibecarbon-traefik
        component: ingress
    spec:
      serviceAccountName: traefik
      nodeSelector:
        node-role.kubernetes.io/control-plane: 'true'
      tolerations:
      - key: node-role.kubernetes.io/control-plane
        operator: Exists
        effect: NoSchedule
      containers:
      - name: traefik
        image: "{{TRAEFIK_IMAGE}}"
        imagePullPolicy: IfNotPresent
        args:
        - --api.dashboard=true
        - --api.insecure=false
        - --providers.kubernetescrd
        - --providers.kubernetesingress
        - --entrypoints.web.address=:80
        - --entrypoints.websecure.address=:443
        - --entrypoints.traefik.address=:8080
        - --ping=true
        - --log.level=INFO
        - --accesslog=true
        - --accesslog.format=json
        - --accesslog.fields.names.RequestPath=drop
        env:
        # Go's own soft limit, kept about 10% under the memory limit below.
        - name: GOMEMLIMIT
          value: 460MiB
        ports:
        - containerPort: 80
          name: web
          hostPort: 80
        - containerPort: 443
          name: websecure
          hostPort: 443
        - containerPort: 8080
          name: traefik
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          # No CPU limit, on purpose: everything the site serves passes through
          # here, so a limit turns one noisy client into a full outage. See
          # docker-compose.prod.yml, which also explains the memory figure.
          # Guarded by tests/unit/template/compose-cpu-limits.test.ts.
          limits:
            memory: 512Mi
        startupProbe:
          httpGet:
            path: /ping
            port: traefik
          periodSeconds: 1
          timeoutSeconds: 3
          failureThreshold: 60
        livenessProbe:
          httpGet:
            path: /ping
            port: traefik
          initialDelaySeconds: 0
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /ping
            port: traefik
          initialDelaySeconds: 0
          periodSeconds: 5
          timeoutSeconds: 3
          failureThreshold: 3
        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
        volumeMounts:
        - name: tmp-vol-0
          mountPath: /tmp
      securityContext:
        seccompProfile:
          type: RuntimeDefault
      volumes:
      - name: tmp-vol-0
        emptyDir: {}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: traefik
  labels:
    app: vibecarbon-traefik
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: traefik
rules:
- apiGroups:
  - ''
  resources:
  - services
  - endpoints
  - secrets
  - configmaps
  - pods
  - nodes
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - discovery.k8s.io
  resources:
  - endpointslices
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - extensions
  - networking.k8s.io
  resources:
  - ingresses
  - ingressclasses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - extensions
  - networking.k8s.io
  resources:
  - ingresses/status
  verbs:
  - update
- apiGroups:
  - traefik.io
  resources:
  - middlewares
  - middlewaretcps
  - ingressroutes
  - traefikservices
  - ingressroutetcps
  - ingressrouteudps
  - tlsoptions
  - tlsstores
  - serverstransports
  - serverstransporttcps
  verbs:
  - get
  - list
  - watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: traefik
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: traefik
subjects:
- kind: ServiceAccount
  name: traefik
  namespace: vibecarbon
