From 8f69112d9c759578d0d809c637c59e35838535ae Mon Sep 17 00:00:00 2001
From: Rick Lane <rlane@datawire.io>
Date: Wed, 16 Oct 2024 23:07:51 -0400
Subject: [PATCH] Update Kubeception client to use production

Signed-off-by: Rick Lane <rlane@datawire.io>
---
 .github/actions/provision-cluster/action.yaml |   4 -
 .../provision-cluster/lib/kubeception.js      | 109 ++++++++++--------
 .../provision-cluster/lib/kubeception.test.js |  11 +-
 provision-cluster/action.yaml                 |   5 -
 4 files changed, 60 insertions(+), 69 deletions(-)

diff --git a/.github/actions/provision-cluster/action.yaml b/.github/actions/provision-cluster/action.yaml
index 2e5211c6..81f0767e 100644
--- a/.github/actions/provision-cluster/action.yaml
+++ b/.github/actions/provision-cluster/action.yaml
@@ -34,10 +34,6 @@ inputs:
   gkeConfig:
     description: "A JSON string containing additional configuration for the given GKE cluster."
     required: false
-  kubeceptionProfile:
-    description: "The profile to use for kubeception clusters."
-    required: false
-    default: "default"
   useAuthProvider:
     description: "For GKE clusters, if true, use an authentication provider."
     required: false
diff --git a/.github/actions/provision-cluster/lib/kubeception.js b/.github/actions/provision-cluster/lib/kubeception.js
index d8e3da10..c0d4f70f 100644
--- a/.github/actions/provision-cluster/lib/kubeception.js
+++ b/.github/actions/provision-cluster/lib/kubeception.js
@@ -7,7 +7,10 @@ const utils = require("./utils.js");
 const yaml = require("yaml");
 
 const MAX_KLUSTER_NAME_LEN = 63;
-const defaultLifespan = 60 * 60; // One hour worth of seconds
+const DEFAULT_ARCHETYPE = "small";
+const DEFAULT_MODE = "active";
+const DEFAULT_LIFESPAN = 60 * 60;
+const KUBECEPTION_URL = "https://kubeception.datawire.io";
 
 class Client {
   constructor(client) {
@@ -47,28 +50,26 @@ class Client {
     }
 
     if (!version) {
-      throw Error("Kluster version is required");
+      throw new Error("Kluster version is required");
     }
 
-    if (
-      typeof lifespan === typeof undefined ||
-      lifespan === "" ||
-      lifespan === 0
-    ) {
-      lifespan = defaultLifespan;
-    }
+    lifespan = lifespan || DEFAULT_LIFESPAN;
 
-    let kubeceptionProfile = core.getInput("kubeceptionProfile");
-    if (
-      typeof kubeceptionProfile !== typeof "" ||
-      kubeceptionProfile.trim() === ""
-    ) {
-      kubeceptionProfile = "default";
-    }
+    let kluster = {
+      name: name,
+      version: version,
+      archetype: DEFAULT_ARCHETYPE,
+      mode: DEFAULT_MODE,
+      timeoutSecs: lifespan,
+    };
 
     return utils.fibonacciRetry(async () => {
-      const response = await this.client.put(
-        `https://sw.bakerstreet.io/kubeception/api/klusters/${name}?version=${version}&profile=${kubeceptionProfile}&timeoutSecs=${lifespan}`
+      const response = await this.client.post(
+        `${KUBECEPTION_URL}/api/klusters`,
+        JSON.stringify(kluster),
+        {
+          "Content-Type": "application/json",
+        }
       );
 
       if (!response || !response.message) {
@@ -101,55 +102,61 @@ class Client {
       throw new Error("Kluster name is required");
     }
 
-    return utils.fibonacciRetry(async () => {
-      const response = await this.client.get(
-        `https://sw.bakerstreet.io/kubeception/api/klusters/${name}/kubeconfig`
-      );
-
-      if (!response || !response.message) {
-        throw new utils.Transient("Unknown error getting response");
-      }
-
-      switch (response.message.statusCode) {
-        case 200:
-        case 201:
-          return await response.readBody();
-        case 202:
-          throw new utils.Retry("Request is still pending");
-        default:
-          if (response.message.statusCode >= 400) {
-            throw new utils.Transient(
-              `Status code ${response.message.statusCode}`
-            );
-          } else {
-            let body = await response.readBody();
-            throw new Error(
-              `Status code ${response.message.statusCode}: ${body}`
-            );
-          }
-      }
-    });
+    return utils.fibonacciRetry(
+      async () => {
+        const response = await this.client.get(
+          `${KUBECEPTION_URL}/api/klusters/${name}/kubeconfig`
+        );
+
+        if (!response || !response.message) {
+          throw new utils.Transient("Unknown error getting response");
+        }
+
+        switch (response.message.statusCode) {
+          case 200:
+          case 201:
+            return await response.readBody();
+          case 202:
+            throw new utils.Retry("Request is still pending");
+          default:
+            if (response.message.statusCode >= 400) {
+              throw new utils.Transient(
+                `Status code ${response.message.statusCode}`
+              );
+            } else {
+              let body = await response.readBody();
+              throw new Error(
+                `Status code ${response.message.statusCode}: ${body}`
+              );
+            }
+        }
+      },
+      600000,
+      1000,
+      600000
+    );
   }
 
   async deleteKluster(name) {
     if (!name) {
-      throw Error("Kluster name is required");
+      throw new Error("Kluster name is required");
     }
 
     const response = await this.client.del(
-      `https://sw.bakerstreet.io/kubeception/api/klusters/${name}`
+      `${KUBECEPTION_URL}/api/klusters/${name}`
     );
+
     if (!response || !response.message) {
-      throw Error("Unknown error getting response");
+      throw new Error("Unknown error getting response");
     }
 
-    if (response.message.statusCode == 200) {
+    if (response.message.statusCode === 200) {
       return {
         done: true,
         status: "deleted",
       };
     } else {
-      throw Error(
+      throw new Error(
         `Expected status code 200 but got ${response.message.statusCode}`
       );
     }
diff --git a/.github/actions/provision-cluster/lib/kubeception.test.js b/.github/actions/provision-cluster/lib/kubeception.test.js
index 36bab30d..9727c61c 100644
--- a/.github/actions/provision-cluster/lib/kubeception.test.js
+++ b/.github/actions/provision-cluster/lib/kubeception.test.js
@@ -6,23 +6,16 @@ const common = require("./common_test.js");
 const mock = require("./mock.js");
 const MOCK = mock.MOCK;
 const cluster = mock.cluster;
-const URL = require("url").URL;
 
-test("kubeception profile", async () => {
+test("kubeception", async () => {
   let inputs = {
     kubeceptionToken: "mock-kube-token",
-    kubeceptionProfile: "mock-profile",
   };
 
   let count = 0;
 
   class MockHttpClient {
-    async put(url) {
-      let parsed = new URL(url);
-      expect(parsed.searchParams.get("profile")).toBe(
-        inputs.kubeceptionProfile
-      );
-
+    async post() {
       return {
         message: {
           statusCode: 200,
diff --git a/provision-cluster/action.yaml b/provision-cluster/action.yaml
index d59cec74..bd9ceb20 100644
--- a/provision-cluster/action.yaml
+++ b/provision-cluster/action.yaml
@@ -34,10 +34,6 @@ inputs:
   gkeConfig:
     description: "A JSON string containing additional configuration for the given GKE cluster."
     required: false
-  kubeceptionProfile:
-    description: "The profile to use for kubeception clusters."
-    required: false
-    default: "default"
   useAuthProvider:
     description: "For GKE clusters, if true, use an authentication provider."
     required: false
@@ -89,5 +85,4 @@ runs:
         kubeceptionToken: ${{ inputs.kubeceptionToken }}
         gkeCredentials: ${{ inputs.gkeCredentials }}
         gkeConfig: ${{ inputs.gkeConfig }}
-        kubeceptionProfile: ${{ inputs.kubeceptionProfile }}
         useAuthProvider: ${{ inputs.useAuthProvider }}