@stolostron/multicluster-sdk 0.7.5 → 0.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -529,7 +529,7 @@ and remote clusters using WebSocket connections for real-time updates.
529
529
 
530
530
  | Function | Type |
531
531
  | ---------- | ---------- |
532
- | `useFleetK8sWatchResource` | `<R extends FleetK8sResourceCommon or FleetK8sResourceCommon[]>(initResource: FleetWatchK8sResource or null) => WatchK8sResult<R> or [undefined, boolean, any]` |
532
+ | `useFleetK8sWatchResource` | `<R extends FleetK8sResourceCommon or FleetK8sResourceCommon[]>(initResource: FleetWatchK8sResource or null) => FleetWatchK8sResult<R>` |
533
533
 
534
534
  Parameters:
535
535
 
@@ -562,15 +562,78 @@ const [deployment, loaded, error] = useFleetK8sWatchResource({
562
562
  ```
563
563
 
564
564
 
565
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/api/useFleetK8sWatchResource.ts#L51)
565
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/api/useFleetK8sWatchResource.ts#L49)
566
566
 
567
567
  ### :gear: useFleetPrometheusPoll
568
568
 
569
+ A fleet version of [`usePrometheusPoll`](https://github.com/openshift/console/blob/main/frontend/packages/console-dynamic-plugin-sdk/docs/api.md#useprometheuspoll) from
570
+ the [dynamic plugin SDK](https://www.npmjs.com/package/@openshift-console/dynamic-plugin-sdk) that polls Prometheus for metrics data from a specific cluster or across all clusters.
571
+
572
+ Although this is intended as a drop-in replacement for usePrometheusPoll there are a couple of considerations:
573
+ 1. The Observabilty service must be running on the hub in order to access metric data outside of the hub. The useIsFleetObservabilityInstalled() hook can check this
574
+ 2. The PromQL query will be different for clusters outside of the hub. The query may be completely different but at the very least it will contain the cluster name(s)
575
+ 3. Ideally the Observabilty team will setup your queries so that you only need to add the cluster name-- see example
576
+
569
577
  | Function | Type |
570
578
  | ---------- | ---------- |
571
579
  | `useFleetPrometheusPoll` | `(props: PrometheusPollProps and { cluster?: string or undefined; } and { allClusters?: boolean or undefined; }) => [response: PrometheusResponse or undefined, loaded: boolean, error: unknown]` |
572
580
 
573
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/api/useFleetPrometheusPoll.ts#L15)
581
+ Parameters:
582
+
583
+ * `endpoint`: - one of the PrometheusEndpoint (label, query, range, rules, targets)
584
+ * `cluster`: - The target cluster name. If not specified or matches hub cluster, queries local Prometheus
585
+ * `allClusters`: - If true, queries across all clusters in the fleet (requires observability)
586
+ * `query`: - (optional) Prometheus query string. If empty or undefined, polling is not started. (See note above on format)
587
+ * `delay`: - (optional) polling delay interval (ms)
588
+ * `endTime`: - (optional) for QUERY_RANGE enpoint, end of the query range
589
+ * `samples`: - (optional) for QUERY_RANGE enpoint
590
+ * `timespan`: - (optional) for QUERY_RANGE enpoint
591
+ * `namespace`: - (optional) a search param to append
592
+ * `timeout`: - (optional) a search param to append
593
+
594
+
595
+ Returns:
596
+
597
+ A tuple containing:
598
+ - `response`: PrometheusResponse object with query results, or undefined if loading/error
599
+ - `loaded`: Boolean indicating if the request has completed (successfully or with error)
600
+ - `error`: Any error that occurred during the request, including dependency check failures
601
+
602
+ Examples:
603
+
604
+ ```typescript
605
+ // (OPTIONAL) Check if the Observability service has been installed
606
+ const [response, loaded, error] = useIsFleetObservabilityInstalled()
607
+ if (!loaded) {
608
+ return <Loading />
609
+ }
610
+ if (error) {
611
+ return <ErrorState error={error} />
612
+ }
613
+
614
+ // Get the query
615
+ const [hubClusterName] = useHubClusterName();
616
+ const clusterFilter = cluster !== hubClusterName ? `,cluster='$cluster}'` : '';
617
+ const sumByCluster = !isEmpty(obj?.cluster) && obj?.cluster === hubClusterName ? ', cluster' : '';
618
+ // NOTE: this assumes your queries are identical between hub and other fleet clusters
619
+ // if not, you may need to use an entirely different query for fleet--consult the Observability team
620
+ const query = `sum(rate(kubevirt_vmi_cpu_usage_seconds_total{name='${name}',namespace='${namespace}'${clusterFilter}}[${duration}])) BY (name, namespace${sumByCluster})`,
621
+
622
+ // Query metrics data
623
+ const [response, loaded, error] = useFleetPrometheusPoll({
624
+ cluster: 'cluster',
625
+ query
626
+ });
627
+ if (!loaded) {
628
+ return <Loading />
629
+ }
630
+ if (error) {
631
+ return <ErrorState error={error} />
632
+ }
633
+ ```
634
+
635
+
636
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/api/useFleetPrometheusPoll.ts#L86)
574
637
 
575
638
  ### :gear: useFleetSearchPoll
576
639
 
@@ -674,7 +737,7 @@ Returns:
674
737
 
675
738
  ### :gear: useIsFleetObservabilityInstalled
676
739
 
677
- Hook that provides is observability installed.
740
+ Hook that determines if the Observability service has been installed on the hub
678
741
 
679
742
  | Function | Type |
680
743
  | ---------- | ---------- |
@@ -682,9 +745,32 @@ Hook that provides is observability installed.
682
745
 
683
746
  Returns:
684
747
 
685
- Array with `isObservabilityInstalled`, `loaded` and `error` values.
748
+ A tuple containing:
749
+ - `response`: Boolean indicating whether the Observability service has been installed
750
+ - `loaded`: Boolean indicating if the request has completed (successfully or with error)
751
+ - `error`: Any error that occurred during the request, including dependency check failures
686
752
 
687
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/api/useIsFleetObservabilityInstalled.ts#L9)
753
+ Examples:
754
+
755
+ ```typescript
756
+ // Check if the Observability service has been installed
757
+ const [response, loaded, error] = useIsFleetObservabilityInstalled()
758
+ if (!loaded) {
759
+ return <Loading />
760
+ }
761
+ if (error) {
762
+ return <ErrorState error={error} />
763
+ }
764
+ if (!loaded) {
765
+ return <Loading />
766
+ }
767
+
768
+ if (error) {
769
+ return <ErrorState error={error} />
770
+ }
771
+
772
+
773
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/api/useIsFleetObservabilityInstalled.ts#L37)
688
774
 
689
775
 
690
776
  ## :wrench: Constants
@@ -723,7 +809,11 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
723
809
  - [FleetK8sResourceCommon](#gear-fleetk8sresourcecommon)
724
810
  - [FleetResourceEventStreamProps](#gear-fleetresourceeventstreamprops)
725
811
  - [FleetResourceLinkProps](#gear-fleetresourcelinkprops)
812
+ - [FleetResourcesObject](#gear-fleetresourcesobject)
726
813
  - [FleetWatchK8sResource](#gear-fleetwatchk8sresource)
814
+ - [FleetWatchK8sResult](#gear-fleetwatchk8sresult)
815
+ - [FleetWatchK8sResults](#gear-fleetwatchk8sresults)
816
+ - [FleetWatchK8sResultsObject](#gear-fleetwatchk8sresultsobject)
727
817
  - [ResourceRoute](#gear-resourceroute)
728
818
  - [ResourceRouteHandler](#gear-resourceroutehandler)
729
819
  - [ResourceRouteProps](#gear-resourcerouteprops)
@@ -751,7 +841,7 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
751
841
  | ---------- | ---------- |
752
842
  | `FleetAccessReviewResourceAttributes` | `Fleet<AccessReviewResourceAttributes>` |
753
843
 
754
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L16)
844
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L33)
755
845
 
756
846
  ### :gear: FleetK8sCreateUpdateOptions
757
847
 
@@ -759,7 +849,7 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
759
849
  | ---------- | ---------- |
760
850
  | `FleetK8sCreateUpdateOptions` | `{ model: K8sModel name?: string ns?: string path?: string cluster?: string queryParams?: QueryParams data: R }` |
761
851
 
762
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L21)
852
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L38)
763
853
 
764
854
  ### :gear: FleetK8sDeleteOptions
765
855
 
@@ -767,7 +857,7 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
767
857
  | ---------- | ---------- |
768
858
  | `FleetK8sDeleteOptions` | `{ model: K8sModel name?: string ns?: string path?: string cluster?: string queryParams?: QueryParams resource: R requestInit?: RequestInit json?: Record<string, any> }` |
769
859
 
770
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L52)
860
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L69)
771
861
 
772
862
  ### :gear: FleetK8sGetOptions
773
863
 
@@ -775,7 +865,7 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
775
865
  | ---------- | ---------- |
776
866
  | `FleetK8sGetOptions` | `{ model: K8sModel name?: string ns?: string path?: string cluster?: string queryParams?: QueryParams requestInit?: RequestInit }` |
777
867
 
778
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L31)
868
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L48)
779
869
 
780
870
  ### :gear: FleetK8sListOptions
781
871
 
@@ -783,7 +873,7 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
783
873
  | ---------- | ---------- |
784
874
  | `FleetK8sListOptions` | `{ model: K8sModel queryParams: { [key: string]: any } requestInit?: RequestInit }` |
785
875
 
786
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L64)
876
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L81)
787
877
 
788
878
  ### :gear: FleetK8sPatchOptions
789
879
 
@@ -791,7 +881,7 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
791
881
  | ---------- | ---------- |
792
882
  | `FleetK8sPatchOptions` | `{ model: K8sModel name?: string ns?: string path?: string cluster?: string queryParams?: QueryParams resource: R data: Patch[] }` |
793
883
 
794
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L41)
884
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L58)
795
885
 
796
886
  ### :gear: FleetK8sResourceCommon
797
887
 
@@ -799,7 +889,7 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
799
889
  | ---------- | ---------- |
800
890
  | `FleetK8sResourceCommon` | `Fleet<K8sResourceCommon>` |
801
891
 
802
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L15)
892
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L13)
803
893
 
804
894
  ### :gear: FleetResourceEventStreamProps
805
895
 
@@ -807,7 +897,7 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
807
897
  | ---------- | ---------- |
808
898
  | `FleetResourceEventStreamProps` | `{ resource: FleetK8sResourceCommon }` |
809
899
 
810
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L19)
900
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L36)
811
901
 
812
902
  ### :gear: FleetResourceLinkProps
813
903
 
@@ -815,7 +905,15 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
815
905
  | ---------- | ---------- |
816
906
  | `FleetResourceLinkProps` | `Fleet<ResourceLinkProps>` |
817
907
 
818
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L18)
908
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L35)
909
+
910
+ ### :gear: FleetResourcesObject
911
+
912
+ | Type | Type |
913
+ | ---------- | ---------- |
914
+ | `FleetResourcesObject` | `{ [key: string]: FleetK8sResourceCommon or FleetK8sResourceCommon[] }` |
915
+
916
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L22)
819
917
 
820
918
  ### :gear: FleetWatchK8sResource
821
919
 
@@ -823,7 +921,31 @@ Array with `isObservabilityInstalled`, `loaded` and `error` values.
823
921
  | ---------- | ---------- |
824
922
  | `FleetWatchK8sResource` | `Fleet<WatchK8sResource>` |
825
923
 
826
- [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L14)
924
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L15)
925
+
926
+ ### :gear: FleetWatchK8sResult
927
+
928
+ | Type | Type |
929
+ | ---------- | ---------- |
930
+ | `FleetWatchK8sResult` | `[ R or undefined, boolean, any, ]` |
931
+
932
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L16)
933
+
934
+ ### :gear: FleetWatchK8sResults
935
+
936
+ | Type | Type |
937
+ | ---------- | ---------- |
938
+ | `FleetWatchK8sResults` | `{ [k in keyof R]: FleetWatchK8sResultsObject<R[k]> }` |
939
+
940
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L29)
941
+
942
+ ### :gear: FleetWatchK8sResultsObject
943
+
944
+ | Type | Type |
945
+ | ---------- | ---------- |
946
+ | `FleetWatchK8sResultsObject` | `{ data: R or undefined loaded: boolean loadError?: any }` |
947
+
948
+ [:link: Source](https://github.com/stolostron/console/blob/main/frontend/packages/multicluster-sdk/tree/../src/types/fleet.ts#L23)
827
949
 
828
950
  ### :gear: ResourceRoute
829
951
 
@@ -1,5 +1,4 @@
1
- import { FleetK8sResourceCommon, FleetWatchK8sResource } from '../types';
2
- import { WatchK8sResult } from '@openshift-console/dynamic-plugin-sdk';
1
+ import { FleetK8sResourceCommon, FleetWatchK8sResource, FleetWatchK8sResult } from '../types';
3
2
  /**
4
3
  * A hook for watching Kubernetes resources with support for multi-cluster environments.
5
4
  * It is equivalent to the [`useK8sWatchResource`](https://github.com/openshift/console/blob/main/frontend/packages/console-dynamic-plugin-sdk/docs/api.md#usek8swatchresource)
@@ -32,5 +31,5 @@ import { WatchK8sResult } from '@openshift-console/dynamic-plugin-sdk';
32
31
  * })
33
32
  * ```
34
33
  */
35
- export declare function useFleetK8sWatchResource<R extends FleetK8sResourceCommon | FleetK8sResourceCommon[]>(initResource: FleetWatchK8sResource | null): WatchK8sResult<R> | [undefined, boolean, any];
34
+ export declare function useFleetK8sWatchResource<R extends FleetK8sResourceCommon | FleetK8sResourceCommon[]>(initResource: FleetWatchK8sResource | null): FleetWatchK8sResult<R>;
36
35
  //# sourceMappingURL=useFleetK8sWatchResource.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useFleetK8sWatchResource.d.ts","sourceRoot":"","sources":["../../src/api/useFleetK8sWatchResource.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AACxE,OAAO,EAIL,cAAc,EACf,MAAM,uCAAuC,CAAA;AAU9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,SAAS,sBAAsB,GAAG,sBAAsB,EAAE,EAClG,YAAY,EAAE,qBAAqB,GAAG,IAAI,GACzC,cAAc,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,EAAE,GAAG,CAAC,CA+L/C"}
1
+ {"version":3,"file":"useFleetK8sWatchResource.d.ts","sourceRoot":"","sources":["../../src/api/useFleetK8sWatchResource.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACrB,mBAAmB,EAEpB,MAAM,UAAU,CAAA;AASjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,SAAS,sBAAsB,GAAG,sBAAsB,EAAE,EAClG,YAAY,EAAE,qBAAqB,GAAG,IAAI,GACzC,mBAAmB,CAAC,CAAC,CAAC,CAiDxB"}
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __rest = (this && this.__rest) || function (s, e) {
12
3
  var t = {};
13
4
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
@@ -24,14 +15,12 @@ exports.useFleetK8sWatchResource = useFleetK8sWatchResource;
24
15
  /* Copyright Contributors to the Open Cluster Management project */
25
16
  const useHubClusterName_1 = require("./useHubClusterName");
26
17
  const dynamic_plugin_sdk_1 = require("@openshift-console/dynamic-plugin-sdk");
27
- const useFleetK8sAPIPath_1 = require("./useFleetK8sAPIPath");
28
18
  const useIsFleetAvailable_1 = require("./useIsFleetAvailable");
29
19
  const react_1 = require("react");
30
- const apiRequests_1 = require("../internal/apiRequests");
31
- const fleetK8sWatchResource_1 = require("../internal/fleetK8sWatchResource");
32
- const fleetK8sWatchResourceStore_1 = require("../internal/fleetK8sWatchResourceStore");
33
- const requirements_1 = require("../internal/requirements");
34
20
  const useDeepCompareMemoize_1 = require("../internal/hooks/useDeepCompareMemoize");
21
+ const constants_1 = require("../internal/constants");
22
+ const useFleetK8sAPIPath_1 = require("./useFleetK8sAPIPath");
23
+ const fleetK8sWatchResource_1 = require("../internal/fleetK8sWatchResource");
35
24
  /**
36
25
  * A hook for watching Kubernetes resources with support for multi-cluster environments.
37
26
  * It is equivalent to the [`useK8sWatchResource`](https://github.com/openshift/console/blob/main/frontend/packages/console-dynamic-plugin-sdk/docs/api.md#usek8swatchresource)
@@ -65,163 +54,45 @@ const useDeepCompareMemoize_1 = require("../internal/hooks/useDeepCompareMemoize
65
54
  * ```
66
55
  */
67
56
  function useFleetK8sWatchResource(initResource) {
68
- var _a, _b;
69
- const [hubClusterName, hubClusterNameLoaded] = (0, useHubClusterName_1.useHubClusterName)();
57
+ const isFleetAvailable = (0, useIsFleetAvailable_1.useIsFleetAvailable)();
58
+ const [hubClusterName, hubClusterNameLoaded, hubClusterNameLoadedError] = (0, useHubClusterName_1.useHubClusterName)();
70
59
  const memoizedResource = (0, useDeepCompareMemoize_1.useDeepCompareMemoize)(initResource, true);
71
- const clusterSpecified = (memoizedResource === null || memoizedResource === void 0 ? void 0 : memoizedResource.cluster) !== undefined;
72
- const shouldWaitForHubClusterName = clusterSpecified && !hubClusterNameLoaded;
73
- const _c = memoizedResource !== null && memoizedResource !== void 0 ? memoizedResource : {}, { cluster } = _c, resource = __rest(_c, ["cluster"]);
74
- const nullResource = !memoizedResource || !(resource === null || resource === void 0 ? void 0 : resource.groupVersionKind);
75
- const selector = memoizedResource === null || memoizedResource === void 0 ? void 0 : memoizedResource.selector;
76
- const { isList, groupVersionKind, namespace, name } = resource !== null && resource !== void 0 ? resource : {};
77
- const [model] = (0, dynamic_plugin_sdk_1.useK8sModel)(groupVersionKind);
60
+ const _a = memoizedResource !== null && memoizedResource !== void 0 ? memoizedResource : {}, { cluster } = _a, resource = __rest(_a, ["cluster"]);
61
+ const { groupVersionKind } = resource;
62
+ const [model, modelLoading] = (0, dynamic_plugin_sdk_1.useK8sModel)(groupVersionKind);
78
63
  const [backendAPIPath, backendPathLoaded] = (0, useFleetK8sAPIPath_1.useFleetK8sAPIPath)(cluster);
79
- const isFleetAvailable = (0, useIsFleetAvailable_1.useIsFleetAvailable)();
80
- const isRemoteCluster = !!cluster && cluster !== hubClusterName;
81
- const useFleet = isFleetAvailable && !shouldWaitForHubClusterName && isRemoteCluster;
82
- const useFallback = !isFleetAvailable || (!shouldWaitForHubClusterName && !isRemoteCluster);
83
- const noCachedValue = (0, react_1.useMemo)(() => (isList ? [] : undefined), [isList]);
84
- const requestPath = (0, react_1.useMemo)(() => backendPathLoaded && model
85
- ? (0, apiRequests_1.buildResourceURL)({
86
- model,
87
- ns: namespace,
88
- name,
89
- cluster,
90
- queryParams: Object.assign(Object.assign({}, ((memoizedResource === null || memoizedResource === void 0 ? void 0 : memoizedResource.fieldSelector) ? { fieldSelector: memoizedResource === null || memoizedResource === void 0 ? void 0 : memoizedResource.fieldSelector } : {})), { labelSelector: (0, requirements_1.selectorToString)((memoizedResource === null || memoizedResource === void 0 ? void 0 : memoizedResource.selector) || {}) }),
91
- basePath: backendAPIPath,
92
- })
93
- : '', [
94
- model,
95
- namespace,
96
- name,
97
- cluster,
98
- backendPathLoaded,
99
- backendAPIPath,
100
- memoizedResource === null || memoizedResource === void 0 ? void 0 : memoizedResource.selector,
101
- memoizedResource === null || memoizedResource === void 0 ? void 0 : memoizedResource.fieldSelector,
102
- ]);
103
- // Subscribe to store changes for this specific resource path
104
- const cachedEntry = (0, fleetK8sWatchResourceStore_1.useFleetK8sWatchResourceStore)((state) => state.resourceCache[requestPath]);
105
- // Get store instance for imperative operations
106
- const store = fleetK8sWatchResourceStore_1.useFleetK8sWatchResourceStore.getState();
107
- const [data, setData] = (0, react_1.useState)((_a = cachedEntry === null || cachedEntry === void 0 ? void 0 : cachedEntry.data) !== null && _a !== void 0 ? _a : noCachedValue);
108
- const [loaded, setLoaded] = (0, react_1.useState)((_b = cachedEntry === null || cachedEntry === void 0 ? void 0 : cachedEntry.loaded) !== null && _b !== void 0 ? _b : false);
109
- const [error, setError] = (0, react_1.useState)(cachedEntry === null || cachedEntry === void 0 ? void 0 : cachedEntry.error);
110
- // Update local state when store changes
64
+ const waitingForHubClusterName = !!cluster && !hubClusterNameLoaded;
65
+ const isFleetQuery = cluster && cluster !== hubClusterName;
66
+ // avoid using the fleet query if it is not available
67
+ // avoid using either query if we are still waiting for the hub name to compare against the supplied cluster name
68
+ const useFleet = isFleetQuery && isFleetAvailable && !waitingForHubClusterName;
69
+ const useLocal = !isFleetQuery && !waitingForHubClusterName;
70
+ const isResourceNull = !memoizedResource || !groupVersionKind;
71
+ const [fleetResult, setFleetResult] = (0, react_1.useState)((0, fleetK8sWatchResource_1.getInitialResult)(memoizedResource, model, backendAPIPath));
72
+ const fleetResultTuple = (0, react_1.useMemo)(() => {
73
+ const { data, loaded, loadError } = fleetResult;
74
+ return [data, loaded, loadError];
75
+ }, [fleetResult]);
76
+ const localResult = (0, dynamic_plugin_sdk_1.useK8sWatchResource)(useLocal && !isResourceNull ? resource : null);
111
77
  (0, react_1.useEffect)(() => {
112
- var _a;
113
- if (cachedEntry) {
114
- setData((_a = cachedEntry.data) !== null && _a !== void 0 ? _a : noCachedValue);
115
- setLoaded(cachedEntry.loaded);
116
- setError(cachedEntry.error);
78
+ if (useFleet && !isResourceNull && backendPathLoaded && backendAPIPath && !modelLoading && model) {
79
+ (0, fleetK8sWatchResource_1.startWatch)(memoizedResource, model, backendAPIPath, setFleetResult);
80
+ return () => (0, fleetK8sWatchResource_1.stopWatch)(memoizedResource, model, backendAPIPath);
117
81
  }
118
- else {
119
- setData(noCachedValue);
120
- setLoaded(false);
121
- setError(undefined);
122
- }
123
- }, [cachedEntry, noCachedValue]);
124
- (0, react_1.useEffect)(() => {
125
- let socket;
126
- const fetchData = () => __awaiter(this, void 0, void 0, function* () {
127
- var _a;
128
- if (!useFleet || nullResource) {
129
- store.setResource(requestPath, noCachedValue, false);
130
- return;
131
- }
132
- if (!backendPathLoaded) {
133
- return;
134
- }
135
- // Check if we already have this resource cached and it's not expired
136
- const existingEntry = store.getResource(requestPath);
137
- if (existingEntry && !store.isResourceExpired(requestPath)) {
138
- return;
139
- }
140
- const socketKey = (0, fleetK8sWatchResourceStore_1.getCacheKey)({ model, cluster, namespace, name });
141
- const existingSocket = store.getSocket(socketKey);
142
- // If we have a valid socket, increment reference count and use it
143
- if (existingSocket && existingSocket.socket.readyState === WebSocket.OPEN) {
144
- store.addSocketRef(socketKey);
145
- socket = existingSocket.socket;
146
- return;
147
- }
148
- try {
149
- store.setResource(requestPath, noCachedValue, false); // Set loading state
150
- const data = (yield (0, dynamic_plugin_sdk_1.consoleFetchJSON)(requestPath, 'GET'));
151
- const processedData = (isList
152
- ? data.items.map((i) => (Object.assign({ cluster }, i)))
153
- : Object.assign({ cluster }, data));
154
- // Store the data in Zustand store
155
- store.setResource(requestPath, processedData, true);
156
- const watchQuery = {
157
- ns: namespace,
158
- cluster,
159
- };
160
- if (name) {
161
- watchQuery.fieldSelector = `metadata.name=${name}`;
162
- }
163
- if (selector) {
164
- watchQuery.labelSelector = selector;
165
- }
166
- if (isList) {
167
- watchQuery.resourceVersion = (_a = data === null || data === void 0 ? void 0 : data.metadata) === null || _a === void 0 ? void 0 : _a.resourceVersion;
168
- }
169
- // Only create a new WebSocket if we don't have a valid one
170
- socket = (0, apiRequests_1.fleetWatch)(model, watchQuery, backendAPIPath);
171
- store.setSocket(socketKey, socket);
172
- socket.onmessage = (event) => {
173
- try {
174
- // Handle WebSocket event - this will update the store and notify all subscribers
175
- (0, fleetK8sWatchResource_1.handleWebsocketEvent)(event, requestPath, isList, cluster);
176
- }
177
- catch (e) {
178
- console.error('Failed to parse WebSocket message', e);
179
- }
180
- };
181
- socket.onclose = () => {
182
- store.removeSocket(socketKey);
183
- };
184
- socket.onerror = (err) => {
185
- console.error('WebSocket error:', err);
186
- store.setResource(requestPath, processedData, true, err);
187
- };
188
- }
189
- catch (err) {
190
- store.setResource(requestPath, noCachedValue, true, err);
191
- }
192
- });
193
- fetchData();
194
- // Cleanup function - decrement reference count for socket
195
- return () => {
196
- if (useFleet && !nullResource && backendPathLoaded && model) {
197
- const socketKey = (0, fleetK8sWatchResourceStore_1.getCacheKey)({ model, cluster, namespace, name });
198
- store.removeSocketRef(socketKey);
199
- }
200
- };
201
- // eslint-disable-next-line react-hooks/exhaustive-deps
202
- }, [
203
- cluster,
204
- isList,
205
- requestPath,
206
- name,
207
- namespace,
208
- nullResource,
209
- noCachedValue,
210
- useFleet,
211
- backendPathLoaded,
212
- model,
213
- backendAPIPath,
214
- selector,
215
- ]);
216
- const fallbackResult = (0, dynamic_plugin_sdk_1.useK8sWatchResource)(useFallback ? resource : null);
217
- if (!nullResource && useFallback) {
218
- return fallbackResult;
82
+ }, [backendAPIPath, backendPathLoaded, isResourceNull, memoizedResource, model, modelLoading, useFleet]);
83
+ if (waitingForHubClusterName) {
84
+ // if we are still waiting for hub name to load,
85
+ // there is no result, loaded is false, and we should return any error fetching the hub name
86
+ return [undefined, false, hubClusterNameLoadedError];
219
87
  }
220
- else if (!nullResource && useFleet) {
221
- return [data, loaded, error];
88
+ else if (isFleetQuery && !isFleetAvailable) {
89
+ // if we need to use fleet support but it it not available,
90
+ // we return an error
91
+ return [undefined, false, constants_1.NO_FLEET_AVAILABLE_ERROR];
222
92
  }
223
93
  else {
224
- return [undefined, false, undefined];
94
+ // otherwise, we return the fleet or single-cluster result accordingly
95
+ return useFleet ? fleetResultTuple : localResult;
225
96
  }
226
97
  }
227
98
  //# sourceMappingURL=useFleetK8sWatchResource.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useFleetK8sWatchResource.js","sourceRoot":"","sources":["../../src/api/useFleetK8sWatchResource.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAkDA,4DAiMC;AAnPD,mEAAmE;AACnE,2DAAuD;AAEvD,8EAK8C;AAC9C,6DAAyD;AACzD,+DAA2D;AAC3D,iCAAoD;AACpD,yDAAsE;AACtE,6EAAwE;AACxE,uFAAmG;AACnG,2DAA2D;AAC3D,mFAA+E;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;A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,EAAE,MAAM,EAAE,SAAS,CAAC,CAAA;IAClC,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC,CAAA;IAEjB,MAAM,WAAW,GAAG,IAAA,wCAAmB,EAAI,QAAQ,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;IAEzF,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,QAAQ,IAAI,CAAC,cAAc,IAAI,iBAAiB,IAAI,cAAc,IAAI,CAAC,YAAY,IAAI,KAAK,EAAE,CAAC;YACjG,IAAA,kCAAU,EAAC,gBAAgB,EAAE,KAAK,EAAE,cAAc,EAAE,cAAc,CAAC,CAAA;YACnE,OAAO,GAAG,EAAE,CAAC,IAAA,iCAAS,EAAC,gBAAgB,EAAE,KAAK,EAAE,cAAc,CAAC,CAAA;QACjE,CAAC;IACH,CAAC,EAAE,CAAC,cAAc,EAAE,iBAAiB,EAAE,cAAc,EAAE,gBAAgB,EAAE,KAAK,EAAE,YAAY,EAAE,QAAQ,CAAC,CAAC,CAAA;IAExG,IAAI,wBAAwB,EAAE,CAAC;QAC7B,gDAAgD;QAChD,4FAA4F;QAC5F,OAAO,CAAC,SAAS,EAAE,KAAK,EAAE,yBAAyB,CAAC,CAAA;IACtD,CAAC;SAAM,IAAI,YAAY,IAAI,CAAC,gBAAgB,EAAE,CAAC;QAC7C,2DAA2D;QAC3D,qBAAqB;QACrB,OAAO,CAAC,SAAS,EAAE,KAAK,EAAE,oCAAwB,CAAC,CAAA;IACrD,CAAC;SAAM,CAAC;QACN,sEAAsE;QACtE,OAAO,QAAQ,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,WAAW,CAAA;IAClD,CAAC;AACH,CAAC"}
@@ -1,5 +1,75 @@
1
1
  import { PrometheusPollProps, PrometheusResponse } from '@openshift-console/dynamic-plugin-sdk';
2
2
  import { Fleet } from '../types';
3
+ /**
4
+ * A fleet version of [`usePrometheusPoll`](https://github.com/openshift/console/blob/main/frontend/packages/console-dynamic-plugin-sdk/docs/api.md#useprometheuspoll) from
5
+ * the [dynamic plugin SDK](https://www.npmjs.com/package/@openshift-console/dynamic-plugin-sdk) that polls Prometheus for metrics data from a specific cluster or across all clusters.
6
+ *
7
+ * Although this is intended as a drop-in replacement for usePrometheusPoll there are a couple of considerations:
8
+ * 1. The Observabilty service must be running on the hub in order to access metric data outside of the hub. The useIsFleetObservabilityInstalled() hook can check this
9
+ * 2. The PromQL query will be different for clusters outside of the hub. The query may be completely different but at the very least it will contain the cluster name(s)
10
+ * 3. Ideally the Observabilty team will setup your queries so that you only need to add the cluster name-- see example
11
+ *
12
+ * @param {PrometheusEndpoint} endpoint - one of the PrometheusEndpoint (label, query, range, rules, targets)
13
+ * @param {string} [cluster] - The target cluster name. If not specified or matches hub cluster, queries local Prometheus
14
+ * @param {boolean} [allClusters] - If true, queries across all clusters in the fleet (requires observability)
15
+ * @param {string} [query] - (optional) Prometheus query string. If empty or undefined, polling is not started. (See note above on format)
16
+ * @param {number} [delay] - (optional) polling delay interval (ms)
17
+ * @param {number} [endTime] - (optional) for QUERY_RANGE enpoint, end of the query range
18
+ * @param {number} [samples] - (optional) for QUERY_RANGE enpoint
19
+ * @param {number} [timespan] - (optional) for QUERY_RANGE enpoint
20
+ * @param {string} [namespace] - (optional) a search param to append
21
+ * @param {string} [timeout] - (optional) a search param to append
22
+ *
23
+ * @returns A tuple containing:
24
+ * - `response`: PrometheusResponse object with query results, or undefined if loading/error
25
+ * - `loaded`: Boolean indicating if the request has completed (successfully or with error)
26
+ * - `error`: Any error that occurred during the request, including dependency check failures
27
+
28
+ * @example
29
+ * ```typescript
30
+ * // (OPTIONAL) Check if the Observability service has been installed
31
+ * const [response, loaded, error] = useIsFleetObservabilityInstalled()
32
+ * if (!loaded) {
33
+ * return <Loading />
34
+ * }
35
+ * if (error) {
36
+ * return <ErrorState error={error} />
37
+ * }
38
+ *
39
+ * // Get the query
40
+ * const [hubClusterName] = useHubClusterName();
41
+ * const clusterFilter = cluster !== hubClusterName ? `,cluster='$cluster}'` : '';
42
+ * const sumByCluster = !isEmpty(obj?.cluster) && obj?.cluster === hubClusterName ? ', cluster' : '';
43
+ * // NOTE: this assumes your queries are identical between hub and other fleet clusters
44
+ * // if not, you may need to use an entirely different query for fleet--consult the Observability team
45
+ * const query = `sum(rate(kubevirt_vmi_cpu_usage_seconds_total{name='${name}',namespace='${namespace}'${clusterFilter}}[${duration}])) BY (name, namespace${sumByCluster})`,
46
+ *
47
+ * // Query metrics data
48
+ * const [response, loaded, error] = useFleetPrometheusPoll({
49
+ * cluster: 'cluster',
50
+ * query
51
+ * });
52
+ * if (!loaded) {
53
+ * return <Loading />
54
+ * }
55
+ * if (error) {
56
+ * return <ErrorState error={error} />
57
+ * }
58
+ * ```
59
+ *
60
+ * @remarks
61
+ * This hook intelligently routes Prometheus queries based on the target cluster:
62
+ * - If no cluster is specified or the cluster matches the hub cluster, it uses the local Prometheus instance
63
+ * - If a specific managed cluster is specified, it uses the fleet observability service (requires multicluster observability to be installed)
64
+ * - If `allClusters` is true, it queries across all clusters in the fleet using the observability service
65
+ *
66
+ * The hook automatically handles:
67
+ * - Checking if multicluster observability is installed when needed using useIsFleetObservabilityInstalled() hook
68
+ * - Determining the hub cluster name for comparison
69
+ * - Routing queries to the appropriate Prometheus endpoint
70
+ * - Providing appropriate error states when dependencies are not available
71
+ *
72
+ */
3
73
  export declare const useFleetPrometheusPoll: (props: Fleet<PrometheusPollProps> & {
4
74
  allClusters?: boolean;
5
75
  }) => [response: PrometheusResponse | undefined, loaded: boolean, error: unknown];
@@ -1 +1 @@
1
- {"version":3,"file":"useFleetPrometheusPoll.d.ts","sourceRoot":"","sources":["../../src/api/useFleetPrometheusPoll.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAqB,MAAM,uCAAuC,CAAA;AAUlH,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AAGhC,eAAO,MAAM,sBAAsB,EAAE,CACnC,KAAK,EAAE,KAAK,CAAC,mBAAmB,CAAC,GAAG;IAAE,WAAW,CAAC,EAAE,OAAO,CAAA;CAAE,KAC1D,CAAC,QAAQ,EAAE,kBAAkB,GAAG,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAsE9E,CAAA"}
1
+ {"version":3,"file":"useFleetPrometheusPoll.d.ts","sourceRoot":"","sources":["../../src/api/useFleetPrometheusPoll.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAqB,MAAM,uCAAuC,CAAA;AAUlH,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AAGhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqEG;AAEH,eAAO,MAAM,sBAAsB,EAAE,CACnC,KAAK,EAAE,KAAK,CAAC,mBAAmB,CAAC,GAAG;IAAE,WAAW,CAAC,EAAE,OAAO,CAAA;CAAE,KAC1D,CAAC,QAAQ,EAAE,kBAAkB,GAAG,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAsE9E,CAAA"}