Skip to content
New issue

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

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

Already on GitHub? # to your account

Resolves #33 Add MINIKUBE_HOME configuration #48

Merged
merged 1 commit into from
Jul 25, 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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"configuration": {
"title": "Minikube",
"properties": {
"minikube.home": {
"type": "string",
"default": "",
"markdownDescription": "Path for the `.minikube` directory that minikube uses for state/configuration. See [documentation](https://minikube.sigs.k8s.io/docs/handbook/config/#environment-variables)"
},
"minikube.cluster.creation.name": {
"type": "string",
"default": "minikube",
Expand Down Expand Up @@ -51,7 +56,6 @@
"scope": "KubernetesProviderConnectionFactory",
"markdownDescription": "Optional mount definition `host-path:container-path` to include during the start of minikube container. See [documentation](https://minikube.sigs.k8s.io/docs/commands/start/#options)"
}

}
},
"menus": {
Expand Down
1 change: 1 addition & 0 deletions src/create-cluster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ vi.mock('@podman-desktop/api', async () => {
vi.mock('./util', async () => {
return {
getMinikubePath: vi.fn(),
getMinikubeHome: vi.fn(),
};
});

Expand Down
4 changes: 3 additions & 1 deletion src/create-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import { getMinikubePath } from './util';
import { getMinikubePath, getMinikubeHome } from './util';

import type { Logger, TelemetryLogger, CancellationToken } from '@podman-desktop/api';
import { process as processApi } from '@podman-desktop/api';
Expand Down Expand Up @@ -51,6 +51,8 @@ export async function createCluster(
// update PATH to include minikube
env.PATH = getMinikubePath();

env.MINIKUBE_HOME = getMinikubeHome();

// now execute the command to create the cluster
try {
await processApi.exec(minikubeCli, startArgs, { env, logger, token });
Expand Down
3 changes: 3 additions & 0 deletions src/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ vi.mock('@podman-desktop/api', async () => {
containerEngine: {
listContainers: vi.fn(),
},
configuration: {
getConfiguration: vi.fn(),
},

process: {
exec: vi.fn(),
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
***********************************************************************/

import * as extensionApi from '@podman-desktop/api';
import { detectMinikube, getMinikubePath } from './util';
import { detectMinikube, getMinikubePath, getMinikubeHome } from './util';
import { MinikubeInstaller } from './minikube-installer';
import type { CancellationToken, Logger } from '@podman-desktop/api';
import { window } from '@podman-desktop/api';
Expand Down Expand Up @@ -128,6 +128,7 @@ async function updateClusters(provider: extensionApi.Provider, containers: exten
delete: async (logger): Promise<void> => {
const env = Object.assign({}, process.env);
env.PATH = getMinikubePath();
env.MINIKUBE_HOME = getMinikubeHome();
await extensionApi.process.exec(minikubeCli, ['delete', '--profile', cluster.name], { env, logger });
},
};
Expand Down
1 change: 1 addition & 0 deletions src/image-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ vi.mock('@podman-desktop/api', async () => {
vi.mock('./util', async () => {
return {
getMinikubePath: vi.fn(),
getMinikubeHome: vi.fn(),
};
});

Expand Down
3 changes: 2 additions & 1 deletion src/image-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import type { MinikubeCluster } from './extension';
import * as extensionApi from '@podman-desktop/api';
import { tmpName } from 'tmp-promise';
import { getMinikubePath } from './util';
import { getMinikubePath, getMinikubeHome } from './util';
import * as fs from 'node:fs';

type ImageInfo = { engineId: string; name?: string; tag?: string };
Expand Down Expand Up @@ -63,6 +63,7 @@ export class ImageHandler {
}

env.PATH = getMinikubePath();
env.MINIKUBE_HOME = getMinikubeHome();
try {
// Create a temporary file to store the image
filename = await tmpName();
Expand Down
3 changes: 3 additions & 0 deletions src/minikube-installer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ vi.mock('@podman-desktop/api', async () => {
env: {
isWindows: vi.fn(),
},
configuration: {
getConfiguration: vi.fn(),
},
ProgressLocation: {
APP_ICON: 1,
},
Expand Down
43 changes: 42 additions & 1 deletion src/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@

import * as extensionApi from '@podman-desktop/api';
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
import { detectMinikube, getMinikubePath, installBinaryToSystem } from './util';
import { detectMinikube, getMinikubePath, getMinikubeHome, installBinaryToSystem } from './util';
import type { MinikubeInstaller } from './minikube-installer';
import * as fs from 'node:fs';
import * as path from 'node:path';

const { config } = vi.hoisted(() => {
return {
config: {
get: vi.fn(),
has: vi.fn(),
update: vi.fn(),
},
};
});

Comment on lines +28 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need the hoisted block, you can return mock (vi.fn) directly in getConfiguration method

using the vi.mockImplementation we have typechecking while here we don't

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried, but not figured out, how to make this return different value on different tests. I tried to do like other tests in this project and podman-desktop project, but without success 😞

a little help, like example would be awesome

vi.mock('@podman-desktop/api', async () => {
return {
window: {
Expand All @@ -41,6 +51,9 @@ vi.mock('@podman-desktop/api', async () => {
isWindows: vi.fn(),
isLinux: vi.fn(),
},
configuration: {
getConfiguration: (): extensionApi.Configuration => config,
},
ProgressLocation: {
APP_ICON: 1,
},
Expand Down Expand Up @@ -85,6 +98,34 @@ test('getMinikubePath on macOS with existing PATH', async () => {
expect(computedPath).toEqual(`${existingPATH}:/usr/local/bin:/opt/homebrew/bin:/opt/local/bin:/opt/podman/bin`);
});

test('getMinikubeHome with empty configuration property', async () => {
const existingEnvHome = '/my-existing-minikube-home';
const existingConfigHome = '';
process.env.MINIKUBE_HOME = existingEnvHome;

const spyGetConfiguration = vi.spyOn(config, 'get');
spyGetConfiguration.mockReturnValue(existingConfigHome);

const computedHome = getMinikubeHome();

expect(computedHome).toEqual(existingEnvHome);
expect(computedHome).not.toEqual(existingConfigHome);
});

test('getMinikubeHome with empty configuration property', async () => {
const existingEnvHome = '/my-existing-minikube-home';
const existingConfigHome = '/my-another-existing-minikube-home';
process.env.MINIKUBE_HOME = existingEnvHome;

const spyGetConfiguration = vi.spyOn(config, 'get');
spyGetConfiguration.mockReturnValue(existingConfigHome);

const computedHome = getMinikubeHome();

expect(computedHome).not.toEqual(existingEnvHome);
expect(computedHome).toEqual(existingConfigHome);
});

test('detectMinikube', async () => {
const fakeMinikubeInstaller = {
getAssetInfo: vi.fn(),
Expand Down
24 changes: 22 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface RunOptions {
logger?: extensionApi.Logger;
}

const minikubeConfiguration = extensionApi.configuration.getConfiguration('minikube');
const macosExtraPath = '/usr/local/bin:/opt/homebrew/bin:/opt/local/bin:/opt/podman/bin';

export function getMinikubePath(): string {
Expand All @@ -48,10 +49,26 @@ export function getMinikubePath(): string {
}
}

export function getMinikubeHome(): string | undefined {
const minikubeHome = minikubeConfiguration.get<string>('home');
// Check env if configuration is not applied in UI
if (!minikubeHome) {
const env = process.env;
return env.MINIKUBE_HOME;
} else {
return minikubeHome;
}
}

// search if minikube is available in the path
export async function detectMinikube(pathAddition: string, installer: MinikubeInstaller): Promise<string> {
try {
await extensionApi.process.exec('minikube', ['version'], { env: { PATH: getMinikubePath() } });
await extensionApi.process.exec('minikube', ['version'], {
env: {
PATH: getMinikubePath(),
MINIKUBE_HOME: getMinikubeHome(),
},
});
return 'minikube';
} catch (e) {
// ignore and try another way
Expand All @@ -61,7 +78,10 @@ export async function detectMinikube(pathAddition: string, installer: MinikubeIn
if (assetInfo) {
try {
await extensionApi.process.exec(assetInfo.name, ['version'], {
env: { PATH: getMinikubePath().concat(path.delimiter).concat(pathAddition) },
env: {
PATH: getMinikubePath().concat(path.delimiter).concat(pathAddition),
MINIKUBE_HOME: getMinikubeHome(),
},
});
return pathAddition
.concat(path.sep)
Expand Down