You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.0 KiB

  1. ---
  2. - hosts: kube_control_plane[0]
  3. vars:
  4. test_image_repo: registry.k8s.io/busybox
  5. test_image_tag: latest
  6. tasks:
  7. - name: Force binaries directory for Flatcar Container Linux by Kinvolk
  8. set_fact:
  9. bin_dir: "/opt/bin"
  10. when: ansible_os_family in ["Flatcar", "Flatcar Container Linux by Kinvolk"]
  11. - name: Force binaries directory for other hosts
  12. set_fact:
  13. bin_dir: "/usr/local/bin"
  14. when: not ansible_os_family in ["Flatcar", "Flatcar Container Linux by Kinvolk"]
  15. - name: Approve kubelet serving certificates
  16. block:
  17. - name: Get certificate signing requests
  18. command: "{{ bin_dir }}/kubectl get csr -o name"
  19. register: get_csr
  20. changed_when: false
  21. - name: Check there are csrs
  22. assert:
  23. that: get_csr.stdout_lines | length > 0
  24. fail_msg: kubelet_rotate_server_certificates is {{ kubelet_rotate_server_certificates }} but no csr's found
  25. - name: Approve certificates
  26. command: "{{ bin_dir }}/kubectl certificate approve {{ get_csr.stdout_lines | join(' ') }}"
  27. register: certificate_approve
  28. when: get_csr.stdout_lines | length > 0
  29. changed_when: certificate_approve.stdout
  30. - debug: # noqa unnamed-task
  31. msg: "{{ certificate_approve.stdout.split('\n') }}"
  32. when: kubelet_rotate_server_certificates | default(false)
  33. - name: Create test namespace
  34. command: "{{ bin_dir }}/kubectl create namespace test"
  35. changed_when: false
  36. - name: Wait for API token of test namespace
  37. shell: "set -o pipefail && {{ bin_dir }}/kubectl describe serviceaccounts default --namespace test | grep Tokens | awk '{print $2}'"
  38. args:
  39. executable: /bin/bash
  40. changed_when: false
  41. register: default_token
  42. until: default_token.stdout | length > 0
  43. retries: 5
  44. delay: 5
  45. - name: Run 2 busybox pods in test ns
  46. command: "{{ bin_dir }}/kubectl run {{ item }} --image={{ test_image_repo }}:{{ test_image_tag }} --namespace test --command -- tail -f /dev/null"
  47. changed_when: false
  48. loop:
  49. - busybox1
  50. - busybox2
  51. - import_role: # noqa unnamed-task
  52. name: cluster-dump
  53. - name: Check that all pods are running and ready
  54. command: "{{ bin_dir }}/kubectl get pods --namespace test --no-headers -o yaml"
  55. changed_when: false
  56. register: run_pods_log
  57. until:
  58. # Check that all pods are running
  59. - '(run_pods_log.stdout | from_yaml)["items"] | map(attribute = "status.phase") | unique | list == ["Running"]'
  60. # Check that all pods are ready
  61. - '(run_pods_log.stdout | from_yaml)["items"] | map(attribute = "status.containerStatuses") | map("map", attribute = "ready") | map("min") | min'
  62. retries: 18
  63. delay: 10
  64. failed_when: false
  65. no_log: true
  66. - name: Get pod names
  67. command: "{{ bin_dir }}/kubectl get pods -n test -o json"
  68. changed_when: false
  69. register: pods
  70. no_log: true
  71. - debug: # noqa unnamed-task
  72. msg: "{{ pods.stdout.split('\n') }}"
  73. failed_when: not run_pods_log is success
  74. - name: Get hostnet pods
  75. command: "{{ bin_dir }}/kubectl get pods -n test -o
  76. jsonpath='{range .items[?(.spec.hostNetwork)]}{.metadata.name} {.status.podIP} {.status.containerStatuses} {end}'"
  77. changed_when: false
  78. register: hostnet_pods
  79. ignore_errors: true # noqa ignore-errors
  80. no_log: true
  81. - name: Get running pods
  82. command: "{{ bin_dir }}/kubectl get pods -n test -o
  83. jsonpath='{range .items[?(.status.phase==\"Running\")]}{.metadata.name} {.status.podIP} {.status.containerStatuses} {end}'"
  84. changed_when: False
  85. register: running_pods
  86. no_log: true
  87. - name: Check kubectl output
  88. command: "{{ bin_dir }}/kubectl get pods --all-namespaces -owide"
  89. changed_when: False
  90. register: get_pods
  91. no_log: true
  92. - debug: # noqa unnamed-task
  93. msg: "{{ get_pods.stdout.split('\n') }}"
  94. - name: Set networking facts
  95. set_fact:
  96. kube_pods_subnet: 10.233.64.0/18
  97. pod_names: "{{ (pods.stdout | from_json)['items'] | map(attribute = 'metadata.name') | list }}"
  98. pod_ips: "{{ (pods.stdout | from_json)['items'] | selectattr('status.podIP', 'defined') | map(attribute = 'status.podIP') | list }}"
  99. pods_hostnet: |
  100. {% set list = hostnet_pods.stdout.split(" ") %}
  101. {{ list }}
  102. pods_running: |
  103. {% set list = running_pods.stdout.split(" ") %}
  104. {{ list }}
  105. - name: Check pods IP are in correct network
  106. assert:
  107. that: item | ipaddr(kube_pods_subnet)
  108. when:
  109. - not item in pods_hostnet
  110. - item in pods_running
  111. with_items: "{{ pod_ips }}"
  112. - name: Ping between pods is working
  113. command: "{{ bin_dir }}/kubectl -n test exec {{ item[0] }} -- ping -c 4 {{ item[1] }}"
  114. when:
  115. - not item[0] in pods_hostnet
  116. - not item[1] in pods_hostnet
  117. with_nested:
  118. - "{{ pod_names }}"
  119. - "{{ pod_ips }}"
  120. - name: Ping between hostnet pods is working
  121. command: "{{ bin_dir }}/kubectl -n test exec {{ item[0] }} -- ping -c 4 {{ item[1] }}"
  122. when:
  123. - item[0] in pods_hostnet
  124. - item[1] in pods_hostnet
  125. with_nested:
  126. - "{{ pod_names }}"
  127. - "{{ pod_ips }}"