Skip to content

Commit

Permalink
Merge pull request #7513 from mook-as/k3sHelper/online-via-events
Browse files Browse the repository at this point in the history
k3sHelper: Use events to check online state
  • Loading branch information
Nino-K authored Sep 23, 2024
2 parents f3ba31b + e55d5f3 commit 5ed9ca4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
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

0 comments on commit 5ed9ca4

Please sign in to comment.