Skip to content

Commit

Permalink
fix: allow wait for multiple DWO webhook pods
Browse files Browse the repository at this point in the history
Signed-off-by: dkwon17 <[email protected]>
  • Loading branch information
dkwon17 committed Jul 16, 2024
1 parent 8e77812 commit 1cee3c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
41 changes: 25 additions & 16 deletions src/api/kube-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,11 @@ export class KubeClient {
return conditions
}

async getPodReadyConditionStatus(selector: string, namespace: string): Promise<string | undefined> {
async getPodReadyConditionStatus(selector: string, namespace: string): Promise<string[] | undefined> {
return this.getPodsReadyConditionStatus(selector, namespace, 1)
}

async getPodsReadyConditionStatus(selector: string, namespace: string, pods: number): Promise<string[] | undefined> {
const k8sCoreApi = this.kubeConfig.makeApiClient(CoreV1Api)
let res
try {
Expand All @@ -758,33 +762,38 @@ export class KubeClient {
throw new Error(`Get pods by selector "${selector}" returned an invalid response.`)
}

if (res.body.items.length < 1) {
// No pods found by the specified selector. So, it's not ready.
return 'False'
}

if (res.body.items.length > 1) {
// Several pods found, rolling update?
if (res.body.items.length < pods) {
// Fewer than expected pods found by the specified selector. So, it's not ready.
return
}

if (!res.body.items[0].status || !res.body.items[0].status.conditions || !(res.body.items[0].status.conditions.length > 0)) {
if (res.body.items.length > pods) {
// More pods than expected found, rolling update?
return
}

const conditions = res.body.items[0].status.conditions
for (const condition of conditions) {
if (condition.type === 'Ready') {
return condition.status
return res.body.items.map(item => {
if (item.status && item.status.conditions) {
for (const condition of item.status.conditions) {
if (condition.type === 'Ready') {
return condition.status
}
}
}
}

return 'False'
})
}

async waitForPodReady(selector: string, namespace: string, intervalMs = 500, timeoutMs = this.podReadyTimeout) {
await this.waitForPodsReady(selector, namespace, 1, intervalMs, timeoutMs)
}

async waitForPodsReady(selector: string, namespace: string, pods: number, intervalMs = 500, timeoutMs = this.podReadyTimeout) {
const iterations = timeoutMs / intervalMs
for (let index = 0; index < iterations; index++) {
const readyStatus = await this.getPodReadyConditionStatus(selector, namespace)
if (readyStatus === 'True') {
const readyStatus = await this.getPodsReadyConditionStatus(selector, namespace, pods)
if (readyStatus && readyStatus.every(status => status === 'True')) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion src/tasks/installers/dev-workspace/dev-workspace-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export namespace DevWorkspacesTasks {
task: async (ctx: any, task: any) => {
const kubeHelper = KubeClient.getInstance()
await kubeHelper.waitForPodReady('app.kubernetes.io/name=devworkspace-controller', ctx[DevWorkspaceContext.NAMESPACE])
await kubeHelper.waitForPodReady('app.kubernetes.io/name=devworkspace-webhook-server', ctx[DevWorkspaceContext.NAMESPACE])
await kubeHelper.waitForPodsReady('app.kubernetes.io/name=devworkspace-webhook-server', ctx[DevWorkspaceContext.NAMESPACE], 2)
task.title = `${task.title}...[OK]`
},
}
Expand Down

0 comments on commit 1cee3c7

Please sign in to comment.