apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    k8s-app: kube-router
    tier: node
  name: kube-router
  namespace: kube-system
spec:
  selector:
    matchLabels:
      k8s-app: kube-router
      tier: node
  template:
    metadata:
      labels:
        k8s-app: kube-router
        tier: node
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "20245"
    spec:
      priorityClassName: system-node-critical
      serviceAccountName: kube-router
      containers:
      - name: kube-router
        image: ${kube_router_image}
        imagePullPolicy: Always
        args:
        - "--run-router=true"
        - "--run-firewall=true"
        - "--bgp-graceful-restart=true"
        - "--kubeconfig=/var/lib/kube-router/kubeconfig"
        - "--metrics-port=20245"
        - '--nodeport-bindon-all-ip'
        - "${kube_router_proxy}"
        ports:
        - name: metrics
          containerPort: 20245
          protocol: TCP
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: KUBE_ROUTER_CNI_CONF_FILE
          value: /etc/cni/net.d/10-kuberouter.conflist
        livenessProbe:
          httpGet:
            path: /healthz
            port: 20244
          initialDelaySeconds: 10
          periodSeconds: 3
        resources:
          requests:
            cpu: 250m
            memory: 250Mi
        securityContext:
          privileged: true
        volumeMounts:
        - name: lib-modules
          mountPath: /lib/modules
          readOnly: true
        - name: cni-conf-dir
          mountPath: /etc/cni/net.d
        - name: kubeconfig
          mountPath: /var/lib/kube-router
          readOnly: true
        - name: xtables-lock
          mountPath: /run/xtables.lock
          readOnly: false
      initContainers:
      - name: install-cni
        image: ${kube_router_cni_image}
        imagePullPolicy: Always
        command:
        - /bin/sh
        - -c
        - set -e -x;
          if [ ! -f /etc/cni/net.d/10-kuberouter.conflist ]; then
            if [ -f /etc/cni/net.d/*.conf ]; then
              rm -f /etc/cni/net.d/*.conf;
            fi;
            if [ -f /etc/cni/net.d/*.conflist ]; then
              rm -f /etc/cni/net.d/*.conflist;
            fi;
            TMP=/etc/cni/net.d/.tmp-kuberouter-cfg;
            cp /etc/kube-router/cni-conf.json $${TMP};
            mv $${TMP} /etc/cni/net.d/10-kuberouter.conflist;
          fi;
          if [ ! -f /var/lib/kube-router/kubeconfig ]; then
            TMP=/var/lib/kube-router/.tmp-kubeconfig;
            cp /etc/kube-router/kubeconfig $${TMP};
            mv $${TMP} /var/lib/kube-router/kubeconfig;
          fi;
          if [ ! -f /opt/cni/bin/bridge ]; then
            apk add gcc build-base ;
            go install --ldflags "-linkmode external -extldflags -static" github.com/containernetworking/plugins/plugins/ipam/host-local@v1.0.0 ;
            go install --ldflags "-linkmode external -extldflags -static" github.com/containernetworking/plugins/plugins/main/bridge@v1.0.0 ;
            go install --ldflags "-linkmode external -extldflags -static" github.com/containernetworking/plugins/plugins/main/loopback@v1.0.0 ;
            mv /go/bin/bridge /opt/cni/bin/ ;
            mv /go/bin/host-local /opt/cni/bin/ ;
            mv /go/bin/loopback /opt/cni/bin/ ;
          fi
        volumeMounts:
        - mountPath: /etc/cni/net.d
          name: cni-conf-dir
        - mountPath: /opt/cni/bin
          name: cni-bin-dir
        - mountPath: /etc/kube-router
          name: kube-router-cfg
        - name: kubeconfig
          mountPath: /var/lib/kube-router
      hostNetwork: true
      tolerations:
      - effect: NoSchedule
        operator: Exists
      - key: CriticalAddonsOnly
        operator: Exists
      - effect: NoExecute
        operator: Exists
      volumes:
      - name: lib-modules
        hostPath:
          path: /lib/modules
      - name: cni-conf-dir
        hostPath:
          path: /etc/cni/net.d
      - name: cni-bin-dir
        hostPath:
          path: /opt/cni/bin
      - name: kube-router-cfg
        configMap:
          name: kube-router-cfg
      - name: kubeconfig
        hostPath:
          path: /var/lib/kube-router
      - name: xtables-lock
        hostPath:
          path: /run/xtables.lock
          type: FileOrCreate
