|
1 | | -import { NamespaceBarProps, ResourceIconProps, ResourceLinkProps } from '@openshift-console/dynamic-plugin-sdk'; |
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { |
| 3 | + K8sGroupVersionKind, |
| 4 | + K8sModel, |
| 5 | + K8sResourceKind, |
| 6 | + K8sResourceKindReference, |
| 7 | + NamespaceBarProps, |
| 8 | + ResourceIconProps, |
| 9 | + ResourceLinkProps, |
| 10 | + ResourceYAMLEditorProps |
| 11 | +} from '@openshift-console/dynamic-plugin-sdk'; |
| 12 | +import { CodeEditor, Language } from '@patternfly/react-code-editor'; |
| 13 | +import _ from 'lodash'; |
2 | 14 | import * as React from 'react'; |
| 15 | +import { GetFlowCollectorJS } from '../src/components/forms/config/templates'; |
3 | 16 | import { useK8sModelsWithColors } from '../src/utils/k8s-models-hook'; |
| 17 | +import { useTheme } from '../src/utils/theme-hook'; |
| 18 | +import { safeJSToYAML } from '../src/utils/yaml'; |
4 | 19 | import { k8sModels } from './k8s-models'; |
5 | 20 |
|
6 | 21 | // This dummy file is used to resolve @Console imports from @openshift-console for JEST / Standalone |
@@ -35,6 +50,139 @@ export function useK8sModels() { |
35 | 50 | ] |
36 | 51 | } |
37 | 52 |
|
| 53 | +export function getK8sModel(k8s: any, k8sGroupVersionKind?: K8sResourceKindReference | K8sGroupVersionKind): K8sModel { |
| 54 | + const models = Object.keys(k8sModels); |
| 55 | + |
| 56 | + for (let i = 0; i < models.length; i++) { |
| 57 | + const model = (k8sModels as any)[models[i]]; |
| 58 | + if (model.kind === k8s.kind) { |
| 59 | + return model; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return { |
| 64 | + abbr: '', |
| 65 | + kind: '', |
| 66 | + label: '', |
| 67 | + labelPlural: '', |
| 68 | + plural: '', |
| 69 | + apiVersion: '' |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +export function k8sGet(k8s: any): Promise<any> { |
| 74 | + console.log("k8sGet", k8s); |
| 75 | + return Promise.resolve(k8s); |
| 76 | +} |
| 77 | + |
| 78 | +export function k8sCreate(k8s: any): Promise<any> { |
| 79 | + console.log("k8sCreate", k8s); |
| 80 | + return Promise.resolve(k8s); |
| 81 | +} |
| 82 | + |
| 83 | +export function k8sUpdate(k8s: any): Promise<any> { |
| 84 | + console.log("k8sUpdate", k8s); |
| 85 | + return Promise.resolve(k8s); |
| 86 | +} |
| 87 | + |
| 88 | +export function useK8sWatchResource(req: any) { |
| 89 | + console.log("useK8sWatchResource", req); |
| 90 | + |
| 91 | + const [loaded, setLoaded] = React.useState(false); |
| 92 | + const [resource, setResource] = React.useState<K8sResourceKind | null>(null); |
| 93 | + |
| 94 | + React.useEffect(() => { |
| 95 | + // simulate a loading |
| 96 | + if (resource == null) { |
| 97 | + setTimeout(() => { |
| 98 | + switch (req.groupVersionKind.kind) { |
| 99 | + case 'FlowCollector': |
| 100 | + const fc = _.cloneDeep(GetFlowCollectorJS()); |
| 101 | + fc.spec!.loki.enable = false; |
| 102 | + fc.status = { |
| 103 | + "conditions": [ |
| 104 | + { |
| 105 | + "lastTransitionTime": "2025-04-08T09:01:44Z", |
| 106 | + "message": "4 ready components, 0 with failure, 1 pending", |
| 107 | + "reason": "Pending", |
| 108 | + "status": "False", |
| 109 | + "type": "Ready" |
| 110 | + }, |
| 111 | + { |
| 112 | + "lastTransitionTime": "2025-04-08T09:01:44Z", |
| 113 | + "message": "Deployment netobserv-plugin not ready: 1/1 (Deployment does not have minimum availability.)", |
| 114 | + "reason": "DeploymentNotReady", |
| 115 | + "status": "True", |
| 116 | + "type": "WaitingFlowCollectorLegacy" |
| 117 | + }, |
| 118 | + { |
| 119 | + "lastTransitionTime": "2025-04-08T09:01:44Z", |
| 120 | + "message": "", |
| 121 | + "reason": "Ready", |
| 122 | + "status": "False", |
| 123 | + "type": "WaitingMonitoring" |
| 124 | + }, |
| 125 | + { |
| 126 | + "lastTransitionTime": "2025-04-08T09:01:43Z", |
| 127 | + "message": "", |
| 128 | + "reason": "Ready", |
| 129 | + "status": "False", |
| 130 | + "type": "WaitingNetworkPolicy" |
| 131 | + }, |
| 132 | + { |
| 133 | + "lastTransitionTime": "2025-04-08T09:01:43Z", |
| 134 | + "message": "", |
| 135 | + "reason": "Valid", |
| 136 | + "status": "False", |
| 137 | + "type": "ConfigurationIssue" |
| 138 | + }, |
| 139 | + { |
| 140 | + "lastTransitionTime": "2025-04-08T09:01:43Z", |
| 141 | + "message": "Loki is not configured in LokiStack mode", |
| 142 | + "reason": "Unused", |
| 143 | + "status": "Unknown", |
| 144 | + "type": "LokiIssue" |
| 145 | + }, |
| 146 | + { |
| 147 | + "lastTransitionTime": "2025-04-08T09:01:45Z", |
| 148 | + "message": "", |
| 149 | + "reason": "Ready", |
| 150 | + "status": "False", |
| 151 | + "type": "WaitingFLPParent" |
| 152 | + }, |
| 153 | + { |
| 154 | + "lastTransitionTime": "2025-04-08T09:01:45Z", |
| 155 | + "message": "", |
| 156 | + "reason": "Ready", |
| 157 | + "status": "False", |
| 158 | + "type": "WaitingFLPMonolith" |
| 159 | + }, |
| 160 | + { |
| 161 | + "lastTransitionTime": "2025-04-08T09:01:44Z", |
| 162 | + "message": "Transformer only used with Kafka", |
| 163 | + "reason": "ComponentUnused", |
| 164 | + "status": "Unknown", |
| 165 | + "type": "WaitingFLPTransformer" |
| 166 | + } |
| 167 | + ] |
| 168 | + } |
| 169 | + setResource(fc); |
| 170 | + break; |
| 171 | + } |
| 172 | + setLoaded(true); |
| 173 | + }, 1000); |
| 174 | + } |
| 175 | + }, [req.groupVersionKind.kind, req.kind, resource]); |
| 176 | + |
| 177 | + return React.useMemo(() => { |
| 178 | + if (!resource) { |
| 179 | + return [null, loaded, null]; |
| 180 | + } else { |
| 181 | + return [[resource], loaded, null]; |
| 182 | + } |
| 183 | + }, [loaded, resource]); |
| 184 | +} |
| 185 | + |
38 | 186 | export const ResourceIcon: React.FC<ResourceIconProps> = ({ |
39 | 187 | className, |
40 | 188 | kind, |
@@ -89,3 +237,26 @@ export const NamespaceBar: React.FC<NamespaceBarProps> = ({ |
89 | 237 | <div>{children}</div> |
90 | 238 | ) |
91 | 239 | }; |
| 240 | + |
| 241 | +export const ResourceYAMLEditor: React.FC<ResourceYAMLEditorProps> = ({ |
| 242 | + initialResource, |
| 243 | + header, |
| 244 | + onSave, |
| 245 | +}) => { |
| 246 | + const isDarkTheme = useTheme(); |
| 247 | + const containerHeight = document.getElementById("editor-content-container")?.clientHeight || 800; |
| 248 | + const footerHeight = document.getElementById("editor-toggle-footer")?.clientHeight || 0; |
| 249 | + return (<> |
| 250 | + <CodeEditor |
| 251 | + isDarkTheme={isDarkTheme} |
| 252 | + isLineNumbersVisible={true} |
| 253 | + isReadOnly={false} |
| 254 | + isMinimapVisible={true} |
| 255 | + isLanguageLabelVisible |
| 256 | + code={safeJSToYAML(initialResource)} |
| 257 | + language={Language.yaml} |
| 258 | + height={`${containerHeight - footerHeight}px`} |
| 259 | + onChange={(value) => onSave && onSave(value)} |
| 260 | + /> |
| 261 | + </>); |
| 262 | +}; |
0 commit comments