Skip to content

Commit

Permalink
Merge pull request #1556 from GowthamShanmugam/bz_2210762
Browse files Browse the repository at this point in the history
Fix label overriding issue when same label is used with different values
  • Loading branch information
openshift-merge-bot[bot] authored Sep 2, 2024
2 parents 5c8097d + 1fe38f5 commit 376ebbf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
PROTECTED_APP_ANNOTATION_WO_SLASH,
} from '@odf/mco/constants';
import { DRPlacementControlKind } from '@odf/mco/types';
import { matchClusters } from '@odf/mco/utils';
import { objectify } from '@odf/shared/modals/EditLabelModal';
import { convertLabelToExpression, matchClusters } from '@odf/mco/utils';
import {
getAPIVersion,
getAnnotations,
Expand Down Expand Up @@ -59,7 +58,7 @@ export const getDRPCKindObj = (
},
preferredCluster: matchClusters(drClusterNames, decisionClusters),
pvcSelector: {
matchLabels: objectify(pvcSelectors),
matchExpressions: convertLabelToExpression(pvcSelectors),
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
DRPolicyKind,
} from '@odf/mco/types';
import {
convertExpressionToLabel,
getReplicationType,
isDRPolicyValidated,
matchClusters,
} from '@odf/mco/utils';
import { getLatestDate } from '@odf/shared/details-page/datetime';
import { arrayify } from '@odf/shared/modals/EditLabelModal';
import { Selector } from '@openshift-console/dynamic-plugin-sdk';
import * as _ from 'lodash-es';
import {
ApplicationType,
Expand All @@ -20,6 +22,11 @@ import {
PlacementType,
} from './types';

const getPVCSelector = (pvcSelector: Selector): string[] => {
const { matchLabels, matchExpressions } = pvcSelector;
return convertExpressionToLabel(matchExpressions) || arrayify(matchLabels);
};

const getDRPolicyInfo = (drPolicy: DRPolicyKind, assignedOn?: string) =>
!_.isEmpty(drPolicy)
? {
Expand Down Expand Up @@ -72,7 +79,7 @@ export const generateDRPlacementControlInfo = (
metadata: drpc.metadata,
drPolicyRef: drpc.spec.drPolicyRef,
placementInfo: plsInfo,
pvcSelector: arrayify(drpc?.spec.pvcSelector.matchLabels) || [],
pvcSelector: getPVCSelector(drpc.spec.pvcSelector),
lastGroupSyncTime: drpc?.status?.lastGroupSyncTime,
},
]
Expand Down
32 changes: 32 additions & 0 deletions packages/mco/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { groupVersionFor, referenceFor } from '@odf/shared/utils';
import {
ObjectReference,
K8sResourceCommon,
MatchExpression,
Operator,
} from '@openshift-console/dynamic-plugin-sdk';

export const getGVKFromK8Resource = (resource: K8sResourceCommon) => {
Expand All @@ -30,3 +32,33 @@ export const getMajorVersion = (version: string): string => {
? version.split('.')[0] + '.' + version.split('.')[1] + '.0'
: '';
};

export const convertLabelToExpression = (
labels: string[]
): MatchExpression[] => {
// appName: busybox-1, appName: busybox-2 will be converted to {key: appName, operator: Equals, values: [busybox-1, busybox-2]}
const labelExpressions = labels.reduce((acc, label) => {
const [key, value = ''] = label.split('=');
if (key in acc) {
acc[key].values.push(value);
} else {
acc[key] = {
key,
operator: Operator.In,
values: [value],
};
}
return acc;
}, {} as { [key in string]: MatchExpression });
return Object.values(labelExpressions);
};

export const convertExpressionToLabel = (
labelExpressions: MatchExpression[]
): string[] =>
// {key: appName, operator: Equals, values: [busybox-1, busybox-2]} will be converted to appName: busybox-1, appName: busybox-2
labelExpressions?.reduce((acc, labelExpression) => {
const { key, values } = labelExpression;
acc.push(...values.map((value) => `${key}=${value}`));
return acc;
}, []);

0 comments on commit 376ebbf

Please sign in to comment.