@stolostron/multicluster-sdk
Version:
Provides extensions and APIs that dynamic plugins can use to leverage multicluster capabilities provided by Red Hat Advanced Cluster Management.
66 lines • 2.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFleetClusterSetNames = useFleetClusterSetNames;
/* Copyright Contributors to the Open Cluster Management project */
const react_1 = require("react");
const useFleetClustersInternal_1 = require("../internal/useFleetClustersInternal");
const clusterUtils_1 = require("../internal/clusterUtils");
/**
* Hook that returns unique cluster set names from managed clusters with optional filtering by cluster proxy addon and availability status.
*
* This hook watches ManagedCluster resources and by default filters them to only include clusters
* that have both the label `feature.open-cluster-management.io/addon-cluster-proxy: available` AND
* the condition `ManagedClusterConditionAvailable` with status `True`. It then collects unique
* values from the `cluster.open-cluster-management.io/clusterset` label.
*
* @param considerAllClusters - Optional boolean to consider all clusters regardless of labels and conditions.
* Defaults to false. When false (default), only considers clusters with the
* 'feature.open-cluster-management.io/addon-cluster-proxy: available' label AND
* 'ManagedClusterConditionAvailable' status: 'True'.
* When true, considers all clusters regardless of labels and conditions.
*
* @returns A tuple containing:
* - clusterSets: Array of unique cluster set names from the clusterset labels
* - loaded: Boolean indicating if the resource watch has loaded
* - error: Any error that occurred during the watch operation
*
* @example
* ```tsx
* // Get cluster sets from only clusters with cluster proxy addon available AND ManagedClusterConditionAvailable: 'True' (default behavior)
* const [availableClusterSets, loaded, error] = useFleetClusterSetNames()
*
* // Get cluster sets from all clusters regardless of labels and conditions
* const [allClusterSets, loaded, error] = useFleetClusterSetNames(true)
*
* // Explicitly filter by cluster proxy addon and availability (same as default)
* const [filteredClusterSets, loaded, error] = useFleetClusterSetNames(false)
*
* if (!loaded) {
* return <Loading />
* }
*
* if (error) {
* return <ErrorState error={error} />
* }
*
* return (
* <div>
* {availableClusterSets.map(setName => (
* <div key={setName}>{setName}</div>
* ))}
* </div>
* )
* ```
*/
function useFleetClusterSetNames(considerAllClusters = false) {
const [filteredClusters, loaded, error] = (0, useFleetClustersInternal_1.useFleetClustersInternal)({
returnAllClusters: considerAllClusters,
});
const uniqueClusterSets = (0, react_1.useMemo)(() => {
const clusterSetNames = filteredClusters.map(clusterUtils_1.getClusterSetName);
// Return unique cluster set names
return Array.from(new Set(clusterSetNames));
}, [filteredClusters]);
return [uniqueClusterSets, loaded, error];
}
//# sourceMappingURL=useFleetClusterSetNames.js.map