# Phase 14: Medical Imaging System
# Kubernetes Deployment Manifest

apiVersion: v1
kind: Namespace
metadata:
  name: medical-imaging
  labels:
    name: medical-imaging
    environment: production

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: medical-imaging-config
  namespace: medical-imaging
data:
  APP_ENV: "production"
  APP_PORT: "8080"
  LOG_LEVEL: "INFO"
  HIPAA_MODE: "enabled"
  PHI_DETECTION: "enabled"
  ANONYMIZATION: "enabled"
  MAX_IMAGE_SIZE_MB: "100"
  SUPPORTED_FORMATS: "dcm,png,jpg,jpeg,tiff"
  PROCESSING_TIMEOUT_SEC: "30"
  MAX_CONCURRENT_ANALYSIS: "10"
  CACHE_ENABLED: "true"
  CACHE_TTL_SECONDS: "3600"
  METRICS_ENABLED: "true"
  METRICS_PORT: "9090"
  HEALTH_CHECK_ENABLED: "true"

---
apiVersion: v1
kind: Secret
metadata:
  name: medical-imaging-secrets
  namespace: medical-imaging
type: Opaque
data:
  # Base64 encoded secrets
  # database_url: <base64-encoded-value>
  # redis_password: <base64-encoded-value>

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: medical-images-pvc
  namespace: medical-imaging
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 100Gi
  storageClassName: fast-ssd

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: analysis-results-pvc
  namespace: medical-imaging
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
  storageClassName: fast-ssd

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: medical-imaging
  namespace: medical-imaging
  labels:
    app: medical-imaging
    version: v1.0.0
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: medical-imaging
  template:
    metadata:
      labels:
        app: medical-imaging
        version: v1.0.0
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9090"
        prometheus.io/path: "/metrics"
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000
        fsGroup: 1000

      containers:
      - name: medical-imaging
        image: swarmcare/medical-imaging:1.0.0
        imagePullPolicy: Always

        ports:
        - name: http
          containerPort: 8080
          protocol: TCP
        - name: metrics
          containerPort: 9090
          protocol: TCP

        envFrom:
        - configMapRef:
            name: medical-imaging-config
        - secretRef:
            name: medical-imaging-secrets
            optional: true

        volumeMounts:
        - name: medical-images
          mountPath: /data/medical-images
        - name: analysis-results
          mountPath: /data/results
        - name: logs
          mountPath: /var/log/medical-imaging

        resources:
          requests:
            memory: "2Gi"
            cpu: "1000m"
          limits:
            memory: "4Gi"
            cpu: "2000m"

        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 60
          periodSeconds: 30
          timeoutSeconds: 10
          failureThreshold: 3

        readinessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3

        startupProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 0
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 30

        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
          capabilities:
            drop:
            - ALL

      volumes:
      - name: medical-images
        persistentVolumeClaim:
          claimName: medical-images-pvc
      - name: analysis-results
        persistentVolumeClaim:
          claimName: analysis-results-pvc
      - name: logs
        emptyDir: {}

      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - medical-imaging
              topologyKey: kubernetes.io/hostname

---
apiVersion: v1
kind: Service
metadata:
  name: medical-imaging
  namespace: medical-imaging
  labels:
    app: medical-imaging
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 80
    targetPort: 8080
    protocol: TCP
  - name: metrics
    port: 9090
    targetPort: 9090
    protocol: TCP
  selector:
    app: medical-imaging
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10800

---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: medical-imaging-hpa
  namespace: medical-imaging
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: medical-imaging
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Percent
        value: 50
        periodSeconds: 60
    scaleUp:
      stabilizationWindowSeconds: 0
      policies:
      - type: Percent
        value: 100
        periodSeconds: 15
      - type: Pods
        value: 2
        periodSeconds: 15
      selectPolicy: Max

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: medical-imaging-ingress
  namespace: medical-imaging
  annotations:
    kubernetes.io/ingress.class: "nginx"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/proxy-body-size: "100m"
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "30"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "30"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "30"
spec:
  tls:
  - hosts:
    - medical-imaging.example.com
    secretName: medical-imaging-tls
  rules:
  - host: medical-imaging.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: medical-imaging
            port:
              number: 80

---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: medical-imaging-pdb
  namespace: medical-imaging
spec:
  minAvailable: 2
  selector:
    matchLabels:
      app: medical-imaging

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: medical-imaging
  namespace: medical-imaging

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: medical-imaging-role
  namespace: medical-imaging
rules:
- apiGroups: [""]
  resources: ["configmaps", "secrets"]
  verbs: ["get", "list", "watch"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: medical-imaging-rolebinding
  namespace: medical-imaging
subjects:
- kind: ServiceAccount
  name: medical-imaging
  namespace: medical-imaging
roleRef:
  kind: Role
  name: medical-imaging-role
  apiGroup: rbac.authorization.k8s.io
