Skip to content

Commit

Permalink
Use ManagedClusterView for ODF info - DRPolicy creation
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjalKatiyar committed Jul 24, 2024
1 parent e4d70c8 commit 361628a
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 191 deletions.
120 changes: 0 additions & 120 deletions packages/mco/components/create-dr-policy/create-dr-policy.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,6 @@ const managedClusters: ACMManagedClusterKind[] = [
name: 'region.open-cluster-management.io',
value: 'us-east-1',
},
{
name: 'cephfsid.odf.openshift.io',
value: 'e6928945-2b95-4ff8-8400-0914ee5cb2ec',
},
{
name: 'storageclustername.odf.openshift.io',
value: 'ocs-storagecluster/openshift-storage',
},
{
name: 'storagesystemname.odf.openshift.io',
value: 'ocs-storagecluster-storagesystem/openshift-storage',
},
{
name: 'version.odf.openshift.io',
value: '4.14.0-rhodf',
},
{
name: 'count.storagecluster.odf.openshift.io',
value: '1',
},
],
conditions: [
{
Expand Down Expand Up @@ -75,26 +55,6 @@ const managedClusters: ACMManagedClusterKind[] = [
name: 'region.open-cluster-management.io',
value: 'us-west-1',
},
{
name: 'cephfsid.odf.openshift.io',
value: '84eea701-7d87-4f1f-a594-6d0c899327e3',
},
{
name: 'storageclustername.odf.openshift.io',
value: 'ocs-storagecluster/openshift-storage',
},
{
name: 'storagesystemname.odf.openshift.io',
value: 'ocs-storagecluster-storagesystem/openshift-storage',
},
{
name: 'version.odf.openshift.io',
value: '4.14.0-rhodf',
},
{
name: 'count.storagecluster.odf.openshift.io',
value: '1',
},
],
conditions: [
{
Expand Down Expand Up @@ -181,26 +141,6 @@ const managedClusters: ACMManagedClusterKind[] = [
name: 'region.open-cluster-management.io',
value: 'us-east-3',
},
{
name: 'cephfsid.odf.openshift.io',
value: 'bc2965d7-f9b1-4c08-b8fa-dd4fdfa38f71',
},
{
name: 'storageclustername.odf.openshift.io',
value: 'ocs-storagecluster/openshift-storage',
},
{
name: 'storagesystemname.odf.openshift.io',
value: 'ocs-storagecluster-storagesystem/openshift-storage',
},
{
name: 'version.odf.openshift.io',
value: '4.14.0-rhodf',
},
{
name: 'count.storagecluster.odf.openshift.io',
value: '2',
},
],
conditions: [
{
Expand Down Expand Up @@ -263,26 +203,6 @@ const managedClusters: ACMManagedClusterKind[] = [
name: 'region.open-cluster-management.io',
value: 'us-east-4',
},
{
name: 'cephfsid.odf.openshift.io',
value: '3e121eef-d363-42fd-93d4-744044b6e4cc',
},
{
name: 'storageclustername.odf.openshift.io',
value: 'ocs-storagecluster/openshift-storage',
},
{
name: 'storagesystemname.odf.openshift.io',
value: 'ocs-storagecluster-storagesystem/openshift-storage',
},
{
name: 'version.odf.openshift.io',
value: '4.13.0-rhodf',
},
{
name: 'count.storagecluster.odf.openshift.io',
value: '1',
},
],
conditions: [
{
Expand Down Expand Up @@ -314,26 +234,6 @@ const managedClusters: ACMManagedClusterKind[] = [
name: 'region.open-cluster-management.io',
value: 'us-west-4',
},
{
name: 'cephfsid.odf.openshift.io',
value: '',
},
{
name: 'storageclustername.odf.openshift.io',
value: 'ocs-storagecluster/openshift-storage',
},
{
name: 'storagesystemname.odf.openshift.io',
value: 'ocs-storagecluster-storagesystem/openshift-storage',
},
{
name: 'version.odf.openshift.io',
value: '4.14.0-rhodf',
},
{
name: 'count.storagecluster.odf.openshift.io',
value: '1',
},
],
conditions: [
{
Expand Down Expand Up @@ -365,26 +265,6 @@ const managedClusters: ACMManagedClusterKind[] = [
name: 'region.open-cluster-management.io',
value: 'us-east-5',
},
{
name: 'cephfsid.odf.openshift.io',
value: 'c1ea826f-1dc2-4faa-87b2-f0fc4665b11a',
},
{
name: 'storageclustername.odf.openshift.io',
value: 'ocs-storagecluster/openshift-storage',
},
{
name: 'storagesystemname.odf.openshift.io',
value: 'ocs-storagecluster-storagesystem/openshift-storage',
},
{
name: 'version.odf.openshift.io',
value: '4.14.0-rhodf',
},
{
name: 'count.storagecluster.odf.openshift.io',
value: '1',
},
],
conditions: [
{
Expand Down
168 changes: 117 additions & 51 deletions packages/mco/components/create-dr-policy/select-cluster-list.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import * as React from 'react';
import { getManagedClusterResourceObj } from '@odf/mco/hooks';
import { ODFInfoYamlObject } from '@odf/mco/types';
import {
getMajorVersion,
ValidateManagedClusterCondition,
getValueFromClusterClaim,
isMinimumSupportedODFVersion,
getManagedClusterViewName,
getNameNamespace,
} from '@odf/mco/utils';
import { StatusBox } from '@odf/shared/generic/status-box';
import { getName, getNamespace } from '@odf/shared/selectors';
import { ConfigMapKind } from '@odf/shared/types';
import { useCustomTranslation } from '@odf/shared/useCustomTranslationHook';
import { referenceForModel } from '@odf/shared/utils';
import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk';
import { Select, SelectOption } from '@patternfly/react-core/deprecated';
import { safeLoad } from 'js-yaml';
import {
DataList,
DataListItem,
Expand All @@ -33,9 +39,11 @@ import {
MANAGED_CLUSTER_REGION_CLAIM,
MANAGED_CLUSTER_JOINED,
MANAGED_CLUSTER_CONDITION_AVAILABLE,
ClusterClaimTypes,
MCO_CREATED_BY_LABEL_KEY,
MCO_CREATED_BY_MC_CONTROLLER,
} from '../../constants';
import { ACMManagedClusterKind } from '../../types';
import { ACMManagedClusterViewModel } from '../../models';
import { ACMManagedClusterKind, ACMManagedClusterViewKind } from '../../types';
import {
DRPolicyAction,
DRPolicyActionType,
Expand All @@ -59,43 +67,61 @@ const getFilteredClusters = (
};

const getODFInfo = (
managedCluster: ACMManagedClusterKind,
requiredODFVersion: string
requiredODFVersion: string,
odfInfoConfigData: { [key: string]: string }
): ODFConfigInfoType => {
const clusterClaims = managedCluster?.status?.clusterClaims;
const odfVersion = getValueFromClusterClaim(
clusterClaims,
ClusterClaimTypes.ODF_VERSION
);
const storageClusterNamespacedName = getValueFromClusterClaim(
clusterClaims,
ClusterClaimTypes.STORAGE_CLUSTER_NAME
);
const storageSystemNamespacedName = getValueFromClusterClaim(
clusterClaims,
ClusterClaimTypes.STORAGE_SYSTEM_NAME
);
const cephFsid = getValueFromClusterClaim(
clusterClaims,
ClusterClaimTypes.CEPH_FSID
);
const storageClusterCount = getValueFromClusterClaim(
clusterClaims,
ClusterClaimTypes.STORAGE_CLUSTER_COUNT
);
return {
odfVersion: odfVersion,
isValidODFVersion: isMinimumSupportedODFVersion(
getMajorVersion(odfVersion),
requiredODFVersion
),
storageClusterCount: Number(storageClusterCount || '0'),
storageClusterInfo: {
storageClusterNamespacedName: storageClusterNamespacedName,
storageSystemNamespacedName: storageSystemNamespacedName,
cephFSID: cephFsid,
},
};
try {
// Managed cluster with multiple StorageSystems is not currently supported for DR
// ToDo: Update this once we add support for multiple clusters
const odfInfoKey = Object.keys(odfInfoConfigData)[0];
const odfInfoYaml = odfInfoConfigData[odfInfoKey];
const odfInfo: ODFInfoYamlObject = safeLoad(odfInfoYaml);

const storageClusterName = odfInfo?.storageCluster?.namespacedName?.name;
const storageClusterNamespace =
odfInfo?.storageCluster?.namespacedName?.namespace;
const storageSystemName = odfInfo?.storageSystemName;

const odfVersion = odfInfo?.version;
const storageClusterCount = Object.keys(odfInfoConfigData).length;
const storageClusterNamespacedName = getNameNamespace(
storageClusterName,
storageClusterNamespace
);
const storageSystemNamespacedName = getNameNamespace(
storageSystemName,
storageClusterNamespace
);
const cephFSID = odfInfo?.storageCluster?.cephClusterFSID;

return {
odfVersion,
isValidODFVersion: isMinimumSupportedODFVersion(
getMajorVersion(odfVersion),
requiredODFVersion
),
storageClusterCount,
storageClusterInfo: {
storageClusterNamespacedName,
storageSystemNamespacedName,
cephFSID,
},
};
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);

return {
odfVersion: '',
isValidODFVersion: false,
storageClusterCount: 0,
storageClusterInfo: {
storageClusterNamespacedName: '',
storageSystemNamespacedName: '',
cephFSID: '',
},
};
}
};

const filterRegions = (filteredClusters: ManagedClusterInfoType[]) =>
Expand All @@ -108,7 +134,8 @@ const filterRegions = (filteredClusters: ManagedClusterInfoType[]) =>

const getManagedClusterInfo = (
cluster: ACMManagedClusterKind,
requiredODFVersion: string
requiredODFVersion: string,
odfInfoConfigData: { [key: string]: string }
): ManagedClusterInfoType => ({
name: getName(cluster),
namesapce: getNamespace(cluster),
Expand All @@ -120,9 +147,36 @@ const getManagedClusterInfo = (
cluster,
MANAGED_CLUSTER_CONDITION_AVAILABLE
),
odfInfo: getODFInfo(cluster, requiredODFVersion),
odfInfo: getODFInfo(requiredODFVersion, odfInfoConfigData),
});

const getManagedClusterInfoTypes = (
managedClusters: ACMManagedClusterKind[],
mcvs: ACMManagedClusterViewKind[],
requiredODFVersion: string
): ManagedClusterInfoType[] =>
managedClusters?.reduce((acc, cluster) => {
if (ValidateManagedClusterCondition(cluster, MANAGED_CLUSTER_JOINED)) {
// OCS creates a ConfigMap on the managed clusters, with details about StorageClusters, Clients.
// MCO creates ManagedClusterView on the hub cluster, referencing that ConfigMap.
const managedClusterName = getName(cluster);
const mcv =
mcvs.find(
(obj: ACMManagedClusterViewKind) =>
getName(obj) === getManagedClusterViewName(managedClusterName) &&
getNamespace(obj) === managedClusterName
) || {};
const odfInfoConfigData =
(mcv.status?.result as ConfigMapKind)?.data || {};
return [
...acc,
getManagedClusterInfo(cluster, requiredODFVersion, odfInfoConfigData),
];
}

return acc;
}, []);

const isChecked = (clusters: ManagedClusterInfoType[], clusterName: string) =>
clusters?.some((cluster) => cluster?.name === clusterName);

Expand All @@ -149,18 +203,30 @@ export const SelectClusterList: React.FC<SelectClusterListProps> = ({
ACMManagedClusterKind[]
>(getManagedClusterResourceObj());

const [mcvs, mcvsLoaded, mcvsLoadError] = useK8sWatchResource<
ACMManagedClusterViewKind[]
>({
kind: referenceForModel(ACMManagedClusterViewModel),
selector: {
// https://github.com/red-hat-storage/odf-multicluster-orchestrator/blob/release-4.17/controllers/utils/managedclusterview.go#L43
matchLabels: { [MCO_CREATED_BY_LABEL_KEY]: MCO_CREATED_BY_MC_CONTROLLER },
},
isList: true,
});

const allLoaded = loaded && mcvsLoaded;
const anyError = loadError || mcvsLoadError;

const clusters: ManagedClusterInfoType[] = React.useMemo(() => {
if (!!requiredODFVersion && loaded && !loadError) {
return managedClusters?.reduce(
(acc, cluster) =>
ValidateManagedClusterCondition(cluster, MANAGED_CLUSTER_JOINED)
? [...acc, getManagedClusterInfo(cluster, requiredODFVersion)]
: acc,
[]
if (!!requiredODFVersion && allLoaded && !anyError)
return getManagedClusterInfoTypes(
managedClusters,
mcvs,
requiredODFVersion
);
}

return [];
}, [requiredODFVersion, managedClusters, loaded, loadError]);
}, [requiredODFVersion, managedClusters, mcvs, allLoaded, anyError]);

const filteredClusters: ManagedClusterInfoType[] = React.useMemo(
() => getFilteredClusters(clusters, region, nameSearch),
Expand Down Expand Up @@ -245,8 +311,8 @@ export const SelectClusterList: React.FC<SelectClusterListProps> = ({
</Toolbar>
<StatusBox
data={!!nameSearch ? filteredClusters : managedClusters}
loadError={loadError}
loaded={loaded && !!clusters.length}
loadError={anyError}
loaded={allLoaded && !!clusters.length}
>
<DataList
aria-label={t('Select cluster list')}
Expand Down
Loading

0 comments on commit 361628a

Please sign in to comment.