Skip to content

Commit

Permalink
controllers/alertmanager: properly deduplicate alertmanager config te…
Browse files Browse the repository at this point in the history
…mplates without base path

it must simplify configuration and allow user to define templates without knowning the path of it
  • Loading branch information
f41gh7 committed Feb 28, 2024
1 parent 9ee567f commit 1128fa9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
16 changes: 11 additions & 5 deletions controllers/factory/alertmanager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ OUTER:
return &ParsedConfig{Data: result, BadObjectsCount: badObjectsCount, ParseErrors: parseErrors}, nil
}

// AddConfigTemplates adds external templates to the given based configuration
func AddConfigTemplates(baseCfg []byte, templates []string) ([]byte, error) {
if len(templates) == 0 {
return baseCfg, nil
Expand All @@ -128,19 +129,24 @@ func AddConfigTemplates(baseCfg []byte, templates []string) ([]byte, error) {
if err := yaml.Unmarshal(baseCfg, &baseYAMlCfg); err != nil {
return nil, fmt.Errorf("cannot parse base cfg :%w", err)
}
templatesSet := make(map[string]struct{})
for _, v := range baseYAMlCfg.Templates {
templatesSet[v] = struct{}{}
templatesSetByIdx := make(map[string]int)
for idx, v := range baseYAMlCfg.Templates {
templatesSetByIdx[v] = idx
}
for _, v := range templates {
if len(strings.TrimSpace(v)) == 0 {
continue
}
if _, ok := templatesSet[v]; ok {
if _, ok := templatesSetByIdx[v]; ok {
continue
}
// override value with correct path
if idx, ok := templatesSetByIdx[path.Base(v)]; ok {
baseYAMlCfg.Templates[idx] = v
continue
}
baseYAMlCfg.Templates = append(baseYAMlCfg.Templates, v)
templatesSet[v] = struct{}{}
templatesSetByIdx[v] = len(baseYAMlCfg.Templates) - 1
}
return yaml.Marshal(baseYAMlCfg)
}
Expand Down
44 changes: 44 additions & 0 deletions controllers/factory/alertmanager/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,50 @@ templates:
want: ``,
wantErr: true,
},
{
name: "add template duplicates without path",
args: args{
config: []byte(`global:
resolve_timeout: 5m
route:
receiver: webhook
group_wait: 30s
group_interval: 5m
repeat_interval: 12h
receivers:
- name: webhook
webhook_configs:
- url: http://localhost:30500/
templates:
- template1.tmpl
- template2.tmpl
- /etc/vm/templates/test/template3.tmpl
`),
templates: []string{
"/etc/vm/templates/test/template1.tmpl",
"/etc/vm/templates/test/template2.tmpl",
"/etc/vm/templates/test/template0.tmpl",
},
},
want: `global:
resolve_timeout: 5m
route:
receiver: webhook
group_wait: 30s
group_interval: 5m
repeat_interval: 12h
receivers:
- name: webhook
webhook_configs:
- url: http://localhost:30500/
templates:
- /etc/vm/templates/test/template1.tmpl
- /etc/vm/templates/test/template2.tmpl
- /etc/vm/templates/test/template3.tmpl
- /etc/vm/templates/test/template0.tmpl
`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 1128fa9

Please sign in to comment.