---
- name: Kubernetes | Fail if etcd deployment type is not "internal" or "external"
  run_once: true
  assert:
    that: .cluster_require.require_etcd_deployment_type | has .etcd.deployment_type
    fail_msg: >-
      Invalid etcd deployment type: "{{ .etcd.deployment_type }}". Expected "internal" or "external".

## https://cwiki.yunify.com/pages/viewpage.action?pageId=145920824
- name: ETCD | Validate disk I/O performance for etcd
  when:
    - .groups.etcd | default list | has .inventory_hostname
  block:
    - name: ETCD | Check if fio is installed
      ignore_errors: true
      command: fio --version
      register: fio_install_version
    - name: ETCD | Run fio disk I/O test
      when: .fio_install_version.error | empty
      block:
        - name: ETCD | Execute fio and collect results
          command: |
            mkdir -p {{ .tmp_dir }}/etcd/test-data
            fio --rw=write --ioengine=sync --fdatasync=1 --directory={{ .tmp_dir }}/etcd/test-data --size=22m --bs=2300 --name=mytest --output-format=json
          register: fio_result
          register_type: json
        - name: ETCD | Assert disk fsync latency meets requirements
          assert:
            that: (index (.fio_result.stdout.jobs | first) "sync" "lat_ns" "percentile" "90.000000") | le .cluster_require.etcd_disk_wal_fysnc_duration_seconds
            fail_msg: >-
              The 90th percentile fsync latency is {{ index (.fio_result.stdout.jobs | first) "sync" "lat_ns" "percentile" "90.000000" }}ns, which exceeds the maximum allowed: {{ .cluster_require.etcd_disk_wal_fysnc_duration_seconds }}ns.
      always:
        - name: ETCD | Clean up fio test data directory
          command: rm -rf {{ .tmp_dir }}/etcd/test-data
