Skip to content

Commit

Permalink
d2-AI prompt:
Browse files Browse the repository at this point in the history
{
  "prompt": "开发 K8s 中 NetworkPolicy 资源的 UI,它在菜单中的名字为“Network Policies”,父级菜单是网络。\n在列表中,增加一列展示 ingress 和 egress rules 的总量,以及一列用于展示出 pod selector 规则。\n\r\n\r\n\r\n在详情区域,增加对 ingress rules 和 egress rules 展示,两个 field 渲染器包括 rule type、CIDR、 Exceptions 等细节。",
  "images": [
    "https://github.com/webzard-io/dovetail-v2/assets/13651389/d6b58a5c-4484-4f13-9977-a88d45d27bc8"
  ]
}
  • Loading branch information
Yuyz0112 committed Dec 14, 2023
1 parent 5268a8d commit f57358a
Show file tree
Hide file tree
Showing 4 changed files with 89 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
18 changes: 18 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,21 @@ export const ServicePodsField = (_: i18n): ShowField<ResourceModel> => {
},
};
};
import { List, Select, Input, Alert } from 'antd';

const RuleDetails: React.FC<{ data: any }> = ({ data }) => {
// This component should be implemented based on the provided JSX example
// and should use antd components instead of tailwindCSS.
return <div>{/* Implement the JSX structure with antd components here */}</div>;
};

export const IngressEgressRulesField = (i18n: i18n): ShowField<ResourceModel> => {
return {
key: 'ingressEgressRules',
title: i18n.t('Ingress/Egress Rules'),
path: ['rawYaml', 'spec', 'ingress'], // or 'spec', 'egress' based on the context
render: data => {
return <RuleDetails data={data} />;
},
};
};
32 changes: 32 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,35 @@ export const ServiceTypeColumnRenderer = <Model extends ResourceModel>(
sorter: CommonSorter(dataIndex),
};
};
import { NetworkPolicy } from 'kubernetes-types/networking/v1';

const RuleCount: React.FC<{ policy: NetworkPolicy }> = ({ policy }) => {
const ingressCount = get(policy, 'spec.ingress.length', 0);
const egressCount = get(policy, 'spec.egress.length', 0);
return <span>{ingressCount + egressCount} rules</span>;
};

const PodSelector: React.FC<{ policy: NetworkPolicy }> = ({ policy }) => {
const selector = get(policy, 'spec.podSelector.matchLabels', {});
return <span>{JSON.stringify(selector)}</span>;
};

export const RuleCountColumnRenderer = (i18n: i18n): Column<ResourceModel> => {
return {
key: 'ruleCount',
display: true,
dataIndex: ['rawYaml'],
title: i18n.t('rule_count'),
render: value => <RuleCount policy={value} />,
};
};

export const PodSelectorColumnRenderer = (i18n: i18n): Column<ResourceModel> => {
return {
key: 'podSelector',
display: true,
dataIndex: ['rawYaml'],
title: i18n.t('pod_selector'),
render: value => <PodSelector policy={value} />,
};
};
37 changes: 37 additions & 0 deletions packages/refine/src/pages/networkpolicies/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { RuleCountColumnRenderer } from '../../hooks/useEagleTable/columns';
import { PodSelectorColumnRenderer } from '../../hooks/useEagleTable/columns';
import { IngressEgressRulesField } 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) => [
RuleCountColumnRenderer(i18n),
PodSelectorColumnRenderer(i18n),
],
showFields: (i18n: i18n) => [[], [], [IngressEgressRulesField(i18n)]],
initValue: NETWORKPOLICY_INIT_VALUE,
};

0 comments on commit f57358a

Please sign in to comment.