Skip to content

Commit

Permalink
Your commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuyz0112 committed Dec 14, 2023
1 parent 5268a8d commit 0f1e28d
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/refine/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Route, Router } from 'react-router-dom';
import { Layout } from './components';
import { Dovetail } from './Dovetail';
import { ConfigMapConfig } from './pages/configmaps';
import { NetworkPolicyConfig } from './pages/networkpolicies';
import { CronJobForm, CronJobList, CronJobShow } from './pages/cronjobs';
import { DaemonSetForm, DaemonSetList, DaemonSetShow } from './pages/daemonsets';
import { DeploymentForm, DeploymentList, DeploymentShow } from './pages/deployments';
Expand Down Expand Up @@ -66,6 +67,7 @@ function App() {
ConfigMapConfig,
SecretsConfig,
ServicesConfig,
NetworkPolicyConfig,
];
}, []);

Expand Down
40 changes: 40 additions & 0 deletions packages/refine/src/components/ShowContent/fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,43 @@ export const ServicePodsField = (_: i18n): ShowField<ResourceModel> => {
},
};
};
import { List } from 'antd';
import _ from 'lodash';

const RuleList: React.FC<{ rules: any[] }> = ({ rules }) => {
return (
<List
bordered
dataSource={rules}
renderItem={(rule, index) => (
<List.Item>
<div className="flex flex-col">
<span className="font-bold">Rule {index + 1}</span>
<span>Type: {rule.type}</span>
<span>CIDR: {rule.cidr}</span>
<span>Exceptions: {rule.exceptions.join(', ')}</span>
{/* Add other details here */}
</div>
</List.Item>
)}
/>
);
};

export const IngressRulesField = (i18n: i18n): ShowField<ResourceModel> => {
return {
key: 'ingressRules',
title: i18n.t('Ingress Rules'),
path: ['rawYaml', 'spec', 'ingress'],
render: rules => <RuleList rules={_.get(rules, 'ingress', [])} />,
};
};

export const EgressRulesField = (i18n: i18n): ShowField<ResourceModel> => {
return {
key: 'egressRules',
title: i18n.t('Egress Rules'),
path: ['rawYaml', 'spec', 'egress'],
render: rules => <RuleList rules={_.get(rules, 'egress', [])} />,
};
};
47 changes: 47 additions & 0 deletions packages/refine/src/hooks/useEagleTable/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,50 @@ export const ServiceTypeColumnRenderer = <Model extends ResourceModel>(
sorter: CommonSorter(dataIndex),
};
};
import _ from 'lodash';

export const NetworkPolicyRulesCountColumnRenderer = (
i18n: i18n
): Column<ResourceModel> => {
return {
key: 'rulesCount',
display: true,
dataIndex: ['rawYaml'],
title: i18n.t('rules_count'),
sortable: true,
sorter: (a, b) => {
const ingressA = _.get(a, 'spec.ingress.length', 0);
const egressA = _.get(a, 'spec.egress.length', 0);
const ingressB = _.get(b, 'spec.ingress.length', 0);
const egressB = _.get(b, 'spec.egress.length', 0);
return ingressA + egressA - (ingressB + egressB);
},
render: value => {
const ingress = _.get(value, 'spec.ingress.length', 0);
const egress = _.get(value, 'spec.egress.length', 0);
return <span>{ingress + egress}</span>;
},
};
};

export const PodSelectorColumnRenderer = (i18n: i18n): Column<ResourceModel> => {
return {
key: 'podSelector',
display: true,
dataIndex: ['rawYaml'],
title: i18n.t('pod_selector'),
sortable: false,
render: value => {
const podSelector = _.get(value, 'spec.podSelector.matchLabels', {});
return (
<div className="flex flex-col">
{Object.entries(podSelector).map(([key, val]) => (
<span key={key} className="truncate">
{key}: {val}
</span>
))}
</div>
);
},
};
};
38 changes: 38 additions & 0 deletions packages/refine/src/pages/networkpolicies/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { NetworkPolicyRulesCountColumnRenderer } from '../../hooks/useEagleTable/columns';
import { PodSelectorColumnRenderer } from '../../hooks/useEagleTable/columns';
import { IngressRulesField } from '../../components/ShowContent/fields';
import { EgressRulesField } from '../../components/ShowContent/fields';
import { i18n } from 'i18next';
import { RESOURCE_GROUP, ResourceConfig, WithId } from '../../types';
import { ResourceModel } from '../../model';
import { NetworkPolicy } from 'kubernetes-types/networking/v1';

const NETWORKPOLICY_INIT_VALUE = {
apiVersion: 'networking.k8s.io/v1',
kind: 'NetworkPolicy',
metadata: {
name: '',
namespace: 'default',
annotations: {},
labels: {},
},
spec: {
podSelector: {},
policyTypes: [],
},
};

export const NetworkPolicyConfig: ResourceConfig<WithId<NetworkPolicy>, ResourceModel> = {
name: 'networkpolicies',
kind: 'NetworkPolicy',
basePath: '/apis/networking.k8s.io/v1',
apiVersion: 'v1',
parent: RESOURCE_GROUP.NETWORK,
label: 'Network Policies',
columns: (i18n: i18n) => [
NetworkPolicyRulesCountColumnRenderer(i18n),
PodSelectorColumnRenderer(i18n),
],
showFields: (i18n: i18n) => [[], [], [IngressRulesField(i18n), EgressRulesField(i18n)]],
initValue: NETWORKPOLICY_INIT_VALUE,
};

0 comments on commit 0f1e28d

Please sign in to comment.