forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodo3.ts
More file actions
143 lines (124 loc) · 4.93 KB
/
odo3.ts
File metadata and controls
143 lines (124 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import { KubernetesObject } from '@kubernetes/client-node';
import { CommandText } from './base/command';
import { CliChannel, CliExitData } from './cli';
import { Command as CommonCommand, loadItems } from './k8s/common';
import { Command as DeploymentCommand } from './k8s/deployment';
import { DeploymentConfig } from './k8s/deploymentConfig';
import { Command } from './odo/command';
import { ComponentDescription } from './odo/componentTypeDescription';
export interface Odo3 {
getNamespaces(): Promise<KubernetesObject[]>;
getDeployments(): Promise<KubernetesObject[]>;
getDeploymentConfigs(): Promise<KubernetesObject[]>;
setNamespace(newNamespace: string): Promise<void>;
describeComponent(contextPath: string): Promise<ComponentDescription | undefined>;
/**
* Bind a component to a bindable service by modifying the devfile
*
* Resolves when the binding it created.
*
* @param contextPath the path to the component
* @param serviceName the name of the service to bind to
* @param serviceNamespace the namespace the the service is in
* @param bindingName the name of the service binding
*/
addBinding(
contextPath: string,
serviceName: string,
serviceNamespace: string,
bindingName: string,
): Promise<void>;
/**
* Returns a list of all the bindable services on the cluster.
*
* @returns a list of all the bindable services on the cluster
*/
getBindableServices(): Promise<KubernetesObject[]>;
}
export class Odo3Impl implements Odo3 {
private async getListItems<T>(command: CommandText, fail = false) {
const listCliExitData = await CliChannel.getInstance().executeTool(command, undefined, fail);
const result = loadItems<T>(listCliExitData.stdout);
return result;
}
async getDeployments(): Promise<KubernetesObject[]> {
return this.getListItems<KubernetesObject>(DeploymentCommand.get());
}
async getDeploymentConfigs(): Promise<KubernetesObject[]> {
return this.getListItems<KubernetesObject>(DeploymentConfig.command.getDeploymentConfigs());
}
async getProjectResources() {
return this.getListItems<KubernetesObject>(CommonCommand.getResourceList('project'), true);
}
async getNamespacesResources() {
return this.getListItems<KubernetesObject>(CommonCommand.getResourceList('namespace'), true);
}
async getNamespaces(): Promise<KubernetesObject[]> {
return Promise.any([
this.getProjectResources(),
this.getNamespacesResources()
]);
}
async setNamespace(newNamespace: string): Promise<void> {
await CliChannel.getInstance().executeTool(
Command.setNamespace(newNamespace), undefined, true
);
}
public async describeComponent(contextPath: string): Promise<ComponentDescription | undefined> {
try {
const describeCmdResult = await CliChannel.getInstance().executeTool(
Command.describeComponentJson(), {cwd: contextPath}, false
);
return JSON.parse(describeCmdResult.stdout) as ComponentDescription;
} catch(error) {
// ignore and return undefined
}
}
async addBinding(contextPath: string, serviceNamespace: string, serviceName: string, bindingName: string) {
const myCommand = Command.addBinding(serviceNamespace, serviceName, bindingName);
await CliChannel.getInstance().executeTool(
myCommand,
{cwd: contextPath},
true
);
}
async getBindableServices(): Promise<KubernetesObject[]> {
const data: CliExitData = await CliChannel.getInstance().executeTool(
Command.getBindableServices()
);
let responseObj;
try {
responseObj = JSON.parse(data.stdout);
} catch {
throw new Error(JSON.parse(data.stderr).message);
}
if (!responseObj.bindableServices) {
return [];
}
return (responseObj.bindableServices as BindableService[]) //
.map(obj => {
return {
kind: obj.kind,
apiVersion: obj.apiVersion,
metadata: {
namespace: obj.namespace,
name: obj.name,
}
} as KubernetesObject;
});
}
}
interface BindableService {
name: string;
namespace: string;
kind: string;
apiVersion: string;
service: string;
}
export function newInstance(): Odo3 {
return new Odo3Impl();
}