Skip to content

DFBUGS-2822: [release-4.19] Fix DR Status issue for discovered apps in Virtual Machine page #2135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release-4.19
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import * as React from 'react';
import {
DISCOVERED_APP_NS,
KUBE_INSTANCE_LABEL,
ODF_RESOURCE_TYPE_LABEL,
} from '@odf/mco/constants';
import { DISCOVERED_APP_NS, KUBE_INSTANCE_LABEL } from '@odf/mco/constants';
import {
getApplicationResourceObj,
getApplicationSetResourceObj,
Expand All @@ -27,19 +23,45 @@ import {
getLabel,
getName,
getNamespace,
VirtualMachineModel,
} from '@odf/shared';
import {
K8sResourceCommon,
useK8sWatchResource,
} from '@openshift-console/dynamic-plugin-sdk';
import { findDRPCUsingVM } from '../../modals/app-manage-policies/utils/parser-utils';
import {
ApplicationSetParser,
SubscriptionParser,
DiscoveredParser,
} from '../parsers';

const findDRPCByNamespaceAndCluster = (
drpcs: DRPlacementControlKind[],
namespace: string,
cluster: string
): DRPlacementControlKind | undefined =>
drpcs.find(
(drpc) =>
drpc?.spec?.protectedNamespaces?.includes(namespace) &&
drpc?.spec?.preferredCluster === cluster
);

const useMatchingDRPC = (
namespace: string,
cluster: string
): DRPlacementControlKind | undefined => {
const drpcWatchResource = React.useMemo(
() => getDRPlacementControlResourceObj({ namespace: DISCOVERED_APP_NS }),
[]
);

const [drpcs, loaded, loadError] =
useK8sWatchResource<DRPlacementControlKind[]>(drpcWatchResource);

if (!loaded || loadError) return undefined;

return findDRPCByNamespaceAndCluster(drpcs, namespace, cluster);
};

const ApplicationSetHelper: React.FC<ParserHelperProps> = ({
vmName,
vmNamespace,
Expand All @@ -65,32 +87,9 @@ const SubscriptionHelper: React.FC<ParserHelperProps> = ({
return <SubscriptionParser application={resource} />;
};

const DiscoveredHelper: React.FC<ParserHelperProps> = ({
vmName,
vmNamespace,
clusterName,
}) => {
const [drpcs, loaded, loadError] = useK8sWatchResource<
DRPlacementControlKind[]
>(
getDRPlacementControlResourceObj({
namespace: DISCOVERED_APP_NS,
selector: {
matchLabels: {
// To optimize the VM DRPC watch
[ODF_RESOURCE_TYPE_LABEL]: VirtualMachineModel.kind.toLowerCase(),
},
},
})
);

const drpc = React.useMemo(
() => findDRPCUsingVM(drpcs, vmName, vmNamespace, clusterName),
[vmName, vmNamespace, drpcs, clusterName]
);

if (!loaded || loadError || !drpc) return null;
return <DiscoveredParser application={drpc} />;
const DiscoveredHelper: React.FC<DiscoveredHelperProps> = ({ application }) => {
if (!application) return null;
return <DiscoveredParser application={application} />;
};

export const VirtualMachineParser: React.FC<VirtualMachineParserProps> = ({
Expand All @@ -109,10 +108,13 @@ export const VirtualMachineParser: React.FC<VirtualMachineParserProps> = ({
: querySubscriptionResourcesForVM(vmName, vmNamespace, clusterName),
[vmName, vmNamespace, clusterName, argoApplicationName]
);

const [searchResult] = useACMSafeFetch(searchQuery);
const matchedDRPC = useMatchingDRPC(vmNamespace, clusterName);

if (matchedDRPC && matchedDRPC.spec && matchedDRPC.metadata?.name) {
return <DiscoveredHelper application={matchedDRPC} />;
}

// Extract managed application CR (Application / ApplicationSet)
const managedApplication: SearchResultItemType =
searchResult?.data?.searchResult?.[0]?.related?.[0]?.items?.[0];

Expand All @@ -131,13 +133,7 @@ export const VirtualMachineParser: React.FC<VirtualMachineParserProps> = ({
);
}

return (
<DiscoveredHelper
vmName={vmName}
vmNamespace={vmNamespace}
clusterName={clusterName}
/>
);
return null;
};

type VirtualMachineParserProps = {
Expand All @@ -147,7 +143,10 @@ type VirtualMachineParserProps = {
type ParserHelperProps = {
vmName: string;
vmNamespace: string;
clusterName?: string;
};

type DiscoveredHelperProps = {
application: DRPlacementControlKind | undefined;
};

export default VirtualMachineParser;
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import * as React from 'react';
import { getPlacementKindObj } from '@odf/mco/components/discovered-application-wizard/utils/k8s-utils';
import {
DISCOVERED_APP_NS,
DRApplication,
ODF_RESOURCE_TYPE_LABEL,
} from '@odf/mco/constants';
import { DISCOVERED_APP_NS, DRApplication } from '@odf/mco/constants';
import {
getDRPlacementControlResourceObj,
getDRPolicyResourceObj,
Expand All @@ -16,7 +12,7 @@ import {
getSearchResultItems,
queryAppWorkloadPVCs,
} from '@odf/mco/utils';
import { getName, getNamespace, VirtualMachineModel } from '@odf/shared';
import { getName, getNamespace } from '@odf/shared';
import {
K8sResourceCommon,
useK8sWatchResource,
Expand Down Expand Up @@ -53,12 +49,6 @@ export const DiscoveredVMParser: React.FC<DiscoveredVMParserProps> = ({
>(
getDRPlacementControlResourceObj({
namespace: DISCOVERED_APP_NS,
selector: {
matchLabels: {
// To optimize the VM DRPC watch
[ODF_RESOURCE_TYPE_LABEL]: VirtualMachineModel.kind.toLowerCase(),
},
},
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
PVC_RESOURCE_SELECTOR,
PROTECTED_VMS,
VM_RECIPE_NAME,
ODF_RESOURCE_TYPE_LABEL,
K8S_RESOURCE_SELECTOR_LABEL_KEY,
IS_CG_ENABLED_ANNOTATION,
} from '@odf/mco/constants';
Expand Down Expand Up @@ -471,9 +470,6 @@ export const assignPromisesForDiscovered = async (
[PVC_RESOURCE_SELECTOR]: [protectionName],
[PROTECTED_VMS]: [vmName],
},
labels: {
[ODF_RESOURCE_TYPE_LABEL]: VirtualMachineModel.kind.toLowerCase(),
},
annotations,
}),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ export const findDRPCUsingVM = (
): DRPlacementControlKind | undefined =>
drpcs.find(
(drpc) =>
getVMNamesFromRecipe(drpc.spec).includes(vmName) &&
(getVMNamesFromRecipe(drpc.spec).includes(vmName) ||
(
drpc.status?.resourceConditions?.resourceMeta?.protectedpvcs || []
).some((pvc) => pvc.startsWith(vmName))) &&
drpc.spec?.protectedNamespaces?.includes(vmNamespace) &&
getPrimaryClusterName(drpc) === cluster
);
3 changes: 0 additions & 3 deletions packages/mco/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,3 @@ export const ADMIN_FLAG = 'ADMIN';
export const DISCOVERED_APP_NS = 'openshift-dr-ops';

export const NAME_NAMESPACE_SPLIT_CHAR = '/';

// Console search optimization selector
export const ODF_RESOURCE_TYPE_LABEL = 'odf.console.selector/resourcetype';