feat(TALE-33): add debug logging with DEBUG env

This commit is contained in:
Aarnav Tale
2024-10-02 13:33:39 -04:00
parent 1d821251a9
commit 1d6066d3f0
8 changed files with 88 additions and 6 deletions
+10 -1
View File
@@ -24,6 +24,7 @@ export default createIntegration<Context>({
isAvailable: async (context) => {
// Check for the HEADSCALE_CONTAINER environment variable first
// to avoid unnecessary fetching of the Docker socket
log.debug('INTG', 'Checking Docker integration availability')
context.container = process.env.HEADSCALE_CONTAINER
?.trim()
.toLowerCase()
@@ -76,7 +77,8 @@ export default createIntegration<Context>({
url.pathname,
)
await access(url.pathname, constants.R_OK)
} catch {
} catch (error) {
log.debug('INTG', 'Failed to access Docker socket: %s', error)
log.error('INTG', 'Failed to access Docker socket: %s',
path,
)
@@ -100,6 +102,12 @@ export default createIntegration<Context>({
let attempts = 0
while (attempts <= context.maxAttempts) {
log.debug(
'INTG', 'Restarting container: %s (attempt %d)',
context.container,
attempts,
)
const response = await context.client.request({
method: 'POST',
path: `/v1.30/containers/${context.container}/restart`,
@@ -123,6 +131,7 @@ export default createIntegration<Context>({
attempts = 0
while (attempts <= context.maxAttempts) {
try {
log.debug('INTG', 'Checking Headscale status (attempt %d)', attempts)
await pull('v1', '')
return
} catch (error) {
+17 -1
View File
@@ -26,6 +26,7 @@ export default createIntegration<Context>({
const svcRoot = Config.SERVICEACCOUNT_ROOT
try {
log.debug('INTG', 'Checking Kubernetes service account at %s', svcRoot)
const files = await readdir(svcRoot)
if (files.length === 0) {
log.error('INTG', 'Kubernetes service account not found')
@@ -39,6 +40,7 @@ export default createIntegration<Context>({
Config.SERVICEACCOUNT_NAMESPACE_PATH,
]
log.debug('INTG', 'Looking for %s', expectedFiles.join(', '))
if (!expectedFiles.every(file => mappedFiles.has(file))) {
log.error('INTG', 'Malformed Kubernetes service account')
return false
@@ -48,6 +50,7 @@ export default createIntegration<Context>({
return false
}
log.debug('INTG', 'Reading Kubernetes service account at %s', svcRoot)
const namespace = await readFile(
Config.SERVICEACCOUNT_NAMESPACE_PATH,
'utf8',
@@ -68,7 +71,13 @@ export default createIntegration<Context>({
return false
}
log.debug('INTG', 'Checking Kubernetes pod %s in namespace %s',
pod,
namespace,
)
try {
log.debug('INTG', 'Attempgin to get cluster KubeConfig')
const kc = new KubeConfig()
kc.loadFromCluster()
@@ -91,6 +100,7 @@ export default createIntegration<Context>({
kCoreV1Api.basePath,
)
log.debug('INTG', 'Reading pod info for %s', pod)
const { response, body } = await kCoreV1Api.readNamespacedPod(
pod,
namespace,
@@ -103,6 +113,7 @@ export default createIntegration<Context>({
return false
}
log.debug('INTG', 'Got pod info: %o', body.spec)
const shared = body.spec?.shareProcessNamespace
if (shared === undefined) {
log.error(
@@ -127,6 +138,7 @@ export default createIntegration<Context>({
}
}
log.debug('INTG', 'Looking for namespaced process in /proc')
const dir = resolve('/proc')
try {
const subdirs = await readdir(dir)
@@ -139,11 +151,14 @@ export default createIntegration<Context>({
const path = join('/proc', dir, 'cmdline')
try {
log.debug('INTG', 'Reading %s', path)
const data = await readFile(path, 'utf8')
if (data.includes('headscale')) {
return pid
}
} catch {}
} catch (error) {
log.debug('INTG', 'Failed to read %s: %s', path, error)
}
})
const results = await Promise.allSettled(promises)
@@ -155,6 +170,7 @@ export default createIntegration<Context>({
}
}
log.debug('INTG', 'Found Headscale processes: %o', pids)
if (pids.length > 1) {
log.error('INTG', 'Found %d Headscale processes: %s',
pids.length,
+6 -1
View File
@@ -22,6 +22,7 @@ export default createIntegration<Context>({
return false
}
log.debug('INTG', 'Checking /proc for Headscale process')
const dir = resolve('/proc')
try {
const subdirs = await readdir(dir)
@@ -34,11 +35,14 @@ export default createIntegration<Context>({
const path = join('/proc', dir, 'cmdline')
try {
log.debug('INTG', 'Reading %s', path)
const data = await readFile(path, 'utf8')
if (data.includes('headscale')) {
return pid
}
} catch {}
} catch (error) {
log.error('INTG', 'Failed to read %s: %s', path, error)
}
})
const results = await Promise.allSettled(promises)
@@ -50,6 +54,7 @@ export default createIntegration<Context>({
}
}
log.debug('INTG', 'Found Headscale processes: %o', pids)
if (pids.length > 1) {
log.error('INTG', 'Found %d Headscale processes: %s',
pids.length,