Skip to content

Commit

Permalink
Resolves #33 Add MINIKUBE_HOME configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Tony-Sol <tony.kent.nar.earth@gmail.com>
  • Loading branch information
tony-sol committed Jun 25, 2024
1 parent ac80eb1 commit bb44334
Show file tree
Hide file tree
Showing 11 changed files with 4,973 additions and 3,029 deletions.
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(),
},
};
});

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 @@ -123,6 +123,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(),
},
};
});

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
Loading

0 comments on commit bb44334

Please # to comment.