Skip to content

Commit

Permalink
add cmd to push image to crc cluster
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>

push instead if move

Signed-off-by: Yevhen Vydolob <[email protected]>

fix format

Signed-off-by: Yevhen Vydolob <[email protected]>

Use ssh connection for podman

Signed-off-by: Yevhen Vydolob <[email protected]>

add cmd to push image to crc cluster

Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob authored and gbraad committed May 5, 2023
1 parent 93e26ec commit 3547d22
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
"description": "Path of image pull secret (download from https://console.redhat.com/openshift/create/local)"
}
}
},
"menus": {
"dashboard/image": [
{
"command": "crc.image.push.to.cluster",
"title": "Push image to OpenShift Local cluster"
}
]
}
},
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import { commandManager } from './command';
import { registerOpenConsoleCommand } from './crc-console';
import { registerLogInCommands } from './login-commands';
import { defaultLogger } from './logger';
import { moveImageToCrcCluster as pushImageToCrcCluster } from './image-handler';

const CRC_PUSH_IMAGE_TO_CLUSTER = 'crc.image.push.to.cluster';

export async function activate(extensionContext: extensionApi.ExtensionContext): Promise<void> {
const crcInstaller = new CrcInstall();
Expand Down Expand Up @@ -111,6 +114,12 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
registerDeleteCommand();

syncPreferences(extensionContext, telemetryLogger);
extensionContext.subscriptions.push(
extensionApi.commands.registerCommand(CRC_PUSH_IMAGE_TO_CLUSTER, image => {
telemetryLogger.logUsage('pushImage');
pushImageToCrcCluster(image);
}),
);

if (!isNeedSetup) {
// initial preset check
Expand Down
59 changes: 59 additions & 0 deletions src/image-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**********************************************************************
* Copyright (C) 2023 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 * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
import * as extensionApi from '@podman-desktop/api';
import { productName, runCliCommand } from './util';
import { getPodmanCli } from './podman-cli';

type ImageInfo = { engineId: string; name?: string; tag?: string };

export async function moveImageToCrcCluster(image: ImageInfo): Promise<void> {
if (!image.name) {
throw new Error('Image selection not supported yet');
}

const filename = path.join(os.tmpdir(), image.name ? image.name.replaceAll('/', '') : image.engineId);
try {
let name = image.name;
if (image.tag) {
name = name + ':' + image.tag;
}
await extensionApi.containerEngine.saveImage(image.engineId, name, filename);
const result = await runCliCommand(getPodmanCli(), [
'--url=ssh://[email protected]:2222/run/podman/podman.sock',
`--identity=${os.homedir()}/.crc/machines/crc/id_ecdsa`,
'load',
'-i',
filename,
]);

if (result.exitCode !== 0) {
throw new Error(result.stdErr);
}
extensionApi.window.showNotification({
body: `Image ${image.name} pushed to ${productName} cluster`,
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : '' + error;
throw new Error(`Error while pushing image ${image.name} to ${productName} cluster: ${errorMessage}`);
} finally {
fs.promises.rm(filename);
}
}
39 changes: 39 additions & 0 deletions src/podman-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**********************************************************************
* Copyright (C) 2023 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 } from '@podman-desktop/api';
import { isWindows } from './util';

export function getPodmanCli(): string {
// If we have a custom binary path regardless if we are running Windows or not
const customBinaryPath = getCustomBinaryPath();
if (customBinaryPath) {
return customBinaryPath;
}

if (isWindows()) {
return 'podman.exe';
}
return 'podman';
}

// Get the Podman binary path from configuration podman.binary.path
// return string or undefined
export function getCustomBinaryPath(): string | undefined {
return configuration.getConfiguration('podman').get('binary.path');
}

0 comments on commit 3547d22

Please sign in to comment.