---
- name: "Check_certs | check if the certs have already been generated on first master"
  find:
    paths: "{{ kube_cert_dir }}"
    patterns: "*.pem"
    get_checksum: true
  delegate_to: "{{groups['kube-master'][0]}}"
  register: kubecert_master
  run_once: true

- name: "Check_certs | Set default value for 'sync_certs', 'gen_certs', and 'secret_changed'  to false"
  set_fact:
    sync_certs: false
    gen_certs: false
    secret_changed: false

- name: "Check_certs | Set 'gen_certs' to true"
  set_fact:
    gen_certs: true
  when: "not item in kubecert_master.files|map(attribute='path') | list"
  run_once: true
  with_items: >-
       ['{{ kube_cert_dir }}/ca.pem',
       '{{ kube_cert_dir }}/apiserver.pem',
       '{{ kube_cert_dir }}/apiserver-key.pem',
       '{{ kube_cert_dir }}/kube-scheduler.pem',
       '{{ kube_cert_dir }}/kube-scheduler-key.pem',
       '{{ kube_cert_dir }}/kube-controller-manager.pem',
       '{{ kube_cert_dir }}/kube-controller-manager-key.pem',
       '{{ kube_cert_dir }}/front-proxy-ca.pem',
       '{{ kube_cert_dir }}/front-proxy-ca-key.pem',
       '{{ kube_cert_dir }}/front-proxy-client.pem',
       '{{ kube_cert_dir }}/front-proxy-client-key.pem',
       '{{ kube_cert_dir }}/service-account-key.pem',
       {% for host in groups['kube-master'] %}
       '{{ kube_cert_dir }}/admin-{{ host }}.pem'
       '{{ kube_cert_dir }}/admin-{{ host }}-key.pem'
       {% if not loop.last %}{{','}}{% endif %}
       {% endfor %}]
       {% for host in groups['k8s-cluster'] %}
       '{{ kube_cert_dir }}/node-{{ host }}.pem'
       '{{ kube_cert_dir }}/node-{{ host }}-key.pem'
       '{{ kube_cert_dir }}/kube-proxy-{{ host }}.pem'
       '{{ kube_cert_dir }}/kube-proxy-{{ host }}-key.pem'
       {% if not loop.last %}{{','}}{% endif %}
       {% endfor %}]

- name: "Check_certs | Set 'gen_master_certs' to true"
  set_fact:
    gen_master_certs: |-
      {%- set gen = False -%}
      {% set existing_certs = kubecert_master.files|map(attribute='path')|list|sort %}
      {% for cert in ['apiserver.pem', 'apiserver-key.pem',
                      'kube-scheduler.pem','kube-scheduler-key.pem',
                      'kube-controller-manager.pem','kube-controller-manager-key.pem',
                      'front-proxy-ca.pem','front-proxy-ca-key.pem',
                      'front-proxy-client.pem','front-proxy-client-key.pem',
                      'service-account-key.pem'] -%}
        {% set cert_file = "%s/%s.pem"|format(kube_cert_dir, cert) %}
        {% if not cert_file in existing_certs -%}
        {%- set gen = True -%}
        {% endif -%}
      {% endfor %}
      {{ gen }}
  run_once: true

- name: "Check_certs | Set 'gen_node_certs' to true"
  set_fact:
    gen_node_certs: |-
      {
      {% set existing_certs = kubecert_master.files|map(attribute='path')|list|sort %}
      {% for host in groups['k8s-cluster'] -%}
        {% set host_cert = "%s/node-%s-key.pem"|format(kube_cert_dir, host) %}
        {% set kube_proxy_cert = "%s/kube-proxy-%s-key.pem"|format(kube_cert_dir, host) %}
        {% if host_cert in existing_certs and kube_proxy_cert in existing_certs -%}
        "{{ host }}": False,
        {% else -%}
        "{{ host }}": True,
        {% endif -%}
      {% endfor %}
      }
  run_once: true
