Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions src/components/App/useLoginWithKubeconfigID.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addByContext } from 'components/Clusters/shared';
import { ClustersState, clustersAtom } from 'state/clustersAtom';
import { SetStateAction, useAtom, useAtomValue } from 'jotai';
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { NavigateFunction, useNavigate, useSearchParams } from 'react-router';
import { useTranslation } from 'react-i18next';
import jsyaml from 'js-yaml';
Expand All @@ -26,7 +26,7 @@ import {
import { useNotification } from 'shared/contexts/NotificationContext';
import {
manualKubeConfigIdAtom,
ManualKubeConfigIdType,
ManualKubeConfigIdController,
} from 'state/manualKubeConfigIdAtom';

export interface KubeconfigIdFeature extends ConfigFeature {
Expand Down Expand Up @@ -66,20 +66,15 @@ export async function loadKubeconfigById(
return payload;
}

const addClusters = async (
const addClusters = (
kubeconfig: ValidKubeconfig,
clusters: ClustersState,
clusterInfo: useClustersInfoType,
kubeconfigIdFeature: KubeconfigIdFeature,
t: TFunction,
notification?: any,
navigate?: NavigateFunction,
manualKubeConfigId?: {
manualKubeConfigId?: ManualKubeConfigIdType;
setManualKubeConfigId?: (
update: SetStateAction<ManualKubeConfigIdType>,
) => void;
},
manualKubeConfigId?: ManualKubeConfigIdController,
) => {
const isOnlyOneCluster = kubeconfig.contexts.length === 1;
const currentContext = kubeconfig['current-context'];
Expand Down Expand Up @@ -133,12 +128,7 @@ const loadKubeconfigIdCluster = async (
clusterInfo: useClustersInfoType,
t: TFunction,
setContextsState?: (update: SetStateAction<KubeConfigMultipleState>) => void,
manualKubeConfigId?: {
manualKubeConfigId?: ManualKubeConfigIdType;
setManualKubeConfigId?: (
update: SetStateAction<ManualKubeConfigIdType>,
) => void;
},
manualKubeConfigId?: ManualKubeConfigIdController,
) => {
try {
const kubeconfig = await loadKubeconfigById(
Expand Down Expand Up @@ -201,6 +191,7 @@ export function useLoginWithKubeconfigID() {
const { setCurrentCluster } = clusterInfo;
const [handledKubeconfigId, setHandledKubeconfigId] =
useState<KubeconfigIdHandleState>('not started');
const lastProcessedKubeconfigIdRef = useRef<string | null>(null);

useEffect(() => {
if (contextsState?.chosenContext) {
Expand Down Expand Up @@ -252,21 +243,26 @@ export function useLoginWithKubeconfigID() {
useEffect(() => {
const dependenciesReady = !!configuration?.features && !!clusters;
const flowStarted = handledKubeconfigId !== 'not started';
const kubeconfigId = search.get('kubeconfigID');

if (search.get('kubeconfigID') && flowStarted) {
setCurrentCluster(undefined);
if (kubeconfigId && flowStarted) {
if (kubeconfigId !== lastProcessedKubeconfigIdRef.current) {
setCurrentCluster(undefined);
setHandledKubeconfigId('not started');
}
return;
}

if (!dependenciesReady || flowStarted) {
return;
}

const kubeconfigId = search.get('kubeconfigID');
if (!kubeconfigId || !kubeconfigIdFeature?.isEnabled) {
setHandledKubeconfigId('done');
return;
}

lastProcessedKubeconfigIdRef.current = kubeconfigId;
setHandledKubeconfigId('loading');
loadKubeconfigIdCluster(
kubeconfigId,
Expand All @@ -282,7 +278,6 @@ export function useLoginWithKubeconfigID() {
}
});
}, [
contextsState,
search,
clusters,
kubeconfigIdFeature,
Expand Down
14 changes: 6 additions & 8 deletions src/components/Clusters/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import { useClustersInfoType } from 'state/utils/getClustersInfo';
import { tryParseOIDCparams } from './components/oidc-params';
import { hasNonOidcAuth, createUserManager } from 'state/authDataAtom';
import { useNavigate } from 'react-router';
import { SetStateAction, useSetAtom } from 'jotai';
import { useSetAtom } from 'jotai';
import { removePreviousPath } from 'state/useAfterInitHook';
import { ManualKubeConfigIdType } from 'state/manualKubeConfigIdAtom';
import {
ManualKubeConfigIdController,
ManualKubeConfigIdType,
} from 'state/manualKubeConfigIdAtom';
import { parseOIDCparams } from 'components/Clusters/components/oidc-params';

export type Users = Array<{
Expand Down Expand Up @@ -163,12 +166,7 @@ export const addByContext = (
config: any;
},
clustersInfo: useClustersInfoType,
manualKubeConfigId?: {
manualKubeConfigId?: ManualKubeConfigIdType;
setManualKubeConfigId?: (
update: SetStateAction<ManualKubeConfigIdType>,
) => void;
},
manualKubeConfigId?: ManualKubeConfigIdController,
) => {
let kubeconfig = userKubeconfig as ValidKubeconfig;
const findUser = () =>
Expand Down
7 changes: 6 additions & 1 deletion src/state/authDataAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ export function useAuthHandler() {
const onAfterLogin = () => {
setIsLoading(false);

if (!getPreviousPath() || getPreviousPath() === '/clusters') {
// Only auto-navigate after an OIDC redirect (which always lands on '/').
const isOidcCallbackPath = window.location.pathname === '/';
if (
isOidcCallbackPath &&
(!getPreviousPath() || getPreviousPath() === '/clusters')
) {
if (cluster.currentContext.namespace) {
navigate(
`/cluster/${encodeURIComponent(cluster.name)}/namespaces/${
Expand Down
6 changes: 6 additions & 0 deletions src/state/clusterAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const getClusters = () => {
};

const getInitialClusterState = (): ActiveClusterState => {
// Don't restore a previous cluster when a kubeconfigID is present — it
// would race with and override the kubeconfigID flow.
if (new URLSearchParams(window.location.search).get('kubeconfigID')) {
return null;
}

const clusters = getClusters();
const clusterName = localStorage.getItem(CLUSTER_NAME_STORAGE_KEY);

Expand Down
9 changes: 8 additions & 1 deletion src/state/manualKubeConfigIdAtom.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { atom } from 'jotai';
import { atom, SetStateAction } from 'jotai';

export type ManualKubeConfigIdType = {
formOpen: boolean;
auth: any;
};

export type ManualKubeConfigIdController = {
manualKubeConfigId?: ManualKubeConfigIdType;
setManualKubeConfigId?: (
update: SetStateAction<ManualKubeConfigIdType>,
) => void;
};

const defaultValue = { formOpen: false, auth: null };

export const manualKubeConfigIdAtom =
Expand Down
Loading