- include: gcr_proxy.yml
  when:
  - custom_registry is defined
  - custom_registry_gcr | default(false)

- name: install s3-curl depencendies
  yum:
    name: "{{ item }}"
    state: present
    disable_gpg_check: yes
  with_items:
  - perl-Digest-HMAC
  - unzip

- name: get s3curl
  unarchive:
    src: http://s3.amazonaws.com/doc/s3-example-code/s3-curl.zip
    dest: /home/vagrant/
    remote_src: yes

- name: make s3curl executable
  file:
    path: /home/vagrant/s3-curl/s3curl.pl
    state: file
    mode: "a+x"

- name: check for kubelet config
  stat:
    path: /etc/kubernetes/kubelet.conf
  register: kubelet

- block:
  - name: get parameters of kubeadm init
    command: kubeadm init --help
    register: kubeadm_init_help

  - name: kubeadm needs deprecated --skip-preflight-checks
    set_fact:
      kubeadm_preflight: '--skip-preflight-checks'
    when: kubeadm_init_help.stdout.find('--skip-preflight-checks') != -1

  - name: kubeadm init
    command: kubeadm init {{ kubeadm_preflight | default('--ignore-preflight-errors=all') }} --token={{ kubernetes_token }} --kubernetes-version=v{{ kube_ver.stdout }}  --apiserver-advertise-address={{ ansible_eth1.ipv4.address }}

  - name: create root kube dir
    file:
      path: /root/.kube
      state: directory
      owner: root
      group: root

  - name: create root kube config
    copy:
      src: /etc/kubernetes/admin.conf
      dest: /root/.kube/config
      remote_src: True
      owner: root
      group: root

  - name: create user kube dir
    file:
      path: /home/vagrant/.kube
      state: directory
      owner: vagrant
      group: vagrant

  - name: create user kube config
    copy:
      src: /etc/kubernetes/admin.conf
      dest: /home/vagrant/.kube/config
      remote_src: True
      owner: vagrant
      group: vagrant
  when: not kubelet.stat.exists

- name: wait for k8s to be ready
  wait_for:
    host: localhost
    port: 6443
    state: started
    timeout: 60

- name: get k8s server and client versions
  command: kubectl version
  register: kubectl_version

- name: create weave network
  command: kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version={{ kubectl_version.stdout | b64encode }}"

# jsonpath tested with kubeadm 1.6, 1.7, 1.8 
- name: get dns service address
  command: kubectl get services --all-namespaces -ojsonpath='{.items[?(@.metadata.name=="kube-dns")].spec.clusterIP}'
  register: kubednsaddress

- name: wait for dns to be ready
  wait_for: host={{ kubednsaddress.stdout }} port=53 state=started timeout=1800
