mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-04-11 11:20:54 +00:00
fix issue when using k8s_drain with disable_eviction set to yes (#418)
fix issue when using k8s_drain with disable_eviction set to yes SUMMARY fixes #416 ISSUE TYPE Bugfix Pull Request COMPONENT NAME k8s_drain ADDITIONAL INFORMATION Reviewed-by: Abhijeet Kasurde <None>
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
bugfixes:
|
||||||
|
- k8s_drain - fix error occurring when trying to drain node with disable_eviction set to yes (https://github.com/ansible-collections/kubernetes.core/issues/416).
|
||||||
@@ -140,7 +140,7 @@ from ansible.module_utils._text import to_native
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
from kubernetes.client.api import core_v1_api
|
from kubernetes.client.api import core_v1_api
|
||||||
from kubernetes.client.models import V1DeleteOptions
|
from kubernetes.client.models import V1DeleteOptions, V1ObjectMeta
|
||||||
from kubernetes.client.exceptions import ApiException
|
from kubernetes.client.exceptions import ApiException
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# ImportError are managed by the common module already.
|
# ImportError are managed by the common module already.
|
||||||
@@ -273,15 +273,8 @@ class K8sDrainAnsible(object):
|
|||||||
self._drain_options = module.params.get("delete_options", {})
|
self._drain_options = module.params.get("delete_options", {})
|
||||||
self._delete_options = None
|
self._delete_options = None
|
||||||
if self._drain_options.get("terminate_grace_period"):
|
if self._drain_options.get("terminate_grace_period"):
|
||||||
self._delete_options = {}
|
self._delete_options = V1DeleteOptions(
|
||||||
self._delete_options.update({"apiVersion": "v1"})
|
grace_period_seconds=self._drain_options.get("terminate_grace_period")
|
||||||
self._delete_options.update({"kind": "DeleteOptions"})
|
|
||||||
self._delete_options.update(
|
|
||||||
{
|
|
||||||
"gracePeriodSeconds": self._drain_options.get(
|
|
||||||
"terminate_grace_period"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self._changed = False
|
self._changed = False
|
||||||
@@ -318,17 +311,16 @@ class K8sDrainAnsible(object):
|
|||||||
|
|
||||||
def evict_pods(self, pods):
|
def evict_pods(self, pods):
|
||||||
for namespace, name in pods:
|
for namespace, name in pods:
|
||||||
definition = {"metadata": {"name": name, "namespace": namespace}}
|
|
||||||
if self._delete_options:
|
|
||||||
definition.update({"delete_options": self._delete_options})
|
|
||||||
try:
|
try:
|
||||||
if self._drain_options.get("disable_eviction"):
|
if self._drain_options.get("disable_eviction"):
|
||||||
body = V1DeleteOptions(**definition)
|
|
||||||
self._api_instance.delete_namespaced_pod(
|
self._api_instance.delete_namespaced_pod(
|
||||||
name=name, namespace=namespace, body=body
|
name=name, namespace=namespace, body=self._delete_options
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
body = v1_eviction(**definition)
|
body = v1_eviction(
|
||||||
|
delete_options=self._delete_options,
|
||||||
|
metadata=V1ObjectMeta(name=name, namespace=namespace),
|
||||||
|
)
|
||||||
self._api_instance.create_namespaced_pod_eviction(
|
self._api_instance.create_namespaced_pod_eviction(
|
||||||
name=name, namespace=namespace, body=body
|
name=name, namespace=namespace, body=body
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
k8s_drain
|
k8s_drain
|
||||||
k8s
|
k8s
|
||||||
k8s_info
|
k8s_info
|
||||||
time=78
|
time=121
|
||||||
|
|||||||
@@ -286,6 +286,72 @@
|
|||||||
state: uncordon
|
state: uncordon
|
||||||
name: '{{ node_to_drain }}'
|
name: '{{ node_to_drain }}'
|
||||||
|
|
||||||
|
- name: Create another Pod
|
||||||
|
k8s:
|
||||||
|
namespace: '{{ test_namespace }}'
|
||||||
|
wait: yes
|
||||||
|
wait_timeout: "{{ k8s_wait_timeout | default(omit) }}"
|
||||||
|
definition:
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: '{{ drain_pod_name }}-01'
|
||||||
|
spec:
|
||||||
|
affinity:
|
||||||
|
nodeAffinity:
|
||||||
|
requiredDuringSchedulingIgnoredDuringExecution:
|
||||||
|
nodeSelectorTerms:
|
||||||
|
- matchFields:
|
||||||
|
- key: metadata.name
|
||||||
|
operator: In
|
||||||
|
values:
|
||||||
|
- '{{ node_to_drain }}'
|
||||||
|
containers:
|
||||||
|
- name: c0
|
||||||
|
image: busybox
|
||||||
|
command:
|
||||||
|
- /bin/sh
|
||||||
|
- -c
|
||||||
|
- while true;do date;sleep 5; done
|
||||||
|
volumeMounts:
|
||||||
|
- mountPath: /emptydir
|
||||||
|
name: emptydir
|
||||||
|
volumes:
|
||||||
|
- name: emptydir
|
||||||
|
emptyDir: {}
|
||||||
|
|
||||||
|
- name: Drain node using disable_eviction set to yes
|
||||||
|
k8s_drain:
|
||||||
|
state: drain
|
||||||
|
name: '{{ node_to_drain }}'
|
||||||
|
delete_options:
|
||||||
|
force: true
|
||||||
|
disable_eviction: yes
|
||||||
|
terminate_grace_period: 0
|
||||||
|
ignore_daemonsets: yes
|
||||||
|
wait_timeout: 0
|
||||||
|
delete_emptydir_data: true
|
||||||
|
register: disable_evict
|
||||||
|
|
||||||
|
- name: assert that node has been drained
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- disable_evict is changed
|
||||||
|
- '"node {{ node_to_drain }} marked unschedulable." in disable_evict.result'
|
||||||
|
|
||||||
|
- name: assert that unmanaged pod were deleted
|
||||||
|
k8s_info:
|
||||||
|
namespace: '{{ test_namespace }}'
|
||||||
|
kind: Pod
|
||||||
|
name: '{{ drain_pod_name }}-01'
|
||||||
|
register: _result
|
||||||
|
failed_when: _result.resources
|
||||||
|
|
||||||
|
- name: Uncordon node
|
||||||
|
k8s_drain:
|
||||||
|
state: uncordon
|
||||||
|
name: '{{ node_to_drain }}'
|
||||||
|
|
||||||
always:
|
always:
|
||||||
- name: Uncordon node
|
- name: Uncordon node
|
||||||
k8s_drain:
|
k8s_drain:
|
||||||
|
|||||||
Reference in New Issue
Block a user