diff --git a/cmd/generateTemplate.go b/cmd/generateTemplate.go index 79811cb..1f37f5d 100644 --- a/cmd/generateTemplate.go +++ b/cmd/generateTemplate.go @@ -1,7 +1,7 @@ package cmd import ( - "alertmanager/config" + "alertmanager/types" "fmt" "github.com/spf13/cobra" @@ -16,7 +16,7 @@ var generateTemplateCmd = &cobra.Command{ } func generateTemplateCmdRun(cmd *cobra.Command, args []string) { - samepleConfig := config.DefaultAlertManagerConfig() + samepleConfig := types.DefaultAlertManagerConfig() fmt.Println(samepleConfig) } diff --git a/config/config.go b/config/config.go index 00b347f..e0136a9 100644 --- a/config/config.go +++ b/config/config.go @@ -1,55 +1,21 @@ package config import ( - "alertmanager/action" - "alertmanager/enrichment" - "alertmanager/logging" + "alertmanager/types" "fmt" "gopkg.in/yaml.v3" ) -type AlertManagerConfig struct { - AlertPipelines []AlertPipelineConfig `yaml:"alert_pipelines"` -} - -type AlertPipelineConfig struct { - AlertName string `yaml:"alert_name"` - Enrichments []enrichment.Enrichment `yaml:"enrichments"` - Actions []action.Action `yaml:"actions"` -} - -func defaultAlertPipelineConfig() AlertPipelineConfig { - return AlertPipelineConfig{ - AlertName: "NOOP_ALERT", - Enrichments: []enrichment.Enrichment{enrichment.GetDefaultEnrichment()}, - Actions: []action.Action{action.GetDefaultAction()}, - } -} - -func DefaultAlertManagerConfig() AlertManagerConfig { - return AlertManagerConfig{ - AlertPipelines: []AlertPipelineConfig{defaultAlertPipelineConfig()}, - } -} - -func (c AlertManagerConfig) String() string { - s, _ := yaml.Marshal(c) - // we dont need to look at this error as we are marshalling a struct. - // all error that can happen from loading random data into a struct are - // handled at the ValidateAndLoad level - return string(s) -} - // we create a global instance of AlertManagerConfig -var AmConfig = new(AlertManagerConfig) +var AmConfig = new(types.AlertManagerConfig) -func GetAmConfig() *AlertManagerConfig { +func GetAmConfig() *types.AlertManagerConfig { return AmConfig } -func ValidateAndLoad(b []byte) (*AlertManagerConfig, error) { +func ValidateAndLoad(b []byte) (*types.AlertManagerConfig, error) { amConfig := GetAmConfig() // todo protect this by a Mutex @@ -58,12 +24,12 @@ func ValidateAndLoad(b []byte) (*AlertManagerConfig, error) { // try to use a strict unmarshalling like in json err := yaml.Unmarshal(b, &amConfig) if err != nil { - return &AlertManagerConfig{}, + return &types.AlertManagerConfig{}, fmt.Errorf("unable to load config, please check format; %s", err) } if len(b) > 0 && amConfig.AlertPipelines == nil { - return &AlertManagerConfig{}, + return &types.AlertManagerConfig{}, fmt.Errorf("unable to load config, please check format") } // todo: @@ -74,15 +40,3 @@ func ValidateAndLoad(b []byte) (*AlertManagerConfig, error) { // maybe check if json-schema etc can help here return amConfig, nil } - -func (am *AlertManagerConfig) GetPipelineForAlert(name string) *AlertPipelineConfig { - logr := logging.GetLogger() - for _, pipes := range am.AlertPipelines { - if pipes.AlertName == name { - logr.Debug("Pipeline found for alert : ", name) - return &pipes - } - } - logr.Debug("no pipelines found for alert : ", name) - return nil -} diff --git a/config/config_test.go b/config/config_test.go index 4b9683b..fe9303c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "alertmanager/types" "reflect" "testing" ) @@ -44,7 +45,7 @@ func TestValidateAndLoad(t *testing.T) { tests := []struct { name string args args - want AlertManagerConfig + want types.AlertManagerConfig wantErr bool }{ { @@ -52,7 +53,7 @@ func TestValidateAndLoad(t *testing.T) { args: args{ b: []byte(goodSingleAlertConfig), }, - want: DefaultAlertManagerConfig(), + want: types.DefaultAlertManagerConfig(), wantErr: false, }, { @@ -60,10 +61,10 @@ func TestValidateAndLoad(t *testing.T) { args: args{ b: []byte(goodDoubleAlertConfig), }, - want: AlertManagerConfig{ - AlertPipelines: []AlertPipelineConfig{ - defaultAlertPipelineConfig(), - defaultAlertPipelineConfig(), + want: types.AlertManagerConfig{ + AlertPipelines: []types.AlertPipelineConfig{ + types.DefaultAlertPipelineConfig(), + types.DefaultAlertPipelineConfig(), }, }, wantErr: false, @@ -74,7 +75,7 @@ func TestValidateAndLoad(t *testing.T) { b: []byte(`randomKey: randonValue randomKey2: randomValue2`), }, - want: AlertManagerConfig{}, + want: types.AlertManagerConfig{}, wantErr: true, }, } diff --git a/server/server.go b/server/server.go index c3cd048..9b3f7eb 100644 --- a/server/server.go +++ b/server/server.go @@ -1,8 +1,8 @@ package server import ( - "alertmanager/config" "alertmanager/logging" + "alertmanager/types" "os" "os/signal" "strconv" @@ -17,7 +17,7 @@ type Server struct { ServerPort int MetricsPort int ManagementPort int - Config *config.AlertManagerConfig + Config *types.AlertManagerConfig Log *logging.Logger } diff --git a/types/alertManagerConfig.go b/types/alertManagerConfig.go new file mode 100644 index 0000000..e8a99f1 --- /dev/null +++ b/types/alertManagerConfig.go @@ -0,0 +1,52 @@ +package types + +import ( + "alertmanager/action" + "alertmanager/enrichment" + "alertmanager/logging" + + "gopkg.in/yaml.v3" +) + +type AlertManagerConfig struct { + AlertPipelines []AlertPipelineConfig `yaml:"alert_pipelines"` +} + +type AlertPipelineConfig struct { + AlertName string `yaml:"alert_name"` + Enrichments []enrichment.Enrichment `yaml:"enrichments"` + Actions []action.Action `yaml:"actions"` +} + +func DefaultAlertPipelineConfig() AlertPipelineConfig { + return AlertPipelineConfig{ + AlertName: "NOOP_ALERT", + Enrichments: []enrichment.Enrichment{enrichment.GetDefaultEnrichment()}, + Actions: []action.Action{action.GetDefaultAction()}, + } +} + +func DefaultAlertManagerConfig() AlertManagerConfig { + return AlertManagerConfig{ + AlertPipelines: []AlertPipelineConfig{DefaultAlertPipelineConfig()}, + } +} +func (c AlertManagerConfig) String() string { + s, _ := yaml.Marshal(c) + // we dont need to look at this error as we are marshalling a struct. + // all error that can happen from loading random data into a struct are + // handled at the ValidateAndLoad level + return string(s) +} + +func (am *AlertManagerConfig) GetPipelineForAlert(name string) *AlertPipelineConfig { + logr := logging.GetLogger() + for _, pipes := range am.AlertPipelines { + if pipes.AlertName == name { + logr.Debug("Pipeline found for alert : ", name) + return &pipes + } + } + logr.Debug("no pipelines found for alert : ", name) + return nil +}