Skip to content

Commit

Permalink
feat: Sets secrets on deployment, also sets resources
Browse files Browse the repository at this point in the history
  • Loading branch information
OysteinVesth committed Feb 19, 2024
1 parent 02ac859 commit 51edff4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 96 deletions.
12 changes: 12 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ inputs:
secrets_string:
required: false
description: 'JSON formatted string with secrets'
memory-limit:
required: false
description: "Memory (MiB) limit for the container (max memory)"
memory-reservation:
required: false
description: "Memory (MiB) reservation for the container (min memory)"
cpu-limit:
required: false
description: "CPU (mCPU) limit for the container (max cpu)"
cpu-reservation:
required: false
description: "CPU (mCPU) reservation for the container (min cpu)"

runs:
using: 'node16'
Expand Down
29 changes: 0 additions & 29 deletions src/Secrets.ts

This file was deleted.

19 changes: 2 additions & 17 deletions src/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,6 @@ export const deleteDeployment = async (
}
};

export const setSecrets = async (authToken: string, location: string) => {
const response = await fetch(location, {
method: "GET",
headers: {
Authorization: `Bearer ${authToken}`,
},
});

if (response.status >= 200 && response.status <= 299) {
const resJson = await response.json();
return resJson.status;
} else {
throw "Deployment not found!";
}
};

export const checkStatus = async (authToken: string, location: string) => {
const response = await fetch(location, {
method: "GET",
Expand Down Expand Up @@ -105,11 +89,12 @@ export const deploy = async (authToken: string, deployment: Deployment) => {
volumes: deployment.volumes,
volumeMounts: deployment.volumeMounts,
proxyBufferSize: deployment.proxyBufferSize,
secrets: deployment.secrets,
resources: deployment.resources
},
null,
2
);
console.log("POST BODY", params);
const response = await fetch(`${deployment.uri}/${deployment.type}`, {
method: "POST",
body: params,
Expand Down
56 changes: 39 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import core = require("@actions/core");
import { context } from "@actions/github";
import { deploy, checkStatus } from "./apiService";
import { ProbeConfig, VolumeConfig, VolumeMountConfig } from "./types";
import { Secrets } from "./Secrets";
import fetch from "node-fetch";

const getDeploymentType = (type: string): string => {
switch (type) {
Expand Down Expand Up @@ -65,12 +63,7 @@ const run = async () => {
const type = getDeploymentType(core.getInput("type"));
console.log("Type ", type);
const isReleaseChannel = core.getBooleanInput("release-channel");
const envVariables = Object.keys(process.env || {})
.filter((x) => x.indexOf("TFSO_") == 0)
.reduce((prev: { [name: string]: string }, cur: string) => {
prev[cur.replace("TFSO_", "")] = process.env[cur];
return prev;
}, {});
const envVariables = getEnvironmentVariables(process.env);
const containerPortString = core.getInput("container-port");
const httpEndpoint = core.getInput("http-endpoint");
const proxyBufferSize = core.getInput("proxy-buffer-size");
Expand All @@ -83,12 +76,17 @@ const run = async () => {
const volumeMounts = core
.getMultilineInput("volume-mounts")
.map((v) => JSON.parse(v) as VolumeMountConfig);
const resources = getResourceSettings(core);
const branch =
context.ref.replace("refs/heads/", "") ||
context.ref.replace("refs/tags/", "");
const deploymentUri =
process.env.DEPLOYMENT_URI || "https://deployment.api.24sevenoffice.com";
console.log("Using url ", deploymentUri);
const secrets = {
...JSON.parse(core.getInput("secrets_string") || "{}"),
...getSecretEnvironmentVariables(process.env),
};

let containerPort: number | undefined = undefined;
if (containerPortString) containerPort = parseInt(containerPortString);
Expand All @@ -114,23 +112,16 @@ const run = async () => {
imageName,
deployerName: deployerName,
proxyBufferSize,
resources,
};
console.log(JSON.stringify(deployParams));
var location = await deploy(token, deployParams);
var location = await deploy(token, { ...deployParams, secrets });
core.setOutput("deploymenturl", location);
if (!location) {
console.log("No location returned. Assume the deployment is ok!");
return;
}

const secrets = core.getInput("secrets_string");
if (secrets) {
console.log("Setting secrets...");
const secretManager = new Secrets(token, new URL(location));
await secretManager.postSecretsString(secrets, fetch);
console.log("Secrets was set.");
}

console.log(
"Checking location ",
location,
Expand All @@ -149,4 +140,35 @@ const run = async () => {
throw "Error : Deployment was not set to active within set period.";
};

function getResourceSettings(c: typeof core) {
return {
requests: {
memoryMib: parseInt(c.getInput("memory-reservation") || "512"),
mCpu: parseInt(c.getInput("cpu-reservation") || "500"),
},
limits: {
memoryMib: parseInt(c.getInput("memory-limit") || "512"),
mCpu: parseInt(c.getInput("cpu-limit") || "1000"),
},
};
}

function getSecretEnvironmentVariables(env: Record<string, string>) {
return Object.keys(env || {})
.filter((x) => x.indexOf("TFSO_SECRET_") == 0)
.reduce((prev: { [name: string]: string }, cur: string) => {
prev[cur.replace("TFSO_SECRET_", "")] = env[cur];
return prev;
}, {});
}

function getEnvironmentVariables(env: Record<string, string>) {
return Object.keys(env || {})
.filter((x) => x.startsWith("TFSO_") && !x.startsWith("TFSO_SECRET_"))
.reduce((prev: { [name: string]: string }, cur: string) => {
prev[cur.replace("TFSO_", "")] = env[cur];
return prev;
}, {});
}

run();
33 changes: 0 additions & 33 deletions src/tests/Secret.test.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ export type VolumeMountConfig = {
name: string;
};

export type Resources = {
requests: {
memoryMib: number;
mCpu: number;
};
limits: {
memoryMib: number;
mCpu: number;
};
};

export type Deployment = {
env: string;
serviceName: string;
Expand All @@ -46,8 +57,10 @@ export type Deployment = {
httpEndpoint: string;
containerPort: number;
environmentVariables: { [name: string]: string };
secrets: { [name: string]: string };
deployerName: string;
proxyBufferSize: string;
resources: Resources;
};

export type FetchResponse = { json: () => Promise<any>; status: number };
Expand Down

0 comments on commit 51edff4

Please sign in to comment.