擴充功能後端

可以使用 ddClient.extension.vm 物件與擴充功能中繼資料 vm 區段 中定義的後端進行通訊。

get

▸ **get**(url): Promise<unknown>

對後端服務執行 HTTP GET 請求。

ddClient.extension.vm.service
 .get("/some/service")
 .then((value: any) => console.log(value)

如需 POST、UPDATE 和 DELETE 等其他方法,請參閱 服務 API 參考

已淘汰的擴充功能後端通訊

以下使用 window.ddClient.backend 的方法已淘汰,將在未來的版本中移除。請使用方法上面指定的方法。

可以使用 window.ddClient.backend 物件與擴充功能中繼資料 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 參考

已淘汰的擴充功能後端指令執行

此方法已淘汰,將在未來的版本中移除。請使用上面指定的方法。

如果您的擴充功能附帶應在後端容器內執行的其他二進位檔,您可以使用 execInVMExtension 函式

const output = await window.ddClient.backend.execInVMExtension(
  `cliShippedInTheVm xxx`
);
console.log(output);

叫用主機上的擴充功能二進位檔

您可以執行在擴充功能中繼資料 host 區段 中定義的二進位檔。

例如,在主機中執行隨附的二進位檔 kubectl -h 指令

await ddClient.extension.host.cli.exec("kubectl", ["-h"]);

只要 kubectl 二進位檔作為擴充功能的一部分隨附,您就可以在主機中產生 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 參考

已淘汰的擴充功能二進位檔叫用

此方法已淘汰,將在未來的版本中移除。請使用上面指定的方法。

若要在主機中執行指令

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) 或在指令之間使用管道)。

您需要為每個指令叫用 exec() 並剖析結果,以便根據需要將參數傳遞給下一個指令。