# Yera Infrastructure MVP Helm Chart

This Helm chart deploys the Yera Infrastructure MVP services to Kubernetes.

## Services

The chart deploys three microservices:

1. **API Server** (port 8989) - Handles computational graph deployment and execution
2. **IAM Server** (port 8990) - Identity and Access Management/RBAC service
3. **Stream Server** (port 8991) - Server-Sent Events (SSE) streaming service

## Prerequisites

- Kubernetes cluster (minikube, kind, or cloud provider)
- Helm 3.x installed
- Docker or Podman installed
- kubectl configured to access your cluster

### Podman-Specific Requirements (macOS)

Podman machine must be initialised with sufficient memory (8GB recommended for building llama-cpp-python):

```bash
podman machine init --memory 8192
```
  
Podman machine must be running before deployment:

```
podman machine start
```

After deployment, you can stop the machine to free resources:

```
podman machine stop
```

## Quick Start

Use the provided deployment script for automated deployment:
```bash
# With Docker (default)
cd kubernetes/infra-mvp
./deploy.sh

# With Podman
./deploy.sh podman

# Skip build step (if image already exists)
./deploy.sh --skip-build

# Skip image loading (for cloud deployments)
./deploy.sh --skip-load
```

The script will:
1. Build the container image
2. Load it into your cluster (if using minikube/kind)
3. Deploy the Helm chart
4. Wait for deployments to be ready
5. Display access instructions

## Manual Deployment

### Building the Container Image

Build with Docker:
```bash
# From the project root
docker build -t yera-infra-mvp:latest .
```

Or with Podman:
```bash
# From the project root
podman build -t yera-infra-mvp:latest .
```

### Loading Image into Local Cluster

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

# Podman
podman save yera-infra-mvp:latest -o /tmp/yera-infra-mvp.tar
minikube image load /tmp/yera-infra-mvp.tar
rm /tmp/yera-infra-mvp.tar
```

For kind:
```bash
# Docker
kind load docker-image yera-infra-mvp:latest

# Podman
podman save yera-infra-mvp:latest -o /tmp/yera-infra-mvp.tar
kind load image-archive /tmp/yera-infra-mvp.tar
rm /tmp/yera-infra-mvp.tar
```

### Installing the Chart

Install all services:
```bash
helm install infra-mvp ./kubernetes/infra-mvp --create-namespace --namespace yera
```

Install with custom values:
```bash
helm install infra-mvp ./kubernetes/infra-mvp \
  --create-namespace \
  --namespace yera \
  --set api.replicaCount=2 \
  --set iam.persistence.size=10Gi
```

Install only specific services:
```bash
# Install only API and IAM services
helm install infra-mvp ./kubernetes/infra-mvp \
  --create-namespace \
  --namespace yera \
  --set stream.enabled=false
```

## Configuration

### Key Configuration Options

| Parameter | Description | Default |
|-----------|-------------|---------|
| `api.enabled` | Enable API server | `true` |
| `api.replicaCount` | Number of API server replicas | `1` |
| `api.service.port` | API server service port | `8989` |
| `iam.enabled` | Enable IAM server | `true` |
| `iam.replicaCount` | Number of IAM server replicas | `1` |
| `iam.service.port` | IAM server service port | `8990` |
| `iam.persistence.enabled` | Enable persistent storage for IAM | `true` |
| `iam.persistence.size` | IAM persistent volume size | `5Gi` |
| `stream.enabled` | Enable Stream server | `true` |
| `stream.replicaCount` | Number of Stream server replicas | `1` |
| `stream.service.port` | Stream server service port | `8991` |

### Custom Values File

Create a `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
```

Install with custom values:
```bash
helm install infra-mvp ./kubernetes/infra-mvp \
  --create-namespace \
  --namespace yera \
  -f custom-values.yaml
```

## Accessing the Services

### Port Forwarding (for local development)
```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 access:
- API: http://localhost:8989
- IAM: http://localhost:8990
- Stream: http://localhost:8991

### Service URLs (within cluster)

Services can be accessed by other pods using these DNS names:
- API: `infra-mvp-api.yera.svc.cluster.local:8989`
- IAM: `infra-mvp-iam.yera.svc.cluster.local:8990`
- Stream: `infra-mvp-stream.yera.svc.cluster.local:8991`

## Health Checks

All services expose a `/health` endpoint:
```bash
# Check API health
curl http://localhost:8989/health

# Check IAM health
curl http://localhost:8990/health

# Check Stream health
curl http://localhost:8991/health
```

## Upgrading
```bash
helm upgrade infra-mvp ./kubernetes/infra-mvp --namespace yera
```

Or use the deployment script:
```bash
./deploy.sh  # Rebuilds and upgrades
```

## Uninstalling
```bash
helm uninstall infra-mvp --namespace yera
```

To also delete the namespace:
```bash
kubectl delete namespace yera
```

## Troubleshooting

### Check pod status
```bash
kubectl get pods -n yera
```

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

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

# Stream Server logs
kubectl logs -n yera -l service=stream

# Follow logs in real-time
kubectl logs -n yera -l service=api -f
```

### Describe resources
```bash
kubectl describe deployment -n yera infra-mvp-api
kubectl describe service -n yera infra-mvp-api
kubectl describe pvc -n yera infra-mvp-iam-pvc
```

### Common Issues

1. **ImagePullBackOff**: Ensure the container image is built and available
   - Check if image exists: `docker images | grep yera-infra-mvp` or `podman images | grep yera-infra-mvp`
   - For minikube: `minikube image ls | grep yera-infra-mvp`
   - Rebuild and reload: `./deploy.sh` (or `./deploy.sh podman`)

2. **CrashLoopBackOff**: Check container logs for errors
```bash
   kubectl logs -n yera <pod-name>
   kubectl describe pod -n yera <pod-name>
```

3. **PVC Pending**: Check storage class availability
```bash
   kubectl get storageclass
```

4. **Service Unreachable**: Verify service endpoints
```bash
   kubectl get endpoints -n yera
   kubectl get svc -n yera
```

5. **Container Runtime Issues**:
   - Verify Docker is running: `docker ps`
   - Verify Podman is running: `podman ps`
   - Check deployment script permissions: `chmod +x deploy.sh`

## Development Workflow

### Rebuild and Redeploy

After code changes:
```bash
# With Docker (default)
./deploy.sh

# With Podman
./deploy.sh podman
```

### Rapid Testing (skip build)

If image hasn't changed:
```bash
./deploy.sh --skip-build
```

### Debug a Service
```bash
# Get shell in running pod
kubectl exec -it -n yera <pod-name> -- /bin/bash

# Check environment variables
kubectl exec -n yera <pod-name> -- env

# Restart a deployment
kubectl rollout restart deployment/infra-mvp-api -n yera
```

## Notes

- The chart uses a single container image that runs different services based on the `SERVICE_TYPE` environment variable
- The `docker-entrypoint.sh` script handles service routing
- IAM service uses SQLite by default with persistent storage
- All services run with CORS enabled for development purposes
- Both Docker and Podman are fully supported via the deployment script