Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker: contextInspect func #400

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion __tests__/docker/docker.test.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
});
});
13 changes: 13 additions & 0 deletions __tests__/docker/docker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
18 changes: 17 additions & 1 deletion src/docker/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -69,6 +69,22 @@ export class Docker {
});
}

public static async contextInspect(name?: string): Promise<ContextInfo> {
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 (<Array<ContextInfo>>JSON.parse(res.stdout.trim()))[0];
});
}

public static async printVersion(): Promise<void> {
await Exec.exec('docker', ['version']);
}
Expand Down
30 changes: 30 additions & 0 deletions src/types/docker/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, EndpointInfo>;
TLSMaterial: Record<string, Array<string>>;
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;
}
Loading