apiVersion: v1
kind: Namespace
metadata:
  name: swarmcare-continuous-learning
  labels:
    name: swarmcare-continuous-learning
    phase: "22"

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: continuous-learning-config
  namespace: swarmcare-continuous-learning
data:
  MODEL_TYPE: "diagnosis_predictor"
  PRIVACY_EPSILON: "1.0"
  ENABLE_FEDERATED: "true"
  LOG_LEVEL: "INFO"

---
apiVersion: v1
kind: Secret
metadata:
  name: continuous-learning-secrets
  namespace: swarmcare-continuous-learning
type: Opaque
stringData:
  # Add secrets here (encryption keys, API keys, etc.)
  ENCRYPTION_KEY: "CHANGE_ME_IN_PRODUCTION"

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: continuous-learning-deployment
  namespace: swarmcare-continuous-learning
  labels:
    app: continuous-learning
    phase: "22"
    version: "1.0"
spec:
  replicas: 3
  revisionHistoryLimit: 3
  selector:
    matchLabels:
      app: continuous-learning
  template:
    metadata:
      labels:
        app: continuous-learning
        phase: "22"
        version: "1.0"
    spec:
      serviceAccountName: continuous-learning-sa
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000
        fsGroup: 1000
      containers:
      - name: continuous-learning
        image: swarmcare/continuous-learning:1.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: http
          protocol: TCP
        envFrom:
        - configMapRef:
            name: continuous-learning-config
        - secretRef:
            name: continuous-learning-secrets
        resources:
          requests:
            cpu: 500m
            memory: 1Gi
          limits:
            cpu: 2000m
            memory: 4Gi
        livenessProbe:
          exec:
            command:
            - python3
            - verify_phase22.py
            - --quick
          initialDelaySeconds: 30
          periodSeconds: 30
          timeoutSeconds: 10
          failureThreshold: 3
        readinessProbe:
          exec:
            command:
            - python3
            - -c
            - "from code.continuous_learning_system import ContinuousLearningSystem; ContinuousLearningSystem(ModelType.DIAGNOSIS_PREDICTOR, enable_federated=False)"
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
        volumeMounts:
        - name: model-data
          mountPath: /app/models
        - name: audit-logs
          mountPath: /app/logs
        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: false
          capabilities:
            drop:
            - ALL
      volumes:
      - name: model-data
        persistentVolumeClaim:
          claimName: model-data-pvc
      - name: audit-logs
        persistentVolumeClaim:
          claimName: audit-logs-pvc

---
apiVersion: v1
kind: Service
metadata:
  name: continuous-learning-service
  namespace: swarmcare-continuous-learning
  labels:
    app: continuous-learning
spec:
  type: LoadBalancer
  selector:
    app: continuous-learning
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
    name: http
  sessionAffinity: ClientIP

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: continuous-learning-sa
  namespace: swarmcare-continuous-learning

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: model-data-pvc
  namespace: swarmcare-continuous-learning
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
  storageClassName: standard

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: audit-logs-pvc
  namespace: swarmcare-continuous-learning
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: standard

---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: continuous-learning-hpa
  namespace: swarmcare-continuous-learning
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: continuous-learning-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80

---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: continuous-learning-pdb
  namespace: swarmcare-continuous-learning
spec:
  minAvailable: 1
  selector:
    matchLabels:
      app: continuous-learning

---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: continuous-learning-network-policy
  namespace: swarmcare-continuous-learning
spec:
  podSelector:
    matchLabels:
      app: continuous-learning
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          name: swarmcare-ingress
    ports:
    - protocol: TCP
      port: 8080
  egress:
  - to:
    - namespaceSelector: {}
    ports:
    - protocol: TCP
      port: 443
