This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
k8s_info: Add support for wait (#235)
Fixes: #18
- Loading branch information
Showing
7 changed files
with
331 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bugfixes: | ||
- k8s_info - add wait functionality (https://github.com/ansible-collections/community.kubernetes/issues/18). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
--- | ||
- block: | ||
- set_fact: | ||
wait_namespace: wait | ||
k8s_pod_name: pod-info-1 | ||
multi_pod_one: multi-pod-1 | ||
multi_pod_two: multi-pod-2 | ||
|
||
- name: Ensure namespace exists | ||
k8s: | ||
definition: | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: "{{ wait_namespace }}" | ||
|
||
- name: Add a simple pod with initContainer | ||
k8s: | ||
definition: | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: "{{ k8s_pod_name }}" | ||
namespace: "{{ wait_namespace }}" | ||
spec: | ||
initContainers: | ||
- name: init-01 | ||
image: python:3.7-alpine | ||
command: ['sh', '-c', 'sleep 20'] | ||
containers: | ||
- name: utilitypod-01 | ||
image: python:3.7-alpine | ||
command: ['sh', '-c', 'sleep 360'] | ||
|
||
- name: Wait and gather information about new pod | ||
k8s_info: | ||
name: "{{ k8s_pod_name }}" | ||
kind: Pod | ||
namespace: "{{ wait_namespace }}" | ||
wait: yes | ||
wait_sleep: 5 | ||
wait_timeout: 400 | ||
register: wait_info | ||
|
||
- name: Assert that pod creation succeeded | ||
assert: | ||
that: | ||
- wait_info is successful | ||
- not wait_info.changed | ||
- wait_info.resources[0].status.phase == "Running" | ||
|
||
- name: Remove Pod | ||
k8s: | ||
api_version: v1 | ||
kind: Pod | ||
name: "{{ k8s_pod_name }}" | ||
namespace: "{{ wait_namespace }}" | ||
state: absent | ||
wait: yes | ||
ignore_errors: yes | ||
register: short_wait_remove_pod | ||
|
||
- name: Check if pod is removed | ||
assert: | ||
that: | ||
- short_wait_remove_pod is successful | ||
- short_wait_remove_pod.changed | ||
|
||
- name: Create multiple pod with initContainer | ||
k8s: | ||
definition: | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
labels: | ||
run: multi-box | ||
name: "{{ multi_pod_one }}" | ||
namespace: "{{ wait_namespace }}" | ||
spec: | ||
initContainers: | ||
- name: init-01 | ||
image: python:3.7-alpine | ||
command: ['sh', '-c', 'sleep 25'] | ||
containers: | ||
- name: multi-pod-01 | ||
image: python:3.7-alpine | ||
command: ['sh', '-c', 'sleep 360'] | ||
|
||
- name: Create another pod with same label as previous pod | ||
k8s: | ||
definition: | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
labels: | ||
run: multi-box | ||
name: "{{ multi_pod_two }}" | ||
namespace: "{{ wait_namespace }}" | ||
spec: | ||
initContainers: | ||
- name: init-02 | ||
image: python:3.7-alpine | ||
command: ['sh', '-c', 'sleep 25'] | ||
containers: | ||
- name: multi-pod-02 | ||
image: python:3.7-alpine | ||
command: ['sh', '-c', 'sleep 360'] | ||
|
||
- name: Wait and gather information about new pods | ||
k8s_info: | ||
kind: Pod | ||
namespace: "{{ wait_namespace }}" | ||
wait: yes | ||
wait_sleep: 5 | ||
wait_timeout: 400 | ||
label_selectors: | ||
- run == multi-box | ||
register: wait_info | ||
|
||
- name: Assert that pod creation succeeded | ||
assert: | ||
that: | ||
- wait_info is successful | ||
- not wait_info.changed | ||
- wait_info.resources[0].status.phase == "Running" | ||
- wait_info.resources[1].status.phase == "Running" | ||
|
||
- name: "Remove Pod {{ multi_pod_one }}" | ||
k8s: | ||
api_version: v1 | ||
kind: Pod | ||
name: "{{ multi_pod_one }}" | ||
namespace: "{{ wait_namespace }}" | ||
state: absent | ||
wait: yes | ||
ignore_errors: yes | ||
register: multi_pod_one_remove | ||
|
||
- name: "Check if {{ multi_pod_one }} pod is removed" | ||
assert: | ||
that: | ||
- multi_pod_one_remove is successful | ||
- multi_pod_one_remove.changed | ||
|
||
- name: "Remove Pod {{ multi_pod_two }}" | ||
k8s: | ||
api_version: v1 | ||
kind: Pod | ||
name: "{{ multi_pod_two }}" | ||
namespace: "{{ wait_namespace }}" | ||
state: absent | ||
wait: yes | ||
ignore_errors: yes | ||
register: multi_pod_two_remove | ||
|
||
- name: "Check if {{ multi_pod_two }} pod is removed" | ||
assert: | ||
that: | ||
- multi_pod_two_remove is successful | ||
- multi_pod_two_remove.changed | ||
|
||
always: | ||
- name: Remove namespace | ||
k8s: | ||
kind: Namespace | ||
name: "{{ wait_namespace }}" | ||
state: absent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright: (c) 2020, Red Hat | Ansible | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# Options for specifying object wait | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
|
||
class ModuleDocFragment(object): | ||
|
||
DOCUMENTATION = r''' | ||
options: | ||
wait: | ||
description: | ||
- Whether to wait for certain resource kinds to end up in the desired state. | ||
- By default the module exits once Kubernetes has received the request. | ||
- Implemented for C(state=present) for C(Deployment), C(DaemonSet) and C(Pod), and for C(state=absent) for all resource kinds. | ||
- For resource kinds without an implementation, C(wait) returns immediately unless C(wait_condition) is set. | ||
default: no | ||
type: bool | ||
wait_sleep: | ||
description: | ||
- Number of seconds to sleep between checks. | ||
default: 5 | ||
type: int | ||
wait_timeout: | ||
description: | ||
- How long in seconds to wait for the resource to end up in the desired state. | ||
- Ignored if C(wait) is not set. | ||
default: 120 | ||
type: int | ||
wait_condition: | ||
description: | ||
- Specifies a custom condition on the status to wait for. | ||
- Ignored if C(wait) is not set or is set to False. | ||
suboptions: | ||
type: | ||
type: str | ||
description: | ||
- The type of condition to wait for. | ||
- For example, the C(Pod) resource will set the C(Ready) condition (among others). | ||
- Required if you are specifying a C(wait_condition). | ||
- If left empty, the C(wait_condition) field will be ignored. | ||
- The possible types for a condition are specific to each resource type in Kubernetes. | ||
- See the API documentation of the status field for a given resource to see possible choices. | ||
status: | ||
type: str | ||
description: | ||
- The value of the status field in your desired condition. | ||
- For example, if a C(Deployment) is paused, the C(Progressing) C(type) will have the C(Unknown) status. | ||
choices: | ||
- True | ||
- False | ||
- Unknown | ||
default: "True" | ||
reason: | ||
type: str | ||
description: | ||
- The value of the reason field in your desired condition | ||
- For example, if a C(Deployment) is paused, The C(Progressing) C(type) will have the C(DeploymentPaused) reason. | ||
- The possible reasons in a condition are specific to each resource type in Kubernetes. | ||
- See the API documentation of the status field for a given resource to see possible choices. | ||
type: dict | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.