Skip to content

Commit 8b6f3a4

Browse files
authored
feat: implement update for kubectl (podman-desktop#5145)
Fixes podman-desktop#5035 Signed-off-by: Jeff MAURY <[email protected]>
1 parent 449a086 commit 8b6f3a4

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

extensions/kubectl-cli/src/extension.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { KubectlGitHubReleases } from './kubectl-github-releases';
2626
import { KubectlDownload } from './download';
2727
import * as path from 'path';
2828
import type { CliTool } from '@podman-desktop/api';
29+
import { installBinaryToSystem } from './cli-run';
2930

3031
interface KubectlVersionOutput {
3132
clientVersion: {
@@ -44,6 +45,7 @@ interface KubectlVersionOutput {
4445

4546
let kubectlVersionMetadata: KubectlGithubReleaseArtifactMetadata | undefined;
4647
let kubectlCliTool: CliTool | undefined;
48+
let kubectlCliToolUpdaterDisposable: extensionApi.Disposable | undefined;
4749
const os = new OS();
4850

4951
// Telemetry
@@ -135,6 +137,11 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
135137
kubectlCliTool.updateVersion({
136138
version: kubectlVersionMetadata.tag.slice(1),
137139
});
140+
// if installed version is the newest, dispose the updater
141+
const lastReleaseMetadata = await kubectlDownload.getLatestVersionAsset();
142+
if (lastReleaseMetadata.tag === kubectlVersionMetadata.tag) {
143+
kubectlCliToolUpdaterDisposable?.dispose();
144+
}
138145
downloaded = true;
139146
} finally {
140147
// Make sure we log the telemetry even if we encounter an error
@@ -219,14 +226,17 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
219226
// Push the CLI tool as well (but it will do it postActivation so it does not block the activate() function)
220227
// Post activation
221228
setTimeout(() => {
222-
postActivate(extensionContext).catch((error: unknown) => {
229+
postActivate(extensionContext, kubectlDownload).catch((error: unknown) => {
223230
console.error('Error activating extension', error);
224231
});
225232
}, 0);
226233
}
227234

228235
// Activate the CLI tool (check version, etc) and register the CLi so it does not block activation.
229-
async function postActivate(extensionContext: extensionApi.ExtensionContext): Promise<void> {
236+
async function postActivate(
237+
extensionContext: extensionApi.ExtensionContext,
238+
kubectlDownload: KubectlDownload,
239+
): Promise<void> {
230240
// The location of the binary (local storage folder)
231241
const binaryPath = path.join(
232242
extensionContext.storagePath,
@@ -255,6 +265,25 @@ async function postActivate(extensionContext: extensionApi.ExtensionContext): Pr
255265
version: binaryVersion,
256266
path: binaryPath,
257267
});
268+
269+
// check if there is a new version to be installed and register the updater
270+
const lastReleaseMetadata = await kubectlDownload.getLatestVersionAsset();
271+
const lastReleaseVersion = lastReleaseMetadata.tag.slice(1);
272+
if (lastReleaseVersion !== binaryVersion) {
273+
kubectlCliToolUpdaterDisposable = kubectlCliTool.registerUpdate({
274+
version: lastReleaseVersion,
275+
doUpdate: async _logger => {
276+
// download, install system wide and update cli version
277+
await kubectlDownload.download(lastReleaseMetadata);
278+
await installBinaryToSystem(binaryPath, 'kubectl');
279+
kubectlCliTool.updateVersion({
280+
version: lastReleaseVersion,
281+
});
282+
kubectlCliToolUpdaterDisposable.dispose();
283+
},
284+
});
285+
}
286+
258287
extensionContext.subscriptions.push(kubectlCliTool);
259288
}
260289

0 commit comments

Comments
 (0)