Skip to content

Commit

Permalink
Fix several bugs with new client API (microsoft#2129)
Browse files Browse the repository at this point in the history
* Fix error when docker context fails

* Fix prune error

* Fix image tag indexing issue
  • Loading branch information
bwateratmsft authored and Dmarch28 committed Mar 4, 2021
1 parent 8d4b98c commit 1ff5c20
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/docker/ContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class DockerContextManager implements ContextManager, Disposable {
}

private async loadContexts(): Promise<DockerContext[]> {
return callWithTelemetryAndErrorHandling(ext.dockerClient ? 'docker-context.change' : 'docker-context.initialize', async (actionContext: IActionContext) => {
let loadResult = await callWithTelemetryAndErrorHandling(ext.dockerClient ? 'docker-context.change' : 'docker-context.initialize', async (actionContext: IActionContext) => {
try {
// docker-context.initialize and docker-context.change should be treated as "activation events", in that they aren't real user action
actionContext.telemetry.properties.isActivationEvent = 'true';
Expand Down Expand Up @@ -200,5 +200,18 @@ export class DockerContextManager implements ContextManager, Disposable {
throw err;
}
});

// If the load failed or is otherwise empty, return the default
// That way a returned value is ensured by this method
if (!loadResult) {
loadResult = [{
...defaultContext,
Current: true,
DockerEndpoint: os.platform() === 'win32' ? WindowsLocalPipe : UnixLocalPipe,
Type: 'moby',
} as DockerContext];
}

return loadResult;
}
}
10 changes: 5 additions & 5 deletions src/docker/DockerodeApiClient/DockerodeApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class DockerodeApiClient extends ContextChangeCancelClient implements Doc
const result = await this.callWithErrorHandling(context, async () => this.dockerodeClient.pruneContainers(), token);
return {
...result,
ObjectsDeleted: result.ContainersDeleted.length,
ObjectsDeleted: result.ContainersDeleted?.length ?? 0,
};
}

Expand Down Expand Up @@ -131,13 +131,13 @@ export class DockerodeApiClient extends ContextChangeCancelClient implements Doc
const result = await this.callWithErrorHandling(context, async () => this.dockerodeClient.pruneImages(), token);
return {
...result,
ObjectsDeleted: result.ImagesDeleted.length,
ObjectsDeleted: result.ImagesDeleted?.length ?? 0,
};
}

public async tagImage(context: IActionContext, ref: string, fullTag: string, token?: CancellationToken): Promise<void> {
const repo = fullTag.substr(0, fullTag.lastIndexOf(':'));
const tag = fullTag.substr(fullTag.lastIndexOf(':'));
const tag = fullTag.substr(fullTag.lastIndexOf(':') + 1);
const image = this.dockerodeClient.getImage(ref);
await this.callWithErrorHandling(context, async () => image.tag({ repo: repo, tag: tag }), token);
}
Expand Down Expand Up @@ -174,7 +174,7 @@ export class DockerodeApiClient extends ContextChangeCancelClient implements Doc
const result = await this.callWithErrorHandling(context, async () => this.dockerodeClient.pruneNetworks(), token);
return {
SpaceReclaimed: 0,
ObjectsDeleted: result.NetworksDeleted.length,
ObjectsDeleted: result.NetworksDeleted?.length ?? 0,
};
}

Expand Down Expand Up @@ -216,7 +216,7 @@ export class DockerodeApiClient extends ContextChangeCancelClient implements Doc
const result = await this.callWithErrorHandling(context, async () => this.dockerodeClient.pruneVolumes(), token);
return {
...result,
ObjectsDeleted: result.VolumesDeleted.length,
ObjectsDeleted: result.VolumesDeleted?.length ?? 0,
};
}

Expand Down

0 comments on commit 1ff5c20

Please sign in to comment.