# Tenuo Control Plane Kubernetes Deployment
#
# This deploys the central control plane service that issues warrants.
# Should run in a secure namespace with restricted access.
#
# Prerequisites:
# 1. Create the root keypair:
#    $ tenuo keygen -o keypair.json
#
# 2. Create the Kubernetes secret:
#    $ kubectl create secret generic tenuo-root-key \
#        --from-literal=secret_key=$(jq -r .secret_key keypair.json)
#
# 3. Create the ConfigMap with the public key (for data planes):
#    $ kubectl create configmap tenuo-public-keys \
#        --from-literal=control_plane=$(jq -r .public_key keypair.json)

apiVersion: v1
kind: Namespace
metadata:
  name: tenuo-system
  labels:
    app.kubernetes.io/name: tenuo
    app.kubernetes.io/component: control-plane

---
apiVersion: v1
kind: Secret
metadata:
  name: tenuo-root-key
  namespace: tenuo-system
type: Opaque
# Create this secret manually with your generated keypair:
# kubectl create secret generic tenuo-root-key --from-literal=secret_key=<hex>
data: {}

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: tenuo-config
  namespace: tenuo-system
data:
  # Public keys that data planes should trust
  # Add the control plane's public key here after generating
  trusted_keys: ""

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tenuo-control
  namespace: tenuo-system
  labels:
    app.kubernetes.io/name: tenuo
    app.kubernetes.io/component: control-plane
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: tenuo
      app.kubernetes.io/component: control-plane
  template:
    metadata:
      labels:
        app.kubernetes.io/name: tenuo
        app.kubernetes.io/component: control-plane
    spec:
      serviceAccountName: tenuo-control
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000
        fsGroup: 1000
      containers:
      - name: control
        image: tenuo/control:latest
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 8080
          protocol: TCP
        env:
        - name: TENUO_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: tenuo-root-key
              key: secret_key
        - name: TENUO_BIND_ADDR
          value: "0.0.0.0:8080"
        resources:
          requests:
            memory: "64Mi"
            cpu: "100m"
          limits:
            memory: "128Mi"
            cpu: "500m"
        livenessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 5
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 2
          periodSeconds: 5
        securityContext:
          readOnlyRootFilesystem: true
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - ALL

---
apiVersion: v1
kind: Service
metadata:
  name: tenuo-control
  namespace: tenuo-system
  labels:
    app.kubernetes.io/name: tenuo
    app.kubernetes.io/component: control-plane
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: http
    protocol: TCP
    name: http
  selector:
    app.kubernetes.io/name: tenuo
    app.kubernetes.io/component: control-plane

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: tenuo-control
  namespace: tenuo-system

---
# Network policy: Only allow access from specific namespaces
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: tenuo-control-access
  namespace: tenuo-system
spec:
  podSelector:
    matchLabels:
      app.kubernetes.io/component: control-plane
  policyTypes:
  - Ingress
  ingress:
  - from:
    # Allow from pods with the tenuo-client label
    - namespaceSelector: {}
      podSelector:
        matchLabels:
          tenuo.io/client: "true"
    ports:
    - protocol: TCP
      port: 8080

