---
# Copyright 2017-present Open Networking Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
kind: ConfigMap
apiVersion: v1
metadata:
  name: multus-scripts
  namespace: kube-system
data:
  install-certs.sh: |
    # Copied from Calico
    # https://github.com/projectcalico/cni-plugin/blob/master/k8s-install/scripts/install-cni.sh
    touch /host/etc/cni/net.d/multus-kubeconfig
    chmod 600 /host/etc/cni/net.d/multus-kubeconfig
    SERVICE_ACCOUNT_PATH=/var/run/secrets/kubernetes.io/serviceaccount
    KUBE_CA_FILE=$SERVICE_ACCOUNT_PATH/ca.crt
    TLS_CFG="certificate-authority-data: $(cat $KUBE_CA_FILE | base64 | tr -d '\n')"
    SERVICEACCOUNT_TOKEN=$(cat $SERVICE_ACCOUNT_PATH/token)
    cat > /host/etc/cni/net.d/multus-kubeconfig <<EOF
    # Kubeconfig file for Multus CNI plugin.
    apiVersion: v1
    kind: Config
    clusters:
    - name: local
      cluster:
        server: https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}
        $TLS_CFG
    users:
    - name: multus
      user:
        token: "${SERVICEACCOUNT_TOKEN}"
    contexts:
    - name: multus-context
      context:
        cluster: local
        user: multus
    current-context: multus-context
    EOF
  install-multus-conf.sh: |
    # If multus config is already exist on nodes (e.g. from kubespray installation)
    # Then we don't need to create one by the script.
    if [ -f /host/etc/cni/net.d/00-multus.conf ]; then
      exit 0;
    fi

    # Select the mainly CNI plugin's configuration and insert into multus's config
    MASTER_PLUGIN="$(ls /host/etc/cni/net.d | grep -E '\.conf(list)?$' | grep -v 'multus' | head -1)"
    MASTER_PLUGIN_JSON="$(cat /host/etc/cni/net.d/$MASTER_PLUGIN)"
    cat > /host/etc/cni/net.d/00-multus.conf <<EOF
    {
      "name": "multus-cni-network",
      "type": "multus",
      "logFile": "/var/log/multus.log",
      "logLevel": "debug",
      "kubeconfig": "/etc/cni/net.d/multus-kubeconfig",
      "delegates": [
                   $MASTER_PLUGIN_JSON
      ]
    }
    EOF
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: sriov-config
  namespace: kube-system
data:
  config.json: |
    {
      "resourceList":
      [
        {
          "resourceName": "sriov",
          "rootDevices": ["04:00.0"],
          "sriovMode": true,
          "deviceType": "netdevice"
        }
      ]
    }
