Skip to content

Commit 77746ff

Browse files
committed
[config] support append mdsv2 config
1 parent 292f934 commit 77746ff

File tree

5 files changed

+67
-15
lines changed

5 files changed

+67
-15
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
"showLog": true,
187187
},
188188
{
189-
"name": "dingoadm upgrade -f",
189+
"name": "dingoadm upgrade -f --local",
190190
"type": "go",
191191
"request": "launch",
192192
"mode": "auto",
@@ -198,6 +198,7 @@
198198
"args": [
199199
"upgrade",
200200
"-f",
201+
"--local"
201202
],
202203
"showLog": true,
203204
},

internal/common/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ const (
139139
CLIENT_CONFIG_DELIMITER = "="
140140
TOOLS_V2_CONFIG_DELIMITER = ": "
141141

142+
// prefix
143+
STORE_GFLAGS_PREFIX = "-"
144+
MDSV2_CONFIG_PREFIX = "--"
145+
142146
// mdsv2
143147
KEY_SKIP_MDSV2_CLI = "SKIP_MDSV2_CLI"
144148

internal/task/step/file.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ type (
6666

6767
Mutate func(string, string, string) (string, error)
6868

69+
DynamicAppend func(string, string) (string, error)
70+
6971
Filter struct {
70-
KVFieldSplit string
71-
Mutate Mutate
72-
Input *string
73-
Output *string
72+
KVFieldSplit string
73+
Mutate Mutate
74+
SerivceConfig map[string]string
75+
Input *string
76+
Output *string
7477
}
7578

7679
SyncFile struct {
@@ -80,6 +83,7 @@ type (
8083
ContainerDestPath string
8184
KVFieldSplit string
8285
Mutate func(string, string, string) (string, error)
86+
SerivceConfig map[string]string
8387
module.ExecOptions
8488
}
8589

@@ -232,6 +236,14 @@ func (s *Filter) kvSplit(line string, key, value *string) error {
232236
func (s *Filter) Execute(ctx *context.Context) error {
233237
var key, value string
234238
output := []string{}
239+
240+
topologySerivceConfigKeys := []string{}
241+
for k := range s.SerivceConfig {
242+
topologySerivceConfigKeys = append(topologySerivceConfigKeys, k)
243+
}
244+
245+
originServiceConfigKeys := []string{}
246+
235247
scanner := bufio.NewScanner(strings.NewReader(string(*s.Input)))
236248
for scanner.Scan() {
237249
in := scanner.Text()
@@ -242,13 +254,36 @@ func (s *Filter) Execute(ctx *context.Context) error {
242254
}
243255
}
244256

257+
originServiceConfigKeys = append(originServiceConfigKeys, key)
258+
245259
out, err := s.Mutate(in, key, value)
246260
if err != nil {
247261
return err
248262
}
249263
output = append(output, out)
250264
}
251265

266+
// extract the key from topologySerivceConfigKeys that are not in originServiceConfigKeys
267+
extraServiceConfigKeys := utils.Filter(topologySerivceConfigKeys, func(k string) bool {
268+
if !strings.HasPrefix(k, "mds_") {
269+
return false
270+
}
271+
mdsv2_key := fmt.Sprintf("%s%s", comm.MDSV2_CONFIG_PREFIX, k)
272+
return !utils.Contains(originServiceConfigKeys, mdsv2_key)
273+
})
274+
275+
if len(extraServiceConfigKeys) > 0 {
276+
output = append(output, "# dynamic config from topology")
277+
for _, k := range extraServiceConfigKeys {
278+
v := s.SerivceConfig[k]
279+
if len(v) > 0 {
280+
// dingo-mdsv2.template.conf
281+
out := fmt.Sprintf("%s%s%s%s", comm.MDSV2_CONFIG_PREFIX, k, s.KVFieldSplit, v)
282+
output = append(output, out)
283+
}
284+
}
285+
}
286+
252287
*s.Output = strings.Join(output, "\n")
253288
return nil
254289
}
@@ -263,10 +298,11 @@ func (s *SyncFile) Execute(ctx *context.Context) error {
263298
ExecOptions: s.ExecOptions,
264299
})
265300
steps = append(steps, &Filter{
266-
KVFieldSplit: s.KVFieldSplit,
267-
Mutate: s.Mutate,
268-
Input: &input,
269-
Output: &output,
301+
KVFieldSplit: s.KVFieldSplit,
302+
Mutate: s.Mutate,
303+
SerivceConfig: s.SerivceConfig,
304+
Input: &input,
305+
Output: &output,
270306
})
271307
steps = append(steps, &InstallFile{
272308
ContainerId: s.ContainerDestId,

internal/task/task/common/sync_config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ func NewMutate(dc *topology.DeployConfig, delimiter string, forceRender bool) st
6161
muteKey := strings.TrimSpace(key)
6262
if dc.GetRole() == topology.ROLE_COORDINATOR || dc.GetRole() == topology.ROLE_STORE {
6363
// key is like -xxx , replace '-' to 'gflags.'
64-
if strings.HasPrefix(key, "-") {
65-
muteKey = fmt.Sprintf("gflags.%s", strings.TrimPrefix(key, "-"))
64+
if strings.HasPrefix(key, comm.STORE_GFLAGS_PREFIX) {
65+
muteKey = fmt.Sprintf("gflags.%s", strings.TrimPrefix(key, comm.STORE_GFLAGS_PREFIX))
6666
}
6767
} else if dc.GetRole() == topology.ROLE_MDS_V2 {
6868
// key is like --xxx , trim '--'
69-
if strings.HasPrefix(key, "--") {
70-
muteKey = strings.TrimPrefix(key, "--")
69+
if strings.HasPrefix(key, comm.MDSV2_CONFIG_PREFIX) {
70+
muteKey = strings.TrimPrefix(key, comm.MDSV2_CONFIG_PREFIX)
7171
}
7272
}
7373

@@ -151,13 +151,14 @@ func NewSyncConfigTask(dingoadm *cli.DingoAdm, dc *topology.DeployConfig) (*task
151151

152152
if dc.GetKind() == topology.KIND_DINGOFS {
153153
for _, conf := range layout.ServiceConfFiles {
154-
t.AddStep(&step.SyncFile{ // sync service config
154+
t.AddStep(&step.SyncFile{ // sync service config, e.g. dingo-mdsv2.template.conf
155155
ContainerSrcId: &containerId,
156156
ContainerSrcPath: conf.SourcePath,
157157
ContainerDestId: &containerId,
158158
ContainerDestPath: conf.TargetPath,
159159
KVFieldSplit: delimiter,
160160
Mutate: NewMutate(dc, delimiter, conf.Name == "nginx.conf"),
161+
SerivceConfig: dc.GetServiceConfig(),
161162
ExecOptions: dingoadm.ExecOptions(),
162163
})
163164
}

internal/utils/common.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,19 @@ func EncryptFile(srcfile, dstfile, secret string) error {
319319

320320
func Contains(list []string, str string) bool {
321321
for _, v := range list {
322-
if v == str {
322+
if strings.TrimSpace(v) == strings.TrimSpace(str) {
323323
return true
324324
}
325325
}
326326
return false
327327
}
328+
329+
func Filter(list []string, filterFunc func(string) bool) []string {
330+
var result []string
331+
for _, item := range list {
332+
if filterFunc(item) {
333+
result = append(result, item)
334+
}
335+
}
336+
return result
337+
}

0 commit comments

Comments
 (0)