Skip to content

Commit

Permalink
salt: Rework kubernetes salt module to use DynamicClient
Browse files Browse the repository at this point in the history
In order to be more flexible and not be tied to a `python-kubernetes`
version compatible with the Kubernetes version embedded in MetalK8s we
rely on `DynamicClient` in our salt Kubernetes execution module

Fixes: #3487
  • Loading branch information
TeddyAndrieux committed Aug 26, 2021
1 parent 0e08fba commit 08fcf61
Show file tree
Hide file tree
Showing 17 changed files with 538 additions and 1,494 deletions.
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
Loading

0 comments on commit 08fcf61

Please sign in to comment.