From ca96c5f257f25d76366a914af2ce2290f5b022ec Mon Sep 17 00:00:00 2001 From: David Kwon Date: Tue, 17 Oct 2023 09:47:42 -0400 Subject: [PATCH 1/4] fix: retry request for '"system:anonymous" cannot get resource' error Signed-off-by: David Kwon --- .../src/services/backend-client/axiosWrapper.ts | 6 ++++++ .../src/services/backend-client/kubernetesNamespaceApi.ts | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/dashboard-frontend/src/services/backend-client/axiosWrapper.ts b/packages/dashboard-frontend/src/services/backend-client/axiosWrapper.ts index 48318327e..e2eff9099 100644 --- a/packages/dashboard-frontend/src/services/backend-client/axiosWrapper.ts +++ b/packages/dashboard-frontend/src/services/backend-client/axiosWrapper.ts @@ -18,6 +18,8 @@ type AxiosFunc = (url: string, config?: AxiosRequestConfig) => Promise; type AxiosFuncWithData = (url: string, data?: any, config?: AxiosRequestConfig) => Promise; export const bearerTokenAuthorizationIsRequiredErrorMsg = 'Bearer Token Authorization is required'; +export const cannotGetResourceErrorMessage = + 'users.user.openshift.io "~" is forbidden: User "system:anonymous" cannot get resource "users" in API group "user.openshift.io" at the cluster scope'; export class AxiosWrapper { protected readonly retryCount = 3; @@ -34,6 +36,10 @@ export class AxiosWrapper { return new AxiosWrapper(axios.create(), bearerTokenAuthorizationIsRequiredErrorMsg); } + static createToRetryCannotGetResourceErrors(): AxiosWrapper { + return new AxiosWrapper(axios.create(), cannotGetResourceErrorMessage); + } + static createToRetryAnyErrors(): AxiosWrapper { return new AxiosWrapper(axios.create()); } diff --git a/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts b/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts index 63afb7500..77f92e884 100644 --- a/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts +++ b/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts @@ -24,7 +24,9 @@ export async function getKubernetesNamespace(): Promise { - const response = await axios.post(`${cheServerPrefix}/kubernetes/namespace/provision`); + const response = await AxiosWrapper.createToRetryCannotGetResourceErrors().post( + `${cheServerPrefix}/kubernetes/namespace/provision`, + ); return response.data; } From 111e925c190614857a6952e6ccfaa1fad205b38b Mon Sep 17 00:00:00 2001 From: David Kwon Date: Tue, 17 Oct 2023 12:05:17 -0400 Subject: [PATCH 2/4] Use 'createToRetryAnyErrors' Signed-off-by: David Kwon --- .../src/services/backend-client/axiosWrapper.ts | 6 ------ .../src/services/backend-client/kubernetesNamespaceApi.ts | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/dashboard-frontend/src/services/backend-client/axiosWrapper.ts b/packages/dashboard-frontend/src/services/backend-client/axiosWrapper.ts index e2eff9099..48318327e 100644 --- a/packages/dashboard-frontend/src/services/backend-client/axiosWrapper.ts +++ b/packages/dashboard-frontend/src/services/backend-client/axiosWrapper.ts @@ -18,8 +18,6 @@ type AxiosFunc = (url: string, config?: AxiosRequestConfig) => Promise; type AxiosFuncWithData = (url: string, data?: any, config?: AxiosRequestConfig) => Promise; export const bearerTokenAuthorizationIsRequiredErrorMsg = 'Bearer Token Authorization is required'; -export const cannotGetResourceErrorMessage = - 'users.user.openshift.io "~" is forbidden: User "system:anonymous" cannot get resource "users" in API group "user.openshift.io" at the cluster scope'; export class AxiosWrapper { protected readonly retryCount = 3; @@ -36,10 +34,6 @@ export class AxiosWrapper { return new AxiosWrapper(axios.create(), bearerTokenAuthorizationIsRequiredErrorMsg); } - static createToRetryCannotGetResourceErrors(): AxiosWrapper { - return new AxiosWrapper(axios.create(), cannotGetResourceErrorMessage); - } - static createToRetryAnyErrors(): AxiosWrapper { return new AxiosWrapper(axios.create()); } diff --git a/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts b/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts index 77f92e884..6d12218a6 100644 --- a/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts +++ b/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts @@ -24,7 +24,7 @@ export async function getKubernetesNamespace(): Promise { - const response = await AxiosWrapper.createToRetryCannotGetResourceErrors().post( + const response = await AxiosWrapper.createToRetryAnyErrors().post( `${cheServerPrefix}/kubernetes/namespace/provision`, ); From a0ce4212e1cbf197f7ac119d19175ca85564434c Mon Sep 17 00:00:00 2001 From: David Kwon Date: Wed, 18 Oct 2023 11:21:34 -0400 Subject: [PATCH 3/4] Remove unused axios import Signed-off-by: David Kwon --- .../src/services/backend-client/kubernetesNamespaceApi.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts b/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts index 6d12218a6..091d501ba 100644 --- a/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts +++ b/packages/dashboard-frontend/src/services/backend-client/kubernetesNamespaceApi.ts @@ -10,8 +10,6 @@ * Red Hat, Inc. - initial API and implementation */ -import axios from 'axios'; - import { AxiosWrapper } from '@/services/backend-client/axiosWrapper'; import { cheServerPrefix } from '@/services/backend-client/const'; From ebc407db853c241602f93017ef5faeaeeed272b0 Mon Sep 17 00:00:00 2001 From: David Kwon Date: Wed, 18 Oct 2023 11:52:55 -0400 Subject: [PATCH 4/4] Edit test to include the unprovided data and config Signed-off-by: David Kwon --- .../backend-client/__tests__/kubernetesNamespaceApi.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dashboard-frontend/src/services/backend-client/__tests__/kubernetesNamespaceApi.spec.tsx b/packages/dashboard-frontend/src/services/backend-client/__tests__/kubernetesNamespaceApi.spec.tsx index 2420f8435..88125d433 100644 --- a/packages/dashboard-frontend/src/services/backend-client/__tests__/kubernetesNamespaceApi.spec.tsx +++ b/packages/dashboard-frontend/src/services/backend-client/__tests__/kubernetesNamespaceApi.spec.tsx @@ -53,7 +53,7 @@ describe('Kubernetes namespace API', () => { await provisionKubernetesNamespace(); expect(mockGet).not.toBeCalled(); - expect(mockPost).toBeCalledWith('/api/kubernetes/namespace/provision'); + expect(mockPost).toBeCalledWith('/api/kubernetes/namespace/provision', undefined, undefined); }); it('should return a list of namespaces', async () => {