@xrystal/core 3.55.64 → 3.55.65
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
CHANGED
|
@@ -6,6 +6,11 @@ import { printCommandFooter, printCommandHeader } from '../helpers/banner.mjs'
|
|
|
6
6
|
import { CLI_PRIMARY_NAME } from '../helpers/cli-identity.mjs'
|
|
7
7
|
import { readCliPackageJson } from '../helpers/project-root.mjs'
|
|
8
8
|
import { formatInfraResolveHint, resolveInfraHelperScript } from '../helpers/resolve-infra-helper.mjs'
|
|
9
|
+
import {
|
|
10
|
+
connectTelepresence,
|
|
11
|
+
quitTelepresence,
|
|
12
|
+
runTelepresenceIntercept,
|
|
13
|
+
} from '../helpers/telepresence-runtime.mjs'
|
|
9
14
|
import {
|
|
10
15
|
ensureServiceTelepresenceEnv,
|
|
11
16
|
resolveTelepresenceServiceDir,
|
|
@@ -13,14 +18,6 @@ import {
|
|
|
13
18
|
} from '../helpers/telepresence-layout.mjs'
|
|
14
19
|
import { findWorkspaceRoot } from '../helpers/workspace-resolve.mjs'
|
|
15
20
|
|
|
16
|
-
function runHelper(scriptPath, args, { cwd } = {}) {
|
|
17
|
-
execSync(`bash "${scriptPath}" ${args.map((a) => JSON.stringify(a)).join(' ')}`, {
|
|
18
|
-
cwd: cwd ?? process.cwd(),
|
|
19
|
-
stdio: 'inherit',
|
|
20
|
-
env: process.env,
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
|
|
24
21
|
function requireInfraScript(name, { cliName } = {}) {
|
|
25
22
|
const script = resolveInfraHelperScript(name, { cwd: process.cwd() })
|
|
26
23
|
if (!script) {
|
|
@@ -53,47 +50,51 @@ export function registerTelepresenceCommand(cli, { cliName = CLI_PRIMARY_NAME }
|
|
|
53
50
|
${chalk.gray('intercept/dev')} = local intercept + run process (NOT the k8s namespace)
|
|
54
51
|
${chalk.gray('--ns')} = Kubernetes namespace (default: dev)
|
|
55
52
|
${chalk.gray('<deploy>')} = ${TELEPRESENCE_DEPLOYMENTS.join(', ')}
|
|
56
|
-
${chalk.gray('
|
|
53
|
+
${chalk.gray('kubeconfig:')} workspace tmp.yml, KUBECONFIG, data/k3s/cluster.kubeconfig
|
|
57
54
|
`,
|
|
58
55
|
)
|
|
59
56
|
|
|
60
|
-
tp
|
|
61
|
-
.command('connect')
|
|
57
|
+
tp.command('connect')
|
|
62
58
|
.description('Connect local machine to cluster namespace')
|
|
63
59
|
.option('--ns <namespace>', 'Kubernetes namespace', 'dev')
|
|
64
60
|
.action((options) => {
|
|
65
61
|
const version = readCliPackageJson().version
|
|
66
62
|
printCommandHeader({ cliName, command: 'telepresence connect', version })
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
63
|
+
try {
|
|
64
|
+
const code = connectTelepresence({ ns: options.ns, cwd: process.cwd() })
|
|
65
|
+
if (code !== 0) process.exit(code)
|
|
66
|
+
} catch (err) {
|
|
67
|
+
console.error(chalk.red(`\n✗ ${err.message}\n`))
|
|
68
|
+
process.exit(1)
|
|
69
|
+
}
|
|
70
70
|
printCommandFooter({ version })
|
|
71
71
|
})
|
|
72
72
|
|
|
73
|
-
tp
|
|
74
|
-
.command('quit')
|
|
73
|
+
tp.command('quit')
|
|
75
74
|
.description('Disconnect Telepresence')
|
|
76
75
|
.action(() => {
|
|
77
76
|
const version = readCliPackageJson().version
|
|
78
77
|
printCommandHeader({ cliName, command: 'telepresence quit', version })
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
try {
|
|
79
|
+
quitTelepresence()
|
|
80
|
+
} catch (err) {
|
|
81
|
+
console.error(chalk.red(`\n✗ ${err.message}\n`))
|
|
82
|
+
process.exit(1)
|
|
83
|
+
}
|
|
81
84
|
printCommandFooter({ version })
|
|
82
85
|
})
|
|
83
86
|
|
|
84
|
-
tp
|
|
85
|
-
.command('install')
|
|
87
|
+
tp.command('install')
|
|
86
88
|
.description('Install Telepresence OSS client (~2.31.x, matches cluster chart)')
|
|
87
89
|
.action(() => {
|
|
88
90
|
const version = readCliPackageJson().version
|
|
89
91
|
printCommandHeader({ cliName, command: 'telepresence install', version })
|
|
90
92
|
const script = requireInfraScript('telepresence-client-install.sh', { cliName })
|
|
91
|
-
|
|
93
|
+
execSync(`bash "${script}"`, { stdio: 'inherit', env: process.env })
|
|
92
94
|
printCommandFooter({ version })
|
|
93
95
|
})
|
|
94
96
|
|
|
95
|
-
|
|
96
|
-
.command('intercept')
|
|
97
|
+
tp.command('intercept')
|
|
97
98
|
.alias('dev')
|
|
98
99
|
.description('Intercept cluster deployment and run local process (--ns = k8s namespace, not this subcommand name)')
|
|
99
100
|
.argument('<deploy>', `Deployment name (${TELEPRESENCE_DEPLOYMENTS.join(', ')})`)
|
|
@@ -124,7 +125,6 @@ export function registerTelepresenceCommand(cli, { cliName = CLI_PRIMARY_NAME }
|
|
|
124
125
|
}
|
|
125
126
|
|
|
126
127
|
const env = ensureServiceTelepresenceEnv(svcDir)
|
|
127
|
-
process.env.TELEPRESENCE_ENV_FILE = env.envRel
|
|
128
128
|
process.env.NS = ns
|
|
129
129
|
if (workspaceRoot) process.env.XRYSTAL_WORKSPACE = workspaceRoot
|
|
130
130
|
|
|
@@ -133,17 +133,24 @@ export function registerTelepresenceCommand(cli, { cliName = CLI_PRIMARY_NAME }
|
|
|
133
133
|
console.log(chalk.gray(` service: ${svcDir}`))
|
|
134
134
|
console.log(chalk.gray(` env: ${env.envRel}${env.created ? chalk.green(' (created)') : ''}\n`))
|
|
135
135
|
|
|
136
|
-
const interceptScript = requireInfraScript('telepresence-intercept.sh', { cliName })
|
|
137
|
-
|
|
138
136
|
const portArg = parsePort(port)
|
|
139
|
-
process.env.PORT = portArg
|
|
140
137
|
const rest = command.args.slice(/^\d+$/.test(String(port ?? '')) ? 2 : 1)
|
|
141
138
|
const localCmd = rest.length ? rest : ['xry', 'dev']
|
|
142
|
-
const args = [`NS=${ns}`, deployName, portArg, '--', ...localCmd]
|
|
143
139
|
|
|
144
140
|
console.log(chalk.gray(` local port: ${portArg} (pod container :3000)`))
|
|
145
141
|
|
|
146
|
-
|
|
147
|
-
|
|
142
|
+
try {
|
|
143
|
+
runTelepresenceIntercept({
|
|
144
|
+
deploy: deployName,
|
|
145
|
+
ns,
|
|
146
|
+
port: portArg,
|
|
147
|
+
envFile: env.envPath,
|
|
148
|
+
localCmd,
|
|
149
|
+
cwd: svcDir,
|
|
150
|
+
})
|
|
151
|
+
} catch (err) {
|
|
152
|
+
console.error(chalk.red(`\n✗ ${err.message}\n`))
|
|
153
|
+
process.exit(1)
|
|
154
|
+
}
|
|
148
155
|
})
|
|
149
156
|
}
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { execSync, spawnSync } from 'node:child_process'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
|
|
6
|
+
import { listInfraRootCandidates } from './resolve-infra-helper.mjs'
|
|
7
|
+
import { getCliConfigInfraDir } from './xrystal-config.mjs'
|
|
8
|
+
import { findWorkspaceRoot, resolveWorkspaceOuterTmp } from './workspace-resolve.mjs'
|
|
9
|
+
|
|
10
|
+
const ALLOWED_NS = new Set(['dev', 'test'])
|
|
11
|
+
const DEFAULT_MANAGER_NS = 'telepresence'
|
|
12
|
+
|
|
13
|
+
function expandHome(filePath) {
|
|
14
|
+
const raw = String(filePath ?? '').trim()
|
|
15
|
+
if (!raw) return raw
|
|
16
|
+
if (raw.startsWith('~/')) return path.join(os.homedir(), raw.slice(2))
|
|
17
|
+
if (raw === '~') return os.homedir()
|
|
18
|
+
return raw
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function parseKubeconfigLine(tmpPath) {
|
|
22
|
+
const text = fs.readFileSync(tmpPath, 'utf8')
|
|
23
|
+
const match = text.match(/^kubeconfig:\s*(.+?)\s*(?:#.*)?$/m)
|
|
24
|
+
if (!match) return null
|
|
25
|
+
let value = match[1].trim()
|
|
26
|
+
if (
|
|
27
|
+
(value.startsWith('"') && value.endsWith('"')) ||
|
|
28
|
+
(value.startsWith("'") && value.endsWith("'"))
|
|
29
|
+
) {
|
|
30
|
+
value = value.slice(1, -1)
|
|
31
|
+
}
|
|
32
|
+
return expandHome(value)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Laptop kubeconfig — no ansible-var; optional infra data/k3s path only as file fallback. */
|
|
36
|
+
export function resolveKubeconfig(cwd = process.cwd()) {
|
|
37
|
+
const fromEnv = process.env.KUBECONFIG
|
|
38
|
+
if (fromEnv) {
|
|
39
|
+
for (const part of fromEnv.split(path.delimiter)) {
|
|
40
|
+
const candidate = expandHome(part.trim())
|
|
41
|
+
if (candidate && fs.existsSync(candidate)) return candidate
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const workspaceRoot = findWorkspaceRoot(cwd)
|
|
46
|
+
if (workspaceRoot) {
|
|
47
|
+
const tmpPath = resolveWorkspaceOuterTmp(workspaceRoot)
|
|
48
|
+
if (tmpPath && fs.existsSync(tmpPath)) {
|
|
49
|
+
const fromTmp = parseKubeconfigLine(tmpPath)
|
|
50
|
+
if (fromTmp && fs.existsSync(fromTmp)) return fromTmp
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const configuredInfra = getCliConfigInfraDir()
|
|
55
|
+
if (configuredInfra) {
|
|
56
|
+
const candidate = path.join(configuredInfra, 'data', 'k3s', 'cluster.kubeconfig')
|
|
57
|
+
if (fs.existsSync(candidate)) return candidate
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
for (const root of listInfraRootCandidates(cwd)) {
|
|
61
|
+
const candidate = path.join(root, 'data', 'k3s', 'cluster.kubeconfig')
|
|
62
|
+
if (fs.existsSync(candidate)) return candidate
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const defaultPath = path.join(os.homedir(), '.kube', 'config')
|
|
66
|
+
if (fs.existsSync(defaultPath)) return defaultPath
|
|
67
|
+
|
|
68
|
+
return null
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function ensureTelepresenceOnPath() {
|
|
72
|
+
if (commandExists('telepresence')) return
|
|
73
|
+
const localBin = path.join(os.homedir(), '.local', 'bin', 'telepresence')
|
|
74
|
+
if (fs.existsSync(localBin)) {
|
|
75
|
+
process.env.PATH = `${path.dirname(localBin)}${path.delimiter}${process.env.PATH ?? ''}`
|
|
76
|
+
}
|
|
77
|
+
if (!commandExists('telepresence')) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
'Install Telepresence client (match cluster chart ~2.31.x): xry tp install — https://telepresence.io/docs/install/client',
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function commandExists(name) {
|
|
85
|
+
try {
|
|
86
|
+
execSync(`command -v ${name}`, { stdio: 'ignore', shell: true })
|
|
87
|
+
return true
|
|
88
|
+
} catch {
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function assertNamespaceAllowed(ns) {
|
|
94
|
+
if (ns === 'prod') {
|
|
95
|
+
throw new Error('Refusing prod namespace — Telepresence is scoped to dev/staging only')
|
|
96
|
+
}
|
|
97
|
+
if (!ALLOWED_NS.has(ns)) {
|
|
98
|
+
throw new Error(`Unknown NS=${ns} (use dev or test)`)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function telepresenceStatusText() {
|
|
103
|
+
try {
|
|
104
|
+
return execSync('telepresence status 2>&1', { encoding: 'utf8' })
|
|
105
|
+
} catch (err) {
|
|
106
|
+
return `${err.stdout ?? ''}${err.stderr ?? ''}${err.message ?? ''}`
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function isTelepresenceConnected(ns) {
|
|
111
|
+
const status = telepresenceStatusText()
|
|
112
|
+
if (!/Traffic Manager.*Connected/i.test(status)) return false
|
|
113
|
+
return new RegExp(`Namespace\\s*:\\s*${ns}([\\s]|$)`).test(status)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function resetDaemonIfExecutableMismatch() {
|
|
117
|
+
try {
|
|
118
|
+
if (!commandExists('telepresence')) return
|
|
119
|
+
const status = telepresenceStatusText()
|
|
120
|
+
if (/executable mismatch/i.test(status)) {
|
|
121
|
+
console.error('Resetting Telepresence daemon (executable path changed)...')
|
|
122
|
+
execSync('telepresence quit -s', { stdio: 'ignore', shell: true })
|
|
123
|
+
execSync('sleep 1', { stdio: 'ignore', shell: true })
|
|
124
|
+
}
|
|
125
|
+
} catch {
|
|
126
|
+
/* ignore */
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function connectTelepresence({
|
|
131
|
+
ns = 'dev',
|
|
132
|
+
managerNs = process.env.TELEPRESENCE_MANAGER_NS || DEFAULT_MANAGER_NS,
|
|
133
|
+
cwd = process.cwd(),
|
|
134
|
+
extraArgs = [],
|
|
135
|
+
} = {}) {
|
|
136
|
+
assertNamespaceAllowed(ns)
|
|
137
|
+
ensureTelepresenceOnPath()
|
|
138
|
+
const kubeconfig = resolveKubeconfig(cwd)
|
|
139
|
+
if (!kubeconfig) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
'Kubeconfig not found — set KUBECONFIG, workspace tmp kubeconfig:, make kubeconfig-sync, or ~/.kube/config',
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
resetDaemonIfExecutableMismatch()
|
|
145
|
+
console.log(`Connecting to cluster (namespace=${ns}, kubeconfig=${kubeconfig})...`)
|
|
146
|
+
console.log(`Traffic manager namespace: ${managerNs}`)
|
|
147
|
+
const env = { ...process.env, KUBECONFIG: kubeconfig }
|
|
148
|
+
const result = spawnSync(
|
|
149
|
+
'telepresence',
|
|
150
|
+
['connect', '--namespace', ns, '--manager-namespace', managerNs, ...extraArgs],
|
|
151
|
+
{ stdio: 'inherit', env },
|
|
152
|
+
)
|
|
153
|
+
if (result.error) throw result.error
|
|
154
|
+
return result.status ?? 1
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function quitTelepresence() {
|
|
158
|
+
ensureTelepresenceOnPath()
|
|
159
|
+
try {
|
|
160
|
+
const json = execSync('telepresence list --output json 2>/dev/null', {
|
|
161
|
+
encoding: 'utf8',
|
|
162
|
+
shell: true,
|
|
163
|
+
})
|
|
164
|
+
let items = []
|
|
165
|
+
try {
|
|
166
|
+
items = JSON.parse(json)
|
|
167
|
+
} catch {
|
|
168
|
+
items = []
|
|
169
|
+
}
|
|
170
|
+
if (!Array.isArray(items)) items = []
|
|
171
|
+
for (const it of items) {
|
|
172
|
+
const name = it?.name || it?.workload
|
|
173
|
+
const itemNs = it?.namespace || 'dev'
|
|
174
|
+
if (!name) continue
|
|
175
|
+
try {
|
|
176
|
+
execSync(`telepresence detach "${name}" --namespace "${itemNs}"`, {
|
|
177
|
+
stdio: 'ignore',
|
|
178
|
+
shell: true,
|
|
179
|
+
})
|
|
180
|
+
} catch {
|
|
181
|
+
/* ignore */
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
} catch {
|
|
185
|
+
/* no active intercepts */
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
execSync('telepresence quit -s', { stdio: 'ignore', shell: true })
|
|
189
|
+
} catch {
|
|
190
|
+
try {
|
|
191
|
+
execSync('telepresence quit', { stdio: 'ignore', shell: true })
|
|
192
|
+
} catch {
|
|
193
|
+
/* ignore */
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
console.log('Telepresence stopped.')
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function resolveCertificatesRoot(cwd) {
|
|
200
|
+
if (process.env.TELEPRESENCE_CERT_ROOT) {
|
|
201
|
+
const root = expandHome(process.env.TELEPRESENCE_CERT_ROOT)
|
|
202
|
+
if (fs.existsSync(root)) return root
|
|
203
|
+
}
|
|
204
|
+
const configuredInfra = getCliConfigInfraDir()
|
|
205
|
+
if (configuredInfra) {
|
|
206
|
+
const candidate = path.join(configuredInfra, 'data', 'certificates')
|
|
207
|
+
if (fs.existsSync(candidate)) return candidate
|
|
208
|
+
}
|
|
209
|
+
for (const root of listInfraRootCandidates(cwd)) {
|
|
210
|
+
const candidate = path.join(root, 'data', 'certificates')
|
|
211
|
+
if (fs.existsSync(candidate)) return candidate
|
|
212
|
+
}
|
|
213
|
+
return null
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function syncTlsForLocalIntercept(cwd) {
|
|
217
|
+
const certRoot = resolveCertificatesRoot(cwd)
|
|
218
|
+
if (!certRoot) {
|
|
219
|
+
console.error('Skipping local TLS sync (set TELEPRESENCE_CERT_ROOT or xry config infra for make tls-copy-from-host)')
|
|
220
|
+
return
|
|
221
|
+
}
|
|
222
|
+
const services = path.join(certRoot, 'services')
|
|
223
|
+
const rootCa = path.join(certRoot, 'root')
|
|
224
|
+
if (!fs.existsSync(rootCa) || !fs.existsSync(services)) {
|
|
225
|
+
console.error(`Missing ${certRoot} — run: make tls-copy-from-host (infra repo)`)
|
|
226
|
+
process.exit(1)
|
|
227
|
+
}
|
|
228
|
+
const dest = process.env.TELEPRESENCE_TLS_DIR || '/etc/tls/infra'
|
|
229
|
+
if (!fs.existsSync(dest)) {
|
|
230
|
+
console.error(`Ensuring ${dest} (may ask sudo once)...`)
|
|
231
|
+
execSync(`sudo mkdir -p "${dest}" && sudo chown "$(id -u):$(id -g)" "${dest}" && chmod 755 "${dest}"`, {
|
|
232
|
+
shell: true,
|
|
233
|
+
stdio: 'inherit',
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
const copy = (destName, src) => {
|
|
237
|
+
if (!fs.existsSync(src)) {
|
|
238
|
+
console.error(`Missing: ${src} — run: make tls-copy-from-host`)
|
|
239
|
+
process.exit(1)
|
|
240
|
+
}
|
|
241
|
+
fs.copyFileSync(src, path.join(dest, destName))
|
|
242
|
+
console.log(` ${path.join(dest, destName)} ← ${src}`)
|
|
243
|
+
}
|
|
244
|
+
console.log(`TLS from ${certRoot} → ${dest}`)
|
|
245
|
+
copy('ca.crt', path.join(rootCa, 'ca.crt'))
|
|
246
|
+
copy('mongodb.pem', path.join(services, 'mongodb', 'mongodb.pem'))
|
|
247
|
+
copy('mongodb.crt', path.join(services, 'mongodb', 'mongodb.crt'))
|
|
248
|
+
copy('redis.pem', path.join(services, 'redis', 'redis.pem'))
|
|
249
|
+
copy('redis.crt', path.join(services, 'redis', 'redis.crt'))
|
|
250
|
+
copy('kafka.crt', path.join(services, 'kafka', 'kafka.crt'))
|
|
251
|
+
copy('truststore.jks', path.join(services, 'kafka', 'truststore.jks'))
|
|
252
|
+
console.log(`TLS ready at ${dest}`)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function preflightReadyDeployment({ deploy, ns, cwd }) {
|
|
256
|
+
if (!commandExists('kubectl')) {
|
|
257
|
+
console.error('Warning: kubectl not in PATH — skipping Ready pod preflight.')
|
|
258
|
+
return
|
|
259
|
+
}
|
|
260
|
+
const kubeconfig = resolveKubeconfig(cwd)
|
|
261
|
+
if (!kubeconfig) {
|
|
262
|
+
console.error('Warning: kubeconfig not found — skipping Ready pod preflight.')
|
|
263
|
+
return
|
|
264
|
+
}
|
|
265
|
+
const env = { ...process.env, KUBECONFIG: kubeconfig }
|
|
266
|
+
let ready = '0'
|
|
267
|
+
try {
|
|
268
|
+
ready = execSync(`kubectl get deployment "${deploy}" -n "${ns}" -o jsonpath='{.status.readyReplicas}'`, {
|
|
269
|
+
encoding: 'utf8',
|
|
270
|
+
env,
|
|
271
|
+
}).trim()
|
|
272
|
+
} catch {
|
|
273
|
+
ready = '0'
|
|
274
|
+
}
|
|
275
|
+
const readyNum = /^\d+$/.test(ready) ? Number(ready) : 0
|
|
276
|
+
if (readyNum >= 1) return
|
|
277
|
+
console.error(
|
|
278
|
+
`Intercept blocked: Deployment "${deploy}" in namespace "${ns}" has no Ready pod (readyReplicas=${readyNum}).`,
|
|
279
|
+
)
|
|
280
|
+
console.error('Telepresence HTTP intercept needs a healthy cluster pod to host the node-agent.')
|
|
281
|
+
try {
|
|
282
|
+
execSync(`kubectl get deployment "${deploy}" -n "${ns}"`, { stdio: 'inherit', env })
|
|
283
|
+
execSync(`kubectl get pods -n "${ns}" -l app=${deploy}`, { stdio: 'inherit', env })
|
|
284
|
+
} catch {
|
|
285
|
+
/* ignore */
|
|
286
|
+
}
|
|
287
|
+
process.exit(1)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function runTelepresenceIntercept({
|
|
291
|
+
deploy,
|
|
292
|
+
ns = 'dev',
|
|
293
|
+
port = '3000',
|
|
294
|
+
containerPort = process.env.TELEPRESENCE_CONTAINER_PORT || '3000',
|
|
295
|
+
envFile,
|
|
296
|
+
localCmd,
|
|
297
|
+
cwd = process.cwd(),
|
|
298
|
+
}) {
|
|
299
|
+
assertNamespaceAllowed(ns)
|
|
300
|
+
ensureTelepresenceOnPath()
|
|
301
|
+
|
|
302
|
+
if (!isTelepresenceConnected(ns)) {
|
|
303
|
+
console.log(`Telepresence not connected — connecting (namespace=${ns})...`)
|
|
304
|
+
const code = connectTelepresence({ ns, cwd })
|
|
305
|
+
if (code !== 0) process.exit(code)
|
|
306
|
+
if (!isTelepresenceConnected(ns)) {
|
|
307
|
+
console.error(`Connect failed. Run: xry tp connect --ns ${ns}`)
|
|
308
|
+
process.exit(1)
|
|
309
|
+
}
|
|
310
|
+
console.log('Connected.')
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
preflightReadyDeployment({ deploy, ns, cwd })
|
|
314
|
+
syncTlsForLocalIntercept(cwd)
|
|
315
|
+
|
|
316
|
+
const rawId = process.env.TELEPRESENCE_DEV_ID || process.env.USER || 'dev'
|
|
317
|
+
const devId = process.env.TELEPRESENCE_DEV_ID ? rawId : rawId.replace(/\./g, '-')
|
|
318
|
+
const header = process.env.TELEPRESENCE_INTERCEPT_HEADER || process.env.TELEPRESENCE_DEV_HEADER || 'x-intercept-who'
|
|
319
|
+
const absEnvFile = path.resolve(cwd, envFile)
|
|
320
|
+
|
|
321
|
+
console.log(
|
|
322
|
+
`Intercept ${deploy} (ns=${ns}) — local :${port} <- pod :${containerPort} | header ${header}: ${devId}`,
|
|
323
|
+
)
|
|
324
|
+
console.log('Cluster pod keeps running for traffic without this header.')
|
|
325
|
+
console.log(`Test: curl -H "${header}: ${devId}" http://${deploy}-service.${ns}:${port}/health`)
|
|
326
|
+
|
|
327
|
+
const tpArgs = [
|
|
328
|
+
'intercept',
|
|
329
|
+
deploy,
|
|
330
|
+
'--namespace',
|
|
331
|
+
ns,
|
|
332
|
+
'--mount',
|
|
333
|
+
'false',
|
|
334
|
+
'--http-header',
|
|
335
|
+
`${header}=${devId}`,
|
|
336
|
+
'--port',
|
|
337
|
+
`${port}:${containerPort}`,
|
|
338
|
+
'--env-file',
|
|
339
|
+
absEnvFile,
|
|
340
|
+
]
|
|
341
|
+
|
|
342
|
+
if (localCmd?.length) {
|
|
343
|
+
const result = spawnSync('telepresence', [...tpArgs, '--', ...localCmd], { stdio: 'inherit', cwd, env: process.env })
|
|
344
|
+
process.exit(result.status ?? 1)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const result = spawnSync('telepresence', tpArgs, { stdio: 'inherit', cwd, env: process.env })
|
|
348
|
+
process.exit(result.status ?? 1)
|
|
349
|
+
}
|