Skip to content

Commit

Permalink
fix: watch global kubeconf and project kubeconf
Browse files Browse the repository at this point in the history
  • Loading branch information
mortada-codes committed Mar 8, 2023
1 parent 0def17b commit aaaed58
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
3 changes: 1 addition & 2 deletions electron/app/initKubeconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ export function watchNamespaces(kubeConfigPath: string, key: string, dispatch: (
}

const readAndNotifyKubeConfig = (kubeConfigPath: string, dispatch: (action: AnyAction) => void) => {
const kubeConfig: KubeConfig = getKubeConfigContext(kubeConfigPath);
dispatch({type: 'config/updateProjectKubeConfig', payload: kubeConfig});
dispatch({type: 'config/setKubeConfig', payload: {path: kubeConfigPath}});
};

export function watchAllClusterNamespaces(kubeConfigPath: string, dispatch: (action: AnyAction) => void) {
Expand Down
61 changes: 59 additions & 2 deletions src/redux/appConfig/appConfig.listeners.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import * as k8s from '@kubernetes/client-node';

import {isAnyOf} from '@reduxjs/toolkit';

import {FSWatcher, watch} from 'chokidar';
import {isEmpty} from 'lodash';

import {AppListenerFn} from '@redux/listeners/base';
import {CONFIG_PATH} from '@redux/services/projectConfig';

import {KubeConfig, KubeConfigContext} from '@shared/models/config';

import {loadProjectKubeConfig, setKubeConfig, updateProjectConfig} from './appConfig.slice';
import {loadProjectKubeConfig, setKubeConfig, setOpenProject, updateProjectConfig} from './appConfig.slice';

const loadKubeConfigListener: AppListenerFn = listen => {
listen({
Expand Down Expand Up @@ -63,4 +67,57 @@ const loadKubeConfigProjectListener: AppListenerFn = listen => {
});
};

export const appConfigListeners = [loadKubeConfigListener, loadKubeConfigProjectListener];
const watchKubeConfigProjectListener: AppListenerFn = listen => {
listen({
matcher: isAnyOf(setOpenProject.fulfilled, updateProjectConfig),
async effect(_action, {dispatch, getState, cancelActiveListeners, ...listenerApi}) {
// Cancel any other listeners that are listening to the same action
cancelActiveListeners();

const configPath = getState().config.projectConfig?.kubeConfig?.path;
if (!configPath) {
return;
}
const absolutePath = CONFIG_PATH(configPath);

let watcher: FSWatcher = watch(absolutePath, {
persistent: true,
usePolling: true,
interval: 1000,
});

watcher.on('all', event => {
if (event === 'unlink') {
watcher.close();
}

let config: KubeConfig;
try {
const kc = new k8s.KubeConfig();
kc.loadFromFile(configPath);
config = {
isPathValid: !isEmpty(kc.contexts),
contexts: kc.contexts as KubeConfigContext[],
currentContext: kc.getCurrentContext(),
path: configPath,
};
} catch (error) {
config = {isPathValid: false, path: configPath};
}
dispatch(loadProjectKubeConfig(config));
});

listenerApi.signal.addEventListener('abort', () => {
watcher.close();
});

await listenerApi.condition(() => listenerApi.signal.aborted);
},
});
};

export const appConfigListeners = [
loadKubeConfigListener,
loadKubeConfigProjectListener,
watchKubeConfigProjectListener,
];

0 comments on commit aaaed58

Please sign in to comment.