@blogic-cz/agent-tools 0.14.12 → 0.14.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/schemas/agent-tools.schema.json +8 -0
- package/src/config/loader.ts +2 -0
- package/src/config/types.ts +4 -0
- package/src/db-tool/service.ts +24 -0
- package/src/k8s-tool/service.ts +41 -11
- package/src/shared/env-template.ts +38 -0
- package/src/shared/exec.ts +2 -0
package/package.json
CHANGED
|
@@ -123,6 +123,10 @@
|
|
|
123
123
|
"type": "object",
|
|
124
124
|
"additionalProperties": false,
|
|
125
125
|
"properties": {
|
|
126
|
+
"kubeconfig": {
|
|
127
|
+
"description": "Optional kubeconfig path. Supports ${ENV_VAR} templates.",
|
|
128
|
+
"type": "string"
|
|
129
|
+
},
|
|
126
130
|
"clusterId": {
|
|
127
131
|
"description": "Cluster identifier.",
|
|
128
132
|
"type": "string"
|
|
@@ -202,6 +206,10 @@
|
|
|
202
206
|
"type": "object",
|
|
203
207
|
"additionalProperties": false,
|
|
204
208
|
"properties": {
|
|
209
|
+
"kubeconfig": {
|
|
210
|
+
"description": "Optional kubeconfig path. Supports ${ENV_VAR} templates.",
|
|
211
|
+
"type": "string"
|
|
212
|
+
},
|
|
205
213
|
"context": {
|
|
206
214
|
"description": "Kubectl context name.",
|
|
207
215
|
"type": "string"
|
package/src/config/loader.ts
CHANGED
|
@@ -75,6 +75,7 @@ const AzureConfigSchema = Schema.Struct({
|
|
|
75
75
|
});
|
|
76
76
|
|
|
77
77
|
const K8sConfigSchema = Schema.Struct({
|
|
78
|
+
kubeconfig: Schema.optionalKey(Schema.String),
|
|
78
79
|
clusterId: Schema.String,
|
|
79
80
|
namespaces: Schema.Record(Schema.String, Schema.String),
|
|
80
81
|
timeoutMs: Schema.optionalKey(Schema.Number),
|
|
@@ -95,6 +96,7 @@ const DatabaseConfigSchema = Schema.Struct({
|
|
|
95
96
|
environments: Schema.Record(Schema.String, DbEnvConfigSchema),
|
|
96
97
|
kubectl: Schema.optionalKey(
|
|
97
98
|
Schema.Struct({
|
|
99
|
+
kubeconfig: Schema.optionalKey(Schema.String),
|
|
98
100
|
context: Schema.String,
|
|
99
101
|
namespace: Schema.String,
|
|
100
102
|
service: Schema.optionalKey(Schema.String),
|
package/src/config/types.ts
CHANGED
|
@@ -63,6 +63,8 @@ export type ProfilePrerequisites = {
|
|
|
63
63
|
|
|
64
64
|
/** Kubernetes cluster profile configuration */
|
|
65
65
|
export type K8sConfig = ProfilePrerequisites & {
|
|
66
|
+
/** Optional kubeconfig path. Supports ${ENV_VAR} templates. */
|
|
67
|
+
kubeconfig?: string;
|
|
66
68
|
clusterId: string;
|
|
67
69
|
/** Named namespaces, e.g. { test: "my-app-test", prod: "my-app-prod" } */
|
|
68
70
|
namespaces: Record<string, string>;
|
|
@@ -86,6 +88,8 @@ export type DatabaseConfig = ProfilePrerequisites & {
|
|
|
86
88
|
/** Named database environments, e.g. { local: {...}, test: {...}, prod: {...} } */
|
|
87
89
|
environments: Record<string, DbEnvConfig>;
|
|
88
90
|
kubectl?: {
|
|
91
|
+
/** Optional kubeconfig path. Supports ${ENV_VAR} templates. */
|
|
92
|
+
kubeconfig?: string;
|
|
89
93
|
context: string;
|
|
90
94
|
namespace: string;
|
|
91
95
|
service?: string;
|
package/src/db-tool/service.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { DbConfig, QueryResult, SchemaMode } from "./types";
|
|
|
5
5
|
|
|
6
6
|
import { ConfigService } from "#config";
|
|
7
7
|
import { isPrerequisiteRunError } from "#shared/prerequisites/errors";
|
|
8
|
+
import { resolveEnvTemplate } from "#shared/env-template";
|
|
8
9
|
import { runWithProfilePrerequisites } from "#shared/prerequisites/runtime";
|
|
9
10
|
import { DbConfigService, DbConfigServiceLayer, TUNNEL_CHECK_INTERVAL_MS } from "./config-service";
|
|
10
11
|
import {
|
|
@@ -75,6 +76,7 @@ export class DbService extends Context.Service<
|
|
|
75
76
|
};
|
|
76
77
|
}
|
|
77
78
|
|
|
79
|
+
const kubectlKubeconfig = dbConfig.kubectl?.kubeconfig;
|
|
78
80
|
const kubectlContext = dbConfig.kubectl?.context;
|
|
79
81
|
const kubectlNamespace = dbConfig.kubectl?.namespace;
|
|
80
82
|
const kubectlService = dbConfig.kubectl?.service ?? "postgresql";
|
|
@@ -262,6 +264,24 @@ export class DbService extends Context.Service<
|
|
|
262
264
|
}
|
|
263
265
|
});
|
|
264
266
|
|
|
267
|
+
const resolveKubeconfig = Effect.fn("DbService.resolveKubeconfig")(function* (
|
|
268
|
+
port: number,
|
|
269
|
+
) {
|
|
270
|
+
if (!kubectlKubeconfig) {
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return yield* resolveEnvTemplate(kubectlKubeconfig).pipe(
|
|
275
|
+
Effect.mapError(
|
|
276
|
+
({ envVar }) =>
|
|
277
|
+
new DbTunnelError({
|
|
278
|
+
message: `Environment variable ${envVar} (required for kubeconfig) is not set.`,
|
|
279
|
+
port,
|
|
280
|
+
}),
|
|
281
|
+
),
|
|
282
|
+
);
|
|
283
|
+
});
|
|
284
|
+
|
|
265
285
|
const startTunnelProcess = (config: DbConfig) =>
|
|
266
286
|
Effect.gen(function* () {
|
|
267
287
|
if (!kubectlContext || !kubectlNamespace) {
|
|
@@ -274,10 +294,14 @@ export class DbService extends Context.Service<
|
|
|
274
294
|
);
|
|
275
295
|
}
|
|
276
296
|
|
|
297
|
+
const kubeconfig = yield* resolveKubeconfig(config.port);
|
|
298
|
+
const kubeconfigArgs = kubeconfig ? ["--kubeconfig", kubeconfig] : [];
|
|
299
|
+
|
|
277
300
|
const proc = yield* executor.spawn(
|
|
278
301
|
ChildProcess.make(
|
|
279
302
|
"kubectl",
|
|
280
303
|
[
|
|
304
|
+
...kubeconfigArgs,
|
|
281
305
|
"port-forward",
|
|
282
306
|
"--context",
|
|
283
307
|
kubectlContext,
|
package/src/k8s-tool/service.ts
CHANGED
|
@@ -11,7 +11,8 @@ import {
|
|
|
11
11
|
} from "./errors";
|
|
12
12
|
import { ConfigService, getToolConfig } from "#config";
|
|
13
13
|
import type { K8sConfig } from "#config";
|
|
14
|
-
import { collectProcessOutput } from "#shared/exec";
|
|
14
|
+
import { collectProcessOutput, quoteShellArg } from "#shared/exec";
|
|
15
|
+
import { resolveEnvTemplate } from "#shared/env-template";
|
|
15
16
|
import { isPrerequisiteRunError } from "#shared/prerequisites/errors";
|
|
16
17
|
import { runWithProfilePrerequisites } from "#shared/prerequisites/runtime";
|
|
17
18
|
import { isKubectlCommandAllowed } from "./security";
|
|
@@ -63,6 +64,28 @@ export class K8sService extends Context.Service<
|
|
|
63
64
|
return k8sConfig;
|
|
64
65
|
});
|
|
65
66
|
|
|
67
|
+
const resolveKubeconfig = Effect.fn("K8sService.resolveKubeconfig")(function* (
|
|
68
|
+
k8sConfig: K8sConfig,
|
|
69
|
+
) {
|
|
70
|
+
const kubeconfig = k8sConfig.kubeconfig;
|
|
71
|
+
if (!kubeconfig) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return yield* resolveEnvTemplate(kubeconfig).pipe(
|
|
76
|
+
Effect.mapError(
|
|
77
|
+
({ envVar }) =>
|
|
78
|
+
new K8sContextError({
|
|
79
|
+
message: `Environment variable ${envVar} (required for kubeconfig) is not set.`,
|
|
80
|
+
clusterId: k8sConfig.clusterId,
|
|
81
|
+
}),
|
|
82
|
+
),
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const withKubeconfig = (command: string, kubeconfig: string | undefined) =>
|
|
87
|
+
kubeconfig ? `KUBECONFIG=${quoteShellArg(kubeconfig)} ${command}` : command;
|
|
88
|
+
|
|
66
89
|
// Cache context by selected profile/cluster instead of a single default profile.
|
|
67
90
|
const contextRef = yield* Ref.make<Record<string, string>>({});
|
|
68
91
|
|
|
@@ -128,14 +151,18 @@ export class K8sService extends Context.Service<
|
|
|
128
151
|
k8sConfig: K8sConfig,
|
|
129
152
|
) {
|
|
130
153
|
const timeoutMs = k8sConfig.timeoutMs ?? 60000;
|
|
131
|
-
const
|
|
154
|
+
const kubeconfig = yield* resolveKubeconfig(k8sConfig);
|
|
155
|
+
const cacheKey = profile ?? `cluster:${k8sConfig.clusterId}:${kubeconfig ?? "default"}`;
|
|
132
156
|
const cached = yield* Ref.get(contextRef);
|
|
133
157
|
const cachedContext = cached[cacheKey];
|
|
134
158
|
if (cachedContext !== undefined) {
|
|
135
|
-
return cachedContext;
|
|
159
|
+
return { context: cachedContext, kubeconfig };
|
|
136
160
|
}
|
|
137
161
|
|
|
138
|
-
const jqCommand =
|
|
162
|
+
const jqCommand = withKubeconfig(
|
|
163
|
+
`kubectl config view -o json | jq -r '.contexts[] | select(.context.cluster == "${k8sConfig.clusterId}") | .name' | head -1`,
|
|
164
|
+
kubeconfig,
|
|
165
|
+
);
|
|
139
166
|
|
|
140
167
|
const contextResultOption = yield* runShellCommand(jqCommand, timeoutMs);
|
|
141
168
|
|
|
@@ -155,10 +182,13 @@ export class K8sService extends Context.Service<
|
|
|
155
182
|
...contexts,
|
|
156
183
|
[cacheKey]: resolvedContextValue,
|
|
157
184
|
}));
|
|
158
|
-
return resolvedContextValue;
|
|
185
|
+
return { context: resolvedContextValue, kubeconfig };
|
|
159
186
|
}
|
|
160
187
|
|
|
161
|
-
const fallbackCommand =
|
|
188
|
+
const fallbackCommand = withKubeconfig(
|
|
189
|
+
`kubectl config view -o json | jq -r '.contexts[] as $ctx | .clusters[] | select(.name == $ctx.context.cluster and (.cluster.server | contains("${k8sConfig.clusterId}"))) | $ctx.name' | head -1`,
|
|
190
|
+
kubeconfig,
|
|
191
|
+
);
|
|
162
192
|
|
|
163
193
|
const fallbackResultOption = yield* runShellCommand(fallbackCommand, timeoutMs);
|
|
164
194
|
|
|
@@ -178,7 +208,7 @@ export class K8sService extends Context.Service<
|
|
|
178
208
|
...contexts,
|
|
179
209
|
[cacheKey]: resolvedContextValue,
|
|
180
210
|
}));
|
|
181
|
-
return resolvedContextValue;
|
|
211
|
+
return { context: resolvedContextValue, kubeconfig };
|
|
182
212
|
}
|
|
183
213
|
|
|
184
214
|
return yield* new K8sContextError({
|
|
@@ -198,8 +228,8 @@ export class K8sService extends Context.Service<
|
|
|
198
228
|
k8sConfig,
|
|
199
229
|
runPrerequisiteCommand,
|
|
200
230
|
Effect.gen(function* () {
|
|
201
|
-
const context = yield* resolveContext(profile, k8sConfig);
|
|
202
|
-
const fullCommand = `kubectl --context ${context} ${cmd}
|
|
231
|
+
const { context, kubeconfig } = yield* resolveContext(profile, k8sConfig);
|
|
232
|
+
const fullCommand = withKubeconfig(`kubectl --context ${context} ${cmd}`, kubeconfig);
|
|
203
233
|
|
|
204
234
|
const resultOption = yield* runShellCommand(fullCommand, timeoutMs);
|
|
205
235
|
|
|
@@ -282,8 +312,8 @@ export class K8sService extends Context.Service<
|
|
|
282
312
|
const startTime = Date.now();
|
|
283
313
|
if (dryRun) {
|
|
284
314
|
const k8sConfig = yield* requireK8sConfig(profile);
|
|
285
|
-
const context = yield* resolveContext(profile, k8sConfig);
|
|
286
|
-
const fullCommand = `kubectl --context ${context} ${cmd}
|
|
315
|
+
const { context, kubeconfig } = yield* resolveContext(profile, k8sConfig);
|
|
316
|
+
const fullCommand = withKubeconfig(`kubectl --context ${context} ${cmd}`, kubeconfig);
|
|
287
317
|
return {
|
|
288
318
|
success: true,
|
|
289
319
|
command: fullCommand,
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
const envTemplateRegex = /\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g;
|
|
4
|
+
|
|
5
|
+
export class EnvTemplateError extends Schema.TaggedErrorClass<EnvTemplateError>()(
|
|
6
|
+
"@agent-tools/EnvTemplateError",
|
|
7
|
+
{ envVar: Schema.String },
|
|
8
|
+
) {}
|
|
9
|
+
|
|
10
|
+
export const resolveEnvTemplate = Effect.fn("resolveEnvTemplate")(function* (value: string) {
|
|
11
|
+
let resolved = "";
|
|
12
|
+
let lastIndex = 0;
|
|
13
|
+
const env = globalThis.Bun?.env ?? process.env;
|
|
14
|
+
|
|
15
|
+
for (const match of value.matchAll(envTemplateRegex)) {
|
|
16
|
+
const fullMatch = match[0];
|
|
17
|
+
const envVar = match[1];
|
|
18
|
+
const index = match.index;
|
|
19
|
+
if (index === undefined) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
resolved += value.slice(lastIndex, index);
|
|
24
|
+
const fromEnv = env[envVar];
|
|
25
|
+
if (fromEnv === undefined) {
|
|
26
|
+
return yield* new EnvTemplateError({ envVar });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
resolved += fromEnv;
|
|
30
|
+
lastIndex = index + fullMatch.length;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (lastIndex === 0) {
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return resolved + value.slice(lastIndex);
|
|
38
|
+
});
|