- name: 创建相关目录
  file: name={{ item }} state=directory
  with_items:
  - /etc/cni/net.d
  - /opt/kube/images
  - /opt/kube/kube-ovn

- name: 配置 crd.yaml 文件
  template: src=crd.yaml.j2 dest=/opt/kube/kube-ovn/crd.yaml

- name: 配置 kube-ovn.yaml 文件
  template: src=kube-ovn.yaml.j2 dest=/opt/kube/kube-ovn/kube-ovn.yaml

- name: 配置 ovn.yaml 文件
  template: src=ovn.yaml.j2 dest=/opt/kube/kube-ovn/ovn.yaml

- name: 配置 kubectl plugin
  template: src=kubectl-ko.j2 dest=/usr/local/bin/kubectl-ko mode=0755

# 【可选】推送离线镜像，可以忽略执行错误
- block:
    - name: 检查是否已下载离线kube_ovn镜像
      command: "ls {{ base_dir }}/down"
      register: download_info
      connection: local
      run_once: true
    
    - name: 尝试推送离线docker 镜像（若执行失败，可忽略）
      copy: src={{ base_dir }}/down/{{ item }} dest=/opt/kube/images/{{ item }}
      when: 'item in download_info.stdout'
      with_items:
      - "pause.tar"
      - "{{ kube_ovn_offline }}"
      ignore_errors: true

    - name: 获取kube_ovn离线镜像推送情况
      command: "ls /opt/kube/images"
      register: image_info

    # 如果目录下有离线镜像，就把它导入到node节点上
    - name: 导入 kube_ovn的离线镜像（若执行失败，可忽略）
      shell: "{{ bin_dir }}/docker load -i /opt/kube/images/{{ item }}"
      with_items:
      - "pause.tar"
      - "{{ kube_ovn_offline }}"
      ignore_errors: true
      when: "item in image_info.stdout and CONTAINER_RUNTIME == 'docker'"

    - name: 导入 kube_ovn的离线镜像（若执行失败，可忽略）
      shell: "{{ bin_dir }}/ctr -n=k8s.io images import /opt/kube/images/{{ item }}"
      with_items:
      - "pause.tar"
      - "{{ kube_ovn_offline }}"
      ignore_errors: true
      when: "item in image_info.stdout and CONTAINER_RUNTIME == 'containerd'"

# 只需单节点执行一次
- name: 运行 kube-ovn网络
  shell: "{{ bin_dir }}/kubectl label node {{ OVN_DB_NODE }} kube-ovn/role=master --overwrite && \
    {{ bin_dir }}/kubectl apply -f /opt/kube/kube-ovn/crd.yaml && sleep 5 && \
	{{ bin_dir }}/kubectl apply -f /opt/kube/kube-ovn/ovn.yaml && sleep 5 && \
	{{ bin_dir }}/kubectl apply -f /opt/kube/kube-ovn/kube-ovn.yaml"
  run_once: true

# 删除原有cni配置
- name: 删除默认cni配置
  file: path=/etc/cni/net.d/10-default.conf state=absent

# 等待网络插件部署成功，视下载镜像速度而定
- name: 轮询等待kube-ovn 运行，视下载镜像速度而定
  shell: "{{ bin_dir }}/kubectl get pod -n kube-ovn -o wide|grep 'kube-ovn-cni'|grep ' {{ inventory_hostname }} '|awk '{print $3}'"
  register: pod_status
  until: pod_status.stdout == "Running"
  retries: 15
  delay: 8
  ignore_errors: true
