From 61967435c1b51c75e4a6d8ac84e2d707aca534f1 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Thu, 4 Jul 2024 10:13:17 +0200 Subject: [PATCH] docker: contextInspect func Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- __tests__/docker/docker.test.itg.ts | 13 ++++++++++++- __tests__/docker/docker.test.ts | 13 +++++++++++++ src/docker/docker.ts | 18 ++++++++++++++++- src/types/docker/docker.ts | 30 +++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/__tests__/docker/docker.test.itg.ts b/__tests__/docker/docker.test.itg.ts index 0cd83be..5996d3d 100644 --- a/__tests__/docker/docker.test.itg.ts +++ b/__tests__/docker/docker.test.itg.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {describe, expect, test} from '@jest/globals'; +import {describe, expect, it, test} from '@jest/globals'; import {Docker} from '../../src/docker/docker'; @@ -54,3 +54,14 @@ maybe('pull', () => { } }, 600000); }); + +maybe('contextInspect', () => { + it('inspect default context', async () => { + const contextInfo = await Docker.contextInspect(); + expect(contextInfo).toBeDefined(); + console.log('contextInfo', contextInfo); + expect(contextInfo?.Name).toBeDefined(); + expect(contextInfo?.Endpoints).toBeDefined(); + expect(Object.keys(contextInfo?.Endpoints).length).toBeGreaterThan(0); + }); +}); diff --git a/__tests__/docker/docker.test.ts b/__tests__/docker/docker.test.ts index cb75ccc..c36b2f3 100644 --- a/__tests__/docker/docker.test.ts +++ b/__tests__/docker/docker.test.ts @@ -118,6 +118,19 @@ describe('context', () => { }); }); +describe('contextInspect', () => { + it('call docker context inspect', async () => { + const execSpy = jest.spyOn(Exec, 'getExecOutput'); + await Docker.contextInspect('foo').catch(() => { + // noop + }); + expect(execSpy).toHaveBeenCalledWith(`docker`, ['context', 'inspect', '--format=json', 'foo'], { + ignoreReturnCode: true, + silent: true + }); + }); +}); + describe('printVersion', () => { it('call docker version', async () => { const execSpy = jest.spyOn(Exec, 'exec'); diff --git a/src/docker/docker.ts b/src/docker/docker.ts index 804b095..e3b55c8 100644 --- a/src/docker/docker.ts +++ b/src/docker/docker.ts @@ -25,7 +25,7 @@ import {Cache} from '../cache'; import {Exec} from '../exec'; import {Util} from '../util'; -import {ConfigFile} from '../types/docker/docker'; +import {ConfigFile, ContextInfo} from '../types/docker/docker'; export class Docker { static get configDir(): string { @@ -69,6 +69,22 @@ export class Docker { }); } + public static async contextInspect(name?: string): Promise { + const args = ['context', 'inspect', '--format=json']; + if (name) { + args.push(name); + } + return await Exec.getExecOutput(`docker`, args, { + ignoreReturnCode: true, + silent: true + }).then(res => { + if (res.stderr.length > 0 && res.exitCode != 0) { + throw new Error(res.stderr.trim()); + } + return (>JSON.parse(res.stdout.trim()))[0]; + }); + } + public static async printVersion(): Promise { await Exec.exec('docker', ['version']); } diff --git a/src/types/docker/docker.ts b/src/types/docker/docker.ts index 66cf24d..efc3658 100644 --- a/src/types/docker/docker.ts +++ b/src/types/docker/docker.ts @@ -64,3 +64,33 @@ export interface AuthConfig { identitytoken?: string; registrytoken?: string; } + +export interface ContextInfo { + Name: string; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Metadata: any; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Endpoints: Record; + TLSMaterial: Record>; + Storage: StorageInfo; +} + +export interface EndpointInfo { + Host?: string; + SkipVerify: boolean; + TLSData?: TLSData; +} + +export interface TLSData { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + CA: any; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Key: any; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Cert: any; +} + +export interface StorageInfo { + MetadataPath: string; + TLSPath: string; +}