# Yera Infrastructure MVP - Kubernetes Deployment Guide

Quick guide for deploying the infra_mvp services to Kubernetes.

## Overview

The `infra-mvp` Helm chart deploys three microservices:
- **API Server** (port 8989) - Graph deployment and execution
- **IAM Server** (port 8990) - Identity and Access Management
- **Stream Server** (port 8991) - SSE streaming for UI

## Quick Start

### 1. Build and Deploy (All-in-One)

```bash
cd kubernetes/infra-mvp
./deploy.sh
```

This script will:
- Build the Docker image
- Load it into your cluster (minikube/kind)
- Create the namespace
- Deploy all services
- Wait for deployments to be ready

### 2. Access Services

```bash
# API Server
kubectl port-forward -n yera svc/infra-mvp-api 8989:8989

# IAM Server  
kubectl port-forward -n yera svc/infra-mvp-iam 8990:8990

# Stream Server
kubectl port-forward -n yera svc/infra-mvp-stream 8991:8991
```

Then visit:
- API: http://localhost:8989/docs
- IAM: http://localhost:8990/docs
- Stream: http://localhost:8991/docs

## Manual Deployment

### Build Docker Image

```bash
cd /Users/jh/code/yk/Yera-main
docker build -t yera-infra-mvp:latest .
```

### Load Image (for local clusters)

```bash
# Minikube
minikube image load yera-infra-mvp:latest

# Kind
kind load docker-image yera-infra-mvp:latest
```

### Deploy with Helm

```bash
cd kubernetes/infra-mvp

# Deploy all services
helm install infra-mvp . --create-namespace --namespace yera

# Deploy with custom config
helm install infra-mvp . \
  --create-namespace \
  --namespace yera \
  --set api.replicaCount=2 \
  --set iam.persistence.size=10Gi
```

## Customization

### Deploy Only Specific Services

```bash
# Only API + IAM (no Stream)
helm install infra-mvp . \
  --create-namespace \
  --namespace yera \
  --set stream.enabled=false

# Only API
helm install infra-mvp . \
  --create-namespace \
  --namespace yera \
  --set iam.enabled=false \
  --set stream.enabled=false
```

### Custom Values File

Create `custom-values.yaml`:

```yaml
api:
  replicaCount: 2
  resources:
    limits:
      cpu: 1000m
      memory: 1Gi

iam:
  persistence:
    size: 10Gi
    storageClass: "fast-ssd"
  config:
    maxUserPolicyAssignmentSize: 200

stream:
  enabled: false
```

Deploy:

```bash
helm install infra-mvp . \
  --create-namespace \
  --namespace yera \
  -f custom-values.yaml
```

## Monitoring

### Check Status

```bash
# Pods
kubectl get pods -n yera

# Services
kubectl get svc -n yera

# Deployments
kubectl get deployments -n yera
```

### View Logs

```bash
# API logs
kubectl logs -n yera -l service=api -f

# IAM logs  
kubectl logs -n yera -l service=iam -f

# Stream logs
kubectl logs -n yera -l service=stream -f
```

### Check Health

```bash
# Forward port first
kubectl port-forward -n yera svc/infra-mvp-api 8989:8989

# Check health
curl http://localhost:8989/health
```

## Troubleshooting

### Pods Not Starting

```bash
# Check pod status
kubectl describe pod -n yera <pod-name>

# Check events
kubectl get events -n yera --sort-by='.lastTimestamp'
```

### Image Pull Issues

For local development with minikube/kind, make sure to load the image:

```bash
# Build
docker build -t yera-infra-mvp:latest .

# Load (minikube)
minikube image load yera-infra-mvp:latest

# Verify
minikube image ls | grep yera
```

### Database Issues (IAM)

Check the PVC:

```bash
kubectl get pvc -n yera
kubectl describe pvc -n yera infra-mvp-iam-pvc
```

Access the pod and check the database:

```bash
kubectl exec -it -n yera <iam-pod-name> -- /bin/bash
ls -la /data/
```

## Upgrade

```bash
# Rebuild image
docker build -t yera-infra-mvp:latest .

# Reload into cluster
minikube image load yera-infra-mvp:latest  # or kind

# Upgrade deployment
helm upgrade infra-mvp ./kubernetes/infra-mvp --namespace yera

# Force pod restart
kubectl rollout restart deployment -n yera
```

## Cleanup

```bash
# Uninstall Helm release
helm uninstall infra-mvp --namespace yera

# Delete namespace (removes everything)
kubectl delete namespace yera
```

## Architecture

```
┌─────────────────────────────────────────────┐
│           Kubernetes Cluster                │
│                                             │
│  ┌──────────────────────────────────────┐  │
│  │  Namespace: yera                    │  │
│  │                                      │  │
│  │  ┌────────────┐  ┌────────────┐    │  │
│  │  │ API Server │  │ IAM Server │    │  │
│  │  │  :8989     │  │  :8990     │    │  │
│  │  │            │  │     │      │    │  │
│  │  └────────────┘  │     ├──────┼────┼──┤
│  │                  │     │ PVC  │    │  │
│  │  ┌────────────┐  └────────────┘    │  │
│  │  │   Stream   │                    │  │
│  │  │   Server   │                    │  │
│  │  │   :8991    │                    │  │
│  │  └────────────┘                    │  │
│  └──────────────────────────────────────┘  │
└─────────────────────────────────────────────┘
```

## Service Communication (within cluster)

Services can communicate using DNS:

```python
# From any pod in the cluster
import requests

# Call API
response = requests.get('http://infra-mvp-api.yera.svc.cluster.local:8989/health')

# Call IAM
response = requests.get('http://infra-mvp-iam.yera.svc.cluster.local:8990/health')

# Call Stream
response = requests.get('http://infra-mvp-stream.yera.svc.cluster.local:8991/health')
```
