Skip to content

Commit

Permalink
feat: support multiple hosts configuration for system cluster initial…
Browse files Browse the repository at this point in the history
…ization
  • Loading branch information
silenceqi committed Jan 22, 2025
1 parent ac147b5 commit 2b58bcf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 28 deletions.
3 changes: 1 addition & 2 deletions config/setup/common/data/agent_relay_gateway_config.dat
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ elasticsearch:
basic_auth:
username: ingest
password: password
endpoints:
- $[[SETUP_ENDPOINT]]
endpoints: $[[SETUP_ENDPOINTS]]

pipeline:
- name: bulk_request_ingest
Expand Down
4 changes: 2 additions & 2 deletions config/setup/common/data/system_ingest_config.dat
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ pipeline:
# key_file: /xxx/client.key
# skip_insecure_verify: false
schema: "$[[SETUP_SCHEME]]"
hosts: # receiver endpoint, fallback in order
- "$[[SETUP_ENDPOINT]]"
# receiver endpoint, fallback in order
hosts: $[[SETUP_HOSTS]]
valid_status_code: [200,201] #panic on other status code
26 changes: 19 additions & 7 deletions plugin/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ func (module *Module) Stop() error {

type SetupRequest struct {
Cluster struct {
Host string `json:"host"`
Schema string `json:"schema"`
Endpoint string `json:"endpoint"`
Username string `json:"username"`
Password string `json:"password"`
Host string `json:"host"`
Schema string `json:"schema"`
Endpoint string `json:"endpoint"`
Username string `json:"username"`
Password string `json:"password"`
Hosts []string `json:"hosts"`
} `json:"cluster"`

Skip bool `json:"skip"`
Expand Down Expand Up @@ -796,8 +797,19 @@ func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request,
return w.Write([]byte(request.Cluster.Password))
case "SETUP_SCHEME":
return w.Write([]byte(strings.Split(request.Cluster.Endpoint, "://")[0]))
case "SETUP_ENDPOINT":
return w.Write([]byte(strings.Split(request.Cluster.Endpoint, "://")[1]))
case "SETUP_ENDPOINTS":
endpoints := []string{request.Cluster.Endpoint}
for _, host := range request.Cluster.Hosts {
endpoint := fmt.Sprintf("%s://%s", request.Cluster.Schema, host)
if !util.StringInArray(endpoints, endpoint) {
endpoints = append(endpoints, endpoint)
}
}
endpointsStr := fmt.Sprintf("[%s]", strings.Join(endpoints, ", "))
return w.Write([]byte(endpointsStr))
case "SETUP_HOSTS":
hostsStr := fmt.Sprintf("[%s]", strings.Join(request.Cluster.Hosts, ", "))
return w.Write([]byte(hostsStr))
case "SETUP_TEMPLATE_NAME":
return w.Write([]byte(cfg1.TemplateName))
case "SETUP_INDEX_PREFIX":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from 'react';
import { Alert, Button, Form, Icon, Input, Switch } from 'antd';
import { Alert, Button, Form, Icon, Input, Switch, Select } from 'antd';
import request from '@/utils/request';
import { formatMessage } from "umi/locale";
import TrimSpaceInput from '@/components/TrimSpaceInput';
Expand Down Expand Up @@ -49,9 +49,9 @@ export default ({ onNext, form, formData, onFormDataChange }) => {
setTestLoading(true);
setTestStatus();
setTestError();
const { host, isTLS, isAuth, username, password } = values;
const { hosts, isTLS, isAuth, username, password } = values;
const body = {
host: host.trim(),
hosts: (hosts || []).map(host=>host.trim()),
schema: isTLS === true ? "https" : "http",
}
if (isAuth) {
Expand Down Expand Up @@ -104,32 +104,41 @@ export default ({ onNext, form, formData, onFormDataChange }) => {

const onFormDataSave = () => {
const values = form.getFieldsValue();
const { host, isAuth, username, password } = form.getFieldsValue();
const { hosts, isAuth, username, password } = values;
onFormDataChange({
host: host.trim(), isAuth, username, password
hosts: (hosts || []).map(host=>host.trim()),
isAuth, username, password
})
onNext();
}
const validateHostsRule = (rule, value, callback) => {
let vals = value || [];
for(let i = 0; i < vals.length; i++) {
if (!/^[\w\.\-_~%]+(\:\d+)?$/.test(vals[i])) {
return callback(formatMessage({ id: 'guide.cluster.host.validate'}));
}
}
// validation passed
callback();
};

const { getFieldDecorator } = form;

return (
<Form {...formItemLayout} onSubmit={onSubmit} colon={false}>
<Form.Item label={formatMessage({ id: 'guide.cluster.host'})}>
{getFieldDecorator("host", {
initialValue: formData.host,
{getFieldDecorator("hosts", {
initialValue: formData.hosts,
rules: [
{
required: true,
message: formatMessage({ id: 'guide.cluster.host.required'}),
},
{
type: "string",
pattern: /^[\w\.\-_~%]+(\:\d+)?$/,
message: formatMessage({ id: 'guide.cluster.host.validate'}),
},
validator: validateHostsRule,
}
],
})(<TrimSpaceInput placeholder="127.0.0.1:9200" onChange={resetTestStatus}/>)}
})(<Select placeholder="127.0.0.1:9200" mode="tags" allowClear={true} onChange={resetTestStatus}/>)}
</Form.Item>
<Form.Item label="TLS">
{getFieldDecorator("isTLS", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ export default ({ onPrev, onNext, form, formData, onFormDataChange }) => {
const onCheck = async () => {
try {
setCheckLoading(true);
const { host, isTLS, isAuth, username, password } = formData;
const { hosts, isTLS, isAuth, username, password } = formData;
const host = hosts[0];
const cluster = {
endpoint: isTLS ? `https://${host}` : `http://${host}`
endpoint: isTLS ? `https://${host}` : `http://${host}`,
hosts: hosts,
schema: isTLS ? "https": "http",
}
if (isAuth) {
cluster.username = username
Expand Down Expand Up @@ -110,14 +113,17 @@ export default ({ onPrev, onNext, form, formData, onFormDataChange }) => {
}
})
const {
host,
hosts,
isTLS,
isAuth,
username,
password,
} = formData;
const host = hosts[0];
const cluster = {
endpoint: isTLS ? `https://${host}` : `http://${host}`,
hosts: hosts,
schema: isTLS ? "https": "http"
};
if (isAuth) {
cluster.username = username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default ({ onPrev, onNext, form, formData, onFormDataChange }) => {
try {
setLoading(true);
const {
host,
hosts,
isTLS,
isAuth,
username,
Expand All @@ -94,8 +94,11 @@ export default ({ onPrev, onNext, form, formData, onFormDataChange }) => {
credential_secret,
} = formData;
const body = {};
const host = hosts[0];
const cluster = {
endpoint: isTLS ? `https://${host}` : `http://${host}`,
hosts: hosts,
schema: isTLS ? "https" : "http",
};
if (isAuth) {
cluster.username = username;
Expand Down Expand Up @@ -191,14 +194,16 @@ export default ({ onPrev, onNext, form, formData, onFormDataChange }) => {

setLoading(true);
const {
host,
hosts,
isTLS,
isAuth,
username,
password,
} = formData;
const host = hosts[0];
const cluster = {
endpoint: isTLS ? `https://${host}` : `http://${host}`,
hosts: hosts,
};
if (isAuth) {
cluster.username = username;
Expand Down

0 comments on commit 2b58bcf

Please sign in to comment.