Skip to content
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

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

Open
GowthamShanmugam opened this issue Jun 19, 2024 · 1 comment

Comments

@GowthamShanmugam
Copy link
Contributor

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
Copy link
Contributor Author

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;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant