apiVersion: v1
kind: Namespace
metadata:
  name: mesh-isp
  labels:
    app.kubernetes.io/part-of: community-mesh
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: gateway-config
  namespace: mesh-isp
data:
  gateway.yaml: |
    node:
      name: community-gateway-01
      region: east-district
      contact: noc@mesh.example.org

    mesh:
      interface: mesh0
      mtu: 1280
      ipv4_cidr: 10.42.0.0/16
      ipv6_prefix: fd42:6d65:7368::/48
      routing:
        protocol: babel
        hello_interval_seconds: 4
        update_interval_seconds: 16
        link_cost: rtt

    uplinks:
      - name: fiber-primary
        interface: wan0
        priority: 100
        health_check:
          target: 1.1.1.1
          interval_seconds: 10
          failure_threshold: 3
      - name: lte-backup
        interface: wan1
        priority: 50
        health_check:
          target: 9.9.9.9
          interval_seconds: 15
          failure_threshold: 4

    dns:
      listen:
        - 10.42.0.1:53
        - "[fd42:6d65:7368::1]:53"
      upstreams:
        - tls://1.1.1.1
        - tls://9.9.9.9
      cache_size: 10000
      block_private_reverse: true

    bandwidth:
      default_subscriber_down_mbps: 50
      default_subscriber_up_mbps: 20
      gateway_total_down_mbps: 1000
      gateway_total_up_mbps: 1000
      fair_queueing: cake
      per_host_fairness: true

    captive_portal:
      enabled: true
      unauthenticated_rate_limit_mbps: 2
      terms_url: https://mesh.example.org/acceptable-use
      session_duration_hours: 720

    telemetry:
      prometheus:
        enabled: true
        listen: 0.0.0.0:9100
      logs:
        format: json
        level: info
      privacy:
        retain_client_metrics_days: 7
        anonymize_client_addresses: true
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mesh-gateway
  namespace: mesh-isp
  labels:
    app: mesh-gateway
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  selector:
    matchLabels:
      app: mesh-gateway
  template:
    metadata:
      labels:
        app: mesh-gateway
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9100"
        prometheus.io/path: /metrics
    spec:
      serviceAccountName: mesh-gateway
      priorityClassName: system-cluster-critical
      containers:
        - name: gateway
          image: ghcr.io/community-mesh/gateway:v2.8.1
          imagePullPolicy: IfNotPresent
          args:
            - --config=/etc/mesh/gateway.yaml
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              add:
                - NET_ADMIN
                - NET_RAW
              drop:
                - ALL
            readOnlyRootFilesystem: true
            runAsNonRoot: true
            runAsUser: 10001
          ports:
            - name: dns-udp
              containerPort: 53
              protocol: UDP
            - name: dns-tcp
              containerPort: 53
              protocol: TCP
            - name: portal
              containerPort: 8080
              protocol: TCP
            - name: metrics
              containerPort: 9100
              protocol: TCP
          env:
            - name: NODE_ID
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: America/Toronto
          resources:
            requests:
              cpu: 250m
              memory: 256Mi
            limits:
              cpu: "2"
              memory: 1Gi
          readinessProbe:
            httpGet:
              path: /ready
              port: portal
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: portal
            initialDelaySeconds: 20
            periodSeconds: 20
          volumeMounts:
            - name: config
              mountPath: /etc/mesh
              readOnly: true
            - name: runtime
              mountPath: /run/mesh
      volumes:
        - name: config
          configMap:
            name: gateway-config
        - name: runtime
          emptyDir:
            sizeLimit: 128Mi
---
apiVersion: v1
kind: Service
metadata:
  name: mesh-gateway
  namespace: mesh-isp
  labels:
    app: mesh-gateway
spec:
  type: LoadBalancer
  externalTrafficPolicy: Local
  selector:
    app: mesh-gateway
  ports:
    - name: dns-udp
      port: 53
      targetPort: dns-udp
      protocol: UDP
    - name: dns-tcp
      port: 53
      targetPort: dns-tcp
      protocol: TCP
    - name: portal
      port: 80
      targetPort: portal
      protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: mesh-gateway-metrics
  namespace: mesh-isp
  labels:
    app: mesh-gateway
spec:
  type: ClusterIP
  selector:
    app: mesh-gateway
  ports:
    - name: metrics
      port: 9100
      targetPort: metrics
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: mesh-gateway
  namespace: mesh-isp
automountServiceAccountToken: false
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: mesh-gateway
  namespace: mesh-isp
spec:
  minAvailable: 1
  selector:
    matchLabels:
      app: mesh-gateway
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: mesh-gateway
  namespace: mesh-isp
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: mesh-gateway
  minReplicas: 2
  maxReplicas: 6
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70