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.

187 lines
6.3 KiB

  1. ---
  2. - name: Testcases for network
  3. hosts: kube_control_plane[0]
  4. vars:
  5. test_image_repo: registry.k8s.io/e2e-test-images/agnhost
  6. test_image_tag: "2.40"
  7. tasks:
  8. - name: Force binaries directory for Flatcar Container Linux by Kinvolk
  9. set_fact:
  10. bin_dir: "/opt/bin"
  11. when: ansible_os_family in ["Flatcar", "Flatcar Container Linux by Kinvolk"]
  12. - name: Force binaries directory for other hosts
  13. set_fact:
  14. bin_dir: "/usr/local/bin"
  15. when: not ansible_os_family in ["Flatcar", "Flatcar Container Linux by Kinvolk"]
  16. - name: Check kubelet serving certificates approved with kubelet_csr_approver
  17. when:
  18. - kubelet_rotate_server_certificates | default(false)
  19. - kubelet_csr_approver_enabled | default(kubelet_rotate_server_certificates | default(false))
  20. vars:
  21. csrs: "{{ csr_json.stdout | from_json }}"
  22. block:
  23. - name: Get certificate signing requests
  24. command: "{{ bin_dir }}/kubectl get csr -o jsonpath-as-json={.items[*]}"
  25. register: csr_json
  26. changed_when: false
  27. - debug: # noqa name[missing]
  28. var: csrs
  29. - name: Check there are csrs
  30. assert:
  31. that: csrs | length > 0
  32. fail_msg: kubelet_rotate_server_certificates is {{ kubelet_rotate_server_certificates }} but no csr's found
  33. - name: Check there are Denied/Pending csrs
  34. assert:
  35. that:
  36. - csrs | rejectattr('status') | length == 0 # Pending == no status
  37. - csrs | map(attribute='status.conditions') | flatten | selectattr('type', 'equalto', 'Denied') | length == 0 # Denied
  38. fail_msg: kubelet_csr_approver is enabled but CSRs are not approved
  39. - name: Approve kubelet serving certificates
  40. when:
  41. - kubelet_rotate_server_certificates | default(false)
  42. - not (kubelet_csr_approver_enabled | default(kubelet_rotate_server_certificates | default(false)))
  43. block:
  44. - name: Get certificate signing requests
  45. command: "{{ bin_dir }}/kubectl get csr -o name"
  46. register: get_csr
  47. changed_when: false
  48. - name: Check there are csrs
  49. assert:
  50. that: get_csr.stdout_lines | length > 0
  51. fail_msg: kubelet_rotate_server_certificates is {{ kubelet_rotate_server_certificates }} but no csr's found
  52. - name: Approve certificates
  53. command: "{{ bin_dir }}/kubectl certificate approve {{ get_csr.stdout_lines | join(' ') }}"
  54. register: certificate_approve
  55. when: get_csr.stdout_lines | length > 0
  56. changed_when: certificate_approve.stdout
  57. - debug: # noqa name[missing]
  58. msg: "{{ certificate_approve.stdout.split('\n') }}"
  59. - name: Create test namespace
  60. command: "{{ bin_dir }}/kubectl create namespace test"
  61. changed_when: false
  62. - name: Run 2 agnhost pods in test ns
  63. command:
  64. cmd: "{{ bin_dir }}/kubectl apply -f -"
  65. stdin: |
  66. apiVersion: v1
  67. kind: Pod
  68. metadata:
  69. name: {{ item }}
  70. namespace: test
  71. spec:
  72. containers:
  73. - name: agnhost
  74. image: {{ test_image_repo }}:{{ test_image_tag }}
  75. command: ['/agnhost', 'netexec', '--http-port=8080']
  76. securityContext:
  77. allowPrivilegeEscalation: false
  78. capabilities:
  79. drop: ['ALL']
  80. runAsUser: 1000
  81. runAsNonRoot: true
  82. seccompProfile:
  83. type: RuntimeDefault
  84. changed_when: false
  85. loop:
  86. - agnhost1
  87. - agnhost2
  88. - import_role: # noqa name[missing]
  89. name: cluster-dump
  90. - name: Check that all pods are running and ready
  91. command: "{{ bin_dir }}/kubectl get pods --namespace test --no-headers -o yaml"
  92. changed_when: false
  93. register: run_pods_log
  94. until:
  95. # Check that all pods are running
  96. - '(run_pods_log.stdout | from_yaml)["items"] | map(attribute = "status.phase") | unique | list == ["Running"]'
  97. # Check that all pods are ready
  98. - '(run_pods_log.stdout | from_yaml)["items"] | map(attribute = "status.containerStatuses") | map("map", attribute = "ready") | map("min") | min'
  99. retries: 18
  100. delay: 10
  101. failed_when: false
  102. - name: Get pod names
  103. command: "{{ bin_dir }}/kubectl get pods -n test -o json"
  104. changed_when: false
  105. register: pods
  106. - debug: # noqa name[missing]
  107. msg: "{{ pods.stdout.split('\n') }}"
  108. failed_when: not run_pods_log is success
  109. - name: Get hostnet pods
  110. command: "{{ bin_dir }}/kubectl get pods -n test -o
  111. jsonpath='{range .items[?(.spec.hostNetwork)]}{.metadata.name} {.status.podIP} {.status.containerStatuses} {end}'"
  112. changed_when: false
  113. register: hostnet_pods
  114. ignore_errors: true # noqa ignore-errors
  115. - name: Get running pods
  116. command: "{{ bin_dir }}/kubectl get pods -n test -o
  117. jsonpath='{range .items[?(.status.phase==\"Running\")]}{.metadata.name} {.status.podIP} {.status.containerStatuses} {end}'"
  118. changed_when: false
  119. register: running_pods
  120. - name: Check kubectl output
  121. command: "{{ bin_dir }}/kubectl get pods --all-namespaces -owide"
  122. changed_when: false
  123. register: get_pods
  124. - debug: # noqa name[missing]
  125. msg: "{{ get_pods.stdout.split('\n') }}"
  126. - name: Set networking facts
  127. set_fact:
  128. kube_pods_subnet: 10.233.64.0/18
  129. pod_names: "{{ (pods.stdout | from_json)['items'] | map(attribute='metadata.name') | list }}"
  130. pod_ips: "{{ (pods.stdout | from_json)['items'] | selectattr('status.podIP', 'defined') | map(attribute='status.podIP') | list }}"
  131. pods_hostnet: |
  132. {% set list = hostnet_pods.stdout.split(" ") %}
  133. {{ list }}
  134. pods_running: |
  135. {% set list = running_pods.stdout.split(" ") %}
  136. {{ list }}
  137. - name: Check pods IP are in correct network
  138. assert:
  139. that: item | ansible.utils.ipaddr(kube_pods_subnet)
  140. when:
  141. - not item in pods_hostnet
  142. - item in pods_running
  143. with_items: "{{ pod_ips }}"
  144. - name: Curl between pods is working
  145. command: "{{ bin_dir }}/kubectl -n test exec {{ item[0] }} -- curl {{ item[1] }}:8080"
  146. when:
  147. - not item[0] in pods_hostnet
  148. - not item[1] in pods_hostnet
  149. with_nested:
  150. - "{{ pod_names }}"
  151. - "{{ pod_ips }}"
  152. - name: Curl between hostnet pods is working
  153. command: "{{ bin_dir }}/kubectl -n test exec {{ item[0] }} -- curl {{ item[1] }}:8080"
  154. when:
  155. - item[0] in pods_hostnet
  156. - item[1] in pods_hostnet
  157. with_nested:
  158. - "{{ pod_names }}"
  159. - "{{ pod_ips }}"