Skip to content

Commit

Permalink
Merge pull request #104 from datawire/rlane/update-kubeception-client
Browse files Browse the repository at this point in the history
Update Kubeception client to use production
  • Loading branch information
rick-a-lane-ii authored Oct 17, 2024
2 parents 3bff7cd + 8f69112 commit eac46d1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 69 deletions.
4 changes: 0 additions & 4 deletions .github/actions/provision-cluster/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
109 changes: 58 additions & 51 deletions .github/actions/provision-cluster/lib/kubeception.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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}`
);
}
Expand Down
11 changes: 2 additions & 9 deletions .github/actions/provision-cluster/lib/kubeception.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions provision-cluster/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,5 +85,4 @@ runs:
kubeceptionToken: ${{ inputs.kubeceptionToken }}
gkeCredentials: ${{ inputs.gkeCredentials }}
gkeConfig: ${{ inputs.gkeConfig }}
kubeceptionProfile: ${{ inputs.kubeceptionProfile }}
useAuthProvider: ${{ inputs.useAuthProvider }}

0 comments on commit eac46d1

Please sign in to comment.