apiVersion: apps/v1
kind: Deployment
metadata:
  name: netbox
  namespace: netbox-community
  labels:
    k8s-app: netbox
spec:
  replicas: 3
  selector:
    matchLabels:
      k8s-app: netbox
  template:
    metadata:
      labels:
        k8s-app: netbox
    spec:
      containers:
      # NetBox container using the netbox-docker image: https://github.com/netbox-community/netbox-docker
      - name: netbox
        image: quay.io/netboxcommunity/netbox:v2.11.12-ldap
        ports:
        - name: http
          containerPort: 8080
        readinessProbe: # will mark the pod as ready once the initialization script is completed and will probe every 20 seconds
          httpGet:
            path: /
            port: http
          initialDelaySeconds: 5
          periodSeconds: 20
        envFrom:
          - configMapRef:
              name: netbox-configmap
        lifecycle: # Will run the post-startup scripts detailed in netbox-startup-configmap.yaml
          postStart:
            exec:
              command:
                - /bin/sh
                - -c
                - >
                  bash /home/install-plugins.sh &&
                  bash /home/start-rqworker.sh &&
                  bash /home/nginx-caching-fix.sh
        volumeMounts:
        # Mounting secrets for netbox and other related components
        - name: auth-ldap-bind-password
          mountPath: /run/secrets/auth_ldap_bind_password
          subPath: auth_ldap_bind_password
          readOnly: true
        - name: db-password
          mountPath: /run/secrets/db_password
          subPath: db_password
          readOnly: true
        - name: email-password
          mountPath: /run/secrets/email_password
          subPath: email_password
          readOnly: true
        - name: napalm-password
          mountPath: /run/secrets/napalm_password
          subPath: napalm_password
          readOnly: true
        - name: redis-password
          mountPath: /run/secrets/redis_password
          subPath: redis_password
        - name: secret-key
          mountPath: /run/secrets/secret_key
          subPath: secret_key
          readOnly: true
        - name: superuser-api-token
          mountPath: /run/secrets/superuser_api_token
          subPath: superuser_api_token
        - name: superuser-password
          mountPath: /run/secrets/superuser_password
          subPath: superuser_password
          readOnly: true
        # Persistent Volume paths
        - name: netbox-media-files # Allows for multiple replicas of the netbox pod to be available
          mountPath: /opt/netbox/netbox/media
        - name: ldap-config # Allows for LDAP authentication to work
          subPath: ldap_config.py
          mountPath: /opt/netbox/netbox/netbox/ldap_config.py
        - mountPath: /etc/netbox/config/netbox-plugins.py
          name: netbox-plugins
          subPath: netbox-plugins.py
        - mountPath: /home/install-plugins.sh
          name: install-plugins
          subPath: install-plugins.sh
        - mountPath: /home/start-rqworker.sh
          name: start-rqworker
          subPath: start-rqworker.sh
        - mountPath: /home/nginx-caching-fix.sh
          name: nginx-caching-fix
          subPath: nginx-caching-fix.sh
        - name: sso-saml2-xml
          mountPath: /opt/netbox/sso-saml2.xml
          subPath: sso-saml2-metadata.xml
      restartPolicy: Always
      volumes:
      # Reference to configmaps and PVs
      - name: netbox-media-files
        persistentVolumeClaim:
          claimName: netbox-media-pvc
          readOnly: false
      - name: ldap-config
        configMap:
          name: netbox-configmap
          items:
            - key: ldap_config.py
              path: ldap_config.py
      - name: netbox-plugins
        configMap:
          name: startup-configmap
          items:
          - key: "netbox-plugins.py"
            path: "netbox-plugins.py"
      - name: install-plugins
        configMap:
          name: startup-configmap
          items:
          - key: "install-plugins.sh"
            path: "install-plugins.sh"
      - name: start-rqworker
        configMap:
          name: startup-configmap
          items:
          - key: "start-rqworker.sh"
            path: "start-rqworker.sh"
      - name: nginx-caching-fix
        configMap:
          name: startup-configmap
          items:
          - key: "nginx-caching-fix.sh"
            path: "nginx-caching-fix.sh"
      - name: sso-saml2-xml
        configMap:
          name: sso-saml2-xml
          items:
            - key: sso-saml2-metadata.xml
              path: sso-saml2-metadata.xml
      # Populate via secret
      - name: auth-ldap-bind-password
        secret:
          secretName: netbox-secret
          items:
          - key: auth_ldap_bind_password
            path: auth_ldap_bind_password
      - name: db-password
        secret:
          secretName: netbox-postgresql
          items:
          - key: password
            path: db_password
      - name: email-password
        secret:
          secretName: netbox-secret
          items:
          - key: email_password
            path: email_password
      - name: napalm-password
        secret:
          secretName: netbox-secret
          items:
          - key: napalm_password
            path: napalm_password
      - name: redis-password
        secret:
          secretName: netbox-redis
          items:
          - key: redis-password
            path: redis_password
      - name: secret-key
        secret:
          secretName: netbox-secret
          items:
          - key: secret_key
            path: secret_key
      - name: superuser-password
        secret:
          secretName: netbox-secret
          items:
          - key: superuser_password
            path: superuser_password
      - name: superuser-api-token
        secret:
          secretName: netbox-secret
          items:
          - key: superuser_api_token
            path: superuser_api_token
