Skip to content

Commit

Permalink
frontend Layout.tsx: Fix comparing new clusters config
Browse files Browse the repository at this point in the history
The cluster config processing in the mentioned file was testing
stateless clusters as coming from the /config endpoint but that's not
the case. Also, it was changing the redux state directly which led to
issues.

These changes simplify this comparison and remove the check for
stateless clusters.

Signed-off-by: Joaquim Rocha <[email protected]>
  • Loading branch information
joaquimrocha authored and knrt10 committed Sep 7, 2024
1 parent 2266251 commit d723ddd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 48 deletions.
21 changes: 3 additions & 18 deletions frontend/src/components/App/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setConfig } from '../../redux/configSlice';
import { ConfigState } from '../../redux/configSlice';
import { useTypedSelector } from '../../redux/reducers/reducers';
import store from '../../redux/stores/store';
import { fetchStatelessClusterKubeConfigs, processClusterComparison } from '../../stateless/';
import { fetchStatelessClusterKubeConfigs, isClusterConfigDifferent } from '../../stateless/';
import ActionsNotifier from '../common/ActionsNotifier';
import AlertNotification from '../common/AlertNotification';
import Sidebar, { NavigationTabs } from '../Sidebar';
Expand Down Expand Up @@ -92,7 +92,6 @@ export default function Layout({}: LayoutProps) {
*/
const fetchConfig = () => {
const clusters = store.getState().config.clusters;
const statelessClusters = store.getState().config.statelessClusters;

request('/config', {}, false, false)
.then(config => {
Expand All @@ -104,23 +103,9 @@ export default function Layout({}: LayoutProps) {
clustersToConfig[cluster.name] = cluster;
});

const configToStore = { ...config, clusters: clustersToConfig };

if (clusters === null) {
if (isClusterConfigDifferent(clusters, clustersToConfig)) {
const configToStore = { ...config, clusters: clustersToConfig };
dispatch(setConfig(configToStore));
} else {
const isConfigDifferent = processClusterComparison(clusters, clustersToConfig, false);

if (
isConfigDifferent ||
Object.keys(clustersToConfig).length !== Object.keys(clusters).length
) {
if (statelessClusters !== null) {
processClusterComparison(clusters, statelessClusters, true);
}

dispatch(setConfig(configToStore));
}
}

/**
Expand Down
60 changes: 30 additions & 30 deletions frontend/src/stateless/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,49 +312,47 @@ function generateSecureToken(length = 16): string {

/**
* Compares the cluster config from the backend and the redux store
* @param clusters
* @param clustersToConfig
* @param isStateless
* @param currentConfig
* @param newConfig
* @returns true if the present stored config is different from the fetched one.
*/
export function processClusterComparison(
clusters: ConfigState['clusters'],
clustersToConfig: ConfigState['clusters'],
isStateless: boolean
export function isClusterConfigDifferent(
currentConfig: ConfigState['clusters'],
newConfig: ConfigState['clusters']
) {
if (!clusters) {
// If no clusters, then add all clusters
return false;
if (!currentConfig || !newConfig) {
// If one of the configs is null but the other is not, we want to update the config
return !currentConfig !== !newConfig;
}

if (!clustersToConfig) {
// If no clustersToConfig, then delete all clusters
if (Object.keys(newConfig).length !== Object.keys(currentConfig).length) {
return true;
}

let isConfigDifferent = false;
Object.keys(newConfig).forEach(key => {
if (!currentConfig[key]) {
return true;
}

Object.keys(clustersToConfig).forEach(key => {
if (!!clusters[key]) {
let clusterToCompare = clusters[key];
let clusterToCompare = currentConfig[key];

if (clusterToCompare.useToken !== undefined) {
clusterToCompare = _.cloneDeep(clusters[key]);
delete clusterToCompare.useToken;
}
if (clusterToCompare.useToken !== undefined) {
clusterToCompare = _.cloneDeep(currentConfig[key]);
delete clusterToCompare.useToken;
}

if (isStateless) {
if (_.isEqual(clustersToConfig[key], clusterToCompare)) {
delete clusters[key];
}
} else {
isConfigDifferent =
isConfigDifferent || !_.isEqual(clustersToConfig[key], clusterToCompare);
}
let newCluster = newConfig[key];
if (newCluster.useToken !== undefined) {
newCluster = _.cloneDeep(newConfig[key]);
delete newCluster.useToken;
}

if (!_.isEqual(newCluster, clusterToCompare)) {
return true;
}
});

return isConfigDifferent;
return false;
}

/**
Expand Down Expand Up @@ -590,10 +588,12 @@ const exportFunctions = {
getStatelessClusterKubeConfigs,
findKubeconfigByClusterName,
getUserIdFromLocalStorage,
processClusterComparison,
isClusterConfigDifferent,
fetchStatelessClusterKubeConfigs,
deleteClusterKubeconfig,
updateStatelessClusterKubeconfig,
// @deprecated - use isClusterConfigDifferent instead
processClusterComparison: isClusterConfigDifferent,
};

export default exportFunctions;

0 comments on commit d723ddd

Please sign in to comment.