---
apiVersion: v1
kind: ConfigMap
metadata:
  name: custom-route-cni
  namespace: openshift-multus
data:
  custom-route: |
    #!/bin/bash
    # -------------------------------------
    # -- Custom Route Chained CNI Plugin --
    # -------------------------------------
    # This adds a route to a pod
    # for 169.254.169.5 via the gw for eth0

    # Error in a CNI specific way, additionally log it with systemd-cat
    cni_error() {
      echo "custom-route CNI ERROR: $1 -- $2" | systemd-cat -p err -t custom-cni-gatewayroute
      local msg="$1"
      local details="$2"
      local code="${3:-100}"

      # Output error in CNI spec format to stderr
      echo "{\"cniVersion\": \"0.3.1\", \"code\": $code, \"msg\": \"$msg\", \"details\": \"$details\"}" >&2

      # Exit non-zero
      exit $code
    }

    stdin=$(cat /dev/stdin)

    if [ -z "$CNI_NETNS" ]; then
      cni_error "CNI_NETNS is not set" "No network namespace provided" 3
    fi

    # get with eth0 inside the network namespace
    GW=$(nsenter --net="$CNI_NETNS" -- ip route show dev eth0 | grep -m 1 -oP '(?<=via )\S+')

    if [ -z "$GW" ]; then
      cni_error "Could not find gateway for eth0" "Network namespace: $CNI_NETNS" 11
    fi

    # Add the route inside the network namespace
    nsenter --net="$CNI_NETNS" -- ip route add 169.254.169.5 via "$GW" dev eth0 || cni_error "Failed to add route" "Gateway: $GW, Network namespace: $CNI_NETNS" 11

    echo "$stdin"

    exit 0

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: install-custom-route-cni
  namespace: openshift-multus
  annotations:
    kubernetes.io/description: |
      This daemon set installs the custom route CNI plugin on each node.
spec:
  selector:
    matchLabels:
      name: install-custom-route-cni
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 10%
  template:
    metadata:
      annotations:
        target.workload.openshift.io/management: '{"effect": "PreferredDuringScheduling"}'
        cluster-autoscaler.kubernetes.io/enable-ds-eviction: "false"
      labels:
        name: install-custom-route-cni
        component: network
        type: infra
        openshift.io/component: network
    spec:
      nodeSelector:
        kubernetes.io/os: linux
      priorityClassName: "system-node-critical"
      tolerations:
      - operator: Exists
      containers:
      - name: install-custom-route-cni
        image: registry.access.redhat.com/ubi8/ubi
        command:
        - /bin/bash
        - -c
        - |
          set -e
          TARGET_PATH="/host/cni/bin/custom-route"
          TEMP_PATH="/host/cni/bin/custom-route.tmp"

          # temp file for atomic copy
          cp /custom-route-cni/custom-route $TEMP_PATH
          mv $TEMP_PATH $TARGET_PATH
          chmod +x $TARGET_PATH
          sleep infinity
        volumeMounts:
        - name: cni-bin
          mountPath: /host/cni/bin
        - name: custom-cni-script
          mountPath: /custom-route-cni
        securityContext:
          privileged: true
      volumes:
      - name: cni-bin
        hostPath:
          path: /var/lib/cni/bin
      - name: custom-cni-script
        configMap:
          name: custom-route-cni
          items:
            - key: custom-route
              path: custom-route
