Skip to content

Add function to copy struct of amconfig.Config #589

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

Merged
merged 1 commit into from
Oct 31, 2017
Merged
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
34 changes: 28 additions & 6 deletions pkg/alertmanager/multitenant.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package alertmanager

import (
"encoding/json"
"flag"
"fmt"
"html/template"
Expand Down Expand Up @@ -382,14 +383,14 @@ func (am *MultitenantAlertmanager) addNewConfigs(cfgs map[string]configs.View) {
}

func (am *MultitenantAlertmanager) transformConfig(userID string, amConfig *amconfig.Config) (*amconfig.Config, error) {
if amConfig == nil && am.fallbackConfig != nil {
log.Infof("using fallback configuration for %v", userID)
amConfig = am.fallbackConfig
}
if amConfig == nil { // shouldn't happen, but check just in case
return nil, fmt.Errorf("no usable Cortex configuration for %v", userID)
}
newConfig := *amConfig // take a copy to modify
newConfig, err := copyConfig(amConfig)
if err != nil {
log.Errorf("cannot copy config: %s", err)
return nil, err
}
// Magic ability to configure a Slack receiver if config requests it
if am.cfg.AutoSlackRoot != "" {
for _, r := range newConfig.Receivers {
Expand All @@ -401,7 +402,28 @@ func (am *MultitenantAlertmanager) transformConfig(userID string, amConfig *amco
}
}

return &newConfig, nil
return newConfig, nil
}

// deep copy because of config struct contains a lot of references types (slices of pointers tec)
// this copy works even if we add/change other fields of config
// or if fields are changing somewhere else in the code
func copyConfig(config *amconfig.Config) (*amconfig.Config, error) {
configBytes, err := json.Marshal(config)
if err != nil {
log.Errorf("cannot marshal config to json, error: %s", err)
return nil, err
}

newConfig := &amconfig.Config{}

err = json.Unmarshal(configBytes, newConfig)
if err != nil {
log.Errorf("cannot unmarshal json to config, error: %s", err)
return nil, err
}

return newConfig, nil
}

// setConfig applies the given configuration to the alertmanager for `userID`,
Expand Down