@stolostron/multicluster-sdk
Version:
Provides extensions and APIs that dynamic plugins can use to leverage multicluster capabilities provided by Red Hat Advanced Cluster Management.
39 lines • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fleetK8sPatch = fleetK8sPatch;
const lodash_1 = require("lodash");
const dynamic_plugin_sdk_1 = require("@openshift-console/dynamic-plugin-sdk");
const apiRequests_1 = require("../internal/apiRequests");
const isHubRequest_1 = require("../internal/isHubRequest");
/**
* A fleet version of [`k8sPatch`](https://github.com/openshift/console/blob/main/frontend/packages/console-dynamic-plugin-sdk/docs/api.md#k8slist) from
* the [dynamic plugin SDK](https://www.npmjs.com/package/@openshift-console/dynamic-plugin-sdk) that patches any resource on the specified cluster, based on the provided options.
*
* The cluster name can be specified in options or the resource, with the value from options taking precedence.
* If the cluster name is not specified or matches the name of the hub cluster, the implementation from the dynamic plugin SDK is used.
*
* When a client needs to perform the partial update, the client can use `fleetK8sPatch`.
* Alternatively, the client can use `fleetK8sUpdate` to replace an existing resource entirely.
* See more https://datatracker.ietf.org/doc/html/rfc6902
* @param options Which are passed as key-value pairs in the map.
* @param options.cluster - the cluster on which to patch the resource
* @param options.model - Kubernetes model
* @param options.resource - The resource to be patched.
* @param options.data - Only the data to be patched on existing resource with the operation, path, and value.
* @param options.path - Appends as subpath if provided.
* @param options.queryParams - The query parameters to be included in the URL.
* @returns A promise that resolves to the response of the resource patched.
* In case of failure promise gets rejected with HTTP error response.
*/
async function fleetK8sPatch(options) {
const cluster = (0, apiRequests_1.getClusterFromOptions)(options);
const optionsWithoutCluster = (0, apiRequests_1.getOptionsWithoutCluster)(options);
const patches = (0, lodash_1.compact)(options.data);
const result = (await (0, isHubRequest_1.isHubRequest)(cluster))
? (0, dynamic_plugin_sdk_1.k8sPatch)(optionsWithoutCluster)
: (0, lodash_1.isEmpty)(patches)
? Promise.resolve(options.resource)
: dynamic_plugin_sdk_1.consoleFetchJSON.patch(await (0, apiRequests_1.getResourceURLFromOptions)(options), patches);
return { ...(await result), cluster };
}
//# sourceMappingURL=fleetK8sPatch.js.map