1
1
import * as React from 'react' ;
2
- import {
3
- DISCOVERED_APP_NS ,
4
- KUBE_INSTANCE_LABEL ,
5
- ODF_RESOURCE_TYPE_LABEL ,
6
- } from '@odf/mco/constants' ;
2
+ import { DISCOVERED_APP_NS , KUBE_INSTANCE_LABEL } from '@odf/mco/constants' ;
7
3
import {
8
4
getApplicationResourceObj ,
9
5
getApplicationSetResourceObj ,
@@ -27,19 +23,45 @@ import {
27
23
getLabel ,
28
24
getName ,
29
25
getNamespace ,
30
- VirtualMachineModel ,
31
26
} from '@odf/shared' ;
32
27
import {
33
28
K8sResourceCommon ,
34
29
useK8sWatchResource ,
35
30
} from '@openshift-console/dynamic-plugin-sdk' ;
36
- import { findDRPCUsingVM } from '../../modals/app-manage-policies/utils/parser-utils' ;
37
31
import {
38
32
ApplicationSetParser ,
39
33
SubscriptionParser ,
40
34
DiscoveredParser ,
41
35
} from '../parsers' ;
42
36
37
+ const findDRPCByNamespaceAndCluster = (
38
+ drpcs : DRPlacementControlKind [ ] ,
39
+ namespace : string ,
40
+ cluster : string
41
+ ) : DRPlacementControlKind | undefined =>
42
+ drpcs . find (
43
+ ( drpc ) =>
44
+ drpc ?. spec ?. protectedNamespaces ?. includes ( namespace ) &&
45
+ drpc ?. spec ?. preferredCluster === cluster
46
+ ) ;
47
+
48
+ const useMatchingDRPC = (
49
+ namespace : string ,
50
+ cluster : string
51
+ ) : DRPlacementControlKind | undefined => {
52
+ const drpcWatchResource = React . useMemo (
53
+ ( ) => getDRPlacementControlResourceObj ( { namespace : DISCOVERED_APP_NS } ) ,
54
+ [ ]
55
+ ) ;
56
+
57
+ const [ drpcs , loaded , loadError ] =
58
+ useK8sWatchResource < DRPlacementControlKind [ ] > ( drpcWatchResource ) ;
59
+
60
+ if ( ! loaded || loadError ) return undefined ;
61
+
62
+ return findDRPCByNamespaceAndCluster ( drpcs , namespace , cluster ) ;
63
+ } ;
64
+
43
65
const ApplicationSetHelper : React . FC < ParserHelperProps > = ( {
44
66
vmName,
45
67
vmNamespace,
@@ -65,32 +87,9 @@ const SubscriptionHelper: React.FC<ParserHelperProps> = ({
65
87
return < SubscriptionParser application = { resource } /> ;
66
88
} ;
67
89
68
- const DiscoveredHelper : React . FC < ParserHelperProps > = ( {
69
- vmName,
70
- vmNamespace,
71
- clusterName,
72
- } ) => {
73
- const [ drpcs , loaded , loadError ] = useK8sWatchResource <
74
- DRPlacementControlKind [ ]
75
- > (
76
- getDRPlacementControlResourceObj ( {
77
- namespace : DISCOVERED_APP_NS ,
78
- selector : {
79
- matchLabels : {
80
- // To optimize the VM DRPC watch
81
- [ ODF_RESOURCE_TYPE_LABEL ] : VirtualMachineModel . kind . toLowerCase ( ) ,
82
- } ,
83
- } ,
84
- } )
85
- ) ;
86
-
87
- const drpc = React . useMemo (
88
- ( ) => findDRPCUsingVM ( drpcs , vmName , vmNamespace , clusterName ) ,
89
- [ vmName , vmNamespace , drpcs , clusterName ]
90
- ) ;
91
-
92
- if ( ! loaded || loadError || ! drpc ) return null ;
93
- return < DiscoveredParser application = { drpc } /> ;
90
+ const DiscoveredHelper : React . FC < DiscoveredHelperProps > = ( { application } ) => {
91
+ if ( ! application ) return null ;
92
+ return < DiscoveredParser application = { application } /> ;
94
93
} ;
95
94
96
95
export const VirtualMachineParser : React . FC < VirtualMachineParserProps > = ( {
@@ -109,10 +108,13 @@ export const VirtualMachineParser: React.FC<VirtualMachineParserProps> = ({
109
108
: querySubscriptionResourcesForVM ( vmName , vmNamespace , clusterName ) ,
110
109
[ vmName , vmNamespace , clusterName , argoApplicationName ]
111
110
) ;
112
-
113
111
const [ searchResult ] = useACMSafeFetch ( searchQuery ) ;
112
+ const matchedDRPC = useMatchingDRPC ( vmNamespace , clusterName ) ;
113
+
114
+ if ( matchedDRPC && matchedDRPC . spec && matchedDRPC . metadata ?. name ) {
115
+ return < DiscoveredHelper application = { matchedDRPC } /> ;
116
+ }
114
117
115
- // Extract managed application CR (Application / ApplicationSet)
116
118
const managedApplication : SearchResultItemType =
117
119
searchResult ?. data ?. searchResult ?. [ 0 ] ?. related ?. [ 0 ] ?. items ?. [ 0 ] ;
118
120
@@ -131,13 +133,7 @@ export const VirtualMachineParser: React.FC<VirtualMachineParserProps> = ({
131
133
) ;
132
134
}
133
135
134
- return (
135
- < DiscoveredHelper
136
- vmName = { vmName }
137
- vmNamespace = { vmNamespace }
138
- clusterName = { clusterName }
139
- />
140
- ) ;
136
+ return null ;
141
137
} ;
142
138
143
139
type VirtualMachineParserProps = {
@@ -147,7 +143,10 @@ type VirtualMachineParserProps = {
147
143
type ParserHelperProps = {
148
144
vmName : string ;
149
145
vmNamespace : string ;
150
- clusterName ?: string ;
146
+ } ;
147
+
148
+ type DiscoveredHelperProps = {
149
+ application : DRPlacementControlKind | undefined ;
151
150
} ;
152
151
153
152
export default VirtualMachineParser ;
0 commit comments