fix(configmap): create portainer configmap if it doesn't exist [r8s-141] (#113)

pull/5049/merge
Ali 2 weeks ago committed by GitHub
parent e6508140f8
commit a2da6f1827

@ -19,12 +19,27 @@ export function useUpdateK8sConfigMapMutation(
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: ({ mutationFn: ({
data, configMap,
configMapName, configMapName,
}: { }: {
data: ConfigMap; configMap: ConfigMap;
configMapName: string; configMapName: string;
}) => updateConfigMap(environmentId, namespace, configMapName, data), }) => {
if (!configMap.metadata?.uid) {
return createConfigMap(
environmentId,
namespace,
configMapName,
configMap
);
}
return updateConfigMap(
environmentId,
namespace,
configMapName,
configMap
);
},
...withInvalidate(queryClient, [ ...withInvalidate(queryClient, [
configMapQueryKeys.configMaps(environmentId, namespace), configMapQueryKeys.configMaps(environmentId, namespace),
]), ]),
@ -50,3 +65,22 @@ async function updateConfigMap(
); );
} }
} }
function createConfigMap(
environmentId: EnvironmentId,
namespace: string,
configMap: string,
data: ConfigMap
) {
try {
return axios.post(
`/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/configmaps`,
data
);
} catch (e) {
throw parseKubernetesAxiosError(
e,
`Unable to create ConfigMap '${configMap}'`
);
}
}

@ -95,7 +95,7 @@ export function AccessDatatable() {
configMap configMap
); );
await updateConfigMapMutation.mutateAsync({ await updateConfigMapMutation.mutateAsync({
data: configMapPayload, configMap: configMapPayload,
configMapName: PortainerNamespaceAccessesConfigMap.configMapName, configMapName: PortainerNamespaceAccessesConfigMap.configMapName,
}); });
notifySuccess('Success', 'Namespace access updated'); notifySuccess('Success', 'Namespace access updated');

@ -12,6 +12,8 @@ import { useConfigMap } from '@/react/kubernetes/configs/queries/useConfigMap';
import { useTeams } from '@/react/portainer/users/teams/queries'; import { useTeams } from '@/react/portainer/users/teams/queries';
import { useUpdateK8sConfigMapMutation } from '@/react/kubernetes/configs/queries/useUpdateK8sConfigMapMutation'; import { useUpdateK8sConfigMapMutation } from '@/react/kubernetes/configs/queries/useUpdateK8sConfigMapMutation';
import { notifyError, notifySuccess } from '@/portainer/services/notifications'; import { notifyError, notifySuccess } from '@/portainer/services/notifications';
import { Configuration } from '@/react/kubernetes/configs/types';
import { useCurrentUser } from '@/react/hooks/useUser';
import { Widget, WidgetBody, WidgetTitle } from '@@/Widget'; import { Widget, WidgetBody, WidgetTitle } from '@@/Widget';
import { TextTip } from '@@/Tip/TextTip'; import { TextTip } from '@@/Tip/TextTip';
@ -28,6 +30,7 @@ export function CreateAccessWidget() {
const { const {
params: { id: namespaceName }, params: { id: namespaceName },
} = useCurrentStateAndParams(); } = useCurrentStateAndParams();
const { user } = useCurrentUser();
const environmentId = useEnvironmentId(); const environmentId = useEnvironmentId();
const isRBACEnabledQuery = useIsRBACEnabled(environmentId); const isRBACEnabledQuery = useIsRBACEnabled(environmentId);
const initialValues: { const initialValues: {
@ -75,7 +78,9 @@ export function CreateAccessWidget() {
initialValues={initialValues} initialValues={initialValues}
enableReinitialize enableReinitialize
validationSchema={validationSchema} validationSchema={validationSchema}
onSubmit={onSubmit} onSubmit={(values, formikHelpers) =>
onSubmit(values, formikHelpers)
}
validateOnMount validateOnMount
> >
{(formikProps) => ( {(formikProps) => (
@ -104,10 +109,10 @@ export function CreateAccessWidget() {
namespaceAccesses, namespaceAccesses,
values.selectedUsersAndTeams, values.selectedUsersAndTeams,
namespaceName, namespaceName,
configMap configMap ?? newConfigMap(user.Username, user.Id)
); );
await updateConfigMapMutation.mutateAsync({ await updateConfigMapMutation.mutateAsync({
data: configMapPayload, configMap: configMapPayload,
configMapName: PortainerNamespaceAccessesConfigMap.configMapName, configMapName: PortainerNamespaceAccessesConfigMap.configMapName,
}); });
notifySuccess('Success', 'Namespace access updated'); notifySuccess('Success', 'Namespace access updated');
@ -117,3 +122,18 @@ export function CreateAccessWidget() {
} }
} }
} }
function newConfigMap(userName: string, userId: number) {
const configMap: Configuration = {
Type: 1,
UID: '',
Name: PortainerNamespaceAccessesConfigMap.configMapName,
Namespace: PortainerNamespaceAccessesConfigMap.namespace,
Data: { [PortainerNamespaceAccessesConfigMap.accessKey]: '{}' },
ConfigurationOwner: userName,
ConfigurationOwnerId: `${userId}`,
IsUsed: false,
Yaml: '',
};
return configMap;
}

Loading…
Cancel
Save