-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move docker cli context select implem from core to docker exte…
…nsion (#10526) * chore: move docker cli context select implem from core to docker extension fixes podman-desktop/podman-desktop#10517 Signed-off-by: Florent Benoit <[email protected]>
- Loading branch information
Showing
13 changed files
with
285 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2025 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
// handle the context information of the docker contexts | ||
// https://docs.docker.com/engine/manage-resources/contexts/ | ||
export interface DockerContextInfo { | ||
name: string; | ||
isCurrentContext: boolean; | ||
metadata: { | ||
description: string; | ||
}; | ||
endpoints: { | ||
docker: { | ||
host: string; | ||
}; | ||
}; | ||
} | ||
|
||
export const WINDOWS_NPIPE = '//./pipe/docker_engine'; | ||
export const UNIX_SOCKET_PATH = '/var/run/docker.sock'; |
130 changes: 130 additions & 0 deletions
130
extensions/docker/src/docker-compatibility-setup.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024-2025 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
import type { Configuration } from '@podman-desktop/api'; | ||
import { configuration, context } from '@podman-desktop/api'; | ||
import { beforeEach, expect, test, vi } from 'vitest'; | ||
|
||
import { DockerCompatibilitySetup } from './docker-compatibility-setup.js'; | ||
import type { DockerContextHandler } from './docker-context-handler.js'; | ||
|
||
vi.mock('@podman-desktop/api', async () => { | ||
return { | ||
context: { | ||
setValue: vi.fn(), | ||
}, | ||
configuration: { | ||
onDidChangeConfiguration: vi.fn(), | ||
getConfiguration: vi.fn(), | ||
}, | ||
env: { | ||
isLinux: false, | ||
isWindows: false, | ||
isMac: false, | ||
}, | ||
}; | ||
}); | ||
|
||
const dockerContextHandler = { | ||
listContexts: vi.fn(), | ||
switchContext: vi.fn(), | ||
} as unknown as DockerContextHandler; | ||
|
||
let dockerCompatibilitySetup: DockerCompatibilitySetup; | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
dockerCompatibilitySetup = new DockerCompatibilitySetup(dockerContextHandler); | ||
}); | ||
|
||
test('check sending the docker cli contexts as context.setValue', async () => { | ||
// return a list of 2 contexts, second one being the current one | ||
vi.mocked(dockerContextHandler.listContexts).mockResolvedValue([ | ||
{ | ||
name: 'context1', | ||
metadata: { | ||
description: 'description1', | ||
}, | ||
endpoints: { | ||
docker: { | ||
host: 'host1', | ||
}, | ||
}, | ||
isCurrentContext: false, | ||
}, | ||
{ | ||
name: 'context2', | ||
metadata: { | ||
description: 'description2', | ||
}, | ||
endpoints: { | ||
docker: { | ||
host: 'host2', | ||
}, | ||
}, | ||
isCurrentContext: true, | ||
}, | ||
]); | ||
|
||
await dockerCompatibilitySetup.init(); | ||
|
||
// check we called listContexts | ||
expect(dockerContextHandler.listContexts).toHaveBeenCalled(); | ||
|
||
// check we called setValue with the expected values | ||
expect(context.setValue).toHaveBeenCalledWith( | ||
'docker.cli.context', | ||
[ | ||
{ | ||
label: 'context1 (host1)', | ||
selected: false, | ||
value: 'context1', | ||
}, | ||
{ | ||
label: 'context2 (host2)', | ||
selected: true, | ||
value: 'context2', | ||
}, | ||
], | ||
'DockerCompatibility', | ||
); | ||
}); | ||
|
||
test('check set the context when configuration change', async () => { | ||
// empty list of context | ||
vi.mocked(dockerContextHandler.listContexts).mockResolvedValue([]); | ||
|
||
await dockerCompatibilitySetup.init(); | ||
|
||
// capture the callback sent to onDidChangeConfiguration | ||
const callback = vi.mocked(configuration.onDidChangeConfiguration).mock.calls[0][0]; | ||
|
||
// mock configuration.getConfiguration | ||
vi.mocked(configuration.getConfiguration).mockReturnValue({ | ||
get: vi.fn(() => 'context1'), | ||
} as unknown as Configuration); | ||
|
||
// mock switchContext | ||
vi.mocked(dockerContextHandler.switchContext).mockResolvedValue(); | ||
|
||
// call the callback | ||
callback({ affectsConfiguration: vi.fn(() => true) }); | ||
|
||
// check we called switchContext | ||
expect(dockerContextHandler.switchContext).toHaveBeenCalledWith('context1'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2025 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
import { configuration, context } from '@podman-desktop/api'; | ||
|
||
import type { DockerContextHandler } from './docker-context-handler'; | ||
|
||
// setup the management of the docker contexts | ||
// registering the DockerCompatibility configuration | ||
export class DockerCompatibilitySetup { | ||
#dockerContextHandler: DockerContextHandler; | ||
|
||
constructor(dockerContextHandler: DockerContextHandler) { | ||
this.#dockerContextHandler = dockerContextHandler; | ||
} | ||
|
||
async init(): Promise<void> { | ||
// get the current contexts | ||
const currentContexts = await this.#dockerContextHandler.listContexts(); | ||
|
||
// define the enum list | ||
const contextEnumItems = currentContexts.map(context => { | ||
return { | ||
label: `${context.name} (${context.endpoints.docker.host})`, | ||
value: context.name, | ||
selected: context.isCurrentContext, | ||
}; | ||
}); | ||
context.setValue('docker.cli.context', contextEnumItems, 'DockerCompatibility'); | ||
|
||
// track the changes operated by the user | ||
configuration.onDidChangeConfiguration(event => { | ||
if (event.affectsConfiguration('docker.cli.context')) { | ||
// get the value | ||
const value = configuration.getConfiguration('docker.cli', 'DockerCompatibility'); | ||
const contextName = value.get<string>('context'); | ||
|
||
if (contextName) { | ||
this.#dockerContextHandler.switchContext(contextName).catch((error: unknown) => { | ||
console.error('Error switching docker context', error); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.