---
# 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: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: pon0-plugin
rules:
  - apiGroups:
      - ""
    resources:
      - pods
    verbs:
      - get
      - update
      - patch
  - apiGroups:
      - "alpha.network.k8s.io"
    resources:
      - logicalnetworks
    verbs:
      - get
      - update
      - patch
  - apiGroups:
      - "alpha.network.k8s.io"
    resources:
      - physicalnetworks
    verbs:
      - get
      - update
      - patch
  - apiGroups:
      - ""
    resources:
      - configmaps
    verbs:
      - get
  - apiGroups:
      - "admissionregistration.k8s.io"
    resources:
      - validatingwebhookconfigurations
    verbs:
      - get
      - update
      - create
      - delete

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: pon0-plugin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: pon0-plugin
subjects:
- kind: ServiceAccount
  name: pon0-plugin
  namespace: kube-system
- kind: Group
  name: system:authenticated
  apiGroup: rbac.authorization.k8s.io

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: pon0-plugin
  namespace: kube-system

---
# This ConfigMap can be used to configure a self-hosted Pon0 installation.
kind: ConfigMap
apiVersion: v1
metadata:
  name: pon0-config
  namespace: kube-system
data:
  # The CNI network configuration to install on each node.
  cni_pon0_network_config: |-
    {
        "name": "pon0",
        "type": "bridge",
        "bridge": "pon0",
        "isGateway": false,
        "ipMask": false,
        "ipam": {
        "type": "host-local",
        "subnet": "10.22.0.0/16"
      }
    }

---
# This ConfigMap can be used to configure a self-hosted Pon0 installation.
kind: ConfigMap
apiVersion: v1
metadata:
  name: pon1-config
  namespace: kube-system
data:
  # The CNI network configuration to install on each node.
  cni_pon1_network_config: |-
    {
        "name": "pon1",
        "type": "bridge",
        "bridge": "pon1",
        "isGateway": true,
        "ipMask": true,
        "ipam": {
        "type": "host-local",
        "subnet": "10.23.0.0/16",
        "routes": [
            { "dst": "0.0.0.0/0" }
        ]
      }
    }

---
# Install Pon0 CNI conf on each slave node.
kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
  name: pon0-plugin
  namespace: kube-system
  labels:
    k8s-app: pon0
spec:
  selector:
    matchLabels:
      k8s-app: pon0
  template:
    metadata:
      labels:
        k8s-app: pon0
      annotations:
        scheduler.alpha.kubernetes.io/critical-pod: ''
        scheduler.alpha.kubernetes.io/tolerations: |
          [
            {
              "key": "dedicated",
              "value": "master",
              "effect": "NoSchedule"
            },
            {
              "key": "CriticalAddonsOnly",
              "operator": "Exists"
            }
          ]
    spec:
      hostNetwork: true
      hostPID: true
      serviceAccountName: pon0-plugin
      containers:
        # Create a container with place_conf.sh that
        # Installs required 20-pon0.conf on slave node.
        - name: install-cni
          image: {{ .Values.pull_docker_registry }}{{ .Values.images.pon0.repository }}:{{ .Values.images.pon0.tag }} 
          imagePullPolicy: {{ .Values.images.pon0.pullPolicy }} 
          command: ["/place_conf.sh"]
          env:
            - name: CNI_NETWORK_CONFIG
              valueFrom:
                configMapKeyRef:
                  name: pon0-config
                  key: cni_pon0_network_config
            - name: CNI_PON1_NETWORK_CONFIG
              valueFrom:
                configMapKeyRef:
                  name: pon1-config
                  key: cni_pon1_network_config
            - name: KUBERNETES_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
          volumeMounts:
            - mountPath: /host/opt/cni/bin
              name: cni-bin-dir
            - mountPath: /host/etc/cni/net.d
              name: cni-net-dir
      volumes:
        - name: cni-bin-dir
          hostPath:
            path: /opt/cni/bin
        - name: cni-net-dir
          hostPath:
            path: /etc/cni/net.d
