Skip to content

Commit

Permalink
feat: add option to customize base image and add mount option (#19)
Browse files Browse the repository at this point in the history
* feat: add option to customize base image and add mount option

fixes #13

Change-Id: I0706aaa3c934aa71f30e2c1948f7811883fd12f5
Signed-off-by: Florent Benoit <fbenoit@redhat.com>
  • Loading branch information
benoitf authored Nov 21, 2023
1 parent 4e2d314 commit 3e7c6f7
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 19 deletions.
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@
],
"scope": "KubernetesProviderConnectionFactory",
"description": "Container Runtime"
},
"minikube.cluster.creation.base-image": {
"type": "string",
"scope": "KubernetesProviderConnectionFactory",
"markdownDescription": "Optional base image to use for docker/podman drivers. See [documentation](https://minikube.sigs.k8s.io/docs/commands/start/#options). Intended for local development"
},
"minikube.cluster.creation.mount-string": {
"type": "string",
"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
69 changes: 69 additions & 0 deletions src/create-cluster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,72 @@ test('expect error if Kubernetes reports error', async () => {
);
}
});

test('expect cluster to be created with custom base image', async () => {
(runCliCommand as Mock).mockReturnValue({ exitCode: 0 });
await createCluster(
{ 'minikube.cluster.creation.base-image': 'myCustomImage' },
undefined,
'',
telemetryLoggerMock,
undefined,
);
expect(telemetryLogUsageMock).toHaveBeenNthCalledWith(
1,
'createCluster',
expect.objectContaining({ driver: 'docker' }),
);
expect(telemetryLogErrorMock).not.toBeCalled();
expect(extensionApi.kubernetes.createResources).not.toBeCalled();
expect(runCliCommand).toBeCalledWith(
'',
[
'start',
'--profile',
'minikube',
'--driver',
'docker',
'--container-runtime',
'docker',
'--base-image',
'myCustomImage',
],
expect.anything(),
undefined,
);
});

test('expect cluster to be created with custom mount', async () => {
(runCliCommand as Mock).mockReturnValue({ exitCode: 0 });
await createCluster(
{ 'minikube.cluster.creation.mount-string': '/foo:/bar' },
undefined,
'',
telemetryLoggerMock,
undefined,
);
expect(telemetryLogUsageMock).toHaveBeenNthCalledWith(
1,
'createCluster',
expect.objectContaining({ driver: 'docker' }),
);
expect(telemetryLogErrorMock).not.toBeCalled();
expect(extensionApi.kubernetes.createResources).not.toBeCalled();
expect(runCliCommand).toBeCalledWith(
'',
[
'start',
'--profile',
'minikube',
'--driver',
'docker',
'--container-runtime',
'docker',
'--mount',
'--mount-string',
'/foo:/bar',
],
expect.anything(),
undefined,
);
});
36 changes: 17 additions & 19 deletions src/create-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,32 @@ export async function createCluster(
telemetryLogger: TelemetryLogger,
token?: CancellationToken,
): Promise<void> {
let clusterName = 'minikube';
if (params['minikube.cluster.creation.name']) {
clusterName = params['minikube.cluster.creation.name'];
}
const clusterName = params['minikube.cluster.creation.name'] ?? 'minikube';
const driver = params['minikube.cluster.creation.driver'] ?? 'docker';
const runtime = params['minikube.cluster.creation.runtime'] ?? 'docker';
const baseImage = params['minikube.cluster.creation.base-image'];
const mountString = params['minikube.cluster.creation.mount-string'];

let driver = 'docker';
if (params['minikube.cluster.creation.driver']) {
driver = params['minikube.cluster.creation.driver'];
}
const env = Object.assign({}, process.env);

let runtime = 'docker';
if (params['minikube.cluster.creation.runtime']) {
runtime = params['minikube.cluster.creation.runtime'];
}
const startArgs = ['start', '--profile', clusterName, '--driver', driver, '--container-runtime', runtime];

const env = Object.assign({}, process.env);
// add base image parameter
if (baseImage) {
startArgs.push('--base-image', baseImage);
}
if (mountString) {
// need to add also the mount option
startArgs.push('--mount');
startArgs.push('--mount-string', mountString);
}

// update PATH to include minikube
env.PATH = getMinikubePath();

// now execute the command to create the cluster
try {
await runCliCommand(
minikubeCli,
['start', '--profile', clusterName, '--driver', driver, '--container-runtime', runtime],
{ env, logger },
token,
);
await runCliCommand(minikubeCli, startArgs, { env, logger }, token);
telemetryLogger.logUsage('createCluster', { driver, runtime });
} catch (error) {
const errorMessage = error instanceof Error ? error.message : error;
Expand Down

0 comments on commit 3e7c6f7

Please sign in to comment.