|
| 1 | +/* |
| 2 | + * Copyright The Cryostat Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import CryostatIcon from '@console-plugin/assets/CryostatIcon'; |
| 17 | +import { k8sGet, K8sResourceKind, useK8sModel, useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk'; |
| 18 | +import { Node } from '@patternfly/react-topology'; |
| 19 | +import * as React from 'react'; |
| 20 | + |
| 21 | +type DeploymentDecoratorProps = { |
| 22 | + element: Node; |
| 23 | + radius: number; |
| 24 | + x: number; |
| 25 | + y: number; |
| 26 | +}; |
| 27 | + |
| 28 | +export const DeploymentDecorator: React.FC<DeploymentDecoratorProps> = ({ element, radius, x, y }) => { |
| 29 | + const [routeModel] = useK8sModel({ group: 'route.openshift.io', version: 'v1', kind: 'Route' }); |
| 30 | + const routeUrl = React.useRef(''); |
| 31 | + const [isRegistered, setIsRegistered] = React.useState(false); |
| 32 | + const [deployment, deploymentLoaded] = useK8sWatchResource<K8sResourceKind>({ |
| 33 | + groupVersionKind: { |
| 34 | + group: 'apps', |
| 35 | + version: 'v1', |
| 36 | + kind: 'Deployment', |
| 37 | + }, |
| 38 | + name: element['resource']?.metadata?.name || undefined, |
| 39 | + namespace: element['resource']?.metadata?.namespace || undefined, |
| 40 | + }); |
| 41 | + const [cryostats, cryostatsLoaded] = useK8sWatchResource<K8sResourceKind[]>({ |
| 42 | + groupVersionKind: { |
| 43 | + group: 'operator.cryostat.io', |
| 44 | + version: 'v1beta2', |
| 45 | + kind: 'Cryostat', |
| 46 | + }, |
| 47 | + isList: true, |
| 48 | + }); |
| 49 | + |
| 50 | + React.useEffect(() => { |
| 51 | + if (deploymentLoaded && cryostatsLoaded) { |
| 52 | + const deploymentLabels = deployment.spec?.template.metadata.labels; |
| 53 | + if (deploymentLabels && deploymentLabels['cryostat.io/name'] && deploymentLabels['cryostat.io/namespace']) { |
| 54 | + setIsRegistered(true); |
| 55 | + } else { |
| 56 | + setIsRegistered(false); |
| 57 | + } |
| 58 | + } |
| 59 | + }, [cryostats, cryostatsLoaded, deployment, deploymentLoaded]); |
| 60 | + |
| 61 | + React.useEffect(() => { |
| 62 | + if (deploymentLoaded && cryostatsLoaded && isRegistered) { |
| 63 | + const labels = deployment.spec?.template.metadata.labels; |
| 64 | + if (labels && labels['cryostat.io/name'] && labels['cryostat.io/namespace']) { |
| 65 | + cryostats.forEach((cryostat) => { |
| 66 | + if ( |
| 67 | + cryostat.metadata?.name === labels['cryostat.io/name'] && |
| 68 | + cryostat.metadata?.namespace === labels['cryostat.io/namespace'] |
| 69 | + ) { |
| 70 | + k8sGet({ |
| 71 | + model: routeModel, |
| 72 | + name: labels['cryostat.io/name'], |
| 73 | + ns: labels['cryostat.io/namespace'], |
| 74 | + }) |
| 75 | + .catch(() => '') |
| 76 | + .then( |
| 77 | + (route: any) => { |
| 78 | + const ingresses = route?.status?.ingress; |
| 79 | + let res = ''; |
| 80 | + if (ingresses && ingresses?.length > 0 && ingresses[0]?.host) { |
| 81 | + res = `http://${ingresses[0].host}`; |
| 82 | + } |
| 83 | + routeUrl.current = res; |
| 84 | + }, |
| 85 | + () => { |
| 86 | + routeUrl.current = ''; |
| 87 | + }, |
| 88 | + ); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + }, [cryostats, cryostatsLoaded, deployment, deploymentLoaded, isRegistered, routeModel]); |
| 94 | + |
| 95 | + if (element['resourceKind'] === 'apps~v1~Deployment' && isRegistered) { |
| 96 | + return ( |
| 97 | + <a |
| 98 | + className="odc-decorator__link" |
| 99 | + href={`${routeUrl.current}/topology`} |
| 100 | + target="_blank" |
| 101 | + rel="noopener noreferrer" |
| 102 | + role="button" |
| 103 | + aria-label="Open Cryostat" |
| 104 | + > |
| 105 | + <g className="pf-topology__node__decorator odc-decorator cryostat-decorator"> |
| 106 | + <circle className="pf-topology__node__decorator__bg" cx={x} cy={y} r={radius}></circle> |
| 107 | + <g transform={`translate(${x}, ${y})`}> |
| 108 | + <g transform="translate(-6.5, -6.5)"> |
| 109 | + <CryostatIcon width={`${radius}px`} height={`${radius}px`}></CryostatIcon> |
| 110 | + </g> |
| 111 | + </g> |
| 112 | + </g> |
| 113 | + </a> |
| 114 | + ); |
| 115 | + } |
| 116 | + return <></>; |
| 117 | +}; |
| 118 | + |
| 119 | +export default DeploymentDecorator; |
0 commit comments