<commit_before>---
- name: Calico | Install Wireguard packages
  package:
    name: "{{ item }}"
    state: present
  with_items: "{{ calico_wireguard_packages }}"
  register: calico_package_install
  until: calico_package_install is succeeded
  retries: 4
  when: calico_wireguard_enabled

- name: Calico | Copy calicoctl binary from download dir
  copy:
    src: "{{ local_release_dir }}/calicoctl"
    dest: "{{ bin_dir }}/calicoctl"
    mode: 0755
    remote_src: yes

- name: Calico | Write Calico cni config
  template:
    src: "cni-calico.conflist.j2"
    dest: "/etc/cni/net.d/calico.conflist.template"
    owner: kube
  register: calico_conflist
  notify: reset_calico_cni

- name: Calico | Create calico certs directory
  file:
    dest: "{{ calico_cert_dir }}"
    state: directory
    mode: 0750
    owner: root
    group: root
  when: calico_datastore == "etcd"

- name: Calico | Link etcd certificates for calico-node
  file:
    src: "{{ etcd_cert_dir }}/{{ item.s }}"
    dest: "{{ calico_cert_dir }}/{{ item.d }}"
    state: hard
    mode: 0640
    force: yes
  with_items:
    - {s: "{{ kube_etcd_cacert_file }}", d: "ca_cert.crt"}
    - {s: "{{ kube_etcd_cert_file }}", d: "cert.crt"}
    - {s: "{{ kube_etcd_key_file }}", d: "key.pem"}
  when: calico_datastore == "etcd"

- name: Calico | Generate typha certs
  include_tasks: typha_certs.yml
  when:
    - typha_secure
    - inventory_hostname == groups['kube_control_plane'][0]

- name: Calico | Install calicoctl wrapper script
  template:
    src: "calicoctl.{{ calico_datastore }}.sh.j2"
    dest: "{{ bin_dir }}/calicoctl.sh"
    mode: 0755
    owner: root
    group: root

- name: Calico | wait for etcd
  uri:
    url: "{{ etcd_access_addresses.split(',') | first }}/health"
    validate_certs: no
    client_cert: "{{ calico_cert_dir }}/cert.crt"
    client_key: "{{ calico_cert_dir }}/key.pem"
  register: result
  until: result.status == 200 or result.status == 401
  retries: 10
  delay: 5
  run_once: true
  when: calico_datastore == "etcd"

- name: Calico | Check if calico network pool has already been configured
  # noqa 306 - grep will exit 1 if no match found
  shell: >
    {{ bin_dir }}/calicoctl.sh get ippool | grep -w "{{ calico_pool_cidr | default(kube_pods_subnet) }}" | wc -l
  args:
    executable: /bin/bash
  register: calico_conf
  retries: 4
  until: calico_conf.rc == 0
  delay: "{{ retry_stagger | random + 3 }}"
  changed_when: false
  when:
    - inventory_hostname == groups['kube_control_plane'][0]

- name: Calico | Ensure that calico_pool_cidr is within kube_pods_subnet when defined
  assert:
    that: "[calico_pool_cidr] | ipaddr(kube_pods_subnet) | length == 1"
    msg: "{{ calico_pool_cidr }} is not within or equal to {{ kube_pods_subnet }}"
  when:
    - inventory_hostname == groups['kube_control_plane'][0]
    - 'calico_conf.stdout == "0"'
    - calico_pool_cidr is defined

- name: Calico | Check if calico IPv6 network pool has already been configured
  # noqa 306 - grep will exit 1 if no match found
  shell: >
    {{ bin_dir }}/calicoctl.sh get ippool | grep -w "{{ calico_pool_cidr_ipv6 | default(kube_pods_subnet_ipv6) }}" | wc -l
  args:
    executable: /bin/bash
  register: calico_conf_ipv6
  retries: 4
  until: calico_conf_ipv6.rc == 0
  delay: "{{ retry_stagger | random + 3 }}"
  changed_when: false
  when:
    - inventory_hostname == groups['kube_control_plane'][0]
    - enable_dual_stack_networks

- name: Calico | Ensure that calico_pool_cidr_ipv6 is within kube_pods_subnet_ipv6 when defined
  assert:
    that: "[calico_pool_cidr_ipv6] | ipaddr(kube_pods_subnet_ipv6) | length == 1"
    msg: "{{ calico_pool_cidr_ipv6 }} is not within or equal to {{ kube_pods_subnet_ipv6 }}"
  when:
    - inventory_hostname == groups['kube_control_plane'][0]
    - calico_conf_ipv6.stdout is defined and calico_conf_ipv6.stdout == "0"
    - calico_pool_cidr_ipv6 is defined
    - enable_dual_stack_networks

- block:
    - name: Calico | Create calico manifests for kdd
      assemble:
        src: "{{ local_release_dir }}/calico-{{ calico_version }}-kdd-crds"
        dest: "{{ kube_config_dir }}/kdd-crds.yml"
        delimiter: "---\n"
        regexp: ".*\\.yaml"
        remote_src: true

    - name: Calico | Create Calico Kubernetes datastore resources
      kube:
        kubectl: "{{ bin_dir }}/kubectl"
        filename: "{{ kube_config_dir }}/kdd-crds.yml"
        state: "latest"
      when:
        - inventory_hostname == groups['kube_control_plane'][0]
  when:
    - inventory_hostname in groups['kube_control_plane']
    - calico_datastore == "kdd"

- name: Calico | Configure calico FelixConfiguration
  command:
    cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
    stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  vars:
    stdin: >
      { "kind": "FelixConfiguration",
        "apiVersion": "projectcalico.org/v3",
        "metadata": {
          "name": "default",
        },
        "spec": {
          "ipipEnabled": {{ calico_ipip_mode != 'Never' }},
          "reportingInterval": "{{ calico_felix_reporting_interval }}",
          "bpfLogLevel": "{{ calico_bpf_log_level }}",
          "bpfEnabled": {{ calico_bpf_enabled | bool }},
          "bpfExternalServiceMode": "{{ calico_bpf_service_mode }}",
          "wireguardEnabled": {{ calico_wireguard_enabled | bool }},
          "logSeverityScreen": "{{ calico_felix_log_severity_screen }}" }}
  when:
    - inventory_hostname == groups['kube_control_plane'][0]

- name: Calico | Configure calico network pool
  command:
    cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
    stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  vars:
    stdin: >
      { "kind": "IPPool",
        "apiVersion": "projectcalico.org/v3",
        "metadata": {
          "name": "{{ calico_pool_name }}",
        },
        "spec": {
          "blockSize": {{ calico_pool_blocksize | default(kube_network_node_prefix) }},
          "cidr": "{{ calico_pool_cidr | default(kube_pods_subnet) }}",
          "ipipMode": "{{ calico_ipip_mode }}",
          "vxlanMode": "{{ calico_vxlan_mode }}",
          "natOutgoing": {{ nat_outgoing|default(false) and not peer_with_router|default(false) }} }}
  when:
    - inventory_hostname == groups['kube_control_plane'][0]
    - 'calico_conf.stdout == "0"'

- name: Calico | Configure calico ipv6 network pool (version >= v3.3.0)
  command:
    cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
    stdin: >
      { "kind": "IPPool",
        "apiVersion": "projectcalico.org/v3",
        "metadata": {
          "name": "{{ calico_pool_name }}-ipv6",
        },
        "spec": {
          "blockSize": {{ calico_pool_blocksize_ipv6 | default(kube_network_node_prefix_ipv6) }},
          "cidr": "{{ calico_pool_cidr_ipv6 | default(kube_pods_subnet_ipv6) }}",
          "ipipMode": "{{ calico_ipip_mode_ipv6 }}",
          "vxlanMode": "{{ calico_vxlan_mode_ipv6 }}",
          "natOutgoing": {{ nat_outgoing_ipv6|default(false) and not peer_with_router_ipv6|default(false) }} }}
  when:
    - inventory_hostname == groups['kube_control_plane'][0]
    - calico_conf_ipv6.stdout is defined and calico_conf_ipv6.stdout == "0"
    - enable_dual_stack_networks | bool

- name: Populate Service External IPs
  set_fact:
    _service_external_ips: "{{ _service_external_ips|default([]) + [ {'cidr': item} ] }}"
  with_items: "{{ calico_advertise_service_external_ips }}"
  run_once: yes

- name: Populate Service LoadBalancer IPs
  set_fact:
    _service_loadbalancer_ips: "{{ _service_loadbalancer_ips|default([]) + [ {'cidr': item} ] }}"
  with_items: "{{ calico_advertise_service_loadbalancer_ips }}"
  run_once: yes

- name: "Determine nodeToNodeMesh needed state"
  set_fact:
    nodeToNodeMeshEnabled: "false"
  when:
    - peer_with_router|default(false) or peer_with_calico_rr|default(false)
    - inventory_hostname in groups['k8s_cluster']
  run_once: yes

- name: Calico | Set up BGP Configuration
  command:
    cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
    stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  vars:
    stdin: >
      { "kind": "BGPConfiguration",
      "apiVersion": "projectcalico.org/v3",
      "metadata": {
          "name": "default",
      },
      "spec": {
          "listenPort": {{ calico_bgp_listen_port }},
          "logSeverityScreen": "Info",
          {% if not calico_no_global_as_num|default(false) %}"asNumber": {{ global_as_num }},{% endif %}
          "nodeToNodeMeshEnabled": {{ nodeToNodeMeshEnabled|default('true') }} ,
          {% if calico_advertise_cluster_ips|default(false) %}"serviceClusterIPs": [{"cidr": "{{ kube_service_addresses }}" }],{% endif %}
          {% if calico_version is version('v3.18.0', '>') and calico_advertise_service_loadbalancer_ips|length > 0  %}"serviceLoadBalancerIPs": {{ _service_loadbalancer_ips }},{% endif %}
          "serviceExternalIPs": {{ _service_external_ips|default([]) }} }}
  changed_when: false
  when:
    - inventory_hostname == groups['kube_control_plane'][0]

- name: Calico | Configure peering with router(s) at global scope
  command:
    cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
    stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  vars:
    stdin: >
      {"apiVersion": "projectcalico.org/v3",
      "kind": "BGPPeer",
      "metadata": {
        "name": "global-{{ item.name | default(item.router_id|replace(':','-')) }}"
      },
      "spec": {
        "asNumber": "{{ item.as }}",
        "peerIP": "{{ item.router_id }}"
      }}
  register: output
  retries: 4
  until: output.rc == 0
  delay: "{{ retry_stagger | random + 3 }}"
  with_items:<commit_msg>Calico: Add kube_service_addresses_ipv6 to serviceClusterIPs (#7889) (#7944)<commit_after>---
- name: Calico | Install Wireguard packages
  package:
    name: "{{ item }}"
    state: present
  with_items: "{{ calico_wireguard_packages }}"
  register: calico_package_install
  until: calico_package_install is succeeded
  retries: 4
  when: calico_wireguard_enabled

- name: Calico | Copy calicoctl binary from download dir
  copy:
    src: "{{ local_release_dir }}/calicoctl"
    dest: "{{ bin_dir }}/calicoctl"
    mode: 0755
    remote_src: yes

- name: Calico | Write Calico cni config
  template:
    src: "cni-calico.conflist.j2"
    dest: "/etc/cni/net.d/calico.conflist.template"
    owner: kube
  register: calico_conflist
  notify: reset_calico_cni

- name: Calico | Create calico certs directory
  file:
    dest: "{{ calico_cert_dir }}"
    state: directory
    mode: 0750
    owner: root
    group: root
  when: calico_datastore == "etcd"

- name: Calico | Link etcd certificates for calico-node
  file:
    src: "{{ etcd_cert_dir }}/{{ item.s }}"
    dest: "{{ calico_cert_dir }}/{{ item.d }}"
    state: hard
    mode: 0640
    force: yes
  with_items:
    - {s: "{{ kube_etcd_cacert_file }}", d: "ca_cert.crt"}
    - {s: "{{ kube_etcd_cert_file }}", d: "cert.crt"}
    - {s: "{{ kube_etcd_key_file }}", d: "key.pem"}
  when: calico_datastore == "etcd"

- name: Calico | Generate typha certs
  include_tasks: typha_certs.yml
  when:
    - typha_secure
    - inventory_hostname == groups['kube_control_plane'][0]

- name: Calico | Install calicoctl wrapper script
  template:
    src: "calicoctl.{{ calico_datastore }}.sh.j2"
    dest: "{{ bin_dir }}/calicoctl.sh"
    mode: 0755
    owner: root
    group: root

- name: Calico | wait for etcd
  uri:
    url: "{{ etcd_access_addresses.split(',') | first }}/health"
    validate_certs: no
    client_cert: "{{ calico_cert_dir }}/cert.crt"
    client_key: "{{ calico_cert_dir }}/key.pem"
  register: result
  until: result.status == 200 or result.status == 401
  retries: 10
  delay: 5
  run_once: true
  when: calico_datastore == "etcd"

- name: Calico | Check if calico network pool has already been configured
  # noqa 306 - grep will exit 1 if no match found
  shell: >
    {{ bin_dir }}/calicoctl.sh get ippool | grep -w "{{ calico_pool_cidr | default(kube_pods_subnet) }}" | wc -l
  args:
    executable: /bin/bash
  register: calico_conf
  retries: 4
  until: calico_conf.rc == 0
  delay: "{{ retry_stagger | random + 3 }}"
  changed_when: false
  when:
    - inventory_hostname == groups['kube_control_plane'][0]

- name: Calico | Ensure that calico_pool_cidr is within kube_pods_subnet when defined
  assert:
    that: "[calico_pool_cidr] | ipaddr(kube_pods_subnet) | length == 1"
    msg: "{{ calico_pool_cidr }} is not within or equal to {{ kube_pods_subnet }}"
  when:
    - inventory_hostname == groups['kube_control_plane'][0]
    - 'calico_conf.stdout == "0"'
    - calico_pool_cidr is defined

- name: Calico | Check if calico IPv6 network pool has already been configured
  # noqa 306 - grep will exit 1 if no match found
  shell: >
    {{ bin_dir }}/calicoctl.sh get ippool | grep -w "{{ calico_pool_cidr_ipv6 | default(kube_pods_subnet_ipv6) }}" | wc -l
  args:
    executable: /bin/bash
  register: calico_conf_ipv6
  retries: 4
  until: calico_conf_ipv6.rc == 0
  delay: "{{ retry_stagger | random + 3 }}"
  changed_when: false
  when:
    - inventory_hostname == groups['kube_control_plane'][0]
    - enable_dual_stack_networks

- name: Calico | Ensure that calico_pool_cidr_ipv6 is within kube_pods_subnet_ipv6 when defined
  assert:
    that: "[calico_pool_cidr_ipv6] | ipaddr(kube_pods_subnet_ipv6) | length == 1"
    msg: "{{ calico_pool_cidr_ipv6 }} is not within or equal to {{ kube_pods_subnet_ipv6 }}"
  when:
    - inventory_hostname == groups['kube_control_plane'][0]
    - calico_conf_ipv6.stdout is defined and calico_conf_ipv6.stdout == "0"
    - calico_pool_cidr_ipv6 is defined
    - enable_dual_stack_networks

- block:
    - name: Calico | Create calico manifests for kdd
      assemble:
        src: "{{ local_release_dir }}/calico-{{ calico_version }}-kdd-crds"
        dest: "{{ kube_config_dir }}/kdd-crds.yml"
        delimiter: "---\n"
        regexp: ".*\\.yaml"
        remote_src: true

    - name: Calico | Create Calico Kubernetes datastore resources
      kube:
        kubectl: "{{ bin_dir }}/kubectl"
        filename: "{{ kube_config_dir }}/kdd-crds.yml"
        state: "latest"
      when:
        - inventory_hostname == groups['kube_control_plane'][0]
  when:
    - inventory_hostname in groups['kube_control_plane']
    - calico_datastore == "kdd"

- name: Calico | Configure calico FelixConfiguration
  command:
    cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
    stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  vars:
    stdin: >
      { "kind": "FelixConfiguration",
        "apiVersion": "projectcalico.org/v3",
        "metadata": {
          "name": "default",
        },
        "spec": {
          "ipipEnabled": {{ calico_ipip_mode != 'Never' }},
          "reportingInterval": "{{ calico_felix_reporting_interval }}",
          "bpfLogLevel": "{{ calico_bpf_log_level }}",
          "bpfEnabled": {{ calico_bpf_enabled | bool }},
          "bpfExternalServiceMode": "{{ calico_bpf_service_mode }}",
          "wireguardEnabled": {{ calico_wireguard_enabled | bool }},
          "logSeverityScreen": "{{ calico_felix_log_severity_screen }}" }}
  when:
    - inventory_hostname == groups['kube_control_plane'][0]

- name: Calico | Configure calico network pool
  command:
    cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
    stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  vars:
    stdin: >
      { "kind": "IPPool",
        "apiVersion": "projectcalico.org/v3",
        "metadata": {
          "name": "{{ calico_pool_name }}",
        },
        "spec": {
          "blockSize": {{ calico_pool_blocksize | default(kube_network_node_prefix) }},
          "cidr": "{{ calico_pool_cidr | default(kube_pods_subnet) }}",
          "ipipMode": "{{ calico_ipip_mode }}",
          "vxlanMode": "{{ calico_vxlan_mode }}",
          "natOutgoing": {{ nat_outgoing|default(false) and not peer_with_router|default(false) }} }}
  when:
    - inventory_hostname == groups['kube_control_plane'][0]
    - 'calico_conf.stdout == "0"'

- name: Calico | Configure calico ipv6 network pool (version >= v3.3.0)
  command:
    cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
    stdin: >
      { "kind": "IPPool",
        "apiVersion": "projectcalico.org/v3",
        "metadata": {
          "name": "{{ calico_pool_name }}-ipv6",
        },
        "spec": {
          "blockSize": {{ calico_pool_blocksize_ipv6 | default(kube_network_node_prefix_ipv6) }},
          "cidr": "{{ calico_pool_cidr_ipv6 | default(kube_pods_subnet_ipv6) }}",
          "ipipMode": "{{ calico_ipip_mode_ipv6 }}",
          "vxlanMode": "{{ calico_vxlan_mode_ipv6 }}",
          "natOutgoing": {{ nat_outgoing_ipv6|default(false) and not peer_with_router_ipv6|default(false) }} }}
  when:
    - inventory_hostname == groups['kube_control_plane'][0]
    - calico_conf_ipv6.stdout is defined and calico_conf_ipv6.stdout == "0"
    - enable_dual_stack_networks | bool

- name: Populate Service External IPs
  set_fact:
    _service_external_ips: "{{ _service_external_ips|default([]) + [ {'cidr': item} ] }}"
  with_items: "{{ calico_advertise_service_external_ips }}"
  run_once: yes

- name: Populate Service LoadBalancer IPs
  set_fact:
    _service_loadbalancer_ips: "{{ _service_loadbalancer_ips|default([]) + [ {'cidr': item} ] }}"
  with_items: "{{ calico_advertise_service_loadbalancer_ips }}"
  run_once: yes

- name: "Determine nodeToNodeMesh needed state"
  set_fact:
    nodeToNodeMeshEnabled: "false"
  when:
    - peer_with_router|default(false) or peer_with_calico_rr|default(false)
    - inventory_hostname in groups['k8s_cluster']
  run_once: yes

- name: Calico | Set up BGP Configuration
  command:
    cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
    stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  vars:
    stdin: >
      { "kind": "BGPConfiguration",
      "apiVersion": "projectcalico.org/v3",
      "metadata": {
          "name": "default",
      },
      "spec": {
          "listenPort": {{ calico_bgp_listen_port }},
          "logSeverityScreen": "Info",
          {% if not calico_no_global_as_num|default(false) %}"asNumber": {{ global_as_num }},{% endif %}
          "nodeToNodeMeshEnabled": {{ nodeToNodeMeshEnabled|default('true') }} ,
          {% if calico_advertise_cluster_ips|default(false) %}
          "serviceClusterIPs": [{"cidr": "{{ kube_service_addresses }}" } {{ ',{"cidr":"' + kube_service_addresses_ipv6 + '"}' if enable_dual_stack_networks else '' }}],{% endif %}
          {% if calico_version is version('v3.18.0', '>') and calico_advertise_service_loadbalancer_ips|length > 0  %}"serviceLoadBalancerIPs": {{ _service_loadbalancer_ips }},{% endif %}
          "serviceExternalIPs": {{ _service_external_ips|default([]) }} }}
  changed_when: false
  when:
    - inventory_hostname == groups['kube_control_plane'][0]

- name: Calico | Configure peering with router(s) at global scope
  command:
    cmd: "{{ bin_dir }}/calicoctl.sh apply -f -"
    stdin: "{{ stdin is string | ternary(stdin, stdin|to_json) }}"
  vars:
    stdin: >
      {"apiVersion": "projectcalico.org/v3",
      "kind": "BGPPeer",
      "metadata": {
        "name": "global-{{ item.name | default(item.router_id|replace(':','-')) }}"
      },
      "spec": {
        "asNumber": "{{ item.as }}",
        "peerIP": "{{ item.router_id }}"
      }}
  register: output
  retries: 4
  until: output.rc == 0
  delay: "{{ retry_stagger | random + 3 }}"
  with_items:
{'fraction_non_alphanumeric': 0.14368642761738415, 'fraction_numerical': 0.01089078393095654, 'mean_word_length': 3.2945069490403704, 'pattern_counts': {'":': 105, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5904161', 'n_tokens_mistral': 7154, 'n_tokens_neox': 7110, 'n_words': 1424}