Skip to content

Commit

Permalink
centralise version refs
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Madigan <[email protected]>
  • Loading branch information
jasonmadigan committed Oct 17, 2024
1 parent fdec7f4 commit 66cfa3b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 40 deletions.
5 changes: 3 additions & 2 deletions src/components/KuadrantAuthPolicyCreatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions src/components/KuadrantDNSPolicyCreatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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,
Expand Down Expand Up @@ -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();

Expand Down
13 changes: 7 additions & 6 deletions src/components/KuadrantOverviewPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -226,10 +227,10 @@ const KuadrantOverviewPage: React.FC = () => {
<CardBody className="pf-u-p-0">
<ResourceList
resources={[
{ group: 'kuadrant.io', version: 'v1beta2', kind: 'AuthPolicy' },
{ group: 'kuadrant.io', version: 'v1alpha1', kind: 'DNSPolicy' },
{ group: 'kuadrant.io', version: 'v1beta2', kind: 'RateLimitPolicy' },
{ group: 'kuadrant.io', version: 'v1alpha1', kind: 'TLSPolicy' }
resourceGVKMapping['AuthPolicy'],
resourceGVKMapping['DNSPolicy'],
resourceGVKMapping['RateLimitPolicy'],
resourceGVKMapping['TLSPolicy']
]}
columns={columns}
namespace='#ALL_NS#'
Expand All @@ -247,7 +248,7 @@ const KuadrantOverviewPage: React.FC = () => {
<CardBody className="pf-u-p-0">
<ResourceList
resources={[
{ group: 'gateway.networking.k8s.io', version: 'v1', kind: 'Gateway' },
resourceGVKMapping['Gateway']
]}
columns={columns}
namespace='#ALL_NS#'
Expand All @@ -261,7 +262,7 @@ const KuadrantOverviewPage: React.FC = () => {
<CardBody className="pf-u-p-0">
<ResourceList
resources={[
{ group: 'gateway.networking.k8s.io', version: 'v1', kind: 'HTTPRoute' },
resourceGVKMapping['HTTPRoute']
]}
columns={columns}
namespace='#ALL_NS#'
Expand Down
9 changes: 5 additions & 4 deletions src/components/KuadrantPoliciesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Title } from '@patternfly/react-core';
import { Alert, AlertGroup } from '@patternfly/react-core';
import ResourceList from './ResourceList';
import './kuadrant.css';
import resourceGVKMapping from '../utils/latest';

interface Resource {
name: string;
Expand All @@ -28,10 +29,10 @@ interface Resource {
}

export const resources: Resource[] = [
{ name: 'AuthPolicies', gvk: { group: 'kuadrant.io', version: 'v1beta2', kind: 'AuthPolicy' } },
{ name: 'DNSPolicies', gvk: { group: 'kuadrant.io', version: 'v1alpha1', kind: 'DNSPolicy' } },
{ name: 'RateLimitPolicies', gvk: { group: 'kuadrant.io', version: 'v1beta2', kind: 'RateLimitPolicy' } },
{ name: 'TLSPolicies', gvk: { group: 'kuadrant.io', version: 'v1alpha1', kind: 'TLSPolicy' } },
{ name: 'AuthPolicies', gvk: resourceGVKMapping['AuthPolicy'] },
{ name: 'DNSPolicies', gvk: resourceGVKMapping['DNSPolicy'] },
{ name: 'RateLimitPolicies', gvk: resourceGVKMapping['RateLimitPolicy'] },
{ name: 'TLSPolicies', gvk: resourceGVKMapping['TLSPolicy'] },
];

export const AllPoliciesListPage: React.FC<{
Expand Down
5 changes: 3 additions & 2 deletions src/components/KuadrantRateLimitPolicyCreatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { useTranslation } from 'react-i18next';
import './kuadrant.css';
import { ResourceYAMLEditor, useActiveNamespace } from '@openshift-console/dynamic-plugin-sdk';
import resourceGVKMapping from '../utils/latest';

const KuadrantRateLimitPolicyCreatePage: React.FC = () => {
const { t } = useTranslation('plugin__kuadrant-console-plugin');
Expand All @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions src/components/KuadrantTLSCreatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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,
Expand All @@ -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
Expand Down
30 changes: 11 additions & 19 deletions src/components/PolicyTopologyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cluster-host>/api/plugins/kuadrant-console/config.js
Expand Down Expand Up @@ -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 {
Expand All @@ -113,11 +101,16 @@ const parseDotToModel = (dotString: string): { nodes: any[]; edges: any[] } => {
const connectedNodeIds = new Set<string>();

// Excluded resource kinds
const excludedKinds = ['Issuer', 'ClusterIssuer'];
const excludedKinds = ['Issuer', 'ClusterIssuer', 'WasmPlugin'];

// Define separate groups
const unassociatedPolicies = new Set<string>(['TLSPolicy', 'DNSPolicy', 'AuthPolicy', 'RateLimitPolicy']);
const kuadrantInternals = new Set<string>(['ConfigMap']);
const unassociatedPolicies = new Set<string>([
'TLSPolicy',
'DNSPolicy',
'AuthPolicy',
'RateLimitPolicy',
]);
const kuadrantInternals = new Set<string>(['ConfigMap', 'WasmPlugin']);

graph.edges().forEach((edge) => {
const sourceNode = graph.node(edge.v);
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions src/utils/latest.tsx
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 66cfa3b

Please sign in to comment.