# Cariflex EMS - Kubernetes Manifests

## Namespace
```yaml
apiVersion: v1
kind: Namespace
metadata:
  name: cariflex
  labels:
    app: cariflex-ems
    environment: production
```

## ConfigMap
```yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: cariflex-config
  namespace: cariflex
data:
  # FlexMeasures
  FM_ENV: "production"
  FM_LOG_LEVEL: "WARNING"
  FM_DB_HOST: "postgres-service"
  FM_REDIS_HOST: "redis-service"
  
  # OpenADR
  VTN_ID: "Cariflex-VTN"
  VEN_ID: "Cariflex-VEN"
  VTN_PORT: "8080"
  
  # CitrineOS
  CITRINEOS_ENV: "production"
  AMQP_HOST: "rabbitmq-service"
  AMQP_PORT: "5672"
  DB_HOST: "citrineos-postgres-service"
```

## Secrets
```yaml
apiVersion: v1
kind: Secret
metadata:
  name: cariflex-secrets
  namespace: cariflex
type: Opaque
stringData:
  FM_SECRET_KEY: "${FM_SECRET_KEY}"
  FM_DB_PASSWORD: "${FM_DB_PASSWORD}"
  REDIS_PASSWORD: "${REDIS_PASSWORD}"
  CITRINEOS_DB_PASSWORD: "${CITRINEOS_DB_PASSWORD}"
  RABBITMQ_PASSWORD: "${RABBITMQ_PASSWORD}"
```

## FlexMeasures Deployment
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: flexmeasures-server
  namespace: cariflex
spec:
  replicas: 2
  selector:
    matchLabels:
      app: flexmeasures-server
  template:
    metadata:
      labels:
        app: flexmeasures-server
    spec:
      containers:
      - name: flexmeasures
        image: lfenergy/flexmeasures:latest
        ports:
        - containerPort: 5000
        envFrom:
        - configMapRef:
            name: cariflex-config
        - secretRef:
            name: cariflex-secrets
        resources:
          requests:
            memory: "1Gi"
            cpu: "500m"
          limits:
            memory: "2Gi"
            cpu: "2"
        livenessProbe:
          httpGet:
            path: /api/v3_0/sensors
            port: 5000
          initialDelaySeconds: 30
          periodSeconds: 30
        readinessProbe:
          httpGet:
            path: /api/v3_0/sensors
            port: 5000
          initialDelaySeconds: 10
          periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
  name: flexmeasures-service
  namespace: cariflex
spec:
  selector:
    app: flexmeasures-server
  ports:
  - port: 5000
    targetPort: 5000
  type: ClusterIP
```

## OpenADR VTN Deployment
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: openadr-vtn
  namespace: cariflex
spec:
  replicas: 1
  selector:
    matchLabels:
      app: openadr-vtn
  template:
    metadata:
      labels:
        app: openadr-vtn
    spec:
      containers:
      - name: vtn
        image: flexmeasures-openadr-vtn:latest
        ports:
        - containerPort: 8080
        envFrom:
        - configMapRef:
            name: cariflex-config
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
  name: openadr-vtn-service
  namespace: cariflex
spec:
  selector:
    app: openadr-vtn
  ports:
  - port: 8080
    targetPort: 8080
  type: ClusterIP
```

## OpenADR VEN Deployment
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: openadr-ven
  namespace: cariflex
spec:
  replicas: 1
  selector:
    matchLabels:
      app: openadr-ven
  template:
    metadata:
      labels:
        app: openadr-ven
    spec:
      containers:
      - name: ven
        image: flexmeasures-openadr-ven:latest
        env:
        - name: VTN_URL
          value: "http://openadr-vtn-service:8080/OpenADR2/Simple/2.0b"
        - name: FM_HOST
          value: "https://cariflex.digitribe.fr"
        envFrom:
        - configMapRef:
            name: cariflex-config
        - secretRef:
            name: cariflex-secrets
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
  name: openadr-ven-service
  namespace: cariflex
spec:
  selector:
    app: openadr-ven
  ports:
  - port: 8080
  type: ClusterIP
```

## CitrineOS Deployment
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: citrineos-server
  namespace: cariflex
spec:
  replicas: 1
  selector:
    matchLabels:
      app: citrineos-server
  template:
    metadata:
      labels:
        app: citrineos-server
    spec:
      containers:
      - name: citrineos
        image: ghcr.io/citrineos/citrineos-server:latest
        ports:
        - containerPort: 8080
        envFrom:
        - configMapRef:
            name: cariflex-config
        - secretRef:
            name: cariflex-secrets
        resources:
          requests:
            memory: "512Mi"
            cpu: "250m"
          limits:
            memory: "1Gi"
            cpu: "1"
---
apiVersion: v1
kind: Service
metadata:
  name: citrineos-service
  namespace: cariflex
spec:
  selector:
    app: citrineos-server
  ports:
  - port: 8080
    targetPort: 8080
  type: ClusterIP
```

## Ingress
```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: cariflex-ingress
  namespace: cariflex
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-prod
    traefik.ingress.kubernetes.io/router.entrypoints: websecure
spec:
  tls:
  - hosts:
    - cariflex.digitribe.fr
    - grafana.digitribe.fr
    - citrineos.digitribe.fr
    secretName: cariflex-tls
  rules:
  - host: cariflex.digitribe.fr
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: flexmeasures-service
            port:
              number: 5000
  - host: grafana.digitribe.fr
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: grafana-service
            port:
              number: 3000
  - host: citrineos.digitribe.fr
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: citrineos-service
            port:
              number: 8080
```

## Deployment Commands
```bash
# Create namespace
kubectl apply -f k8s/namespace.yaml

# Apply config and secrets
kubectl apply -f k8s/configmap.yaml
kubectl apply -f k8s/secrets.yaml

# Deploy applications
kubectl apply -f k8s/deployments/flexmeasures.yaml
kubectl apply -f k8s/deployments/openadr-vtn.yaml
kubectl apply -f k8s/deployments/openadr-ven.yaml
kubectl apply -f k8s/deployments/citrineos.yaml

# Apply services
kubectl apply -f k8s/services/

# Apply ingress
kubectl apply -f k8s/ingress.yaml

# Verify
kubectl get pods -n cariflex
kubectl get svc -n cariflex
kubectl get ingress -n cariflex
```
