@stolostron/multicluster-sdk
Version:
Provides extensions and APIs that dynamic plugins can use to leverage multicluster capabilities provided by Red Hat Advanced Cluster Management.
89 lines • 4.43 kB
JavaScript
;
/* Copyright Contributors to the Open Cluster Management project */
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleWebsocketEvent = exports.getCacheKey = exports.fleetSocketCache = exports.fleetResourceCache = void 0;
exports.clearFleetK8sWatchResourceCache = clearFleetK8sWatchResourceCache;
/**
* Cache for fleet resources.
* The key is the request path, and the value is the resource data.
*/
exports.fleetResourceCache = {};
/**
* Cache for fleet WebSockets.
* The key is a cache key generated by getCacheKey, and the value is the WebSocket instance.
*/
exports.fleetSocketCache = {};
/**
* Generates a cache key for a given resource.
* @param model - The Kubernetes model for the resource.
* @param cluster - The name of the cluster.
* @param namespace - The namespace of the resource.
* @param name - The name of the resource.
* @returns A unique cache key string.
*/
const getCacheKey = ({ model, cluster, namespace, name, }) => {
return [cluster, model === null || model === void 0 ? void 0 : model.apiVersion, model === null || model === void 0 ? void 0 : model.kind, namespace, name].join('|');
}; /**
* Clears the cache for fleet resources and closes any open WebSockets.
*/
exports.getCacheKey = getCacheKey;
function clearFleetK8sWatchResourceCache() {
Object.keys(exports.fleetResourceCache).forEach((key) => delete exports.fleetResourceCache[key]);
Object.values(exports.fleetSocketCache).forEach((socket) => {
if (socket.readyState === WebSocket.OPEN) {
socket.close();
}
});
Object.keys(exports.fleetSocketCache).forEach((key) => delete exports.fleetSocketCache[key]);
}
const handleWebsocketEvent = (event, requestPath, setData, isList, fleetResourceCache, cluster) => {
var _a;
if (!event) {
console.warn('Received undefined event', event);
return;
}
const eventDataParsed = JSON.parse(event.data);
const eventType = eventDataParsed === null || eventDataParsed === void 0 ? void 0 : eventDataParsed.type;
const object = eventDataParsed === null || eventDataParsed === void 0 ? void 0 : eventDataParsed.object;
if (!object)
return;
if (!isList) {
if (eventType === 'ADDED' && fleetResourceCache[requestPath])
return;
const processedEventData = Object.assign({ cluster }, object);
if (processedEventData) {
fleetResourceCache[requestPath] = processedEventData;
setData(processedEventData);
}
return;
}
if (eventType === 'DELETED') {
const storedData = fleetResourceCache[requestPath];
const updatedData = storedData.filter((i) => { var _a, _b; return ((_a = i.metadata) === null || _a === void 0 ? void 0 : _a.uid) !== ((_b = object === null || object === void 0 ? void 0 : object.metadata) === null || _b === void 0 ? void 0 : _b.uid); });
fleetResourceCache[requestPath] = updatedData;
setData(updatedData);
return;
}
if (eventType !== 'ADDED' && eventType !== 'MODIFIED') {
return;
}
if (!((_a = object === null || object === void 0 ? void 0 : object.metadata) === null || _a === void 0 ? void 0 : _a.uid)) {
console.warn('Event object does not have a metadata.uid', eventDataParsed);
return;
}
const storedData = fleetResourceCache[requestPath];
const objectExists = storedData.some((i) => { var _a, _b; return ((_a = i.metadata) === null || _a === void 0 ? void 0 : _a.uid) === ((_b = object === null || object === void 0 ? void 0 : object.metadata) === null || _b === void 0 ? void 0 : _b.uid); });
if (objectExists && eventType === 'MODIFIED') {
const updatedData = storedData.map((i) => { var _a, _b; return (((_a = i.metadata) === null || _a === void 0 ? void 0 : _a.uid) === ((_b = object === null || object === void 0 ? void 0 : object.metadata) === null || _b === void 0 ? void 0 : _b.uid) ? Object.assign({ cluster }, object) : i); });
fleetResourceCache[requestPath] = updatedData;
setData(updatedData);
return;
}
if (!objectExists) {
const updatedData = [...storedData, Object.assign({ cluster }, object)];
fleetResourceCache[requestPath] = updatedData;
setData(updatedData);
}
};
exports.handleWebsocketEvent = handleWebsocketEvent;
//# sourceMappingURL=fleetK8sWatchResource.js.map