Skip to content

Commit 5af8ffa

Browse files
authored
adding model and wait changes (#168)
Signed-off-by: Paige Patton <[email protected]>
1 parent 63e5898 commit 5af8ffa

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/krkn_lib/k8s/krkn_kubernetes.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
AffectedPod,
3131
ApiRequestException,
3232
Container,
33+
NodeResources,
3334
Pod,
3435
PodsMonitorThread,
3536
PodsStatus,
3637
ServiceHijacking,
3738
Volume,
3839
VolumeMount,
39-
NodeResources,
4040
)
4141
from krkn_lib.models.krkn import HogConfig, HogType
4242
from krkn_lib.models.telemetry import ClusterEvent, NodeInfo, Taint
@@ -851,7 +851,9 @@ def get_all_services(self, namespace: str) -> list[str]:
851851

852852
# Outputs a json blob with informataion about all pods in a given namespace
853853
def get_all_pod_info(
854-
self, namespace: str = "default", label_selector: str = None
854+
self,
855+
namespace: str = "default",
856+
label_selector: str = None,
855857
) -> list[str]:
856858
"""
857859
Get details of all pods in a namespace
@@ -1137,8 +1139,18 @@ def delete_pod(self, name: str, namespace: str = "default"):
11371139
:param namespace: namespace (optional default `default`)
11381140
"""
11391141
try:
1142+
starting_creation_timestamp = self.cli.read_namespaced_pod(
1143+
name=name, namespace=namespace
1144+
).metadata.creation_timestamp
11401145
self.cli.delete_namespaced_pod(name=name, namespace=namespace)
1146+
11411147
while self.cli.read_namespaced_pod(name=name, namespace=namespace):
1148+
1149+
ending_creation_timestamp = self.cli.read_namespaced_pod(
1150+
name=name, namespace=namespace
1151+
).metadata.creation_timestamp
1152+
if starting_creation_timestamp != ending_creation_timestamp:
1153+
break
11421154
time.sleep(1)
11431155
except ApiException as e:
11441156
if e.status == 404:
@@ -1540,6 +1552,7 @@ def get_pod_info(self, name: str, namespace: str = "default") -> Pod:
15401552
nodeName=response.spec.node_name,
15411553
volumes=volume_list,
15421554
status=response.status.phase,
1555+
creation_timestamp=response.metadata.creation_timestamp,
15431556
)
15441557
return pod_info
15451558
else:
@@ -2699,12 +2712,14 @@ def select_pods_by_namespace_pattern_and_label(
26992712
pods_and_namespaces = [
27002713
pod for pod in pods_and_namespaces if namespace_re.match(pod[1])
27012714
]
2715+
27022716
# select only running pods
27032717
pods_and_namespaces = [
27042718
(pod[0], pod[1])
27052719
for pod in pods_and_namespaces
27062720
if not self.is_pod_terminating(pod[0], pod[1])
27072721
]
2722+
27082723
return pods_and_namespaces
27092724

27102725
def monitor_pods_by_label(
@@ -2951,12 +2966,20 @@ def __monitor_pods_worker(
29512966
# respawned with the same names
29522967
if set(pods_and_namespaces) == set(current_pods_and_namespaces):
29532968
for pod in current_pods_and_namespaces:
2954-
if not self.is_pod_running(pod[0], pod[1]):
2969+
pod_info = self.get_pod_info(pod[0], pod[1])
2970+
if pod_info is not None:
2971+
pod_creation_timestamp = (
2972+
pod_info.creation_timestamp.timestamp()
2973+
)
2974+
else:
2975+
continue
2976+
if (
2977+
pod_info.status
2978+
and start_time < pod_creation_timestamp
2979+
):
2980+
# in this case the pods to wait have been respawn
2981+
# with the same name
29552982
missing_pods.add(pod)
2956-
if len(missing_pods) == 0:
2957-
continue
2958-
# in this case the pods to wait have been respawn
2959-
# with the same name
29602983
pods_to_wait.update(missing_pods)
29612984

29622985
# pods have been killed but respawned with different names
@@ -2992,6 +3015,8 @@ def __monitor_pods_worker(
29923015
with ThreadPoolExecutor() as executor:
29933016
for pod_and_namespace in pods_to_wait:
29943017
if pod_and_namespace not in pods_already_watching:
3018+
3019+
# need name of new pod
29953020
future = executor.submit(
29963021
self.__wait_until_pod_is_ready_worker,
29973022
pod_name=pod_and_namespace[0],

src/krkn_lib/models/k8s/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from concurrent.futures import Future, ThreadPoolExecutor
22
from dataclasses import dataclass
3-
from typing import Optional
3+
from typing import Any, Optional
44

55

66
@dataclass(frozen=True, order=False)
@@ -94,6 +94,10 @@ class Pod:
9494
"""
9595
Status of the Pod
9696
"""
97+
creation_timestamp: Any
98+
"""
99+
Creation timestamp of the Pod
100+
"""
97101
podIP: str
98102
"""
99103
Pod ip address

0 commit comments

Comments
 (0)