# 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.
- name: Install Kubernetes dependencies
  become: yes
  apt:
    name: "{{ item }}"
    state: latest
  with_items:
    - apt-transport-https
    - jq
    
- name: Get apt signing key from Google Cloud
  become: yes
  apt_key:
    url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
    state: present

- name: Add Kubernetes apt repository
  become: yes
  apt_repository:
    repo: 'deb http://apt.kubernetes.io/ kubernetes-xenial main'
    filename: kubernetes
    state: present
    update_cache: yes

- name: Install Kubernetes
  become: yes
  apt:
    name: "{{ item }}"
    state: present
  with_items:
    - kubeadm=1.9.3-00
    - kubectl=1.9.3-00
    - kubelet=1.9.3-00
    - kubernetes-cni=0.6.0-00

- name: Initialize node as Kubernetes master
  become: yes
  command: "kubeadm init --pod-network-cidr=192.168.0.0/16 --apiserver-advertise-address=10.100.198.220"

- name: Create .kube directory under home
  become: yes
  file:
    dest: /home/ubuntu/.kube
    mode: 0755
    owner: ubuntu
    group: ubuntu
    state: directory

- name: Copy config to home directory
  become: yes
  command: "sudo cp /etc/kubernetes/admin.conf /home/ubuntu/.kube/config"

- name: Change ownership of ~/.kube/config
  become: yes
  file:
    path: /home/ubuntu/.kube/config
    mode: 0600
    owner: ubuntu
    group: ubuntu

- name: Set proxy-mode flag in kube-proxy daemonset (workaround for https://github.com/kubernetes/kubernetes/issues/34101)
  become: yes
  shell: "kubectl --kubeconfig=/etc/kubernetes/admin.conf -n kube-system get ds -l 'k8s-app==kube-proxy' -o json | jq '.items[0].spec.template.spec.containers[0].command |= .+ [\"--proxy-mode=userspace\"]' | kubectl --kubeconfig=/etc/kubernetes/admin.conf apply -f - && kubectl --kubeconfig=/etc/kubernetes/admin.conf -n kube-system delete pods -l 'k8s-app==kube-proxy'"
  register: proxy
  until: proxy.rc == 0
  retries: 60
  delay: 10

- name: Allow workloads on Kubernetes master
  become: yes
  command: "kubectl --kubeconfig=/etc/kubernetes/admin.conf taint nodes --all node-role.kubernetes.io/master-"

- name: Install pod network
  become: yes
  command: "kubectl --kubeconfig=/etc/kubernetes/admin.conf apply -f /cord/incubator/voltha/k8s/calico-1.6.yml"
