Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added chash #429

Merged
merged 5 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/pages/Route/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ const Page: React.FC<Props> = (props) => {
...form2.getFieldsValue(),
...data,
});
setStep2Data({ ...form2.getFieldsValue(), ...params } as RouteModule.Step2Data);
});
return;
}
setStep2Data({ ...form2.getFieldsValue(), ...params } as RouteModule.Step2Data);
}}
Expand Down
263 changes: 154 additions & 109 deletions src/pages/Route/components/Step2/RequestRewriteView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import { Input, Row, Col, InputNumber, Button, Select } from 'antd';
import { PlusOutlined, MinusCircleOutlined } from '@ant-design/icons';
import { useIntl } from 'umi';

import { FORM_ITEM_LAYOUT, FORM_ITEM_WITHOUT_LABEL } from '@/pages/Route/constants';
import {
FORM_ITEM_LAYOUT,
FORM_ITEM_WITHOUT_LABEL,
HASH_KEY_LIST,
HASH_ON_LIST,
} from '@/pages/Route/constants';
import PanelSection from '@/components/PanelSection';
import styles from '../../Create.less';
import { fetchUpstreamList } from '../../service';
Expand All @@ -48,117 +53,157 @@ const RequestRewriteView: React.FC<Props> = ({ data, form, disabled, onChange })
});
}, []);
const renderUpstreamMeta = () => (
<Form.List name="upstreamHostList">
{(fields, { add, remove }) => (
<>
<Form.Item label="类型" name="type" rules={[{ required: true }]}>
<Select disabled={upstreamDisabled} onChange={(params) => onChange({ type: params })}>
<Select.Option value="roundrobin">roundrobin</Select.Option>
<Select.Option value="chash">chash</Select.Option>
</Select>
</Form.Item>
{step2Data.type === 'chash' && (
<>
{fields.map((field, index) => (
<Form.Item
required
key={field.key}
{...(index === 0 ? FORM_ITEM_LAYOUT : FORM_ITEM_WITHOUT_LABEL)}
label={
index === 0 ? formatMessage({ id: 'route.request.override.domain.name.or.ip' }) : ''
}
extra={
index === 0
? formatMessage({ id: 'route.request.override.use.domain.name.default.analysis' })
: ''
}
>
<Row style={{ marginBottom: '10px' }} gutter={16}>
<Col span={9}>
<Form.Item
style={{ marginBottom: 0 }}
name={[field.name, 'host']}
rules={[
{
required: true,
message: formatMessage({ id: 'route.request.override.input.domain.or.ip' }),
},
{
pattern: new RegExp(
/(^([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])(\.(25[0-5]|1\d{2}|2[0-4]\d|[1-9]?\d)){3}$|^(?![0-9.]+$)([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+){0,}$)/,
'g',
),
message: formatMessage({ id: 'route.request.override.domain.or.ip.rules' }),
},
]}
>
<Input
placeholder={formatMessage({
id: 'route.request.override.domain.name.or.ip',
})}
disabled={upstreamDisabled}
/>
</Form.Item>
</Col>
<Col span={4}>
<Form.Item
style={{ marginBottom: 0 }}
name={[field.name, 'port']}
rules={[
{
required: true,
message: formatMessage({ id: 'route.request.override.input.port.number' }),
},
]}
>
<InputNumber
placeholder={formatMessage({ id: 'route.request.override.port.number' })}
disabled={upstreamDisabled}
min={1}
max={65535}
/>
</Form.Item>
</Col>
<Col span={4} offset={1}>
<Form.Item
style={{ marginBottom: 0 }}
name={[field.name, 'weight']}
rules={[
{
required: true,
message: formatMessage({ id: 'route.request.override.input.weight' }),
},
]}
>
<InputNumber
placeholder={formatMessage({ id: 'route.request.override.weight' })}
disabled={upstreamDisabled}
min={0}
max={1000}
/>
</Form.Item>
</Col>
<Col>
{!upstreamDisabled &&
(fields.length > 1 ? (
<MinusCircleOutlined
style={{ margin: '0 8px' }}
onClick={() => {
remove(field.name);
}}
/>
) : null)}
</Col>
</Row>
</Form.Item>
))}
{!upstreamDisabled && (
<Form.Item {...FORM_ITEM_WITHOUT_LABEL}>
<Button
type="dashed"
onClick={() => {
add();
}}
>
<PlusOutlined /> {formatMessage({ id: 'route.request.override.create' })}
</Button>
</Form.Item>
)}
<Form.Item label="Hash On" name="hash_on">
<Select disabled={upstreamDisabled}>
{HASH_ON_LIST.map((item) => (
<Select.Option value={item} key={item}>
{item}
</Select.Option>
))}
</Select>
</Form.Item>
<Form.Item label="Key" name="key">
<Select disabled={upstreamDisabled}>
{HASH_KEY_LIST.map((item) => (
<Select.Option value={item} key={item}>
{item}
</Select.Option>
))}
</Select>
</Form.Item>
</>
)}
</Form.List>
<Form.List name="upstreamHostList">
{(fields, { add, remove }) => (
<>
{fields.map((field, index) => (
<Form.Item
required
key={field.key}
{...(index === 0 ? FORM_ITEM_LAYOUT : FORM_ITEM_WITHOUT_LABEL)}
label={
index === 0
? formatMessage({ id: 'route.request.override.domain.name.or.ip' })
: ''
}
extra={
index === 0
? formatMessage({
id: 'route.request.override.use.domain.name.default.analysis',
})
: ''
}
>
<Row style={{ marginBottom: '10px' }} gutter={16}>
<Col span={9}>
<Form.Item
style={{ marginBottom: 0 }}
name={[field.name, 'host']}
rules={[
{
required: true,
message: formatMessage({
id: 'route.request.override.input.domain.or.ip',
}),
},
{
pattern: new RegExp(
/(^([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])(\.(25[0-5]|1\d{2}|2[0-4]\d|[1-9]?\d)){3}$|^(?![0-9.]+$)([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+){0,}$)/,
'g',
),
message: formatMessage({
id: 'route.request.override.domain.or.ip.rules',
}),
},
]}
>
<Input
placeholder={formatMessage({
id: 'route.request.override.domain.name.or.ip',
})}
disabled={upstreamDisabled}
/>
</Form.Item>
</Col>
<Col span={4}>
<Form.Item
style={{ marginBottom: 0 }}
name={[field.name, 'port']}
rules={[
{
required: true,
message: formatMessage({
id: 'route.request.override.input.port.number',
}),
},
]}
>
<InputNumber
placeholder={formatMessage({ id: 'route.request.override.port.number' })}
disabled={upstreamDisabled}
min={1}
max={65535}
/>
</Form.Item>
</Col>
<Col span={4} offset={1}>
<Form.Item
style={{ marginBottom: 0 }}
name={[field.name, 'weight']}
rules={[
{
required: true,
message: formatMessage({ id: 'route.request.override.input.weight' }),
},
]}
>
<InputNumber
placeholder={formatMessage({ id: 'route.request.override.weight' })}
disabled={upstreamDisabled}
min={0}
max={1000}
/>
</Form.Item>
</Col>
<Col>
{!upstreamDisabled &&
(fields.length > 1 ? (
<MinusCircleOutlined
style={{ margin: '0 8px' }}
onClick={() => {
remove(field.name);
}}
/>
) : null)}
</Col>
</Row>
</Form.Item>
))}
{!upstreamDisabled && (
<Form.Item {...FORM_ITEM_WITHOUT_LABEL}>
<Button
type="dashed"
onClick={() => {
add();
}}
>
<PlusOutlined /> {formatMessage({ id: 'route.request.override.create' })}
</Button>
</Form.Item>
)}
</>
)}
</Form.List>
</>
);

const renderTimeUnit = () => <span style={{ margin: '0 8px' }}>ms</span>;
Expand Down
16 changes: 16 additions & 0 deletions src/pages/Route/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const DEFAULT_STEP_2_DATA: RouteModule.Step2Data = {
upstreamHostList: [{} as RouteModule.UpstreamHost],
upstreamHeaderList: [],
upstreamPath: undefined,
type: 'roundrobin',
mappingStrategy: undefined,
rewriteType: 'keep',
timeout: {
Expand All @@ -82,3 +83,18 @@ export const INIT_CHART = {
selected: {},
hovered: {},
};

export const HASH_KEY_LIST = [
'remote_addr',
'host',
'uri',
'server_name',
'server_addr',
'request_uri',
'query_string',
'remote_port',
'hostname',
'arg_id',
];

export const HASH_ON_LIST = ['vars', 'header', 'cookie', 'consumer'];
12 changes: 11 additions & 1 deletion src/pages/Route/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export const transformStepData = ({
upstream_header[header.header_name] = header.header_value || '';
});

const chashData: any = {};
if (step2Data.type === 'chash') {
chashData.key = step2Data.key;
chashData.hash_on = step2Data.hash_on;
}

let redirect: RouteModule.Redirect = {};
if (step1Data.redirectOption === 'disabled') {
redirect = {};
Expand Down Expand Up @@ -70,7 +76,8 @@ export const transformStepData = ({
return [key, operator, value];
}),
upstream: {
type: 'roundrobin',
type: step2Data.type,
...chashData,
nodes,
timeout: step2Data.timeout,
},
Expand Down Expand Up @@ -199,6 +206,9 @@ export const transformRouteData = (data: RouteModule.Body) => {
const step2Data: RouteModule.Step2Data = {
upstream_protocol,
upstreamHeaderList,
type: upstream ? upstream.type : 'roundrobin',
hash_on: upstream ? upstream.hash_on : undefined,
key: upstream ? upstream.key : undefined,
upstreamHostList: transformUpstreamNodes(upstream?.nodes),
upstream_id,
upstreamPath: upstream_path?.to,
Expand Down
5 changes: 5 additions & 0 deletions src/pages/Route/typing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ declare namespace RouteModule {

type Step2Data = {
upstream_protocol: 'http' | 'https' | 'keep';
type: 'roundrobin' | 'chash';
hash_on?: string;
key?: string;
upstreamHostList: UpstreamHost[];
mappingStrategy: string | undefined;
rewriteType: string | undefined;
Expand Down Expand Up @@ -121,6 +124,8 @@ declare namespace RouteModule {
vars: [string, Operator, string][];
upstream: {
type: 'roundrobin' | 'chash';
hash_on?: string;
key?: string;
nodes: {
[key: string]: number;
};
Expand Down
Loading