Skip to content

Commit

Permalink
Fix k8s connection creation, remove daemon start
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Apr 27, 2023
1 parent 45c7025 commit 9b00000
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 54 deletions.
40 changes: 1 addition & 39 deletions src/crc-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import type { ChildProcess } from 'node:child_process';

import { spawn } from 'node:child_process';
import { isMac, isWindows } from './util';

Expand All @@ -24,8 +24,6 @@ import type { Logger } from '@podman-desktop/api';
const macosExtraPath = '/usr/local/bin:/opt/local/bin';
const crcWindowsInstallPath = 'c:\\Program Files\\Red Hat OpenShift Local';

let daemonProcess: ChildProcess;

export function getInstallationPath(): string {
const env = process.env;
if (isWindows()) {
Expand Down Expand Up @@ -132,39 +130,3 @@ export async function getCrcVersion(): Promise<CrcVersion | undefined> {

return undefined;
}

export async function daemonStart(): Promise<boolean> {
let command = getCrcCli();
let args = ['daemon', '--watchdog'];

const env = Object.assign({}, process.env); // clone original env object

// In production mode, applications don't have access to the 'user' path like brew
if (isMac() || isWindows()) {
env.PATH = getInstallationPath();
} else if (env.FLATPAK_ID) {
// need to execute the command on the host
args = ['--host', command, ...args];
command = 'flatpak-spawn';
}

// launching the daemon
daemonProcess = spawn(command, args, {
detached: true,
windowsHide: true,
env,
});

daemonProcess.on('error', err => {
const msg = `CRC daemon failure, daemon failed to start: ${err}`;
console.error('CRC failure', msg);
});

return true;
}

export function daemonStop() {
if (daemonProcess && daemonProcess.exitCode !== null) {
daemonProcess.kill();
}
}
32 changes: 17 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import * as path from 'node:path';
import * as os from 'node:os';
import * as fs from 'node:fs';
import type { Status } from './daemon-commander';
import { commander } from './daemon-commander';
import { isWindows, productName, providerId } from './util';
import { daemonStart, daemonStop, getCrcVersion } from './crc-cli';
import { getCrcVersion } from './crc-cli';
import { getCrcDetectionChecks } from './detection-checks';
import { CrcInstall } from './install/crc-install';

Expand Down Expand Up @@ -73,8 +74,6 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
});
extensionContext.subscriptions.push(provider);

const daemonStarted = await daemonStart();

const providerLifecycle: extensionApi.ProviderLifecycle = {
status: () => crcStatus.getProviderStatus(),

Expand All @@ -100,11 +99,6 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):

extensionContext.subscriptions.push(provider.registerLifecycle(providerLifecycle));

if (!daemonStarted) {
crcStatus.setErrorStatus();
return;
}

commandManager.setExtContext(extensionContext);

registerOpenTerminalCommand();
Expand Down Expand Up @@ -168,9 +162,6 @@ function registerPodmanConnection(provider: extensionApi.Provider, extensionCont

export function deactivate(): void {
console.log('stopping crc extension');

daemonStop();

crcStatus.stopStatusUpdate();
}

Expand Down Expand Up @@ -204,9 +195,17 @@ async function registerOpenShiftLocalCluster(
extensionContext.subscriptions.push(disposable);
}

function readPreset(crcStatus: Status): 'Podman' | 'OpenShift' | 'MicroShift' | 'unknown' {
async function readPreset(crcStatus: Status): Promise<'Podman' | 'OpenShift' | 'MicroShift' | 'unknown'> {
let preset: string;
//preset could be undefined if vm not created yet, use preferences instead
if (crcStatus.Preset === undefined) {
const config = await commander.configGet();
preset = config.preset;
} else {
preset = crcStatus.Preset;
}
try {
switch (crcStatus.Preset) {
switch (preset) {
case 'podman':
return 'Podman';
case 'openshift':
Expand All @@ -227,11 +226,14 @@ async function connectToCrc(): Promise<void> {
crcStatus.startStatusUpdate();
}

function presetChanged(provider: extensionApi.Provider, extensionContext: extensionApi.ExtensionContext): void {
async function presetChanged(
provider: extensionApi.Provider,
extensionContext: extensionApi.ExtensionContext,
): Promise<void> {
// TODO: handle situation if some cluster/connection was registered already

// detect preset of CRC
const preset = readPreset(crcStatus.status);
const preset = await readPreset(crcStatus.status);
if (preset === 'Podman') {
// podman connection
registerPodmanConnection(provider, extensionContext);
Expand Down

0 comments on commit 9b00000

Please sign in to comment.