Skip to content

Commit

Permalink
fix: fix cluster namespace control
Browse files Browse the repository at this point in the history
  • Loading branch information
WitoDelnat committed Apr 17, 2023
1 parent 6ee8b11 commit 74d2a4a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
28 changes: 10 additions & 18 deletions src/components/organisms/PageHeader/ClusterControl/Buttons.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {useCallback, useEffect, useState} from 'react';
import {useCallback} from 'react';

import {Button, Spin} from 'antd';

import {ApiOutlined, CloseCircleFilled, LoadingOutlined} from '@ant-design/icons';

import styled from 'styled-components';

import {kubeConfigContextSelector, kubeConfigPathSelector, kubeConfigPathValidSelector} from '@redux/appConfig';
import {getContext, selectKubeconfig} from '@redux/cluster/selectors';
import {connectCluster} from '@redux/cluster/thunks/connect';
import {useAppDispatch, useAppSelector} from '@redux/hooks';
import {toggleStartProjectPane} from '@redux/reducers/ui';
Expand All @@ -18,40 +18,32 @@ import {Colors} from '@shared/styles';

export function ConnectButton() {
const dispatch = useAppDispatch();
const isKubeConfigPathValid = useAppSelector(kubeConfigPathValidSelector);
const kubeConfigContext = useAppSelector(kubeConfigContextSelector);
const kubeConfigPath = useAppSelector(kubeConfigPathSelector);
const kubeConfig = useAppSelector(selectKubeconfig);
const highlightedItems = useAppSelector(state => state.ui.highlightedItems);
const isStartProjectPaneVisible = useAppSelector(state => state.ui.isStartProjectPaneVisible);
const clusterConnection = useAppSelector(state => state.main.clusterConnection);
const clusterConnectionOptions = useAppSelector(state => state.main.clusterConnectionOptions);
const lastNamespaceLoaded = clusterConnectionOptions.lastNamespaceLoaded;
const [isClusterActionDisabled, setIsClusterActionDisabled] = useState(
Boolean(!kubeConfigPath) || !isKubeConfigPathValid
);
const loading = Boolean(clusterConnectionOptions.isLoading);

useEffect(() => {
setIsClusterActionDisabled(Boolean(!kubeConfigPath) || !isKubeConfigPathValid);
}, [kubeConfigPath, isKubeConfigPathValid]);

const loadOrReload = async () => {
if (isClusterActionDisabled && loading) {
const loadOrReload = useCallback(() => {
const context = getContext(kubeConfig);
if (!context || loading) {
return;
}

if (isStartProjectPaneVisible) {
dispatch(toggleStartProjectPane());
}

console.log('TEST WITO', context);
dispatch(
connectCluster({
context: kubeConfigContext,
namespace: lastNamespaceLoaded,
context: context.name,
namespace: context.namespace,
reload: clusterConnection !== undefined,
})
);
};
}, [clusterConnection, dispatch, isStartProjectPaneVisible, kubeConfig, loading]);

if (loading) {
return (
Expand Down
6 changes: 6 additions & 0 deletions src/redux/cluster/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export const selectKubeContext = (
return context;
};

export function getContext(kubeconfig: ModernKubeConfig | undefined, context?: string): Context | undefined {
if (!kubeconfig?.isValid) return undefined;
const contextName = context ?? kubeconfig.currentContext;
return kubeconfig.contexts.find(c => c.name === contextName);
}

export const selectKubeconfigPaths = (state: RootState): string[] => {
const configsFromClusterSlice = state.cluster.configPaths;
const globalConfig = state.config.kubeConfig.path;
Expand Down
4 changes: 2 additions & 2 deletions src/redux/cluster/thunks/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ export const connectCluster = createAsyncThunk<ConnectResponse, ConnectArgs, Thu
await dispatch(
reloadClusterResources({
context: context.name,
namespace: context.namespace ?? 'default',
namespace: payload.namespace ?? context.namespace ?? 'default',
port: setupResponse.port,
})
);
} else {
await dispatch(
loadClusterResources({
context: context.name,
namespace: context.namespace ?? 'default',
namespace: payload.namespace ?? context.namespace ?? 'default',
port: setupResponse.port,
})
);
Expand Down

0 comments on commit 74d2a4a

Please sign in to comment.