apiVersion: apps/v1
kind: Deployment
metadata:
  name: chatapp-api
  namespace: chatapp
  labels:
    app: chatapp-api
spec:
  replicas: 2
  selector:
    matchLabels:
      app: chatapp-api
  template:
    metadata:
      labels:
        app: chatapp-api
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 10001
        runAsGroup: 10001
        fsGroup: 10001
      containers:
      - name: api
        image: chatapp-api:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 3010
        envFrom:
        - configMapRef:
            name: chatapp-config
        - secretRef:
            name: chatapp-secrets
        resources:
          requests:
            memory: "256Mi"
            cpu: "250m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
          capabilities:
            drop:
              - ALL
        livenessProbe:
          httpGet:
            path: /api/v1/health
            port: 3010
          initialDelaySeconds: 15
          periodSeconds: 10
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /api/v1/health
            port: 3010
          initialDelaySeconds: 5
          periodSeconds: 5
          failureThreshold: 3
        startupProbe:
          httpGet:
            path: /api/v1/health
            port: 3010
          failureThreshold: 30
          periodSeconds: 10
        volumeMounts:
        - name: tmp
          mountPath: /tmp
        - name: app-tmp
          mountPath: /app/tmp
      volumes:
      - name: tmp
        emptyDir: {}
      - name: app-tmp
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: api
  namespace: chatapp
spec:
  selector:
    app: chatapp-api
  ports:
  - port: 3010
    targetPort: 3010
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: chatapp-api-pdb
  namespace: chatapp
spec:
  minAvailable: 1
  selector:
    matchLabels:
      app: chatapp-api
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: chatapp-api-netpol
  namespace: chatapp
spec:
  podSelector:
    matchLabels:
      app: chatapp-api
  policyTypes:
    - Ingress
    - Egress
  ingress:
    - from:
        - namespaceSelector:
            matchLabels:
              name: chatapp
      ports:
        - protocol: TCP
          port: 3010
  egress:
    # DNS
    - to:
        - namespaceSelector: {}
      ports:
        - protocol: UDP
          port: 53
    # PostgreSQL
    - to:
        - podSelector:
            matchLabels:
              app: postgres
      ports:
        - protocol: TCP
          port: 5432
    # Redis
    - to:
        - podSelector:
            matchLabels:
              app: redis
      ports:
        - protocol: TCP
          port: 6379