扩展后端
这ddClient.extension.vm
对象可用于与扩展元数据的 VM 部分中定义的后端进行通信。
获取
▸ 获取(url
):Promise
<unknown
>
向后端服务执行 HTTP GET 请求。
ddClient.extension.vm.service
.get("/some/service")
.then((value: any) => console.log(value)
请参阅 服务 API 参考 以了解 POST、UPDATE 和 DELETE 等其他方法。
已弃用的扩展后端通信
下面使用
window.ddClient.backend
已弃用,并将在未来版本中删除。使用上面指定的方法。
这window.ddClient.backend
Object 可用于与后端通信
在 VM 的
扩展元数据。客户端已连接到后端。
示例用法:
window.ddClient.backend
.get("/some/service")
.then((value: any) => console.log(value));
window.ddClient.backend
.post("/some/service", { ... })
.then((value: any) => console.log(value));
window.ddClient.backend
.put("/some/service", { ... })
.then((value: any) => console.log(value));
window.ddClient.backend
.patch("/some/service", { ... })
.then((value: any) => console.log(value));
window.ddClient.backend
.delete("/some/service")
.then((value: any) => console.log(value));
window.ddClient.backend
.head("/some/service")
.then((value: any) => console.log(value));
window.ddClient.backend
.request({ url: "/url", method: "GET", headers: { 'header-key': 'header-value' }, data: { ... }})
.then((value: any) => console.log(value));
在扩展后端容器中运行命令
例如,执行命令ls -l
在后端容器内:
await ddClient.extension.vm.cli.exec("ls", ["-l"]);
流式传输在后端容器中执行的命令的输出。例如,生成命令ls -l
在后端容器内:
await ddClient.extension.vm.cli.exec("ls", ["-l"], {
stream: {
onOutput(data) {
if (data.stdout) {
console.error(data.stdout);
} else {
console.log(data.stderr);
}
},
onError(error) {
console.error(error);
},
onClose(exitCode) {
console.log("onClose with exit code " + exitCode);
},
},
});
有关更多详细信息,请参阅扩展 VM API 参考
已弃用的扩展后端命令执行
此方法已弃用,并将在将来的版本中删除。使用上面指定的方法。
如果您的扩展附带了应在
backend 容器中,您可以使用execInVMExtension
功能:
const output = await window.ddClient.backend.execInVMExtension(
`cliShippedInTheVm xxx`
);
console.log(output);
在主机上调用扩展Binaries
您可以运行在扩展元数据的 host 部分中定义的Binaries。
例如,执行 Shipped 的Binarieskubectl -h
命令:
await ddClient.extension.host.cli.exec("kubectl", ["-h"]);
只要kubectl
binary 作为扩展的一部分提供,您可以生成kubectl -h
命令并获取输出流:
await ddClient.extension.host.cli.exec("kubectl", ["-h"], {
stream: {
onOutput(data: { stdout: string } | { stderr: string }): void {
if (data.stdout) {
console.error(data.stdout);
} else {
console.log(data.stderr);
}
},
onError(error: any): void {
console.error(error);
},
onClose(exitCode: number): void {
console.log("onClose with exit code " + exitCode);
},
},
});
您可以流式传输在后端容器或主机中执行的命令的输出。
有关更多详细信息,请参阅扩展主机 API 参考
已弃用扩展Binaries的调用
此方法已弃用,并将在将来的版本中删除。使用上面指定的方法。
要在主机中执行命令:
window.ddClient.execHostCmd(`cliShippedOnHost xxx`).then((cmdResult: any) => {
console.log(cmdResult);
});
要流式传输在后端容器或主机中执行的命令的输出,请执行以下作:
window.ddClient.spawnHostCmd(
`cliShippedOnHost`,
[`arg1`, `arg2`],
(data: any, err: any) => {
console.log(data.stdout, data.stderr);
// Once the command exits we get the status code
if (data.code) {
console.log(data.code);
}
}
);
注意
您不能使用它来将命令链接在单个
exec()
调用(如cmd1 $(cmd2)
或在命令之间使用 PIPE 进行)。您需要调用
exec()
对于每个命令并解析结果,以将参数传递给下一个命令(如果需要)。