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 15, 2024
1 parent 8e77812 commit 4a7e561
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
36 changes: 23 additions & 13 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,39 @@ 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 < pods) {
// Fewer than expected pods found by the specified selector. So, it's not ready.
return
}

if (res.body.items.length > 1) {
// Several pods found, rolling update?
if (res.body.items.length > pods) {
// More pods than expected found, rolling update?
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.some((item) => !item.status || !item.status.conditions || !(item.status.conditions.length > 0))) {

Check failure on line 775 in src/api/kube-client.ts

View workflow job for this annotation

GitHub Actions / release-test

Unexpected parentheses around single function argument

Check failure on line 775 in src/api/kube-client.ts

View workflow job for this annotation

GitHub Actions / unit-tests

Unexpected parentheses around single function argument
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 => {
for (const condition of item.status?.conditions!) {

Check failure on line 780 in src/api/kube-client.ts

View workflow job for this annotation

GitHub Actions / release-test

Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError

Check failure on line 780 in src/api/kube-client.ts

View workflow job for this annotation

GitHub Actions / release-test

Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong

Check failure on line 780 in src/api/kube-client.ts

View workflow job for this annotation

GitHub Actions / unit-tests

Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError

Check failure on line 780 in src/api/kube-client.ts

View workflow job for this annotation

GitHub Actions / unit-tests

Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong
if (condition.type === 'Ready') {
return condition.status
}
}
}
return 'False'

Check failure on line 785 in src/api/kube-client.ts

View workflow job for this annotation

GitHub Actions / release-test

Expected blank line before this statement

Check failure on line 785 in src/api/kube-client.ts

View workflow job for this annotation

GitHub Actions / unit-tests

Expected blank line before this statement
})
}

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') {
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 4a7e561

Please sign in to comment.