Skip to content

Commit

Permalink
MGMT-19591: Update BMH hostname annotation instead of Agent's spec (#…
Browse files Browse the repository at this point in the history
…2770)

Co-authored-by: Julie Gyselová <jgyselov@redhat.com>
  • Loading branch information
rawagner and jgyselov authored Feb 25, 2025
1 parent ba0292b commit 70cc84a
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import noop from 'lodash-es/noop.js';
import { Stack, StackItem } from '@patternfly/react-core';
import { Host } from '@openshift-assisted/types/assisted-installer-service';
import {
Expand Down Expand Up @@ -28,13 +27,13 @@ import {
} from '../../../common';
import { ClusterDeploymentHostDiscoveryTableProps } from '../ClusterDeployment/types';
import MassApproveAgentModal from '../modals/MassApproveAgentModal';
import { MassChangeHostnameModalProps } from '../../../common/components/hosts/MassChangeHostnameModal';
import MassApproveAction from '../modals/MassApproveAction';
import { usePagination } from '../../../common/components/hosts/usePagination';
import { useTranslation } from '../../../common/hooks/use-translation-wrapper';
import { ExpandComponent } from '../Agent/AgentsSelectionTable';
import { HostsTableDetailContextProvider } from '../../../common/components/hosts/HostsTableDetailContext';
import { agentStatus, bmhStatus } from '../helpers/agentStatus';
import { onAgentChangeHostname } from '../helpers';

const ClusterDeploymentHostDiscoveryTable: React.FC<ClusterDeploymentHostDiscoveryTableProps> = ({
agents,
Expand Down Expand Up @@ -126,19 +125,6 @@ const ClusterDeploymentHostDiscoveryTable: React.FC<ClusterDeploymentHostDiscove
/>,
];

const onAgentChangeHostname: MassChangeHostnameModalProps['onChangeHostname'] = async (
host,
hostname,
) => {
const agent = agents.find((a) => a.metadata?.uid === host.id);
if (agent) {
return onChangeHostname(agent, hostname);
} else {
const bmh = bareMetalHosts.find((bmh) => bmh.metadata?.uid === host.id);
return bmh ? onChangeBMHHostname(bmh, hostname) : noop;
}
};

const paginationProps = usePagination(hosts.length);
const itemIDs = hosts.map((h) => h.id);

Expand Down Expand Up @@ -185,7 +171,12 @@ const ClusterDeploymentHostDiscoveryTable: React.FC<ClusterDeploymentHostDiscove
isOpen={isMassChangeHostOpen}
hosts={hosts}
selectedHostIDs={selectedHostIDs}
onChangeHostname={onAgentChangeHostname}
onChangeHostname={onAgentChangeHostname(
agents,
bareMetalHosts,
onChangeHostname,
onChangeBMHHostname,
)}
onClose={() => setMassChangeHostOpen(false)}
canChangeHostname={canChangeHostname(agents, agentStatuses, bareMetalHosts, t)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
HostsNotShowingLink,
VMRebootConfigurationInfo,
} from '../../../common';
import { getIsSNOCluster } from '../helpers';
import { getIsSNOCluster, onAgentChangeHostname } from '../helpers';
import MinimalHWRequirements from '../Agent/MinimalHWRequirements';
import { ClusterDeploymentHostsDiscoveryProps } from './types';
import { EditBMHModal, EditAgentModal } from '../modals';
Expand Down Expand Up @@ -57,7 +57,7 @@ const ClusterDeploymentHostsDiscovery: React.FC<ClusterDeploymentHostsDiscoveryP
infraEnv,
infraNMStates,
usedHostnames,
onSaveAgent,
onChangeHostname,
onEditRole,
onSetInstallationDiskId,
onSaveBMH,
Expand Down Expand Up @@ -119,7 +119,7 @@ const ClusterDeploymentHostsDiscovery: React.FC<ClusterDeploymentHostsDiscoveryP
onEditRole={onEditRole}
onSetInstallationDiskId={onSetInstallationDiskId}
onEditBMH={setEditBMH}
onChangeHostname={onSaveAgent}
onChangeHostname={onChangeHostname}
onChangeBMHHostname={onChangeBMHHostname}
onApprove={onApproveAgent}
width={contentRect.bounds?.width}
Expand All @@ -138,13 +138,19 @@ const ClusterDeploymentHostsDiscovery: React.FC<ClusterDeploymentHostsDiscoveryP
fetchSecret={fetchSecret}
usedHostnames={usedHostnames || []}
/>
<EditAgentModal
isOpen={!!editAgent}
onClose={() => setEditAgent(undefined)}
usedHostnames={usedHostnames}
agent={editAgent}
onSave={onSaveAgent}
/>
{editAgent && (
<EditAgentModal
onClose={() => setEditAgent(undefined)}
usedHostnames={usedHostnames}
agent={editAgent}
onSave={onAgentChangeHostname(
agents,
bareMetalHosts,
onChangeHostname,
onChangeBMHHostname,
)}
/>
)}
</GridItem>
<DiscoveryTroubleshootingModal
isOpen={isDiscoveryHintModalOpen}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const ClusterDeploymentWizard: React.FC<ClusterDeploymentWizardProps> = ({
clusterDeployment,
agentClusterInstall,
agents,
bareMetalHosts,
clusterImages,
aiConfigMap,
infraEnv,
Expand All @@ -37,6 +38,7 @@ const ClusterDeploymentWizard: React.FC<ClusterDeploymentWizardProps> = ({
onSaveISOParams,
onCreateBMH,
isNutanix,
onChangeBMHHostname,
...rest
}) => {
const [currentStepId, setCurrentStepId] = React.useState<ClusterDeploymentWizardStepsType>(
Expand Down Expand Up @@ -83,15 +85,12 @@ const ClusterDeploymentWizard: React.FC<ClusterDeploymentWizardProps> = ({
clusterDeployment={clusterDeployment}
agentClusterInstall={agentClusterInstall}
agents={agents}
bareMetalHosts={[] /* TODO(mlibra) */}
bareMetalHosts={bareMetalHosts}
aiConfigMap={aiConfigMap}
infraEnv={infraEnv}
fetchSecret={fetchSecret}
onClose={onClose}
onChangeBMHHostname={(bmh, _hostname) => {
// console.log('onChangeBMHHostname is not implemented: ', hostname);
return Promise.resolve(bmh);
}}
onChangeBMHHostname={onChangeBMHHostname}
onEditRole={hostActions.onEditRole}
onSetInstallationDiskId={hostActions.onSetInstallationDiskId}
onDeleteHost={hostActions.onDeleteHost}
Expand Down
13 changes: 7 additions & 6 deletions libs/ui-lib/lib/cim/components/ClusterDeployment/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ import { BMCFormProps } from '../Agent/types';
import { AgentMachineK8sResource } from '../Hypershift/types';
import { AddHostDropdownProps, ProvisioningConfigResult } from '../InfraEnv/types';
import { AddHostModalProps, EditBMHModalProps, UploadActionModalProps } from '../modals/types';
import { Host } from '@openshift-assisted/types/assisted-installer-service';

export type EditAgentModalProps = {
agent: AgentK8sResource | undefined;
isOpen: boolean;
agent: AgentK8sResource;
usedHostnames: string[] | undefined;
onClose: () => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onSave: (agent: AgentK8sResource, hostname: string) => Promise<any>;
onSave: (host: Host, hostname: string) => Promise<unknown>;
};

export type AgentTableActions = {
Expand Down Expand Up @@ -146,6 +145,7 @@ export type ClusterDeploymentWizardProps = {
clusterDeployment: ClusterDeploymentK8sResource;
agentClusterInstall: AgentClusterInstallK8sResource;
agents: AgentK8sResource[];
bareMetalHosts: BareMetalHostK8sResource[];
aiConfigMap?: ConfigMapK8sResource;
infraEnv?: InfraEnvK8sResource;
infraNMStates: NMStateK8sResource[];
Expand All @@ -155,7 +155,8 @@ export type ClusterDeploymentWizardProps = {
setPreviewOpen: (open: boolean) => void;
fetchManagedClusters: () => Promise<K8sResourceCommon[]>;
fetchKlusterletAddonConfig: () => Promise<K8sResourceCommon[]>;
onSaveAgent: EditAgentModalProps['onSave'];
onChangeHostname: ClusterDeploymentHostsDiscoveryStepProps['onChangeHostname'];
onChangeBMHHostname: ClusterDeploymentHostsDiscoveryStepProps['onChangeBMHHostname'];
onSaveBMH: EditBMHModalProps['onEdit'];
onSaveISOParams?: AddHostModalProps['onSaveISOParams'];
onSaveHostsDiscovery: ClusterDeploymentHostsDiscoveryStepProps['onSaveHostsDiscovery'];
Expand Down Expand Up @@ -225,7 +226,7 @@ export type ClusterDeploymentHostsDiscoveryProps = AddHostDropdownProps & {
aiConfigMap?: ConfigMapK8sResource;
infraNMStates: NMStateK8sResource[];

onSaveAgent: EditAgentModalProps['onSave'];
onChangeHostname: InfraEnvAgentTableProps['onChangeHostname'];
onEditRole: ClusterDeploymentHostDiscoveryTableProps['onEditRole'];
onSetInstallationDiskId: AgentTableActions['onSetInstallationDiskId'];
onSaveBMH: EditBMHModalProps['onEdit'];
Expand Down
23 changes: 7 additions & 16 deletions libs/ui-lib/lib/cim/components/InfraEnv/InfraEnvAgentTable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { Button, Stack, StackItem } from '@patternfly/react-core';
import { DropdownItem } from '@patternfly/react-core/deprecated';
import noop from 'lodash-es/noop.js';

import { Host } from '@openshift-assisted/types/assisted-installer-service';
import {
Expand Down Expand Up @@ -33,12 +32,12 @@ import {
} from '../../../common';
import { InfraEnvAgentTableProps } from '../ClusterDeployment/types';
import { MassApproveAgentModal, MassDeleteAgentModal } from '../modals';
import { MassChangeHostnameModalProps } from '../../../common/components/hosts/MassChangeHostnameModal';
import MassApproveAction from '../modals/MassApproveAction';
import { usePagination } from '../../../common/components/hosts/usePagination';
import InfraTableToolbar from './InfraTableToolbar';
import { useTranslation } from '../../../common/hooks/use-translation-wrapper';
import { agentStatus, bmhStatus } from '../helpers/agentStatus';
import { onAgentChangeHostname } from '../helpers';

type NoFilterMatchStateProps = {
onClearFilters: VoidFunction;
Expand Down Expand Up @@ -205,19 +204,6 @@ const InfraEnvAgentTable: React.FC<InfraEnvAgentTableProps> = ({
<DeleteHostAction key="delete" onDeleteHost={() => setMassDeleteOpen(!isMassDeleteOpen)} />,
];

const onAgentChangeHostname: MassChangeHostnameModalProps['onChangeHostname'] = async (
host,
hostname,
) => {
const agent = agents.find((a) => a.metadata?.uid === host.id);
if (agent) {
return onChangeHostname(agent, hostname);
} else {
const bmh = bareMetalHosts.find((bmh) => bmh.metadata?.uid === host.id);
return bmh ? onChangeBMHHostname(bmh, hostname) : noop;
}
};

const paginationProps = usePagination(hosts.length);

return (
Expand Down Expand Up @@ -272,7 +258,12 @@ const InfraEnvAgentTable: React.FC<InfraEnvAgentTableProps> = ({
isOpen={isMassChangeHostOpen}
hosts={hosts}
selectedHostIDs={selectedHostIDs}
onChangeHostname={onAgentChangeHostname}
onChangeHostname={onAgentChangeHostname(
agents,
bareMetalHosts,
onChangeHostname,
onChangeBMHHostname,
)}
onClose={() => setMassChangeHostOpen(false)}
canChangeHostname={canChangeHostname(agents, agentStatuses, bareMetalHosts, t)}
/>
Expand Down
31 changes: 30 additions & 1 deletion libs/ui-lib/lib/cim/components/helpers/agents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AgentStatus, getAgentStatusKey } from './status';
import { Host } from '@openshift-assisted/types/assisted-installer-service';
import { AgentK8sResource, BareMetalHostK8sResource } from '../../types';
import { INFRAENV_AGENTINSTALL_LABEL_KEY } from '../common/constants';
import { AGENT_BMH_NAME_LABEL_KEY, INFRAENV_AGENTINSTALL_LABEL_KEY } from '../common/constants';

const AGENT_FOR_SELECTION_STATUSES: AgentStatus[] = [
'known',
Expand Down Expand Up @@ -33,3 +33,32 @@ export const getInfraEnvNameOfAgent = (resource?: AgentK8sResource | BareMetalHo

export const getClusterNameOfAgent = (agent?: AgentK8sResource) =>
agent?.spec?.clusterDeploymentName?.name;

type OnAgentChangeHostname<A = AgentK8sResource, B = BareMetalHostK8sResource> = (
agents: A[],
bmhs: B[],
onChangeHostname: (agent: A, hostname: string) => Promise<unknown>,
onChangeBMHHostname: (bmh: B, hostname: string) => Promise<unknown>,
) => (host: Host, hostname: string) => Promise<unknown>;

export const onAgentChangeHostname: OnAgentChangeHostname<
AgentK8sResource,
BareMetalHostK8sResource
> = (agents, bmhs, onChangeHostname, onChangeBMHHostname) => (host, hostname) => {
const agent = agents.find((a) => a.metadata?.uid === host.id);
let bmh: BareMetalHostK8sResource | undefined;
if (agent) {
const bmhName = agent.metadata?.labels?.[AGENT_BMH_NAME_LABEL_KEY];
if (!bmhName) {
return onChangeHostname(agent, hostname);
}
// If Agent is backed by BMH, we need to update hostname on BMH
bmh = bmhs.find(
(bmh) =>
bmh.metadata?.namespace === agent.metadata?.namespace && bmh.metadata?.name === bmhName,
);
} else {
bmh = bmhs.find((bmh) => bmh.metadata?.uid === host.id);
}
return bmh ? onChangeBMHHostname(bmh, hostname) : Promise.resolve(undefined);
};
3 changes: 2 additions & 1 deletion libs/ui-lib/lib/cim/components/modals/EditAgentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const EditAgentModal: React.FC<EditAgentModalProps> = ({ agent, onSave, ...rest
const [host] = agent ? getAIHosts([agent]) : [];
return (
<EditHostModal
isOpen
host={host}
inventory={agent?.status?.inventory}
{...rest}
onSave={async ({ hostname }) => {
agent && (await onSave(agent, hostname));
agent && (await onSave(host, hostname));
rest.onClose();
}}
/>
Expand Down
14 changes: 12 additions & 2 deletions libs/ui-lib/lib/cim/components/modals/ScaleUpModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import EditAgentModal from './EditAgentModal';
import { getAgentsHostsNames } from '../ClusterDeployment/helpers';
import { getErrorMessage } from '../../../common/utils';
import { useTranslation } from '../../../common/hooks/use-translation-wrapper';
import { onAgentChangeHostname } from '../helpers';
import { BareMetalHostK8sResource } from '../../types';

const getAgentsToAdd = (
selectedHostIds: ScaleUpFormValues['selectedHostIds'] | ScaleUpFormValues['autoSelectedHostIds'],
Expand Down Expand Up @@ -56,7 +58,9 @@ type ScaleUpModalProps = {
addHostsToCluster: (agentsToAdd: AgentK8sResource[]) => Promise<void>;
clusterDeployment: ClusterDeploymentK8sResource;
agents: AgentK8sResource[];
bareMetalHosts: BareMetalHostK8sResource[];
onChangeHostname: (agent: AgentK8sResource, hostname: string) => Promise<AgentK8sResource>;
onChangeBMHHostname: (bmh: BareMetalHostK8sResource, hostname: string) => Promise<unknown>;
onSetInstallationDiskId: AgentTableActions['onSetInstallationDiskId'];
isNutanix: boolean;
};
Expand All @@ -67,7 +71,9 @@ const ScaleUpModal: React.FC<ScaleUpModalProps> = ({
addHostsToCluster,
clusterDeployment,
agents,
bareMetalHosts,
onChangeHostname,
onChangeBMHHostname,
onSetInstallationDiskId,
isNutanix,
}) => {
Expand Down Expand Up @@ -176,9 +182,13 @@ const ScaleUpModal: React.FC<ScaleUpModalProps> = ({
{editAgent && (
<EditAgentModal
agent={editAgent}
isOpen
onClose={() => setEditAgent(undefined)}
onSave={onChangeHostname}
onSave={onAgentChangeHostname(
agents,
bareMetalHosts,
onChangeHostname,
onChangeBMHHostname,
)}
usedHostnames={getAgentsHostsNames(clusterAgents)}
/>
)}
Expand Down
1 change: 1 addition & 0 deletions libs/ui-lib/lib/common/components/hosts/EditHostForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ const EditHostForm = ({
data-testid="change-hostname-form__button-change"
type={ButtonType.submit}
isDisabled={isSubmitting || !isValid || !dirty}
isLoading={isSubmitting}
>
{t('ai:Change')}
</Button>
Expand Down

0 comments on commit 70cc84a

Please sign in to comment.