Skip to content

Commit

Permalink
feat(pci.kubernetes): add test and admission plugin page
Browse files Browse the repository at this point in the history
ref: TAPC-33
Signed-off-by: Pierre-Philippe <[email protected]>
  • Loading branch information
Pierre-Philippe committed Oct 17, 2024
1 parent 8c40e73 commit a2136b1
Show file tree
Hide file tree
Showing 12 changed files with 405 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"common_stepper_skip_this_step": "Ignorer cette étape",
"common_stepper_next_button_label": "Suivant",
"common_stepper_submit_button_label": "Envoyer",
"common_save_button_label": "Enregistrer",
"common_stepper_cancel_button_label": "Annuler",
"common_file_attachmentsHeading": "Fichier(s) joint(s)",
"common_file_dropArea": "Glissez/déposez des fichiers ou",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
"kube_service_cluster_admission_plugins_mutation": "Activer/désactiver les plugins",
"kube_service_cluster_admission_plugins_activated": "Activé",
"kube_service_cluster_admission_plugins_desactivated": "Désactivé",
"": "Activer",
"kube_service_cluster_admission_plugins_to_activate": "Activer",
"kube_service_cluster_admission_plugins_info_restrictions": "Pour des raisons de sécurité, il n'est pas possible de désactiver le plugin Node Restriction.",
"kube_service_cluster_admission_plugins_to_desactivate": "Désactiver",
"kube_service_cluster_admission_plugins_error": "Une erreur est survenue lors de la réinitialisation de votre cluster : {{ message }}",
"kube_service_cluster_version": "Version mineure de Kubernetes",
"kube_service_cluster_update_available": "Une nouvelle mise à jour touchant à la sécurité (<i>patch version</i>) est disponible",
"kube_service_cluster_minor_update_available": "Une mise à jour mineure est disponible",
Expand Down Expand Up @@ -87,6 +89,6 @@
"kube_service_cluster_network_vrack_default_gateway": "Passerelle par défaut du vRACK (DHCP)",
"kube_service_cluster_network_vrack_customer_gateway": "Passerelle : {{ vRackGatewayIp }}",
"kubernetes_add_private_network": "Configurer un réseau",
"kube_service_cluster_network_vrack_customer_gateway": "Passerelle : {{ vRackGatewayIp }}",

"kube_service_network_edit": "Modifier les paramètres du réseau"
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,23 @@ describe('deleteSubscription', () => {
).rejects.toThrow('Network Error');
});
});

describe('updateAdmissionPlugin', () => {
it('calls v6.put with the correct parameters', async () => {
const projectId = 'project-id';
const kubeId = 'kube-id';
const customization = {
apiServer: { admissionPlugins: { enabled: [], disabled: [] } },
};

const expectedUrl = `/cloud/project/${projectId}/kube/${kubeId}/customization`;
vi.mocked(v6.put).mockResolvedValue({ customization });
await ApiKubernetesModule.updateAdmissionPlugin({
projectId,
kubeId,
customization,
});

expect(v6.put).toHaveBeenCalledWith(expectedUrl, customization);
});
});
13 changes: 13 additions & 0 deletions packages/manager/apps/pci-kubernetes/src/api/data/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,16 @@ export async function editNetwork(
}
return Promise.all(todo);
}

export const updateAdmissionPlugin = ({
projectId,
kubeId,
customization,
}: {
projectId: string;
kubeId: string;
customization: TKube['customization'];
}) => {
const url = `/cloud/project/${projectId}/kube/${kubeId}/customization`;
return v6.put(url, customization);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useMutation } from '@tanstack/react-query';
import queryClient from '@/queryClient';
import { getAllKubeQueryKey } from '../useKubernetes';

import { updateAdmissionPlugin } from '@/api/data/kubernetes';
import { TKube } from '@/types';

export const useUpdateAdmissionPlugin = ({
onError,
projectId,
onSuccess,
kubeId,
}) => {
const mutation = useMutation({
mutationFn: async (customization: TKube['customization']) =>
updateAdmissionPlugin({ projectId, kubeId, customization }),
onError,
onSuccess: async () => {
await queryClient.invalidateQueries({
queryKey: getAllKubeQueryKey(projectId),
});
onSuccess();
},
});
return {
updateAdmissionPlugins: mutation.mutate,
...mutation,
};
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useCallback } from 'react';
import {
ODS_TEXT_COLOR_INTENT,
ODS_TEXT_LEVEL,
ODS_TEXT_SIZE,
} from '@ovhcloud/ods-components';
import { useNavigate } from 'react-router-dom';
import { ODS_THEME_COLOR_INTENT } from '@ovhcloud/ods-common-theming';
import { OsdsChip, OsdsText, OsdsLink } from '@ovhcloud/ods-components/react';
import { useTranslation } from 'react-i18next';
import { TKube } from '@/types';
import usePluginState from '@/hooks/usePluginState';

export const plugins = [
{
Expand All @@ -29,24 +30,8 @@ const AdmissionPlugins = ({
}: TKube['customization']['apiServer']['admissionPlugins']) => {
const { t } = useTranslation(['service']);

const getChipContent = useCallback(
(pluginName) => {
if (enabled.includes(pluginName)) {
return {
label: t('kube_service_cluster_admission_plugins_activated'),
color: ODS_THEME_COLOR_INTENT.success,
};
}
if (disabled.includes(pluginName)) {
return {
label: t('kube_service_cluster_admission_plugins_desactivated'),
color: ODS_THEME_COLOR_INTENT.warning,
};
}
return null;
},
[disabled, enabled, t],
);
const pluginsState = usePluginState(enabled, disabled);
const navigate = useNavigate();

return (
<div className="mb-4 flex flex-wrap justify-between gap-4">
Expand All @@ -64,14 +49,22 @@ const AdmissionPlugins = ({
{plugin.label}
</OsdsText>
<OsdsChip
color={getChipContent(plugin.name)?.color}
color={
pluginsState(plugin.name) === 'enabled'
? ODS_THEME_COLOR_INTENT.success
: ODS_THEME_COLOR_INTENT.warning
}
data-testid={`admission-plugin-chip ${plugin.name}`}
>
{getChipContent(plugin.name)?.label}
{pluginsState(plugin.name) === 'enabled' &&
t('kube_service_cluster_admission_plugins_activated')}
{pluginsState(plugin.name) === 'disabled' &&
t('kube_service_cluster_admission_plugins_desactivated')}
</OsdsChip>
</div>
))}
<OsdsLink
onClick={() => navigate('./admission-plugin')}
color={ODS_THEME_COLOR_INTENT.primary}
className="flex font-bold"
>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { renderHook } from '@testing-library/react';
import usePluginState from './usePluginState';

describe('usePluginState', () => {
it('should return "enabled" if the plugin is in the enabled list', () => {
const enabled = ['plugin1', 'plugin2'];
const disabled = ['plugin3'];
const { result } = renderHook(() => usePluginState(enabled, disabled));

expect(result.current('plugin1')).toBe('enabled');
expect(result.current('plugin2')).toBe('enabled');
});

it('should return "disabled" if the plugin is in the disabled list', () => {
const enabled = ['plugin1', 'plugin2'];
const disabled = ['plugin3'];
const { result } = renderHook(() => usePluginState(enabled, disabled));

expect(result.current('plugin3')).toBe('disabled');
});

it('should return null if the plugin is not in either list', () => {
const enabled = ['plugin1', 'plugin2'];
const disabled = ['plugin3'];
const { result } = renderHook(() => usePluginState(enabled, disabled));

expect(result.current('plugin4')).toBeNull();
});
});
17 changes: 17 additions & 0 deletions packages/manager/apps/pci-kubernetes/src/hooks/usePluginState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useCallback } from 'react';

const usePluginState = (enabled: string[], disabled: string[]) =>
useCallback(
(pluginName: string) => {
if (enabled.includes(pluginName)) {
return 'enabled';
}
if (disabled.includes(pluginName)) {
return 'disabled';
}
return null;
},
[enabled, disabled],
);

export default usePluginState;
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useEffect } from 'react';

export const useResponsiveModal = (size) => {
useEffect(() => {
const getStyles = () => {
if (window.innerWidth > 768) {
return {
maxWidth: size,
minWidth: 'auto',
};
}
return {
maxWidth: 'inherit',
minWidth: 'inherit',
};
};

const applyStyles = (element: HTMLElement) => {
const styles = getStyles();
Object.assign(element.style, styles);
};

const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList' || mutation.type === 'attributes') {
const popup = document
.querySelector('osds-modal')
?.shadowRoot?.querySelector(
'dialog > div > div.popup',
) as HTMLElement;

if (popup) {
applyStyles(popup);
window.addEventListener('resize', () => applyStyles(popup));
}
}
});
});

observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
});

const initialPopup = document
.querySelector('osds-modal')
?.shadowRoot?.querySelector('dialog > div > div.popup') as HTMLElement;

if (initialPopup) {
applyStyles(initialPopup);
window.addEventListener('resize', () => applyStyles(initialPopup));
}

return () => {
observer.disconnect();
window.removeEventListener('resize', () => applyStyles(initialPopup));
};
}, []);
};
Loading

0 comments on commit a2136b1

Please sign in to comment.