Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-write Kubernetes Salt module to use DynamicClient #3510

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
### Enhancements
- Bump `containerd` version to 1.4.8 (PR [#3466](https://github.com/scality/metalk8s/pull/3466)).

- [#3487](https://github.com/scality/metalk8s/issues/3487) - Make Salt
Kubernetes execution module more flexible relying on `DynamicClient`
from `python-kubernetes`
(PR[#3510](https://github.com/scality/metalk8s/pull/3510))

## Release 2.10.3 (in development)
### Enhancements

Expand Down
1 change: 0 additions & 1 deletion buildchain/buildchain/salt_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,6 @@ def _get_parts(self) -> Iterator[str]:
Path("salt/_states/metalk8s_sysctl.py"),
Path("salt/_states/metalk8s_volumes.py"),
Path("salt/_utils/metalk8s_utils.py"),
Path("salt/_utils/kubernetes_utils.py"),
Path("salt/_utils/pillar_utils.py"),
Path("salt/_utils/volume_utils.py"),
# This image is defined here and not in the `image` module since it is
Expand Down
41 changes: 23 additions & 18 deletions salt/_modules/metalk8s_drain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from salt.exceptions import CommandExecutionError

try:
import kubernetes
from kubernetes.client.rest import ApiException
from kubernetes.client.models.v1_delete_options import V1DeleteOptions
from kubernetes.client.models.v1_object_meta import V1ObjectMeta
from kubernetes.client.models.v1beta1_eviction import V1beta1Eviction
from urllib3.exceptions import HTTPError
Expand Down Expand Up @@ -68,7 +68,7 @@ def _mirrorpod_filter(pod):
"""
mirror_annotation = "kubernetes.io/config.mirror"

annotations = pod["metadata"]["annotations"]
annotations = pod["metadata"].get("annotations")
if annotations and mirror_annotation in annotations:
return False, ""
return True, ""
Expand All @@ -81,8 +81,8 @@ def _has_local_storage(pod):
- pod: kubernetes pod object
Returns: True if the pod uses local storage, False if not
"""
for volume in pod["spec"]["volumes"]:
if volume["empty_dir"] is not None:
for volume in pod["spec"].get("volumes", []):
if volume.get("emptyDir") is not None:
return True
return False

Expand All @@ -98,9 +98,9 @@ def _get_controller_of(pod):
- pod: kubernetes pod object
Returns: the reference to a controller object
"""
if pod["metadata"]["owner_references"]:
for owner_ref in pod["metadata"]["owner_references"]:
if owner_ref["controller"]:
if pod["metadata"].get("ownerReferences"):
for owner_ref in pod["metadata"]["ownerReferences"]:
if owner_ref.get("controller"):
return owner_ref
return None

Expand Down Expand Up @@ -233,7 +233,7 @@ def get_controller(self, namespace, controller_ref):
return __salt__["metalk8s_kubernetes.get_object"](
name=controller_ref["name"],
kind=controller_ref["kind"],
apiVersion=controller_ref["api_version"],
apiVersion=controller_ref["apiVersion"],
namespace=namespace,
**self._kwargs
)
Expand Down Expand Up @@ -494,25 +494,30 @@ def evict_pod(name, namespace="default", grace_period=1, **kwargs):
Returns: whether the eviction was successfully created or not
Raises: CommandExecutionError in case of API error
"""
kind_info = __utils__["metalk8s_kubernetes.get_kind_info"](
{"kind": "PodEviction", "apiVersion": "v1"}
)

delete_options = V1DeleteOptions()
delete_options = kubernetes.client.V1DeleteOptions()
if grace_period >= 0:
delete_options.grace_period = grace_period

object_meta = V1ObjectMeta(name=name, namespace=namespace)

kubeconfig, context = __salt__["metalk8s_kubernetes.get_kubeconfig"](**kwargs)

client = kind_info.client
client.configure(config_file=kubeconfig, context=context)
client = kubernetes.dynamic.DynamicClient(
kubernetes.config.new_client_from_config(kubeconfig, context)
)

# DynamicClient does not handle Pod eviction, so compute the path manually
path = (
client.resources.get(api_version="v1", kind="Pod").path(
name=name, namespace=namespace
)
+ "/eviction"
)

try:
client.create(
name=name,
namespace=namespace,
client.request(
"post",
path,
body=V1beta1Eviction(delete_options=delete_options, metadata=object_meta),
)
except (ApiException, HTTPError) as exc:
Expand Down