Skip to content

Commit

Permalink
add readiness-probe check to k8sV1 backend
Browse files Browse the repository at this point in the history
  • Loading branch information
sotojn committed Aug 21, 2024
1 parent 6330ea1 commit 93a87a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export class KubernetesClusterBackend {

const pod = await this.k8s.waitForSelectedPod(
`${controllerLabel}=${controllerUid}`,
'pod-status',
undefined,
this.context.sysconfig.teraslice.slicer_timeout
);
Expand Down Expand Up @@ -162,6 +163,14 @@ export class KubernetesClusterBackend {
// @ts-expect-error
execution.k8sUid = jobs.items[0].metadata.uid;

/// Wait for ex readiness probe to return 'Ready'
await this.k8s.waitForSelectedPod(
selector,
'readiness-probe',
undefined,
this.context.sysconfig.teraslice.slicer_timeout
);

const kr = new K8sResource(
'deployments',
'worker',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ export class K8s {
* NOTE: If your selector will return multiple pods, this method probably
* won't work for you.
* @param {String} selector kubernetes selector, like 'controller-uid=XXX'
* @param {String} statusType type of status to check, either pod status or readiness probe
* @param {String} ns namespace to search, this will override the default
* @param {Number} timeout time, in ms, to wait for pod to start
* @return {Object} pod
*
* TODO: Should this use the cluster state that gets polled periodically,
* rather than making it's own k8s API calls
*/
async waitForSelectedPod(selector: string, ns?: string, timeout = 10000) {
async waitForSelectedPod(selector: string, statusType: string, ns?: string, timeout = 10000) {
const namespace = ns || this.defaultNamespace;
let now = Date.now();
const end = now + timeout;
Expand All @@ -112,7 +113,20 @@ export class K8s {
}

if (typeof pod !== 'undefined' && pod) {
if (get(pod, 'status.phase') === 'Running') return pod;
if (statusType === 'readiness-probe') {
if (pod.status?.conditions) {
for (const condition of pod.status.conditions) {
if (
condition.type === 'ContainersReady'
&& condition.status === 'True'
) {
return pod;
}
}
}
} else if (statusType === 'pod-status') {
if (get(pod, 'status.phase') === 'Running') return pod;
}
}
if (now > end) throw new Error(`Timeout waiting for pod matching: ${selector}`);
this.logger.debug(`waiting for pod matching: ${selector}`);
Expand Down

0 comments on commit 93a87a2

Please sign in to comment.