red-hat-storage / odf-console

UI Plugin for ODF Operator.
Apache License 2.0
3 stars 28 forks source link

[MCO] Create a common promise util function between the discovered and managed application components #1449

Open GowthamShanmugam opened 1 week ago

GowthamShanmugam commented 1 week ago

Discovered and managed application components are using different promise utils functions for the same behavior. Create one common util function and reuse the same in these components.

Ref: https://github.com/red-hat-storage/odf-console/blob/master/packages/mco/components/modals/app-manage-policies/utils/k8s-utils.ts#L67

https://github.com/red-hat-storage/odf-console/blob/master/packages/mco/components/modals/app-manage-policies/utils/k8s-utils.ts#L95

GowthamShanmugam commented 1 week ago

Rough idea:

import {
  k8sDelete,
  K8sModel,
  k8sPatch,
  K8sResourceCommon,
  Patch,
} from '@openshift-console/dynamic-plugin-sdk';
import { ACMPlacementModel, DRPlacementControlModel } from '../models';
import { getName, getNamespace } from '@odf/shared/selectors';
import { DO_NOT_DELETE_PVC_ANNOTATION_WO_SLASH } from '../constants';

export const patchK8sResource = (
  resource: K8sResourceCommon,
  model: K8sModel,
  patch: Patch[]
) =>
  k8sPatch({
    model,
    resource: {
      metadata: {
        name: getName(resource),
        namespace: getNamespace(resource),
      },
    },
    data: patch,
  });

export const deleteK8sResource = (
  resource: K8sResourceCommon,
  model: K8sModel
) =>
  k8sDelete({
    model,
    resource,
    requestInit: null,
    json: null,
  });

// Used K8sResourceCommon as a genric type to handle both custom and actual DRPC types
export const doNotDeletePVCAnnotationPromises = (
  drpcs: K8sResourceCommon[]
): Promise<K8sResourceCommon>[] => {
  const promises: Promise<K8sResourceCommon>[] = [];
  const patch = [
    {
      op: 'add',
      path: `/metadata/annotations/${DO_NOT_DELETE_PVC_ANNOTATION_WO_SLASH}`,
      value: 'true',
    },
  ];
  drpcs.forEach((drpc) => {
    promises.push(patchK8sResource(drpc, DRPlacementControlModel, patch));
  });
  return promises;
};

// Used K8sResourceCommon as a genric type to handle both custom and actual DRPC types
export const removeDRPromises = (drpcs: K8sResourceCommon[], isDicoveredApp?: boolean): Promise<K8sResourceCommon>[] => {
  const promises: Promise<K8sResourceCommon>[] = [];
  drpcs.forEach((drpc) => {
    const {name, namespace} = drpc?.|
    promises.push(deleteK8sResource(drpc, DRPlacementControlModel));
    isDicoveredApp && promises.push(deleteK8sResource(drpc, ACMPlacementModel))
  });
  return promises;
};