-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an ability to execute commands on K8s (minikube) using Kubectl (#…
…22168) * Add an ability to execute commands on K8s (minikube) using Kubectl (#22167) Signed-off-by: mdolhalo <mdolhalo@redhat.com> * KubernetesCommandLineToolsExecutor.setNamespace() method updated Signed-off-by: mdolhalo <mdolhalo@redhat.com> --------- Signed-off-by: mdolhalo <mdolhalo@redhat.com> Co-authored-by: mdolhalo <mdolhalo@redhat.com>
- Loading branch information
Showing
6 changed files
with
169 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { echo, exec, ShellString } from 'shelljs'; | ||
import { KubernetesCommandLineTool, TestConstants } from '../constants/TestConstants'; | ||
import { Logger } from './Logger'; | ||
import { ShellExecutor } from './ShellExecutor'; | ||
|
||
export class KubernetesCommandLineToolsExecutor extends ShellExecutor { | ||
private static container: string; | ||
private static pod: string; | ||
private readonly namespace: string; | ||
private readonly workspaceName: string | undefined; | ||
private readonly KUBERNETES_COMMAND_LINE_TOOL: string = TestConstants.TS_API_TEST_KUBERNETES_COMMAND_LINE_TOOL; | ||
|
||
constructor(_workspaceName?: string, _namespace?: string) { | ||
super(); | ||
this.workspaceName = _workspaceName; | ||
this.namespace = this.setNamespace(_namespace); | ||
} | ||
|
||
get getWorkspaceName(): string { | ||
return <string>this.workspaceName; | ||
} | ||
|
||
get getNamespace(): string { | ||
return this.namespace; | ||
} | ||
|
||
// login to Openshift cluster with username and password | ||
loginToOcp(): void { | ||
if (this.KUBERNETES_COMMAND_LINE_TOOL === KubernetesCommandLineTool.OC) { | ||
Logger.debug(`${this.getLoggingName(this.loginToOcp.name)}: Login to the "OC" client`); | ||
const url: string = this.getServerUrl(); | ||
Logger.debug(url, TestConstants.TS_SELENIUM_OCP_USERNAME); | ||
exec(`sleep 5 | ||
oc login --server=${url} -u=${TestConstants.TS_SELENIUM_OCP_USERNAME} -p=${TestConstants.TS_SELENIUM_OCP_PASSWORD} --insecure-skip-tls-verify`); | ||
} else { | ||
Logger.debug(`${this.getLoggingName(this.loginToOcp.name)}: doesn't support login command`); | ||
} | ||
} | ||
|
||
getContainerName(): string { | ||
Logger.debug(`${this.getLoggingName(this.getContainerName.name)}: Get container name.`); | ||
const output: ShellString = this.execWithLog(`${(this.KUBERNETES_COMMAND_LINE_TOOL)} get ${(KubernetesCommandLineToolsExecutor.pod)} -o jsonpath='{.spec.containers[*].name}' -n ${this.namespace}`); | ||
echo('\n'); | ||
return output.stderr ? output.stderr : output.stdout; | ||
} | ||
|
||
getWorkspacePodName(): string { | ||
Logger.debug(`${this.getLoggingName(this.getWorkspacePodName.name)}: Get workspace pod name.`); | ||
const output: ShellString = this.execWithLog(`${(this.KUBERNETES_COMMAND_LINE_TOOL)} get pod -l controller.devfile.io/devworkspace_name=${this.workspaceName} -n ${this.namespace} -o name`); | ||
return output.stderr ? output.stderr : output.stdout.replace('\n', ''); | ||
} | ||
|
||
deleteDevWorkspace(): void { | ||
Logger.debug(`${this.getLoggingName(this.deleteDevWorkspace.name)}: Delete '${this.workspaceName}' workspace`); | ||
this.execWithLog(`${(this.KUBERNETES_COMMAND_LINE_TOOL)} patch dw ${this.workspaceName} -n ${this.namespace} -p '{ "metadata": { "finalizers": null }}' --type merge || true`); | ||
this.execWithLog(`${(this.KUBERNETES_COMMAND_LINE_TOOL)} delete dw ${this.workspaceName} -n ${this.namespace} || true`); | ||
} | ||
|
||
applyAndWaitDevWorkspace(yamlConfiguration: string): ShellString { | ||
if (this.KUBERNETES_COMMAND_LINE_TOOL === KubernetesCommandLineTool.KUBECTL) { | ||
this.createNamespace(); | ||
} | ||
this.applyYamlConfigurationAsStringOutput(yamlConfiguration); | ||
const output: ShellString = this.waitDevWorkspace(); | ||
KubernetesCommandLineToolsExecutor.pod = this.getWorkspacePodName(); | ||
KubernetesCommandLineToolsExecutor.container = this.getContainerName(); | ||
return output; | ||
} | ||
|
||
executeCommand(commandToExecute: string): ShellString { | ||
Logger.debug(`${this.getLoggingName(this.executeCommand.name)}:`); | ||
return this.execWithLog(`${(this.KUBERNETES_COMMAND_LINE_TOOL)} exec -i ${KubernetesCommandLineToolsExecutor.pod} -n ${this.namespace} -c ${KubernetesCommandLineToolsExecutor.container} -- sh -c "${commandToExecute}"`); | ||
} | ||
|
||
applyYamlConfigurationAsStringOutput(yamlConfiguration: string): void { | ||
Logger.debug(`${this.getLoggingName(this.applyYamlConfigurationAsStringOutput.name)}:`); | ||
this.execWithLog(`cat <<EOF | ${this.KUBERNETES_COMMAND_LINE_TOOL} apply -n ${this.namespace} -f - \n` + | ||
yamlConfiguration + '\n' + | ||
'EOF'); | ||
} | ||
|
||
waitDevWorkspace(timeout: number = 360): ShellString { | ||
Logger.debug(`${this.getLoggingName(this.waitDevWorkspace.name)}: Wait till workspace ready.`); | ||
return this.execWithLog(`${(this.KUBERNETES_COMMAND_LINE_TOOL)} wait -n ${this.namespace} --for=condition=Ready dw ${this.workspaceName} --timeout=${timeout}s`); | ||
} | ||
|
||
createNamespace(): void { | ||
Logger.debug(`${this.getLoggingName(this.createNamespace.name)}: Create namespace "${this.namespace}".`); | ||
this.execWithLog(`${this.KUBERNETES_COMMAND_LINE_TOOL} create namespace ${this.namespace}`); | ||
} | ||
|
||
private getLoggingName(methodName: string): string { | ||
return `${this.constructor.name}.${methodName} - ${(this.KUBERNETES_COMMAND_LINE_TOOL)}`; | ||
} | ||
|
||
private setNamespace(_namespace: string | undefined): string { | ||
_namespace = _namespace !== undefined ? _namespace | ||
: TestConstants.TS_SELENIUM_BASE_URL.includes('devspaces') ? TestConstants.TS_SELENIUM_OCP_USERNAME + '-devspaces' | ||
: TestConstants.TS_SELENIUM_BASE_URL.includes('che') ? TestConstants.TS_SELENIUM_OCP_USERNAME + '-che' | ||
: 'default'; | ||
return _namespace; | ||
} | ||
|
||
private getServerUrl(): string { | ||
Logger.debug(`${this.getLoggingName(this.getServerUrl.name)}: Get server api url.`); | ||
return TestConstants.TS_SELENIUM_BASE_URL.replace('devspaces.apps', 'api') + ':6443'; | ||
} | ||
} | ||
|
||
export namespace KubernetesCommandLineToolsExecutor { | ||
export class ContainerTerminal extends KubernetesCommandLineToolsExecutor { | ||
constructor(cluster: KubernetesCommandLineToolsExecutor) { | ||
super(cluster.getWorkspaceName, cluster.getNamespace); | ||
} | ||
|
||
ls(path: string = ''): ShellString { | ||
return this.executeCommand('ls ' + path); | ||
} | ||
|
||
pwd(): ShellString { | ||
return this.executeCommand('pwd'); | ||
} | ||
|
||
cd(path: string): ShellString { | ||
return this.executeCommand('cd ' + path); | ||
} | ||
|
||
gitClone(repository: string): ShellString { | ||
return this.executeCommand('git clone ' + repository); | ||
} | ||
|
||
removeFolder(path: string): ShellString { | ||
return this.executeCommand('rm -rf ' + path); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { echo, exec, ShellString } from 'shelljs'; | ||
|
||
export class ShellExecutor { | ||
|
||
protected execWithLog(command: string): ShellString { | ||
echo(command); | ||
return exec(command); | ||
} | ||
} |