apiVersion: v1
kind: Namespace
metadata:
  name: nebu
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nebu-sa
  namespace: nebu
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: nebu-role
  namespace: nebu
rules:
  - apiGroups: ["batch"]
    resources: ["jobs"]
    verbs: ["create", "get", "list", "watch", "delete", "update"]
  - apiGroups: [""]
    resources: ["pods", "services", "secrets", "persistentvolumeclaims", "pods/log"]
    verbs: ["create", "get", "list", "watch", "delete", "update"]
  - apiGroups: ["apps"]
    resources: ["deployments", "deployments/status"]
    verbs: ["create", "get", "list", "watch", "delete", "update"]
  - apiGroups: ["kueue.x-k8s.io"]
    resources: ["*"]
    verbs: ["*"]
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nebu-clusterrole
rules:
  - apiGroups: ["kueue.x-k8s.io"]
    resources: ["*"]
    verbs: ["*"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: nebu-clusterrolebinding
subjects:
  - kind: ServiceAccount
    name: nebu-sa
    namespace: nebu
roleRef:
  kind: ClusterRole
  name: nebu-clusterrole
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: nebu-binding
  namespace: nebu
subjects:
  - kind: ServiceAccount
    name: nebu-sa
roleRef:
  kind: Role
  name: nebu-role
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: Service
metadata:
  name: nebu-server
  namespace: nebu
spec:
  selector:
    app: nebu-server
  ports:
    - protocol: TCP
      port: 3000
      targetPort: 3000
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: nebu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:latest
          command:
            - /bin/sh
            - -c
          args:
            - |
              exec redis-server --requirepass "$REDIS_PASSWORD"
          env:
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: redis-secret
                  key: password
          ports:
            - containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: nebu
spec:
  selector:
    app: redis
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: huggingface-cache-pvc
  namespace: nebu
spec:
  storageClassName: efs-sc
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 300Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: adapter-pvc
  namespace: nebu
spec:
  storageClassName: efs-sc
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 300Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dataset-pvc
  namespace: nebu
spec:
  storageClassName: efs-sc
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 300Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: model-pvc
  namespace: nebu
spec:
  storageClassName: efs-sc
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1000Gi
---
apiVersion: kueue.x-k8s.io/v1beta1
kind: ResourceFlavor
metadata:
  name: default-flavor
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nebu-server
  namespace: nebu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nebu-server
  template:
    metadata:
      labels:
        app: nebu-server
    spec:
      serviceAccountName: nebu-sa
      containers:
        - name: nebu-server
          image: us-docker.pkg.dev/agentsea-dev/nebulous/server:latest
          ports:
            - containerPort: 3000
          env:
            - name: DATABASE_URL
              value: postgresql://postgres:changeme@postgres.nebu.svc.cluster.local:5432/nebu
            - name: MESSAGE_QUEUE_TYPE
              value: redis
            - name: REDIS_URL
              value: redis://redis.nebu.svc.cluster.local:6379
            - name: RUST_LOG
              value: debug
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: redis-secret
                  key: password
            - name: RUNPOD_API_KEY
              valueFrom:
                secretKeyRef:
                  name: runpod-secret
                  key: RUNPOD_API_KEY
            - name: AWS_ACCESS_KEY_ID
              valueFrom:
                secretKeyRef:
                  name: aws-secret
                  key: AWS_ACCESS_KEY_ID
            - name: AWS_SECRET_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: aws-secret
                  key: AWS_SECRET_ACCESS_KEY
            - name: NEBU_ENCRYPTION_KEY
              valueFrom:
                secretKeyRef:
                  name: nebu-secret
                  key: NEBU_ENCRYPTION_KEY
          volumeMounts:
            - name: huggingface-cache
              mountPath: /huggingface
            - name: adapter-pvc
              mountPath: /adapters
            - name: dataset-pvc
              mountPath: /datasets
            - name: model-pvc
              mountPath: /models
      volumes:
        - name: huggingface-cache
          persistentVolumeClaim:
            claimName: huggingface-cache-pvc
        - name: adapter-pvc
          persistentVolumeClaim:
            claimName: adapter-pvc
        - name: dataset-pvc
          persistentVolumeClaim:
            claimName: dataset-pvc
        - name: model-pvc
          persistentVolumeClaim:
            claimName: model-pvc
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: nebu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:latest
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_DB
              value: nebu
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: username
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: password
            - name: PGDATA
              value: "/var/lib/postgresql/data/pgdata"
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: nebu
spec:
  selector:
    app: postgres
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
  type: ClusterIP
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
  namespace: nebu
spec:
  storageClassName: gp2 
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
---
apiVersion: v1
kind: Secret
metadata:
  name: postgres-secret
  namespace: nebu
type: Opaque
data:
  username: cG9zdGdyZXM=
  password: Y2hhbmdlbWU=
