# Cloud Services

Refactored cloud microservices from project-ai.

## Structure

```
cloud-services/
├── pkg/                    # Shared Go packages
│   ├── kafka/              # Pure Go Kafka client (franz-go)
│   ├── redis/              # Redis connection pool
│   ├── dbc/                # CAN database signal definitions
│   ├── can-go/             # CAN protocol library
│   └── ...                 # Other shared modules
├── services/
│   └── gateway/            # API gateway service
│       └── Dockerfile      # Service-specific Dockerfile
├── deploy/
│   ├── base/               # Base k8s manifests (configMapGenerator)
│   │   ├── kustomization.yaml
│   │   └── config.env      # Common environment config
│   └── overlays/
│       └── development/    # Dev environment overlay
│           ├── kustomization.yaml
│           ├── secrets.yaml
│           └── services/
│               └── gateway/
│                   ├── deployment.yaml
│                   ├── ingress.yaml
│                   └── external-secret.yaml
├── argocd-apps/
│   └── development/
│       └── cloud-services.yaml  # ArgoCD Application
└── scripts/                # Build and utility scripts
```

## Quick Start

```bash
# Build all
go build ./...

# Build gateway
go build ./services/gateway

# Run tests
go test ./...

# Build Docker image (arm64 for Mac Mini cluster)
docker build --platform linux/arm64 -t localhost:32000/gateway:latest -f services/gateway/Dockerfile .
```

## Services

### Gateway
WebSocket gateway for TRex, HMI, and Mobile connections. Handles auth, message routing to Kafka.

- Port 8077: HTTP/WebSocket
- Port 11011: Health check

## Development

### Prerequisites
- Go 1.25+
- Docker (for container builds)
- devbox (optional, for consistent dev environment)

### Module Structure
Uses Go workspaces (`go.work`) for local development:
- `./pkg` - shared packages
- `./pkg/can-go` - CAN protocol library  
- `./services/gateway` - gateway service

### Generating DBC Code
CAN signal definitions are generated from DBC files. See `pkg/dbc/README.md`.

```bash
./scripts/generate-dbc.sh /path/to/dbc/files
```

## Deployment

### Kustomize Structure

Uses `configMapGenerator` with hash suffixes for automatic rolling updates when config changes:

```bash
# Preview what will be deployed
kustomize build deploy/overlays/development

# Apply directly (without ArgoCD)
kustomize build deploy/overlays/development | kubectl apply -f -
```

### ArgoCD

The `argocd-apps/development/cloud-services.yaml` Application points to `deploy/overlays/development` and auto-syncs changes from git.

### Secrets

Database credentials are managed via ExternalSecrets from Vault:
- `cloud-db-credentials` - DB_PASSWORD, MONGO_USER, MONGO_PASSWORD

## Environment Variables

Configured in `deploy/base/config.env`:

| Variable | Default | Description |
|----------|---------|-------------|
| `DB_HOST` | `cloud-dev-rw.cnpg-system.svc.cluster.local` | PostgreSQL host |
| `KAFKA_HOSTS` | `cloud-dev-kafka-bootstrap.kafka.svc.cluster.local:9092` | Kafka brokers |
| `REDIS_HOST` | `cloud-dev.redis.svc.cluster.local` | Redis host |
| `REDIS_PORT` | `6379` | Redis port |
| `MONGO_HOST` | `cloud-dev-svc.mongodb.svc.cluster.local` | MongoDB host |
| `OIDC_ISSUER` | Keycloak URL | OIDC issuer for JWT validation |
| `LOG_LEVEL` | `info` | Log level |
