Skip to content

Commit

Permalink
use CL2 k8s client
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssa1303 committed Nov 27, 2024
1 parent d8ed698 commit 514b28f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 80 deletions.
58 changes: 0 additions & 58 deletions modules/python/client/kubernetes_client.py

This file was deleted.

58 changes: 50 additions & 8 deletions modules/python/clusterloader2/kubernetes_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TODO: Move this file to a separate folder called 'clients'
from kubernetes import client, config


# https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/#taint-based-evictions
# https://kubernetes.io/docs/reference/labels-annotations-taints/
builtin_taints_keys = [
Expand All @@ -20,22 +20,27 @@ class KubernetesClient:
def __init__(self, kubeconfig=None):
config.load_kube_config(kubeconfig)
self.api = client.CoreV1Api()
self.app = client.AppsV1Api()
self.storage = client.StorageV1Api()

def get_app_client(self):
return self.app

def describe_node(self, node_name):
return self.api.read_node(node_name)

def get_nodes(self, label_selector=None, field_selector=None):
return self.api.list_node(label_selector=label_selector, field_selector=field_selector).items
def get_ready_nodes(self):

def get_ready_nodes(self, label_selector=None, field_selector=None):
"""
Get a list of nodes that are ready to be scheduled. Should apply all those conditions:
- 'Ready' condition status is True
- 'NetworkUnavailable' condition status is not present or is False
- Spec unschedulable is False
- Spec taints do not have any of the builtin taints keys with effect 'NoSchedule' or 'NoExecute'
"""
nodes = self.get_nodes()
nodes = self.get_nodes(label_selector=label_selector, field_selector=field_selector)
return [
node for node in nodes
if self._is_node_schedulable(node) and self._is_node_untainted(node)
Expand All @@ -50,16 +55,53 @@ def _is_node_schedulable(self, node):
)
if not is_schedulable:
print(f"Node NOT Ready: '{node.metadata.name}' is not schedulable. status_conditions: {status_conditions}. unschedulable: {node.spec.unschedulable}")

return is_schedulable

def _is_node_untainted(self, node):
if not node.spec.taints:
return True

for taint in node.spec.taints:
if taint.key in builtin_taints_keys and taint.effect in ("NoSchedule", "NoExecute"):
print(f"Node NOT Ready: '{node.metadata.name}' has taint '{taint.key}' with effect '{taint.effect}'")
return False

return True
return True

def get_pods_by_namespace(self, namespace, label_selector=None, field_selector=None):
return self.api.list_namespaced_pod(namespace=namespace, label_selector=label_selector, field_selector=field_selector).items

def get_running_pods_by_namespace(self, namespace=None, label_selector=None):
return self.get_pods_by_namespace(namespace=namespace, label_selector=label_selector, field_selector="status.phase=Running")

def get_persistent_volume_claims_by_namespace(self, namespace):
return self.api.list_namespaced_persistent_volume_claim(namespace=namespace).items

def get_bound_persistent_volume_claims_by_namespace(self, namespace):
claims = self.get_persistent_volume_claims_by_namespace(namespace=namespace)
return [claim for claim in claims if claim.status.phase == "Bound"]

def delete_persistent_volume_claim_by_namespace(self, namespace):
pvcs = self.get_persistent_volume_claims_by_namespace(namespace=namespace)
for pvc in pvcs:
self.api.delete_namespaced_persistent_volume_claim(pvc.metadata.name, namespace, body=client.V1DeleteOptions())

def get_attached_volume_attachments(self):
volume_attachments = self.storage.list_volume_attachment().items
return [attachment for attachment in volume_attachments if attachment.status.attached]

def create_namespace(self, namespace):
# Check if namespace exists
try:
namespace = self.api.read_namespace(namespace)
return namespace
except client.rest.ApiException as e:
if e.status == 404:
body = client.V1Namespace(metadata=client.V1ObjectMeta(name=namespace))
return self.api.create_namespace(body)
else:
raise e

def delete_namespace(self, namespace):
return self.api.delete_namespace(namespace)
2 changes: 1 addition & 1 deletion modules/python/csi/csi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
from datetime import datetime, timezone
from concurrent.futures import ThreadPoolExecutor, as_completed
from client.kubernetes_client import KubernetesClient, client
from clusterloader2.kubernetes_client import KubernetesClient, client

KUBERNETERS_CLIENT=KubernetesClient()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
trigger: none
schedules:
- cron: "0 16 1-31/2 * *"
- cron: "0 20 1-31/2 * *"
displayName: "Every Odd Day"
branches:
include:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,6 @@ eks_config_list = [{
vpc_name = "client-vpc"
policy_arns = ["AmazonEKSClusterPolicy", "AmazonEKSVPCResourceController", "AmazonEKSWorkerNodePolicy", "AmazonEKS_CNI_Policy", "AmazonEC2ContainerRegistryReadOnly"]
eks_managed_node_groups = [
# {
# name = "default"
# ami_type = "AL2_x86_64"
# instance_types = ["m4.large"]
# min_size = 3
# max_size = 3
# desired_size = 3
# capacity_type = "ON_DEMAND"
# },
{
name = "user"
ami_type = "AL2_x86_64"
Expand All @@ -80,9 +71,6 @@ eks_config_list = [{
}
]
eks_addons = [
# {
# name = "vpc-cni"
# },
{
name = "aws-ebs-csi-driver"
service_account = "ebs-csi-controller-sa"
Expand Down

0 comments on commit 514b28f

Please sign in to comment.