Skip to content

Commit f979bf5

Browse files
authored
Merge pull request #2794 from headlamp-k8s/default-namespace-fallback
frontend: Fallback to default namespace found in the kubeconfig when all namespaces fail to load
2 parents bdd56af + 6cb5fdc commit f979bf5

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

frontend/src/components/common/NamespacesAutocomplete.tsx

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import Checkbox from '@mui/material/Checkbox';
55
import { useTheme } from '@mui/material/styles';
66
import TextField from '@mui/material/TextField';
77
import Typography from '@mui/material/Typography';
8-
import React, { useMemo } from 'react';
8+
import React, { useEffect, useMemo } from 'react';
99
import { useTranslation } from 'react-i18next';
1010
import { useDispatch } from 'react-redux';
1111
import { useHistory, useLocation } from 'react-router-dom';
1212
import helpers, { addQuery } from '../../helpers';
13-
import { useCluster } from '../../lib/k8s';
13+
import { useCluster, useClustersConf } from '../../lib/k8s';
1414
import Namespace from '../../lib/k8s/namespace';
1515
import { setNamespaceFilter } from '../../redux/filterSlice';
1616
import { useTypedSelector } from '../../redux/reducers/reducers';
@@ -152,10 +152,47 @@ export function NamespacesAutocomplete() {
152152
);
153153
}
154154

155+
/**
156+
* This hook will try to select a namespace in a specific case
157+
*
158+
* If we failed to load namespaces it might be because the user
159+
* doesn't have access to list all the namespaces but still has
160+
* access to a specific namespace
161+
*
162+
* Sometimes in the kubeconfig there will be a default namespace set
163+
* which we can try to use as a fallback
164+
*/
165+
const useDefaultNamespaceFallback = (
166+
namespacesList: Namespace[] | null,
167+
isNamespaceError: boolean
168+
) => {
169+
const selectedNamespaces = useTypedSelector(state => state.filter.namespaces);
170+
const allClustersConfigs = useClustersConf();
171+
const currentCluster = useCluster();
172+
const dispatch = useDispatch();
173+
174+
useEffect(() => {
175+
if (
176+
currentCluster &&
177+
allClustersConfigs &&
178+
isNamespaceError &&
179+
(!namespacesList || namespacesList?.length === 0) &&
180+
selectedNamespaces.size === 0
181+
) {
182+
const defaultNamespaceFromKubeconfig =
183+
allClustersConfigs[currentCluster]?.meta_data.namespace;
184+
185+
if (defaultNamespaceFromKubeconfig) {
186+
dispatch(setNamespaceFilter([defaultNamespaceFromKubeconfig]));
187+
}
188+
}
189+
}, [namespacesList, isNamespaceError, currentCluster]);
190+
};
191+
155192
function NamespacesFromClusterAutocomplete(
156193
props: Omit<PureNamespacesAutocompleteProps, 'namespaceNames'>
157194
) {
158-
const [namespacesList] = Namespace.useList();
195+
const [namespacesList, error] = Namespace.useList();
159196
const namespaceNames = useMemo(
160197
() =>
161198
namespacesList
@@ -165,5 +202,7 @@ function NamespacesFromClusterAutocomplete(
165202
[namespacesList]
166203
);
167204

205+
useDefaultNamespaceFallback(namespacesList, Boolean(error));
206+
168207
return <PureNamespacesAutocomplete namespaceNames={namespaceNames} {...props} />;
169208
}

0 commit comments

Comments
 (0)