Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k3sHelper: Use events to check online state #7513

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions pkg/rancher-desktop/backend/k3sHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Architecture, VMExecutor } from './backend';
import * as K8s from '@pkg/backend/k8s';
import { KubeClient } from '@pkg/backend/kube/client';
import { loadFromString, exportConfig } from '@pkg/backend/kubeconfig';
import { checkConnectivity } from '@pkg/main/networking';
import mainEvents from '@pkg/main/mainEvents';
import { isUnixError } from '@pkg/typings/unix.interface';
import DownloadProgressListener from '@pkg/utils/DownloadProgressListener';
import * as childProcess from '@pkg/utils/childProcess';
Expand All @@ -46,6 +46,12 @@ const console = Logging.k8s;
*/
export type ShortVersion = string;

let isOnline = true;

mainEvents.on('update-network-status', (connected) => {
isOnline = connected;
});

export interface ReleaseAPIEntry {
tag_name: string;
assets: {
Expand Down Expand Up @@ -372,7 +378,7 @@ export default class K3sHelper extends events.EventEmitter {
{ headers: { Accept: this.channelApiAccept } });
} catch (ex: any) {
console.log(`updateCache: error: ${ ex }`);
if (!(await checkConnectivity('k3s.io'))) {
if (!isOnline) {
return;
}

Expand Down Expand Up @@ -565,16 +571,15 @@ export default class K3sHelper extends events.EventEmitter {
get availableVersions(): Promise<SemanticVersionEntry[]> {
return (async() => {
await this.initialize();
const upstreamSeemsReachable = await checkConnectivity('k3s.io');
const wrappedVersions = Object.values(this.versions);
const finalOptions = upstreamSeemsReachable ? wrappedVersions : await K3sHelper.filterVersionsAgainstCache(wrappedVersions);
const finalOptions = isOnline ? wrappedVersions : await K3sHelper.filterVersionsAgainstCache(wrappedVersions);

return finalOptions.sort((a, b) => b.version.compare(a.version));
})();
}

static async cachedVersionsOnly(): Promise<boolean> {
return !(await checkConnectivity('k3s.io'));
static cachedVersionsOnly(): Promise<boolean> {
return Promise.resolve(!isOnline);
}

static async filterVersionsAgainstCache(fullVersionList: SemanticVersionEntry[]): Promise<SemanticVersionEntry[]> {
Expand Down
6 changes: 6 additions & 0 deletions pkg/rancher-desktop/main/mainEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ interface MainEventNames {
*/
'network-ready'(): void;

/**
* Emitted when the network comes online or goes offline.
* @param connected The new network state.
*/
'update-network-status'(connected: boolean): void;

/**
* Emitted when the integration state has changed.
*
Expand Down
1 change: 1 addition & 0 deletions pkg/rancher-desktop/main/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ export class Tray {
} else {
this.currentNetworkStatus = await checkConnectivity('k3s.io') ? networkStatus.CONNECTED : networkStatus.OFFLINE;
}
mainEvents.emit('update-network-status', this.currentNetworkStatus === networkStatus.CONNECTED);
send('update-network-status', this.currentNetworkStatus === networkStatus.CONNECTED);
this.updateMenu();
}
Expand Down
Loading