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 和 egress 的展示。\r\n\r\n在 ingress 详情中,每个 ingress 用一个 tab 展示。渲染每个 ingress 时,根据 ingress.from 中各条规则的类型分别渲染:\r\n- IP block 类型,展示 CIDR 和 exceptions 字段;\r\n- Namespace Selector 类型,展示 key、operator、value,并统计选中了多少个 namespace;\r\n- Pod selector 类型,展示 key、operator、value,并统计选中了多少个 pod。\r\n将 ingress.ports 展示出来,包含 port 和 protocol 信息。\r\n\r\n在 egress 详情中,每个 egress 用一个 tab 展示。渲染每个 egress 时,根据 egress.to 中各条规则的类型分别渲染:\r\n- IP block 类型,展示 CIDR 和 exceptions 字段;\r\n将 egress.ports 展示出来,包含 port 和 protocol 信息。\r\n\r\nnetwork policy spec 部分的数据示例如下:\r\n```yaml\r\nspec:\r\n  podSelector:\r\n    matchLabels:\r\n      role: db\r\n  ingress:\r\n    - ports:\r\n        - protocol: TCP\r\n          port: 6379\r\n      from:\r\n        - ipBlock:\r\n            cidr: 172.17.0.0/16\r\n            except:\r\n              - 172.17.1.0/24\r\n        - namespaceSelector:\r\n            matchLabels:\r\n              project: myproject\r\n        - podSelector:\r\n            matchLabels:\r\n              role: frontend\r\n  egress:\r\n    - ports:\r\n        - protocol: TCP\r\n          port: 5978\r\n      to:\r\n        - ipBlock:\r\n            cidr: 10.0.0.0/24\r\n  policyTypes:\r\n    - Ingress\r\n    - Egress\r\n```\n> \r\n> 在详情区域,增加对 ingress 和 egress 的展示。\r\n> \r\n> 在 ingress 详情中,每个 ingress 用一个 tab 展示。渲染每个 ingress 时,根据 ingress.from 中各条规则的类型分别渲染:\r\n> \r\n> * IP block 类型,展示 CIDR 和 exceptions 字段;\r\n> * Namespace Selector 类型,展示 key、operator、value,并统计选中了多少个 namespace;\r\n> * Pod selector 类型,展示 key、operator、value,并统计选中了多少个 pod。\r\n>   最后再将 ingress.ports 展示出来,包含 port 和 protocol 信息。\r\n> \r\n> 详情中 ingress.from 的数据示例如下:\r\n> \r\n> ```yaml\r\n> from:\r\n>   - ipBlock:\r\n>       cidr: 172.17.0.0/16\r\n>       except:\r\n>         - 172.17.1.0/24\r\n>   - namespaceSelector:\r\n>       matchLabels:\r\n>         project: myproject\r\n>   - podSelector:\r\n>       matchLabels:\r\n>         role: frontend\r\n> ```\r\n> \r\n> 在 egress 详情中,每个 egress 用一个 tab 展示。渲染每个 egress 时,根据 egress.to 中各条规则的类型分别渲染:\r\n> \r\n> * IP block 类型,展示 CIDR 和 exceptions 字段;\r\n>   将 egress.ports 展示出来,包含 port 和 protocol 信息。\r\n> \r\n> 详情中 egress.to 的数据示例如下:\r\n> \r\n> ```yaml\r\n>       to:\r\n>         - ipBlock:\r\n>             cidr: 10.0.0.0/24\r\n> ```\r\n\r\n这个是给 AI 的指令,还是关于PR的描述?",
  "images": [
    "https://github.com/webzard-io/dovetail-v2/assets/13651389/a4976e91-fd81-4560-8fc9-fa7695b25a92",
    "https://private-user-images.githubusercontent.com/13651389/290609115-a4976e91-fd81-4560-8fc9-fa7695b25a92.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTEiLCJleHAiOjE3MDI2MDgzODEsIm5iZiI6MTcwMjYwODA4MSwicGF0aCI6Ii8xMzY1MTM4OS8yOTA2MDkxMTUtYTQ5NzZlOTEtZmQ4MS00NTYwLThmYzktZmE3Njk1YjI1YTkyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFJV05KWUFYNENTVkVINTNBJTJGMjAyMzEyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjMxMjE1VDAyNDEyMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTE2ODQ1Y2Q0MDg0OTAwYzUwYTE1NTUxNzI1YzEzMDM3Njg1ZjI3ZGZkNDE5MmUwZjY5Mjc3OTVlNmRkMzI3MjYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.mft9n4H2B91YJQGrhcKaVA-QKv_LBcuJHnrwuSR1B2c"
  ]
}
  • Loading branch information
Yuyz0112 committed Dec 15, 2023
1 parent db50e60 commit 7ea6b2f
Show file tree
Hide file tree
Showing 4 changed files with 171 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
89 changes: 89 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,92 @@ export const ServicePodsField = (_: i18n): ShowField<ResourceModel> => {
},
};
};
import { List, Tabs, Tag } from 'antd';
import { get } from 'lodash';

const { TabPane } = Tabs;

// Helper component to display key, operator, and value
const SelectorList: React.FC<{
data?: {
matchLabels?: Record<string, string>;
matchExpressions?: Array<{ key: string; operator: string; values: Array<string> }>;
};
}> = ({ data }) => {
// Process matchLabels into an array of label strings
const labels = get(data, 'matchLabels', {})
? Object.entries(data.matchLabels).map(([key, value]) => `${key}=${value}`)
: [];
// Process matchExpressions into an array of expression strings
const expressions = get(data, 'matchExpressions', []).map(
expr => `${expr.key} ${expr.operator} ${expr.values.join(', ')}`
);
// Combine and map to ListItem
const items = [...labels, ...expressions];
return (
<List
bordered
dataSource={items}
renderItem={item => <List.Item>{item}</List.Item>}
/>
);
};

// Create a field renderer for Ingress
export const IngressField = (i18n: i18n): ShowField<ResourceModel> => ({
key: 'ingress',
title: i18n.t('Ingress'),
path: ['rawYaml', 'spec', 'ingress'],
render: ingressRules => {
if (!ingressRules) return null;

return (
<Tabs defaultActiveKey="1" onChange={() => {}}>
{ingressRules.map((rule, index) => (
<TabPane tab={`${i18n.t('Ingress')} ${index + 1}`} key={String(index)}>
<h4>{i18n.t('Source')}</h4>
<p>CIDR: {get(rule, 'from[0].ipBlock.cidr')}</p>
<p>Exceptions: {get(rule, 'from[0].ipBlock.except', []).join(', ')}</p>
// TODO: NamespaceSelector and PodSelector // TODO: Implement table or list
with labels
<h4>{i18n.t('Ports')}</h4>
{get(rule, 'ports', []).map((port, portIndex) => (
<p key={portIndex}>
{i18n.t('Port')}: {port.port}, {i18n.t('Protocol')}: {port.protocol}
</p>
))}
</TabPane>
))}
</Tabs>
);
},
});

// Create a field renderer for Egress
export const EgressField = (i18n: i18n): ShowField<ResourceModel> => ({
key: 'egress',
title: i18n.t('Egress'),
path: ['rawYaml', 'spec', 'egress'],
render: egressRules => {
if (!egressRules) return null;

return (
<Tabs defaultActiveKey="1" onChange={() => {}}>
{egressRules.map((rule, index) => (
<TabPane tab={`${i18n.t('Egress')} ${index + 1}`} key={String(index)}>
<h4>{i18n.t('Destination')}</h4>
<p>CIDR: {get(rule, 'to[0].ipBlock.cidr')}</p>
<p>Exceptions: {get(rule, 'to[0].ipBlock.except', []).join(', ')}</p>
// TODO: Implement table or list with labels
<h4>{i18n.t('Ports')}</h4>
{get(rule, 'ports', []).map((port, portIndex) => (
<p key={portIndex}>
{i18n.t('Port')}: {port.port}, {i18n.t('Protocol')}: {port.protocol}
</p>
))}
</TabPane>
))}
</Tabs>
);
},
});
34 changes: 34 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,37 @@ export const ServiceTypeColumnRenderer = <Model extends ResourceModel>(
sorter: CommonSorter(dataIndex),
};
};
import { NetworkPolicy } from 'kubernetes-types/networking/v1';

export const RulesCountColumnRenderer = (i18n: i18n): Column<NetworkPolicy> => {
return {
key: 'rulesCount',
display: true,
dataIndex: ['rawYaml'],
title: i18n.t('rules_count'),
sortable: true,
render: (value: NetworkPolicy) => {
const ingressRules = get(value, 'spec.ingress.length', 0);
const egressRules = get(value, 'spec.egress.length', 0);
const totalRules = ingressRules + egressRules;
return <span>{totalRules}</span>;
},
};
};

export const PodSelectorColumnRenderer = (i18n: i18n): Column<NetworkPolicy> => {
return {
key: 'podSelector',
display: true,
dataIndex: ['rawYaml'],
title: i18n.t('pod_selector'),
sortable: true,
render: (value: NetworkPolicy) => {
const podSelector = get(value, 'spec.podSelector.matchLabels', {});
const podSelectorRules = Object.entries(podSelector)
.map(([key, val]) => `${key}=${val}`)
.join(', ');
return <span>{podSelectorRules}</span>;
},
};
};
46 changes: 46 additions & 0 deletions packages/refine/src/pages/networkpolicies/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { RulesCountColumnRenderer } from '../../hooks/useEagleTable/columns';
import { PodSelectorColumnRenderer } from '../../hooks/useEagleTable/columns';
import { IngressField } from '../../components/ShowContent/fields';
import { EgressField } 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';

// a example k8s yaml for the network policy resource
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) => [
RulesCountColumnRenderer(i18n),
PodSelectorColumnRenderer(i18n),
],
showFields: (i18n: i18n) => [
// define the fields to show in the first line area as the first array
[],
// define the fields to show in the second line area as the second array
[],
// define the fields to show in the tabs area as the third array
[IngressField(i18n), EgressField(i18n)],
],
initValue: NETWORKPOLICY_INIT_VALUE,
};

0 comments on commit 7ea6b2f

Please sign in to comment.