2016-02-10 18:51:39 +08:00
|
|
|
---
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Testcases for network
|
|
|
|
hosts: kube_control_plane[0]
|
2019-04-24 07:12:00 +08:00
|
|
|
vars:
|
2022-10-14 21:10:46 +08:00
|
|
|
test_image_repo: registry.k8s.io/e2e-test-images/agnhost
|
|
|
|
test_image_tag: "2.40"
|
2016-02-10 18:51:39 +08:00
|
|
|
|
|
|
|
tasks:
|
2020-08-28 17:28:53 +08:00
|
|
|
- name: Force binaries directory for Flatcar Container Linux by Kinvolk
|
2016-11-16 01:17:30 +08:00
|
|
|
set_fact:
|
|
|
|
bin_dir: "/opt/bin"
|
2021-10-02 00:11:23 +08:00
|
|
|
when: ansible_os_family in ["Flatcar", "Flatcar Container Linux by Kinvolk"]
|
2016-11-16 01:17:30 +08:00
|
|
|
|
2019-05-16 15:27:43 +08:00
|
|
|
- name: Force binaries directory for other hosts
|
|
|
|
set_fact:
|
2016-11-16 01:17:30 +08:00
|
|
|
bin_dir: "/usr/local/bin"
|
2021-10-02 00:11:23 +08:00
|
|
|
when: not ansible_os_family in ["Flatcar", "Flatcar Container Linux by Kinvolk"]
|
2016-11-16 01:17:30 +08:00
|
|
|
|
2023-05-11 08:49:09 +08:00
|
|
|
- name: Check kubelet serving certificates approved with kubelet_csr_approver
|
2023-08-10 15:57:27 +08:00
|
|
|
when:
|
|
|
|
- kubelet_rotate_server_certificates | default(false)
|
|
|
|
- kubelet_csr_approver_enabled | default(kubelet_rotate_server_certificates | default(false))
|
2024-10-25 20:53:24 +08:00
|
|
|
vars:
|
|
|
|
csrs: "{{ csr_json.stdout | from_json }}"
|
2023-05-11 08:49:09 +08:00
|
|
|
block:
|
|
|
|
|
|
|
|
- name: Get certificate signing requests
|
2024-10-25 20:53:24 +08:00
|
|
|
command: "{{ bin_dir }}/kubectl get csr -o jsonpath-as-json={.items[*]}"
|
|
|
|
register: csr_json
|
2023-05-11 08:49:09 +08:00
|
|
|
changed_when: false
|
|
|
|
|
2023-06-30 17:51:57 +08:00
|
|
|
- debug: # noqa name[missing]
|
2024-10-25 20:53:24 +08:00
|
|
|
var: csrs
|
2023-05-11 08:49:09 +08:00
|
|
|
|
|
|
|
- name: Check there are csrs
|
|
|
|
assert:
|
2024-10-25 20:53:24 +08:00
|
|
|
that: csrs | length > 0
|
2023-05-11 08:49:09 +08:00
|
|
|
fail_msg: kubelet_rotate_server_certificates is {{ kubelet_rotate_server_certificates }} but no csr's found
|
|
|
|
|
|
|
|
- name: Check there are Denied/Pending csrs
|
|
|
|
assert:
|
2024-10-25 20:53:24 +08:00
|
|
|
that:
|
|
|
|
- csrs | rejectattr('status') | length == 0 # Pending == no status
|
|
|
|
- csrs | map(attribute='status.conditions') | flatten | selectattr('type', 'equalto', 'Denied') | length == 0 # Denied
|
|
|
|
|
2023-05-11 08:49:09 +08:00
|
|
|
fail_msg: kubelet_csr_approver is enabled but CSRs are not approved
|
|
|
|
|
2020-09-03 22:25:41 +08:00
|
|
|
- name: Approve kubelet serving certificates
|
2023-08-10 15:57:27 +08:00
|
|
|
when:
|
|
|
|
- kubelet_rotate_server_certificates | default(false)
|
|
|
|
- not (kubelet_csr_approver_enabled | default(kubelet_rotate_server_certificates | default(false)))
|
2020-09-03 22:25:41 +08:00
|
|
|
block:
|
|
|
|
|
|
|
|
- name: Get certificate signing requests
|
|
|
|
command: "{{ bin_dir }}/kubectl get csr -o name"
|
|
|
|
register: get_csr
|
|
|
|
changed_when: false
|
|
|
|
|
|
|
|
- name: Check there are csrs
|
|
|
|
assert:
|
|
|
|
that: get_csr.stdout_lines | length > 0
|
|
|
|
fail_msg: kubelet_rotate_server_certificates is {{ kubelet_rotate_server_certificates }} but no csr's found
|
|
|
|
|
|
|
|
- name: Approve certificates
|
|
|
|
command: "{{ bin_dir }}/kubectl certificate approve {{ get_csr.stdout_lines | join(' ') }}"
|
|
|
|
register: certificate_approve
|
|
|
|
when: get_csr.stdout_lines | length > 0
|
|
|
|
changed_when: certificate_approve.stdout
|
|
|
|
|
2023-06-30 17:51:57 +08:00
|
|
|
- debug: # noqa name[missing]
|
2020-09-03 22:25:41 +08:00
|
|
|
msg: "{{ certificate_approve.stdout.split('\n') }}"
|
|
|
|
|
|
|
|
|
2020-09-10 18:45:54 +08:00
|
|
|
- name: Create test namespace
|
|
|
|
command: "{{ bin_dir }}/kubectl create namespace test"
|
|
|
|
changed_when: false
|
2019-04-24 07:12:00 +08:00
|
|
|
|
2022-10-14 21:10:46 +08:00
|
|
|
- name: Run 2 agnhost pods in test ns
|
2024-10-25 20:53:24 +08:00
|
|
|
command:
|
|
|
|
cmd: "{{ bin_dir }}/kubectl apply -f -"
|
|
|
|
stdin: |
|
2022-10-14 21:10:46 +08:00
|
|
|
apiVersion: v1
|
|
|
|
kind: Pod
|
|
|
|
metadata:
|
|
|
|
name: {{ item }}
|
|
|
|
namespace: test
|
|
|
|
spec:
|
|
|
|
containers:
|
|
|
|
- name: agnhost
|
|
|
|
image: {{ test_image_repo }}:{{ test_image_tag }}
|
|
|
|
command: ['/agnhost', 'netexec', '--http-port=8080']
|
|
|
|
securityContext:
|
|
|
|
allowPrivilegeEscalation: false
|
|
|
|
capabilities:
|
|
|
|
drop: ['ALL']
|
|
|
|
runAsUser: 1000
|
|
|
|
runAsNonRoot: true
|
|
|
|
seccompProfile:
|
|
|
|
type: RuntimeDefault
|
2020-07-28 23:39:47 +08:00
|
|
|
changed_when: false
|
2020-05-04 03:36:34 +08:00
|
|
|
loop:
|
2022-10-14 21:10:46 +08:00
|
|
|
- agnhost1
|
|
|
|
- agnhost2
|
2019-04-24 07:12:00 +08:00
|
|
|
|
2023-06-30 17:51:57 +08:00
|
|
|
- import_role: # noqa name[missing]
|
2020-04-01 22:23:29 +08:00
|
|
|
name: cluster-dump
|
|
|
|
|
2020-09-10 18:45:54 +08:00
|
|
|
- name: Check that all pods are running and ready
|
|
|
|
command: "{{ bin_dir }}/kubectl get pods --namespace test --no-headers -o yaml"
|
2020-07-28 23:39:47 +08:00
|
|
|
changed_when: false
|
2019-04-20 21:11:40 +08:00
|
|
|
register: run_pods_log
|
2017-09-26 17:38:58 +08:00
|
|
|
until:
|
2019-04-20 21:11:40 +08:00
|
|
|
# Check that all pods are running
|
|
|
|
- '(run_pods_log.stdout | from_yaml)["items"] | map(attribute = "status.phase") | unique | list == ["Running"]'
|
|
|
|
# Check that all pods are ready
|
|
|
|
- '(run_pods_log.stdout | from_yaml)["items"] | map(attribute = "status.containerStatuses") | map("map", attribute = "ready") | map("min") | min'
|
|
|
|
retries: 18
|
|
|
|
delay: 10
|
|
|
|
failed_when: false
|
2016-11-28 20:18:06 +08:00
|
|
|
no_log: true
|
|
|
|
|
2020-09-10 18:45:54 +08:00
|
|
|
- name: Get pod names
|
|
|
|
command: "{{ bin_dir }}/kubectl get pods -n test -o json"
|
2020-07-28 23:39:47 +08:00
|
|
|
changed_when: false
|
2017-09-26 17:38:58 +08:00
|
|
|
register: pods
|
|
|
|
no_log: true
|
|
|
|
|
2023-06-30 17:51:57 +08:00
|
|
|
- debug: # noqa name[missing]
|
2019-05-16 15:27:43 +08:00
|
|
|
msg: "{{ pods.stdout.split('\n') }}"
|
2019-04-24 07:12:00 +08:00
|
|
|
failed_when: not run_pods_log is success
|
|
|
|
|
2020-07-28 23:39:47 +08:00
|
|
|
- name: Get hostnet pods
|
2019-05-03 05:24:21 +08:00
|
|
|
command: "{{ bin_dir }}/kubectl get pods -n test -o
|
2020-06-30 03:39:59 +08:00
|
|
|
jsonpath='{range .items[?(.spec.hostNetwork)]}{.metadata.name} {.status.podIP} {.status.containerStatuses} {end}'"
|
2020-07-28 23:39:47 +08:00
|
|
|
changed_when: false
|
2016-11-28 20:18:06 +08:00
|
|
|
register: hostnet_pods
|
2021-07-12 15:00:47 +08:00
|
|
|
ignore_errors: true # noqa ignore-errors
|
2017-09-15 20:19:37 +08:00
|
|
|
no_log: true
|
2016-11-28 20:18:06 +08:00
|
|
|
|
2020-07-28 23:39:47 +08:00
|
|
|
- name: Get running pods
|
2019-05-03 05:24:21 +08:00
|
|
|
command: "{{ bin_dir }}/kubectl get pods -n test -o
|
2020-06-30 03:39:59 +08:00
|
|
|
jsonpath='{range .items[?(.status.phase==\"Running\")]}{.metadata.name} {.status.podIP} {.status.containerStatuses} {end}'"
|
2024-08-28 13:30:56 +08:00
|
|
|
changed_when: false
|
2016-11-28 20:18:06 +08:00
|
|
|
register: running_pods
|
2017-09-15 20:19:37 +08:00
|
|
|
no_log: true
|
|
|
|
|
2020-09-10 18:45:54 +08:00
|
|
|
- name: Check kubectl output
|
|
|
|
command: "{{ bin_dir }}/kubectl get pods --all-namespaces -owide"
|
2024-08-28 13:30:56 +08:00
|
|
|
changed_when: false
|
2017-09-15 20:19:37 +08:00
|
|
|
register: get_pods
|
|
|
|
no_log: true
|
|
|
|
|
2023-06-30 17:51:57 +08:00
|
|
|
- debug: # noqa name[missing]
|
2019-05-16 15:27:43 +08:00
|
|
|
msg: "{{ get_pods.stdout.split('\n') }}"
|
2016-02-10 18:51:39 +08:00
|
|
|
|
2019-05-16 15:27:43 +08:00
|
|
|
- name: Set networking facts
|
|
|
|
set_fact:
|
2017-02-23 22:14:28 +08:00
|
|
|
kube_pods_subnet: 10.233.64.0/18
|
2023-07-05 11:36:54 +08:00
|
|
|
pod_names: "{{ (pods.stdout | from_json)['items'] | map(attribute='metadata.name') | list }}"
|
|
|
|
pod_ips: "{{ (pods.stdout | from_json)['items'] | selectattr('status.podIP', 'defined') | map(attribute='status.podIP') | list }}"
|
2016-11-28 20:18:06 +08:00
|
|
|
pods_hostnet: |
|
|
|
|
{% set list = hostnet_pods.stdout.split(" ") %}
|
2019-05-03 05:24:21 +08:00
|
|
|
{{ list }}
|
2016-11-28 20:18:06 +08:00
|
|
|
pods_running: |
|
|
|
|
{% set list = running_pods.stdout.split(" ") %}
|
2019-05-03 05:24:21 +08:00
|
|
|
{{ list }}
|
2016-02-10 18:51:39 +08:00
|
|
|
|
|
|
|
- name: Check pods IP are in correct network
|
|
|
|
assert:
|
2023-12-06 10:49:25 +08:00
|
|
|
that: item | ansible.utils.ipaddr(kube_pods_subnet)
|
2019-05-16 15:27:43 +08:00
|
|
|
when:
|
|
|
|
- not item in pods_hostnet
|
|
|
|
- item in pods_running
|
2019-05-03 05:24:21 +08:00
|
|
|
with_items: "{{ pod_ips }}"
|
2016-02-10 18:51:39 +08:00
|
|
|
|
2022-10-14 21:10:46 +08:00
|
|
|
- name: Curl between pods is working
|
|
|
|
command: "{{ bin_dir }}/kubectl -n test exec {{ item[0] }} -- curl {{ item[1] }}:8080"
|
2019-05-16 15:27:43 +08:00
|
|
|
when:
|
|
|
|
- not item[0] in pods_hostnet
|
|
|
|
- not item[1] in pods_hostnet
|
2016-11-28 20:18:06 +08:00
|
|
|
with_nested:
|
2019-05-03 05:24:21 +08:00
|
|
|
- "{{ pod_names }}"
|
|
|
|
- "{{ pod_ips }}"
|
2016-11-28 20:18:06 +08:00
|
|
|
|
2022-10-14 21:10:46 +08:00
|
|
|
- name: Curl between hostnet pods is working
|
|
|
|
command: "{{ bin_dir }}/kubectl -n test exec {{ item[0] }} -- curl {{ item[1] }}:8080"
|
2019-05-16 15:27:43 +08:00
|
|
|
when:
|
|
|
|
- item[0] in pods_hostnet
|
|
|
|
- item[1] in pods_hostnet
|
2016-11-28 20:18:06 +08:00
|
|
|
with_nested:
|
2019-05-03 05:24:21 +08:00
|
|
|
- "{{ pod_names }}"
|
|
|
|
- "{{ pod_ips }}"
|