Skip to content

Commit

Permalink
feat: extension types update (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrimbda authored Nov 24, 2023
1 parent b4241fd commit 80d8664
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 57 deletions.
78 changes: 26 additions & 52 deletions apps/agent/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default (context: IExtensionContext) => {
type: 'string',
},
},
// one_json: agentConfSchema,
},
}),
make_docker_compose_file: async (ctx, envCtx) => {
Expand Down Expand Up @@ -64,47 +63,18 @@ export default (context: IExtensionContext) => {
},
make_k8s_resource_objects: async (ctx, envCtx) => {
// if there's no AGENT_CONF_PATH, we will use the default agent_conf.json
if (ctx?.env?.AGENT_CONF_PATH === undefined && ctx.one_json === undefined) {
throw new Error('AGENT_CONF_PATH or volume_data.agent_conf is required for k8s deployment');
if (ctx.one_json === undefined) {
throw new Error('volume_data.agent_conf is required for k8s deployment');
}

let agentConf: IAgentConf;
if (ctx?.env?.AGENT_CONF_PATH !== undefined) {
const agentConfText = await envCtx.readFile(ctx.env!.AGENT_CONF_PATH);
agentConf = JSON.parse(agentConfText!) as IAgentConf;
} else {
agentConf = JSON.parse(ctx.one_json!) as IAgentConf;
}

const agentConf: IAgentConf = JSON.parse(ctx.one_json);
const kernel_id = agentConf.kernel_id;
if (kernel_id === undefined) {
throw new Error('kernel_id is required');
}
if (!agentConf.bundled_code) {
throw new Error(`bundled_code is required`);
}

const filesystemMapping =
ctx.one_json === undefined
? Object.fromEntries(
await Promise.all(
[ctx.env!.AGENT_CONF_PATH].map(
async (path) =>
[
await envCtx.createHashOfSHA256(path),
{ path, content: await envCtx.readFileAsBase64(path) },
] as const,
),
),
)
: {
[await envCtx.createHashOfSHA256('/agent_conf.json')]: {
path: '/agent_conf.json',
content: await envCtx.toBase64String(JSON.stringify(agentConf)),
},
};

const escapedKernelID = kernel_id.replace(/\/|_/g, '').toLocaleLowerCase();
const manifest_key = ctx.key;

return {
deployment: {
Expand All @@ -114,25 +84,25 @@ export default (context: IExtensionContext) => {
labels: {
'y.ntnl.io/version': ctx.version ?? envCtx.version,
'y.ntnl.io/component': 'agent',
'y.ntnl.io/kernel_id': escapedKernelID,
'y.ntnl.io/manifest-key': manifest_key,
},
name: `agent-${escapedKernelID}`,
name: `agent-${manifest_key}`,
namespace: 'yuan',
},
spec: {
replicas: 1,
selector: {
matchLabels: {
'y.ntnl.io/component': 'agent',
'y.ntnl.io/kernel_id': escapedKernelID,
'y.ntnl.io/manifest-key': manifest_key,
},
},
template: {
metadata: {
labels: {
'y.ntnl.io/version': ctx.version ?? envCtx.version,
'y.ntnl.io/component': 'agent',
'y.ntnl.io/kernel_id': escapedKernelID,
'y.ntnl.io/manifest-key': manifest_key,
},
},
spec: {
Expand All @@ -148,8 +118,8 @@ export default (context: IExtensionContext) => {
memory: ctx.memory?.max ?? '256Mi',
},
requests: {
cpu: ctx.cpu?.min ?? '200m',
memory: ctx.memory?.min ?? '256Mi',
cpu: ctx.cpu?.min ?? '20m',
memory: ctx.memory?.min ?? '128Mi',
},
},
volumeMounts: [
Expand All @@ -164,13 +134,13 @@ export default (context: IExtensionContext) => {
{
name: 'agent-config',
secret: {
secretName: `agent-${escapedKernelID}-config`,
items: await Promise.all(
Object.entries(filesystemMapping).map(async ([k, { path }]) => ({
key: k,
path: path.substring(1),
})),
),
secretName: `agent-${manifest_key}`,
items: [
{
key: 'agent_conf.json',
path: 'agent_conf.json',
},
],
},
},
],
Expand All @@ -188,15 +158,18 @@ export default (context: IExtensionContext) => {
apiVersion: 'v1',
kind: 'Secret',
metadata: {
name: `agent-${escapedKernelID}-config`,
name: `agent-${manifest_key}`,
namespace: 'yuan',
labels: {
'y.ntnl.io/version': ctx.version ?? envCtx.version,
'y.ntnl.io/component': 'agent',
'y.ntnl.io/kernel_id': escapedKernelID,
'y.ntnl.io/manifest-key': manifest_key,
},
},
data: Object.fromEntries(Object.entries(filesystemMapping).map(([k, { content }]) => [k, content])),
// data: Object.fromEntries(Object.entries(filesystemMapping).map(([k, { content }]) => [k, content])),
data: {
'agent_conf.json': await envCtx.toBase64String(ctx.one_json!),
},
},
prometheusRule: {
apiVersion: 'monitoring.coreos.com/v1',
Expand All @@ -205,14 +178,15 @@ export default (context: IExtensionContext) => {
labels: {
'y.ntnl.io/component': 'agent',
'y.ntnl.io/version': ctx.version ?? envCtx.version,
'y.ntnl.io/manifest-key': manifest_key,
},
name: 'agent-prometheus-rules',
name: `agent-${manifest_key}`,
namespace: 'yuan',
},
spec: {
groups: [
{
name: 'agent.rules',
name: `agent.rules-${manifest_key}`,
rules: [
{
alert: 'AgentDataSelfCheckError',
Expand Down
10 changes: 10 additions & 0 deletions common/changes/@yuants/app-agent/2023-11-24-10-31.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/app-agent",
"comment": "update extension k8s objects generation logic",
"type": "patch"
}
],
"packageName": "@yuants/app-agent"
}
10 changes: 10 additions & 0 deletions common/changes/@yuants/extension/2023-11-24-10-31.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/extension",
"comment": "refine types",
"type": "minor"
}
],
"packageName": "@yuants/extension"
}
10 changes: 8 additions & 2 deletions libraries/extension/etc/extension.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { JSONSchema7 } from 'json-schema';

// @public
export interface IDeployProvider {
make_docker_compose_file: (ctx: IDeploySpec, envCtx: IEnvContext) => Promise<object>;
make_docker_compose_file: (ctx: IDeploySpec, envCtx: IEnvContext) => Promise<unknown>;
make_json_schema: () => JSONSchema7;
make_k8s_resource_objects: (ctx: IDeploySpec, envCtx: IEnvContext) => Promise<object>;
make_k8s_resource_objects: (ctx: IDeploySpec, envCtx: IEnvContext) => Promise<Record<string, unknown>>;
}

// @public
Expand All @@ -33,11 +33,17 @@ export interface IDeploySpec {
// @public
export interface IEnvContext {
createHashOfSHA256: (content: string) => Promise<string>;
// @deprecated (undocumented)
isDirectory: (path: string) => Promise<boolean>;
// @deprecated (undocumented)
readdir: (path: string) => Promise<string[]>;
// @deprecated (undocumented)
readFile: (path: string) => Promise<string>;
// @deprecated (undocumented)
readFileAsBase64: (path: string) => Promise<string>;
// @deprecated (undocumented)
resolveLocal: (path: string) => Promise<string>;
// @deprecated (undocumented)
toBase64String: (str: string) => Promise<string>;
version: string;
}
Expand Down
10 changes: 8 additions & 2 deletions libraries/extension/src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface IEnvContext {
version: string;

/**
* @deprecated
* Resolves the path and gives the absolute path. Resolve does not verify the existence of the file.
*
* @param path - An absolute path under the current Workspace, or a relative path to the Manifests' own absolute path.
Expand All @@ -103,6 +104,7 @@ export interface IEnvContext {
resolveLocal: (path: string) => Promise<string>;

/**
* @deprecated
* Reads a file and encodes it in UTF-8 format.
*
* Reads the file pointed to by `path` and returns the UTF-8 encoded string. If the path does not point to a file, returns undefined.
Expand All @@ -119,6 +121,7 @@ export interface IEnvContext {
*/

/**
* @deprecated
* Reads the file pointed to by `path` and returns the base64 encoded string. If the path does not point to a file, returns undefined.
* @param path - An absolute path under the current Workspace, or a relative path to the Manifests' own absolute path.
* It is required to be resolvable by the provider of {@link IEnvContext}.
Expand All @@ -127,13 +130,15 @@ export interface IEnvContext {
readFileAsBase64: (path: string) => Promise<string>;

/**
* @deprecated
* Encodes a UTF-8 string in base64 format.
* @param str - The string to encode.
* @returns The base64 encoded string.
*/
toBase64String: (str: string) => Promise<string>;

/**
* @deprecated
* Reads the file names in the directory pointed to by `path`. If the path does not point to a directory, returns an empty array.
* @param path - An absolute path under the current Workspace, or a relative path to the Manifests' own absolute path.
* It is required to be resolvable by the provider of {@link IEnvContext}.
Expand All @@ -142,6 +147,7 @@ export interface IEnvContext {
readdir: (path: string) => Promise<string[]>;

/**
* @deprecated
* Determines whether the path points to a directory.
* @param path - An absolute path under the current Workspace, or a relative path to the Manifests' own absolute path.
* It is required to be resolvable by the provider of {@link IEnvContext}.
Expand Down Expand Up @@ -261,15 +267,15 @@ export interface IDeployProvider {
* @param envCtx - The environment context.
* @returns The Docker Compose file.
*/
make_docker_compose_file: (ctx: IDeploySpec, envCtx: IEnvContext) => Promise<object>;
make_docker_compose_file: (ctx: IDeploySpec, envCtx: IEnvContext) => Promise<unknown>;

/**
* Generates Kubernetes resource objects for the deployment specification.
* @param ctx - The deployment specification.
* @param envCtx - The environment context.
* @returns The Kubernetes resource objects.
*/
make_k8s_resource_objects: (ctx: IDeploySpec, envCtx: IEnvContext) => Promise<object>;
make_k8s_resource_objects: (ctx: IDeploySpec, envCtx: IEnvContext) => Promise<Record<string, unknown>>;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ui/web/src/modules/Deploy/DeployConfigForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ registerPage('DeployConfigForm', () => {
return [config, envCtx, task] as const;
}),
concatMap(async ([ctx, envCtx, tasks]) => await tasks.make_docker_compose_file(ctx, envCtx)),
reduce((acc, cur) => ({ ...acc, ...cur }), {}),
reduce((acc, cur: any) => ({ ...acc, ...cur }), {}),
map((v) => ({
version: '3.9',
services: v,
Expand Down

0 comments on commit 80d8664

Please sign in to comment.