Skip to content

Commit

Permalink
Refactor env config to be centralized
Browse files Browse the repository at this point in the history
  • Loading branch information
adb-sh committed Sep 17, 2024
1 parent 9510946 commit aceb2a1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"chart.js": "^4.4.4",
"color-hash": "^2.0.2",
"daisyui": "^4.6.0",
"deepmerge": "^4.3.1",
"echarts": "^5.5.1",
"humanize-duration": "^3.32.1",
"mongoose-zod": "^0.1.1",
Expand All @@ -33,7 +34,7 @@
"solid-supabase": "^0.5.0",
"tailwindcss": "^3.3.3",
"vinxi": "^0.4.1",
"zod": "^3.20.6"
"zod": "^3.23.8"
},
"engines": {
"node": ">=18"
Expand Down
11 changes: 6 additions & 5 deletions src/k8s.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import k8s from "@kubernetes/client-node";
import { Knative } from "./knative";
import { config } from "./lib/config";

const kc = new k8s.KubeConfig();
if (typeof process.env.DEPLOYCAT_KUBECONFIG_PATH === "string") {
if (config.kubeconfig?.path) {
console.log("load kubeconfig from file");
kc.loadFromFile(process.env.DEPLOYCAT_KUBECONFIG_PATH);
} else if (typeof process.env.DEPLOYCAT_KUBECONFIG === "string") {
kc.loadFromFile(config.kubeconfig.path);
} else if (config.kubeconfig?.base64) {
console.log("load kubeconfig from env (base64 encoded)");
kc.loadFromString(atob(process.env.DEPLOYCAT_KUBECONFIG));
} else if (process.env.DEPLOYCAT_KUBECONFIG_FROM_CLUSTER) {
kc.loadFromString(atob(config.kubeconfig?.base64));
} else if (config.kubeconfig?.fromcluster) {
console.log("load kubeconfig from cluster");
kc.loadFromCluster();
} else {
Expand Down
57 changes: 57 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { z } from "zod";
import fs from "fs/promises";
import merge from "deepmerge";

const configPath = `${process.cwd()}/config.json`;

const schemaConfig = z.object({
database: z.object({
url: z.string(),
}),
kubeconfig: z
.object({
base64: z.string().optional(),
path: z.string().optional(),
fromcluster: z.boolean().optional(),
})
.optional(),
prometheus: z.object({
url: z.string(),
}),
});

const parseEnv = ({ prefix = "", envs = process.env } = {}) => {
const parsed = {} as any;
Object.entries(envs).forEach(([key, value]) => {
const seq = (obj: any, arr: Array<string>, v: string | undefined) => {
if (typeof obj === "string") return;
if (arr.length > 1) {
const [pos] = arr.splice(0, 1);
if (!obj[pos]) obj[pos] = {};
seq(obj[pos], arr, v);
} else {
obj[arr[0]] = v;
}
};
const keyArr = key.split("_").map((e) => e.toLocaleLowerCase());
seq(parsed, keyArr, value);
});
return prefix !== "" ? parsed?.[prefix] : parsed;
};

const parseJSON = async () => {
try {
return JSON.parse((await fs.readFile(configPath)).toString());
} catch (e) {
console.info("no json config found, get config only from envs");
return {};
}
};

const getConfig = async () => {
const config = merge(await parseJSON(), parseEnv({ prefix: "deploycat" }));
console.log("config:", config);
return config;
};

export const config = schemaConfig.parse(await getConfig());
3 changes: 2 additions & 1 deletion src/lib/prometheus.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { cache } from "@solidjs/router";
import { getUser } from "./server";
import { knative } from "~/k8s";
import { config } from "./config";

const baseUrl = `${process.env.DEPLOYCAT_PROMETHEUS_URL}/api/v1`;
const baseUrl = `${config.prometheus.url}/api/v1`;

export const rangeQuery = async ({
query,
Expand Down

0 comments on commit aceb2a1

Please sign in to comment.