# https://github.com/ivanmorenoj/k8s-wireguard
apiVersion: v1
kind: Namespace
metadata:
  name: wireguard
  labels:
    name: wireguard
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-wireguard
  namespace: wireguard
spec:
  storageClassName: "nfs-client"
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100M
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: wireguard-configmap
  namespace: wireguard
data:
  PUID: "1000"
  PGID: "1000"
  TZ: "Australia/Melbourne"
  SERVERPORT: "31820"
  PEERS: "5"
  PEERDNS: "10.96.0.10" #  get dns ip, kubectl -n kube-system get svc | grep kube-dns | awk '{print $3}'
  ALLOWEDIPS: "0.0.0.0/0, ::/0"
  INTERNAL_SUBNET: "10.13.13.0"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wireguard
  namespace: wireguard
  labels:
    app: wireguard
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wireguard
  template:
    metadata:
      labels:
        app: wireguard
    spec:
      imagePullSecrets:
        - name: regcred
      containers:
      - name: wireguard
        image: ghcr.io/linuxserver/wireguard
        envFrom:
        - configMapRef:
            name: wireguard-configmap
        securityContext:
          capabilities:
            add:
              - NET_ADMIN
              - SYS_MODULE
          privileged: true
        volumeMounts:
          - name: wg-config
            mountPath: /config
          - name: host-volumes
            mountPath: /lib/modules
        ports:
        - containerPort: 51820
          protocol: UDP
        resources:
          requests:
            memory: "64Mi"
            cpu: "100m"
          limits:
            memory: "128Mi"
            cpu: "200m"
      volumes:
        - name: wg-config
          persistentVolumeClaim:
            claimName: pv-claim-wireguard
        - name: host-volumes
          hostPath:
            path: /lib/modules
            type: Directory
---
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: wireguard
  name: wireguard-service
  namespace: wireguard
spec:
  type: NodePort
  ports:
  - port: 51820
    nodePort: 31820
    protocol: UDP
    targetPort: 51820
  selector:
    app: wireguard
