@stolostron/multicluster-sdk
Version:
Provides extensions and APIs that dynamic plugins can use to leverage multicluster capabilities provided by Red Hat Advanced Cluster Management.
104 lines • 5.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFleetSearchPoll = void 0;
/* Copyright Contributors to the Open Cluster Management project */
const search_sdk_1 = require("./search-sdk");
const react_1 = require("react");
const search_client_1 = require("./search-client");
const useFleetSearchPoll = (watchOptions, advancedSearch) => {
var _a;
const { groupVersionKind, limit, namespace, namespaced, name, isList } = watchOptions;
const { group, version, kind } = groupVersionKind !== null && groupVersionKind !== void 0 ? groupVersionKind : {};
const searchInput = (0, react_1.useMemo)(() => {
const filters = [];
// Add filters from watchOptions (these take precedence)
const watchOptionsProperties = new Set();
if (group) {
filters.push({ property: 'apigroup', values: [group] });
watchOptionsProperties.add('apigroup');
}
if (version) {
filters.push({ property: 'apiversion', values: [version] });
watchOptionsProperties.add('apiversion');
}
if (kind) {
filters.push({ property: 'kind', values: [kind] });
watchOptionsProperties.add('kind');
}
if (namespaced && namespace) {
filters.push({ property: 'namespace', values: [namespace] });
watchOptionsProperties.add('namespace');
}
if (name && name.trim()) {
// Use exact match instead of wildcard
filters.push({ property: 'name', values: [name] });
watchOptionsProperties.add('name');
}
// Add filters from advancedSearch, excluding properties already specified in watchOptions
if (advancedSearch) {
for (const { property, values } of advancedSearch) {
if (property && values !== undefined && !watchOptionsProperties.has(property)) {
filters.push({ property, values });
}
}
}
return {
filters,
limit: limit !== null && limit !== void 0 ? limit : -1,
};
}, [group, version, kind, namespaced, namespace, name, advancedSearch, limit]);
const { data: result, loading, error, } = (0, search_sdk_1.useSearchResultItemsQuery)({
client: search_client_1.searchClient,
skip: kind === undefined,
variables: {
input: [searchInput],
},
});
const data = (0, react_1.useMemo)(() => {
var _a, _b, _c;
return (_c = (_b = (_a = result === null || result === void 0 ? void 0 : result.searchResult) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.items) === null || _c === void 0 ? void 0 : _c.map((item) => {
let label = {};
if (item === null || item === void 0 ? void 0 : item.label) {
label = Object.fromEntries(item.label.split(';').map((pair) => pair.trimStart().split('=')));
}
const resource = {
cluster: item.cluster,
apiVersion: `${item.apigroup ? `${item.apigroup}/` : ''}${item.apiversion}`,
kind: item.kind,
metadata: {
creationTimestamp: item.created,
name: item.name,
namespace: item.namespace,
labels: label,
},
};
// Reverse the flattening of specific resources by the search-collector
// See https://github.com/stolostron/search-collector/blob/main/pkg/transforms/genericResourceConfig.go
switch (kind) {
case 'VirtualMachine':
resource.spec = {
running: item._specRunning,
runStrategy: item._specRunStrategy,
template: { spec: { domain: { cpu: { cores: item.cpu }, memory: { guest: item.memory } } } },
};
resource.status = { conditions: [{ type: 'Ready', status: item.ready }], printableStatus: item.status };
break;
case 'VirtualMachineInstance':
resource.status = {
conditions: [
{ type: 'LiveMigratable', status: item.liveMigratable },
{ type: 'Ready', status: item.ready },
],
interfaces: [{ ipAddress: item.ipaddress, name: 'default' }],
nodeName: item.node,
phase: item.phase,
};
}
return resource;
});
}, [kind, result]);
const nullResponse = (0, react_1.useMemo)(() => (isList ? [] : undefined), [isList]);
return [(_a = data) !== null && _a !== void 0 ? _a : nullResponse, !loading, error];
};
exports.useFleetSearchPoll = useFleetSearchPoll;
//# sourceMappingURL=useFleetSearchPoll.js.map