mirror of
https://github.com/portainer/portainer.git
synced 2025-08-05 13:55:21 +02:00
refactor(namespace): migrate namespace access view to react [r8s-141] (#87)
This commit is contained in:
parent
8ed7cd80cb
commit
e9fc6d5598
62 changed files with 1018 additions and 610 deletions
|
@ -1,2 +1,11 @@
|
|||
export const configurationOwnerUsernameLabel =
|
||||
export const ConfigurationOwnerUsernameLabel =
|
||||
'io.portainer.kubernetes.configuration.owner';
|
||||
|
||||
export const ConfigurationOwnerIdLabel =
|
||||
'io.portainer.kubernetes.configuration.owner.id';
|
||||
|
||||
export const PortainerNamespaceAccessesConfigMap = {
|
||||
namespace: 'portainer',
|
||||
configMapName: 'portainer-config',
|
||||
accessKey: 'NamespaceAccessPolicies',
|
||||
};
|
||||
|
|
|
@ -9,33 +9,37 @@ import { Configuration } from '../types';
|
|||
import { configMapQueryKeys } from './query-keys';
|
||||
import { ConfigMapQueryParams } from './types';
|
||||
|
||||
export function useConfigMap(
|
||||
export function useConfigMap<T = Configuration>(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string,
|
||||
configMap: string,
|
||||
options?: { autoRefreshRate?: number } & ConfigMapQueryParams
|
||||
options?: {
|
||||
autoRefreshRate?: number;
|
||||
select?: (data: Configuration) => T;
|
||||
enabled?: boolean;
|
||||
} & ConfigMapQueryParams
|
||||
) {
|
||||
return useQuery(
|
||||
configMapQueryKeys.configMap(environmentId, namespace, configMap),
|
||||
() => getConfigMap(environmentId, namespace, configMap, { withData: true }),
|
||||
{
|
||||
...withGlobalError('Unable to retrieve ConfigMaps for cluster'),
|
||||
refetchInterval() {
|
||||
return options?.autoRefreshRate ?? false;
|
||||
},
|
||||
select: options?.select,
|
||||
enabled: options?.enabled,
|
||||
refetchInterval: () => options?.autoRefreshRate ?? false,
|
||||
...withGlobalError(`Unable to retrieve ConfigMap '${configMap}'`),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// get a configmap
|
||||
async function getConfigMap(
|
||||
export async function getConfigMap(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string,
|
||||
configMap: string,
|
||||
params?: { withData?: boolean }
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.get<Configuration[]>(
|
||||
const { data } = await axios.get<Configuration>(
|
||||
`/kubernetes/${environmentId}/namespaces/${namespace}/configmaps/${configMap}`,
|
||||
{ params }
|
||||
);
|
||||
|
|
63
app/react/kubernetes/configs/queries/useK8sConfigMaps.ts
Normal file
63
app/react/kubernetes/configs/queries/useK8sConfigMaps.ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
import { ConfigMap, ConfigMapList } from 'kubernetes-types/core/v1';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import axios from '@/portainer/services/axios';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import { error as notifyError } from '@/portainer/services/notifications';
|
||||
|
||||
import { parseKubernetesAxiosError } from '../../axiosError';
|
||||
|
||||
export const configMapQueryKeys = {
|
||||
configMaps: (environmentId: EnvironmentId, namespace?: string) => [
|
||||
'environments',
|
||||
environmentId,
|
||||
'kubernetes',
|
||||
'configmaps',
|
||||
'namespaces',
|
||||
namespace,
|
||||
],
|
||||
};
|
||||
|
||||
/**
|
||||
* returns a usequery hook for the list of configmaps from the kubernetes API
|
||||
*/
|
||||
export function useK8sConfigMaps(
|
||||
environmentId: EnvironmentId,
|
||||
namespace?: string
|
||||
) {
|
||||
return useQuery(
|
||||
configMapQueryKeys.configMaps(environmentId, namespace),
|
||||
() => (namespace ? getConfigMaps(environmentId, namespace) : []),
|
||||
{
|
||||
onError: (err) => {
|
||||
notifyError(
|
||||
'Failure',
|
||||
err as Error,
|
||||
`Unable to get ConfigMaps in namespace '${namespace}'`
|
||||
);
|
||||
},
|
||||
enabled: !!namespace,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// get all configmaps for a namespace
|
||||
async function getConfigMaps(environmentId: EnvironmentId, namespace: string) {
|
||||
try {
|
||||
const { data } = await axios.get<ConfigMapList>(
|
||||
buildUrl(environmentId, namespace)
|
||||
);
|
||||
const configMapsWithKind: ConfigMap[] = data.items.map((configmap) => ({
|
||||
...configmap,
|
||||
kind: 'ConfigMap',
|
||||
}));
|
||||
return configMapsWithKind;
|
||||
} catch (e) {
|
||||
throw parseKubernetesAxiosError(e, 'Unable to retrieve ConfigMaps');
|
||||
}
|
||||
}
|
||||
|
||||
function buildUrl(environmentId: number, namespace: string, name?: string) {
|
||||
const url = `/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/configmaps`;
|
||||
return name ? `${url}/${name}` : url;
|
||||
}
|
63
app/react/kubernetes/configs/queries/useK8sSecrets.ts
Normal file
63
app/react/kubernetes/configs/queries/useK8sSecrets.ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
import { Secret, SecretList } from 'kubernetes-types/core/v1';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import axios from '@/portainer/services/axios';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import { error as notifyError } from '@/portainer/services/notifications';
|
||||
|
||||
import { parseKubernetesAxiosError } from '../../axiosError';
|
||||
|
||||
export const secretQueryKeys = {
|
||||
secrets: (environmentId: EnvironmentId, namespace?: string) => [
|
||||
'environments',
|
||||
environmentId,
|
||||
'kubernetes',
|
||||
'secrets',
|
||||
'namespaces',
|
||||
namespace,
|
||||
],
|
||||
};
|
||||
|
||||
/**
|
||||
* returns a usequery hook for the list of secrets from the kubernetes API
|
||||
*/
|
||||
export function useK8sSecrets(
|
||||
environmentId: EnvironmentId,
|
||||
namespace?: string
|
||||
) {
|
||||
return useQuery(
|
||||
secretQueryKeys.secrets(environmentId, namespace),
|
||||
() => (namespace ? getSecrets(environmentId, namespace) : []),
|
||||
{
|
||||
onError: (err) => {
|
||||
notifyError(
|
||||
'Failure',
|
||||
err as Error,
|
||||
`Unable to get secrets in namespace '${namespace}'`
|
||||
);
|
||||
},
|
||||
enabled: !!namespace,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// get all secrets for a namespace
|
||||
async function getSecrets(environmentId: EnvironmentId, namespace: string) {
|
||||
try {
|
||||
const { data } = await axios.get<SecretList>(
|
||||
buildUrl(environmentId, namespace)
|
||||
);
|
||||
const secretsWithKind: Secret[] = data.items.map((secret) => ({
|
||||
...secret,
|
||||
kind: 'Secret',
|
||||
}));
|
||||
return secretsWithKind;
|
||||
} catch (e) {
|
||||
throw parseKubernetesAxiosError(e, 'Unable to retrieve secrets');
|
||||
}
|
||||
}
|
||||
|
||||
function buildUrl(environmentId: number, namespace: string, name?: string) {
|
||||
const url = `/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/secrets`;
|
||||
return name ? `${url}/${name}` : url;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { ConfigMap } from 'kubernetes-types/core/v1';
|
||||
|
||||
import axios from '@/portainer/services/axios';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import { withInvalidate } from '@/react-tools/react-query';
|
||||
|
||||
import { parseKubernetesAxiosError } from '../../axiosError';
|
||||
|
||||
import { configMapQueryKeys } from './useK8sConfigMaps';
|
||||
|
||||
/**
|
||||
* useUpdateK8sConfigMapMutation returns a mutation hook for updating a Kubernetes ConfigMap using the Kubernetes proxy API.
|
||||
*/
|
||||
export function useUpdateK8sConfigMapMutation(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string
|
||||
) {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
data,
|
||||
configMapName,
|
||||
}: {
|
||||
data: ConfigMap;
|
||||
configMapName: string;
|
||||
}) => updateConfigMap(environmentId, namespace, configMapName, data),
|
||||
...withInvalidate(queryClient, [
|
||||
configMapQueryKeys.configMaps(environmentId, namespace),
|
||||
]),
|
||||
// handle success notifications in the calling component
|
||||
});
|
||||
}
|
||||
|
||||
async function updateConfigMap(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string,
|
||||
configMap: string,
|
||||
data: ConfigMap
|
||||
) {
|
||||
try {
|
||||
return await axios.put(
|
||||
`/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/configmaps/${configMap}`,
|
||||
data
|
||||
);
|
||||
} catch (e) {
|
||||
throw parseKubernetesAxiosError(
|
||||
e,
|
||||
`Unable to update ConfigMap '${configMap}'`
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,157 +0,0 @@
|
|||
import { Secret, SecretList } from 'kubernetes-types/core/v1';
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { queryClient, withError } from '@/react-tools/react-query';
|
||||
import axios from '@/portainer/services/axios';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import {
|
||||
error as notifyError,
|
||||
notifySuccess,
|
||||
} from '@/portainer/services/notifications';
|
||||
import { isFulfilled, isRejected } from '@/portainer/helpers/promise-utils';
|
||||
import { pluralize } from '@/portainer/helpers/strings';
|
||||
|
||||
import { parseKubernetesAxiosError } from '../axiosError';
|
||||
|
||||
export const secretQueryKeys = {
|
||||
secrets: (environmentId: EnvironmentId, namespace?: string) => [
|
||||
'environments',
|
||||
environmentId,
|
||||
'kubernetes',
|
||||
'secrets',
|
||||
'namespaces',
|
||||
namespace,
|
||||
],
|
||||
secretsForCluster: (environmentId: EnvironmentId) => [
|
||||
'environments',
|
||||
environmentId,
|
||||
'kubernetes',
|
||||
'secrets',
|
||||
],
|
||||
};
|
||||
|
||||
// returns a usequery hook for the list of secrets from the kubernetes API
|
||||
export function useSecrets(environmentId: EnvironmentId, namespace?: string) {
|
||||
return useQuery(
|
||||
secretQueryKeys.secrets(environmentId, namespace),
|
||||
() => (namespace ? getSecrets(environmentId, namespace) : []),
|
||||
{
|
||||
onError: (err) => {
|
||||
notifyError(
|
||||
'Failure',
|
||||
err as Error,
|
||||
`Unable to get secrets in namespace '${namespace}'`
|
||||
);
|
||||
},
|
||||
enabled: !!namespace,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export function useSecretsForCluster(
|
||||
environmentId: EnvironmentId,
|
||||
namespaces?: string[],
|
||||
options?: { autoRefreshRate?: number }
|
||||
) {
|
||||
return useQuery(
|
||||
secretQueryKeys.secretsForCluster(environmentId),
|
||||
() => namespaces && getSecretsForCluster(environmentId, namespaces),
|
||||
{
|
||||
...withError('Unable to retrieve secrets for cluster'),
|
||||
enabled: !!namespaces?.length,
|
||||
refetchInterval() {
|
||||
return options?.autoRefreshRate ?? false;
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export function useMutationDeleteSecrets(environmentId: EnvironmentId) {
|
||||
return useMutation(
|
||||
async (secrets: { namespace: string; name: string }[]) => {
|
||||
const promises = await Promise.allSettled(
|
||||
secrets.map(({ namespace, name }) =>
|
||||
deleteSecret(environmentId, namespace, name)
|
||||
)
|
||||
);
|
||||
const successfulSecrets = promises
|
||||
.filter(isFulfilled)
|
||||
.map((_, index) => secrets[index].name);
|
||||
const failedSecrets = promises
|
||||
.filter(isRejected)
|
||||
.map(({ reason }, index) => ({
|
||||
name: secrets[index].name,
|
||||
reason,
|
||||
}));
|
||||
return { failedSecrets, successfulSecrets };
|
||||
},
|
||||
{
|
||||
...withError('Unable to remove secrets'),
|
||||
onSuccess: ({ failedSecrets, successfulSecrets }) => {
|
||||
queryClient.invalidateQueries(
|
||||
secretQueryKeys.secretsForCluster(environmentId)
|
||||
);
|
||||
// show an error message for each secret that failed to delete
|
||||
failedSecrets.forEach(({ name, reason }) => {
|
||||
notifyError(
|
||||
`Failed to remove secret '${name}'`,
|
||||
new Error(reason.message) as Error
|
||||
);
|
||||
});
|
||||
// show one summary message for all successful deletes
|
||||
if (successfulSecrets.length) {
|
||||
notifySuccess(
|
||||
`${pluralize(
|
||||
successfulSecrets.length,
|
||||
'Secret'
|
||||
)} successfully removed`,
|
||||
successfulSecrets.join(', ')
|
||||
);
|
||||
}
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async function getSecretsForCluster(
|
||||
environmentId: EnvironmentId,
|
||||
namespaces: string[]
|
||||
) {
|
||||
const secrets = await Promise.all(
|
||||
namespaces.map((namespace) => getSecrets(environmentId, namespace))
|
||||
);
|
||||
return secrets.flat();
|
||||
}
|
||||
|
||||
// get all secrets for a namespace
|
||||
async function getSecrets(environmentId: EnvironmentId, namespace: string) {
|
||||
try {
|
||||
const { data } = await axios.get<SecretList>(
|
||||
buildUrl(environmentId, namespace)
|
||||
);
|
||||
const secretsWithKind: Secret[] = data.items.map((secret) => ({
|
||||
...secret,
|
||||
kind: 'Secret',
|
||||
}));
|
||||
return secretsWithKind;
|
||||
} catch (e) {
|
||||
throw parseKubernetesAxiosError(e, 'Unable to retrieve secrets');
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteSecret(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string,
|
||||
name: string
|
||||
) {
|
||||
try {
|
||||
await axios.delete(buildUrl(environmentId, namespace, name));
|
||||
} catch (e) {
|
||||
throw parseKubernetesAxiosError(e, 'Unable to remove secret');
|
||||
}
|
||||
}
|
||||
|
||||
function buildUrl(environmentId: number, namespace: string, name?: string) {
|
||||
const url = `/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/secrets`;
|
||||
return name ? `${url}/${name}` : url;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue