Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Inspect checluster status and fail if status.reason is not empty #1259

Merged
merged 3 commits into from
May 31, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions src/tasks/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
import { V1ContainerStateWaiting, V1PodCondition } from '@kubernetes/client-node'
import { cli } from 'cli-ux'
import * as Listr from 'listr'

import { KubeHelper } from '../api/kube'

interface FailState {
reason?: string
message?: string
}

export class KubeTasks {
private readonly interval = 500
private readonly kubeHelper: KubeHelper
Expand All @@ -28,18 +32,28 @@ export class KubeTasks {
const taskTitle = task.title
const iterations = this.kubeHelper.podWaitTimeout / this.interval
for (let i = 1; i <= iterations; i++) {
// check cheCluster status
const cheClusterFailState = await this.getCheClusterFailState(namespace)

// check 'PodScheduled' condition
const failedCondition = await this.getFailedPodCondition(namespace, selector, 'PodScheduled')
if (failedCondition) {
const podFailState = await this.getPodFailState(namespace, selector, 'PodScheduled')

if (cheClusterFailState || podFailState) {
tolusha marked this conversation as resolved.
Show resolved Hide resolved
task.title = `${taskTitle}...failed, rechecking...`

// for instance we need some time for pvc provisioning...
await cli.wait(this.kubeHelper.podErrorRecheckTimeout)

const failedCondition = await this.getFailedPodCondition(namespace, selector, 'PodScheduled')
if (failedCondition) {
const cheClusterFailState = await this.getCheClusterFailState(namespace)
if (cheClusterFailState) {
task.title = `${taskTitle}...failed`
throw new Error(`Failed to schedule a pod, reason: ${failedCondition.reason}, message: ${failedCondition.message}. Consider increasing error recheck timeout with --k8spoderrorrechecktimeout flag.`)
throw new Error(`Eclipse Che operator failed, reason: ${cheClusterFailState.reason}, message: ${cheClusterFailState.message}. Consider increasing error recheck timeout with --k8spoderrorrechecktimeout flag.`)
}

const podFailState = await this.getPodFailState(namespace, selector, 'PodScheduled')
if (podFailState) {
task.title = `${taskTitle}...failed`
throw new Error(`Failed to schedule a pod, reason: ${podFailState.reason}, message: ${podFailState.message}. Consider increasing error recheck timeout with --k8spoderrorrechecktimeout flag.`)
}
}

Expand All @@ -61,12 +75,12 @@ export class KubeTasks {
const taskTitle = task.title
const iterations = this.kubeHelper.podDownloadImageTimeout / this.interval
for (let i = 1; i <= iterations; i++) {
const failedState = await this.getFailedWaitingState(namespace, selector, 'Pending')
const failedState = await this.getContainerFailState(namespace, selector, 'Pending')
if (failedState) {
task.title = `${taskTitle}...failed, rechecking...`
await cli.wait(this.kubeHelper.podErrorRecheckTimeout)

const failedState = await this.getFailedWaitingState(namespace, selector, 'Pending')
const failedState = await this.getContainerFailState(namespace, selector, 'Pending')
if (failedState) {
task.title = `${taskTitle}...failed`
throw new Error(`Failed to download image, reason: ${failedState.reason}, message: ${failedState.message}.`)
Expand All @@ -92,12 +106,21 @@ export class KubeTasks {
const taskTitle = task.title
const iterations = this.kubeHelper.podReadyTimeout / this.interval
for (let i = 1; i <= iterations; i++) {
const failedState = await this.getFailedWaitingState(namespace, selector, 'Running')
if (failedState) {
// check cheCluster status
const cheClusterFailState = await this.getCheClusterFailState(namespace)

const failedState = await this.getContainerFailState(namespace, selector, 'Running')
if (cheClusterFailState || failedState) {
tolusha marked this conversation as resolved.
Show resolved Hide resolved
task.title = `${taskTitle}...failed, rechecking...`
await cli.wait(this.kubeHelper.podErrorRecheckTimeout)

const failedState = await this.getFailedWaitingState(namespace, selector, 'Running')
const cheClusterFailState = await this.getCheClusterFailState(namespace)
if (cheClusterFailState) {
task.title = `${taskTitle}...failed`
throw new Error(`Eclipse Che operator failed, reason: ${cheClusterFailState.reason}, message: ${cheClusterFailState.message}. Consider increasing error recheck timeout with --k8spoderrorrechecktimeout flag.`)
}

const failedState = await this.getContainerFailState(namespace, selector, 'Running')
if (failedState) {
task.title = `${taskTitle}...failed`
throw new Error(`Failed to start a pod, reason: ${failedState.reason}, message: ${failedState.message}`)
Expand Down Expand Up @@ -129,7 +152,7 @@ export class KubeTasks {
])
}

private async getFailedPodCondition(namespace: string, selector: string, conditionType: string): Promise<V1PodCondition | undefined> {
private async getPodFailState(namespace: string, selector: string, conditionType: string): Promise<FailState | undefined> {
const status = await this.kubeHelper.getPodCondition(namespace, selector, conditionType)
return status.find(s => s.status === 'False' && s.message && s.reason)
}
Expand All @@ -143,7 +166,7 @@ export class KubeTasks {
/**
* Checks if there is any reason for a given pod state and returns message if so.
*/
private async getFailedWaitingState(namespace: string, selector: string, state: string): Promise<V1ContainerStateWaiting | undefined> {
private async getContainerFailState(namespace: string, selector: string, state: string): Promise<FailState | undefined> {
const waitingState = await this.kubeHelper.getPodWaitingState(namespace, selector, state)
if (waitingState && waitingState.reason && waitingState.message) {
return waitingState
Expand Down Expand Up @@ -176,4 +199,11 @@ export class KubeTasks {

return errorMessage
}

private async getCheClusterFailState(namespace: string): Promise<FailState | undefined> {
const cheCluster = await this.kubeHelper.getCheCluster(namespace)
if (cheCluster && cheCluster.status && cheCluster.status.reason && cheCluster.status.message) {
return cheCluster.status
}
}
}