Skip to content

Commit

Permalink
Add domain check for Kubernetes family infrastructures
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Morhun <mmorhun@redhat.com>
  • Loading branch information
mmorhun committed Apr 2, 2020
1 parent 884028b commit 5a77845
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/commands/server/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export default class Start extends Command {
'skip-version-check': flags.boolean({
description: 'Skip minimal versions check.',
default: false
}),
'skip-cluster-check': flags.boolean({
description: 'Skip cluster availability check.',
default: false
})
}

Expand Down
56 changes: 56 additions & 0 deletions src/tasks/platforms/common-platform-tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*********************************************************************
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import * as http from 'http'
import * as Listr from 'listr'

export namespace CommonPlatformTasks {
/**
* Checks whether cluster on which Che is being deployed accessible.
* Requires flags.domain to be set.
*/
export function getPingClusterTask(flags: any): Listr.ListrTask {
return {
title: 'Check if cluster accessible',
skip: () => flags['skip-cluster-check'],
task: async (_ctx: any, task: any) => {
const domain: string = flags.domain
if (!domain) {
task.title = `${task.title}... "--domain" flag is not set. Cannot check cluster availability.'`
return
}

if (!await checkServer(domain, 80) && !await checkServer(domain, 443)) {
throw new Error(`Cannot reach cluster "${domain}". To skip this check add "--skip-cluster-check" flag.`)
}

task.title = `${task.title}... ok`
}
}
}

/**
* Sends request to given server to check if it is accessible.
*/
function checkServer(host: string, port: number): Promise<boolean> {
return new Promise(resolve => {
http.get({ host, port }, response => {
// It is ok to have 404, but not 5xx
if (response.statusCode && response.statusCode / 100 < 5) {
resolve(true)
}
resolve(false)
}).on('error', () => {
resolve(false)
})
})
}

}
3 changes: 3 additions & 0 deletions src/tasks/platforms/k8s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import * as Listr from 'listr'
import { KubeHelper } from '../../api/kube'
import { VersionHelper } from '../../api/version'

import { CommonPlatformTasks } from './common-platform-tasks'

export class K8sTasks {
/**
* Returns tasks list which perform preflight platform checks.
Expand Down Expand Up @@ -52,6 +54,7 @@ export class K8sTasks {
task.title = `${task.title}...set to ${flags.domain}.`
}
},
CommonPlatformTasks.getPingClusterTask(flags)
],
{ renderer: flags['listr-renderer'] as any }
)
Expand Down
3 changes: 3 additions & 0 deletions src/tasks/platforms/microk8s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import * as Listr from 'listr'

import { VersionHelper } from '../../api/version'

import { CommonPlatformTasks } from './common-platform-tasks'

export class MicroK8sTasks {
/**
* Returns tasks list which perform preflight platform checks.
Expand Down Expand Up @@ -94,6 +96,7 @@ export class MicroK8sTasks {
task.title = `${task.title}...${flags.domain}.`
}
},
CommonPlatformTasks.getPingClusterTask(flags)
], { renderer: flags['listr-renderer'] as any })
}

Expand Down
3 changes: 3 additions & 0 deletions src/tasks/platforms/minikube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import * as Listr from 'listr'

import { VersionHelper } from '../../api/version'

import { CommonPlatformTasks } from './common-platform-tasks'

export class MinikubeTasks {
/**
* Returns tasks list which perform preflight platform checks.
Expand Down Expand Up @@ -77,6 +79,7 @@ export class MinikubeTasks {
task.title = `${task.title}...${flags.domain}.`
}
},
CommonPlatformTasks.getPingClusterTask(flags)
], { renderer: flags['listr-renderer'] as any })
}

Expand Down

0 comments on commit 5a77845

Please sign in to comment.