Skip to content

Commit

Permalink
Warn on manual hooks in plan
Browse files Browse the repository at this point in the history
Signed-off-by: yaacov <kobi.zamir@gmail.com>
  • Loading branch information
yaacov committed Jun 6, 2024
1 parent d6aa49f commit 9efd612
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useReducer } from 'react';
import { Base64 } from 'js-base64';
import SectionHeading from 'src/components/headers/SectionHeading';
import { AlertMessageForModals } from 'src/modules/Providers/modals';
import { useForkliftTranslation } from 'src/utils/i18n';

import { FormGroupWithHelpText } from '@kubev2v/common';
Expand All @@ -27,7 +28,7 @@ import { onUpdatePlanHooks } from './utils';
export const PlanHooks: React.FC<{ name: string; namespace: string }> = ({ name, namespace }) => {
const { t } = useForkliftTranslation();

const [plan, preHookResource, postHookResource, loaded, loadError] = usePlanHooks(
const [plan, preHookResource, postHookResource, loaded, loadError, warning] = usePlanHooks(
name,
namespace,
);
Expand Down Expand Up @@ -96,6 +97,24 @@ export const PlanHooks: React.FC<{ name: string; namespace: string }> = ({ name,
<Suspend obj={plan} loaded={loaded} loadError={loadError}>
{state.alertMessage && <PageSection variant="light">{state.alertMessage}</PageSection>}

{warning && (
<PageSection
variant="light"
className="forklift-page-plan-details-vm-status__section-actions"
>
<AlertMessageForModals
variant="warning"
title={'The plan hooks were manually configured'}
message={
<>
<p>Warning: {warning},</p>
<p>updating the hooks will override the current configuration.</p>
</>
}
/>
</PageSection>
)}

<PageSection
variant="light"
className="forklift-page-plan-details-vm-status__section-actions"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,43 @@ export const usePlanHooks = (name: string, namespace: string) => {
const postHookResource = hookRecourses.find((h) => h.metadata.name === postHook?.hook?.name);
const preHookResource = hookRecourses.find((h) => h.metadata.name === preHook?.hook?.name);

return [plan, preHookResource, postHookResource, loaded, loadError];
// Check for unsupported hooks
const warning = validateHooks(plan);

return [plan, preHookResource, postHookResource, loaded, loadError, warning];
};

/**
* Validates that there is only one 'PostHook' and one 'PreHook' defined
* and that the exact same hooks are defined on all VMs in the plan.
*
* @param {V1beta1Plan} plan - The plan object containing VM specifications.
* @returns {string} - Returns a warning string.
*/
function validateHooks(plan: V1beta1Plan): string {
if (!plan?.spec?.vms) {
return;
}

const hooksOnFirstVM = plan.spec.vms[0]?.hooks || [];

const hasMultiplePostHook = hooksOnFirstVM.filter((h) => h.step === 'PostHook').length > 1;
const hasMultiplePreHook = hooksOnFirstVM.filter((h) => h.step === 'PreHook').length > 1;

if (hasMultiplePostHook || hasMultiplePreHook) {
return 'the plan is configured with more then one hook per step';
}

const sortedFirstVMHooks = hooksOnFirstVM.sort((a, b) => a.step.localeCompare(b.step));

const sameHooks = plan.spec.vms.every((vm) => {
const sortedVMHooks = (vm.hooks || []).sort((a, b) => a.step.localeCompare(b.step));
return JSON.stringify(sortedFirstVMHooks) === JSON.stringify(sortedVMHooks);
});

if (!sameHooks) {
return 'the plan is configured with different hooks for different virtual machines';
}

return;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { Alert } from '@patternfly/react-core';

import './alerts.style.css';

export const AlertMessageForModals: React.FC<{ title: string; message: string }> = ({
title,
message,
}) => (
<Alert className="co-alert forklift-alert--margin-top" isInline variant="danger" title={title}>
export const AlertMessageForModals: React.FC<{
title: string;
message: React.ReactNode | string;
variant?: 'default' | 'danger' | 'success' | 'warning' | 'info';
}> = ({ title, message, variant = 'danger' }) => (
<Alert className="co-alert forklift-alert--margin-top" isInline variant={variant} title={title}>
{message}
</Alert>
);

0 comments on commit 9efd612

Please sign in to comment.