From 66cfa3b2067efdcf004b429dff65a9adc2e52d6c Mon Sep 17 00:00:00 2001 From: Jason Madigan Date: Thu, 17 Oct 2024 16:12:58 +0100 Subject: [PATCH] centralise version refs Signed-off-by: Jason Madigan --- .../KuadrantAuthPolicyCreatePage.tsx | 5 ++-- .../KuadrantDNSPolicyCreatePage.tsx | 12 +++++--- src/components/KuadrantOverviewPage.tsx | 13 ++++---- src/components/KuadrantPoliciesPage.tsx | 9 +++--- .../KuadrantRateLimitPolicyCreatePage.tsx | 5 ++-- src/components/KuadrantTLSCreatePage.tsx | 10 +++++-- src/components/PolicyTopologyPage.tsx | 30 +++++++------------ src/utils/latest.tsx | 14 +++++++++ 8 files changed, 58 insertions(+), 40 deletions(-) create mode 100644 src/utils/latest.tsx diff --git a/src/components/KuadrantAuthPolicyCreatePage.tsx b/src/components/KuadrantAuthPolicyCreatePage.tsx index fb020bb..f587404 100644 --- a/src/components/KuadrantAuthPolicyCreatePage.tsx +++ b/src/components/KuadrantAuthPolicyCreatePage.tsx @@ -3,14 +3,15 @@ import Helmet from 'react-helmet'; import { Button, Modal, ModalBox, ModalBoxHeader, ModalBoxBody, ModalBoxFooter, ButtonVariant } from '@patternfly/react-core'; import { useTranslation } from 'react-i18next'; import { ResourceYAMLEditor, useActiveNamespace } from '@openshift-console/dynamic-plugin-sdk'; +import resourceGVKMapping from '../utils/latest'; const KuadrantAuthPolicyCreatePage: React.FC = () => { const { t } = useTranslation('plugin__kuadrant-console-plugin'); const [selectedNamespace] = useActiveNamespace(); const yamlResource = { - apiVersion: 'kuadrant.io/v1beta2', - kind: 'AuthPolicy', + apiVersion: resourceGVKMapping['AuthPolicy'].group + '/' + resourceGVKMapping['AuthPolicy'].version, + kind: resourceGVKMapping['AuthPolicy'].kind, metadata: { name: 'example-authpolicy', namespace: selectedNamespace, diff --git a/src/components/KuadrantDNSPolicyCreatePage.tsx b/src/components/KuadrantDNSPolicyCreatePage.tsx index bbaa05a..2676373 100644 --- a/src/components/KuadrantDNSPolicyCreatePage.tsx +++ b/src/components/KuadrantDNSPolicyCreatePage.tsx @@ -26,6 +26,7 @@ import GatewaySelect from './gateway/GatewaySelect'; import yaml from 'js-yaml'; import KuadrantCreateUpdate from './KuadrantCreateUpdate' import { handleCancel } from '../utils/cancel'; +import resourceGVKMapping from '../utils/latest'; const KuadrantDNSPolicyCreatePage: React.FC = () => { @@ -51,8 +52,8 @@ const KuadrantDNSPolicyCreatePage: React.FC = () => { const createDNSPolicy = () => { const hasHealthCheck = healthCheck.endpoint || healthCheck.failureThreshold || healthCheck.port || healthCheck.protocol; return { - apiVersion: 'kuadrant.io/v1alpha1', - kind: 'DNSPolicy', + apiVersion: resourceGVKMapping['DNSPolicy'].group + '/' + resourceGVKMapping['DNSPolicy'].version, + kind: resourceGVKMapping['DNSPolicy'].kind, metadata: { name: policyName, namespace: selectedNamespace, @@ -86,8 +87,11 @@ const KuadrantDNSPolicyCreatePage: React.FC = () => { const [yamlInput, setYamlInput] = React.useState(createDNSPolicy) const dnsPolicy = createDNSPolicy(); - const dnsPolicyGVK = getGroupVersionKindForResource({ apiVersion: 'kuadrant.io/v1alpha1', kind: 'DNSPolicy' }); - const [dnsPolicyModel] = useK8sModel({ group: dnsPolicyGVK.group, version: dnsPolicyGVK.version, kind: dnsPolicyGVK.kind }); + const dnsPolicyGVK = getGroupVersionKindForResource({ + apiVersion: `${resourceGVKMapping['DNSPolicy'].group}/${resourceGVKMapping['DNSPolicy'].version}`, + kind: resourceGVKMapping['DNSPolicy'].kind, + }); + const [dnsPolicyModel] = useK8sModel({ group: dnsPolicyGVK.group, version: dnsPolicyGVK.version, kind: dnsPolicyGVK.kind }); const history = useHistory(); diff --git a/src/components/KuadrantOverviewPage.tsx b/src/components/KuadrantOverviewPage.tsx index 9433c9a..59e2c9f 100644 --- a/src/components/KuadrantOverviewPage.tsx +++ b/src/components/KuadrantOverviewPage.tsx @@ -35,6 +35,7 @@ import './kuadrant.css'; import ResourceList from './ResourceList'; import { sortable } from '@patternfly/react-table'; import { INTERNAL_LINKS, EXTERNAL_LINKS } from '../constants/links'; +import resourceGVKMapping from '../utils/latest'; const KuadrantOverviewPage: React.FC = () => { const { t } = useTranslation('plugin__kuadrant-console-plugin'); @@ -226,10 +227,10 @@ const KuadrantOverviewPage: React.FC = () => { { { { const { t } = useTranslation('plugin__kuadrant-console-plugin'); @@ -17,8 +18,8 @@ const KuadrantRateLimitPolicyCreatePage: React.FC = () => { const [errorModalMsg] = React.useState(''); const rateLimitPolicy = { - apiVersion: 'kuadrant.io/v1beta2', - kind: 'RateLimitPolicy', + apiVersion: resourceGVKMapping['RateLimitPolicy'].group + '/' + resourceGVKMapping['RateLimitPolicy'].version, + kind: resourceGVKMapping['RateLimitPolicy'].kind, metadata: { name: 'example-ratelimitpolicy', namespace: selectedNamespace, diff --git a/src/components/KuadrantTLSCreatePage.tsx b/src/components/KuadrantTLSCreatePage.tsx index fb93ee9..c923af3 100644 --- a/src/components/KuadrantTLSCreatePage.tsx +++ b/src/components/KuadrantTLSCreatePage.tsx @@ -36,6 +36,7 @@ import { Gateway } from './gateway/types'; import GatewaySelect from './gateway/GatewaySelect'; import KuadrantCreateUpdate from './KuadrantCreateUpdate' import { K8sResourceCommon } from '@openshift-console/dynamic-plugin-sdk'; +import resourceGVKMapping from '../utils/latest'; const KuadrantTLSCreatePage: React.FC = () => { @@ -59,8 +60,8 @@ const KuadrantTLSCreatePage: React.FC = () => { // Creates TLS policy object to be used for form and yaml creation of the resource const createTlsPolicy = () => ({ - apiVersion: 'kuadrant.io/v1alpha1', - kind: 'TLSPolicy', + apiVersion: resourceGVKMapping['TLSPolicy'].group + '/' + resourceGVKMapping['TLSPolicy'].version, + kind: resourceGVKMapping['TLSPolicy'].kind, metadata: { name: policyName, namespace: selectedNamespace, @@ -87,7 +88,10 @@ const KuadrantTLSCreatePage: React.FC = () => { }) const tlsPolicy = createTlsPolicy(); - const tlsPolicyGVK = getGroupVersionKindForResource({ apiVersion: 'kuadrant.io/v1alpha1', kind: 'TLSPolicy' }); + const tlsPolicyGVK = getGroupVersionKindForResource({ + apiVersion: `${resourceGVKMapping['TLSPolicy'].group}/${resourceGVKMapping['TLSPolicy'].version}`, + kind: resourceGVKMapping['TLSPolicy'].kind, + }); const [tlsPolicyModel] = useK8sModel({ group: tlsPolicyGVK.group, version: tlsPolicyGVK.version, kind: tlsPolicyGVK.kind }); // K8sResourceCommon by default does not contain spec etc which is needed for updating resource forms diff --git a/src/components/PolicyTopologyPage.tsx b/src/components/PolicyTopologyPage.tsx index f6bc270..58b463c 100644 --- a/src/components/PolicyTopologyPage.tsx +++ b/src/components/PolicyTopologyPage.tsx @@ -39,6 +39,7 @@ import { import { CubesIcon, CloudUploadAltIcon, TopologyIcon, RouteIcon } from '@patternfly/react-icons'; import * as dot from 'graphlib-dot'; import './kuadrant.css'; +import resourceGVKMapping from '../utils/latest'; // Fetch the config.js file dynamically at runtime // Normally served from /api/plugins/kuadrant-console/config.js @@ -76,19 +77,6 @@ export const kindToAbbr = (kind: string) => { return (kind.replace(/[^A-Z]/g, '') || kind.toUpperCase()).slice(0, 4); }; -// TODO: need a generic way to fetch latest versions of resources based on kind + group -const resourceGVKMapping: { [key: string]: { group: string; version: string; kind: string } } = { - Gateway: { group: 'gateway.networking.k8s.io', version: 'v1', kind: 'Gateway' }, - HTTPRoute: { group: 'gateway.networking.k8s.io', version: 'v1', kind: 'HTTPRoute' }, - TLSPolicy: { group: 'kuadrant.io', version: 'v1alpha1', kind: 'TLSPolicy' }, - DNSPolicy: { group: 'kuadrant.io', version: 'v1alpha1', kind: 'DNSPolicy' }, - AuthPolicy: { group: 'kuadrant.io', version: 'v1beta2', kind: 'AuthPolicy' }, - RateLimitPolicy: { group: 'kuadrant.io', version: 'v1beta2', kind: 'RateLimitPolicy' }, - ConfigMap: { group: '', version: 'v1', kind: 'ConfigMap' }, - Listener: { group: 'gateway.networking.k8s.io', version: 'v1', kind: 'Listener' }, - GatewayClass: { group: 'gateway.networking.k8s.io', version: 'v1', kind: 'GatewayClass' }, -}; - // Convert DOT graph to PatternFly node/edge models const parseDotToModel = (dotString: string): { nodes: any[]; edges: any[] } => { try { @@ -113,11 +101,16 @@ const parseDotToModel = (dotString: string): { nodes: any[]; edges: any[] } => { const connectedNodeIds = new Set(); // Excluded resource kinds - const excludedKinds = ['Issuer', 'ClusterIssuer']; + const excludedKinds = ['Issuer', 'ClusterIssuer', 'WasmPlugin']; // Define separate groups - const unassociatedPolicies = new Set(['TLSPolicy', 'DNSPolicy', 'AuthPolicy', 'RateLimitPolicy']); - const kuadrantInternals = new Set(['ConfigMap']); + const unassociatedPolicies = new Set([ + 'TLSPolicy', + 'DNSPolicy', + 'AuthPolicy', + 'RateLimitPolicy', + ]); + const kuadrantInternals = new Set(['ConfigMap', 'WasmPlugin']); graph.edges().forEach((edge) => { const sourceNode = graph.node(edge.v); @@ -171,7 +164,7 @@ const parseDotToModel = (dotString: string): { nodes: any[]; edges: any[] } => { // Group unconnected resources const unassociatedPolicyNodes = nodes.filter( - (node) => !connectedNodeIds.has(node.id) && unassociatedPolicies.has(node.resourceType) + (node) => !connectedNodeIds.has(node.id) && unassociatedPolicies.has(node.resourceType), ); if (unassociatedPolicyNodes.length > 0) { groups.push({ @@ -188,7 +181,7 @@ const parseDotToModel = (dotString: string): { nodes: any[]; edges: any[] } => { // Group Kuadrant Internals (ConfigMap) const kuadrantInternalNodes = nodes.filter( - (node) => !connectedNodeIds.has(node.id) && kuadrantInternals.has(node.resourceType) + (node) => !connectedNodeIds.has(node.id) && kuadrantInternals.has(node.resourceType), ); if (kuadrantInternalNodes.length > 0) { groups.push({ @@ -424,7 +417,6 @@ const PolicyTopologyPage: React.FC = () => { React.useEffect(() => { if (loaded && !loadError && configMap) { const dotString = configMap.data?.topology || ''; - console.log('DOTfile: ', dotString); if (dotString) { try { const { nodes, edges } = parseDotToModel(dotString); diff --git a/src/utils/latest.tsx b/src/utils/latest.tsx new file mode 100644 index 0000000..b50a659 --- /dev/null +++ b/src/utils/latest.tsx @@ -0,0 +1,14 @@ +const resourceGVKMapping: { [key: string]: { group: string; version: string; kind: string } } = { + Gateway: { group: 'gateway.networking.k8s.io', version: 'v1', kind: 'Gateway' }, + HTTPRoute: { group: 'gateway.networking.k8s.io', version: 'v1', kind: 'HTTPRoute' }, + TLSPolicy: { group: 'kuadrant.io', version: 'v1alpha1', kind: 'TLSPolicy' }, + DNSPolicy: { group: 'kuadrant.io', version: 'v1alpha1', kind: 'DNSPolicy' }, + AuthPolicy: { group: 'kuadrant.io', version: 'v1beta2', kind: 'AuthPolicy' }, + RateLimitPolicy: { group: 'kuadrant.io', version: 'v1beta2', kind: 'RateLimitPolicy' }, + ConfigMap: { group: '', version: 'v1', kind: 'ConfigMap' }, + Listener: { group: 'gateway.networking.k8s.io', version: 'v1', kind: 'Listener' }, + GatewayClass: { group: 'gateway.networking.k8s.io', version: 'v1', kind: 'GatewayClass' }, + WasmPlugin: { group: 'extensions.istio.io', version: 'v1alpha1', kind: 'WasmPlugin' }, +}; + +export default resourceGVKMapping;