apiVersion: apps/v1
kind: Deployment
metadata:
  name: pihole
  labels:
    app: pihole
spec:
  revisionHistoryLimit: 2
  replicas: 1
  selector:
    matchLabels:
      app: pihole
  template:
    metadata:
      labels:
        app: pihole
        name: pihole
    spec:
      nodeSelector:
         gateway: "true"
      tolerations:
        - effect: NoSchedule
          operator: Exists
      imagePullSecrets:
        - name: regcred
      containers:
        - name: pihole
          image: pihole/pihole:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
              name: pihole-http
              protocol: TCP
            - containerPort: 53
              name: dns
              protocol: TCP
            - containerPort: 53
              name: dns-udp
              protocol: UDP
            - containerPort: 443
              name: pihole-ssl
              protocol: TCP
            - containerPort: 67
              name: client-udp
              protocol: UDP
          env:
            - name: TZ
              value: "Australia/Melbourne"
            - name: WEBPASSWORD
              valueFrom:
                secretKeyRef:
                  name: pihole-admin-password
                  key: webpassword
            - name: DNS1
              value: '1.1.1.1'
            - name: DNSMASQ_USER
              value: 'root'
            - name: DNS2
              value: '8.8.8.8'
            - name: VIRTUAL_HOST
              value: 'pihole.junv.me'
          volumeMounts:
            - name: configs
              mountPath: "/etc/pihole"
              subPath: pihole
            - name: configs
              mountPath: "/etc/dnsmasq.d"
              subPath: dnsmasq
          resources:
            requests:
              cpu: 100m
              memory: 200Mi
            limits:
              cpu: 200m
              memory: 500Mi
      volumes:
      - name: configs
        persistentVolumeClaim:
          claimName: configs-pvc

---

apiVersion: v1
kind: Service
metadata:
  name: pihole-dns
  labels:
    name: pihole-dns
spec:
  type: NodePort
  externalIPs:
    - 192.168.1.2
  selector:
    app: pihole
  externalTrafficPolicy: Local
  ports:
  - name: dns
    port: 53
    protocol: UDP
  - name: dnstcp
    port: 53
    protocol: TCP

---

apiVersion: v1
kind: Service
metadata:
  name: pihole-web
  labels:
    name: pihole-web
spec:
  type: ClusterIP
  selector:
    app: pihole
  ports:
    - name: pihole
      targetPort: pihole-http
      port: 80
      protocol: TCP

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: pihole-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    kubernetes.io/tls-acme: "true"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/auth-url: "https://id.junv.me/oauth2/auth"
    nginx.ingress.kubernetes.io/auth-signin: "https://id.junv.me/oauth2/start?rd=https%3A%2F%2F$host$request_uri"
spec:
  tls:
  - hosts:
    - pihole.junv.me
    secretName: pihole-tls
  rules:
  - host: pihole.junv.me
    http:
      paths:
      - backend:
          service:
            name: pihole-web
            port:
              number: 80
        path: /
        pathType: Prefix
