From e7a23454eedc0ef8ee8308d22fb1f58c90d3624e Mon Sep 17 00:00:00 2001 From: Sohom Bhattacharjee Date: Sat, 29 Jun 2024 21:07:29 +0530 Subject: [PATCH] move action and enrichment to types --- action/action.go | 15 +++++++-------- action/actionLUT.go | 7 ------- action/noopAction.go | 3 ++- alert/alert.go | 25 ++++--------------------- enrichment/enrichment.go | 15 +++++++-------- enrichment/enrichmentLUT.go | 7 ------- enrichment/noopEnrichment.go | 3 ++- types/action.go | 10 ++++++++++ types/alertManagerConfig.go | 12 +++++------- types/enrichment.go | 10 ++++++++++ 10 files changed, 47 insertions(+), 60 deletions(-) delete mode 100644 action/actionLUT.go delete mode 100644 enrichment/enrichmentLUT.go create mode 100644 types/action.go create mode 100644 types/enrichment.go diff --git a/action/action.go b/action/action.go index f3dc7d1..26177f5 100644 --- a/action/action.go +++ b/action/action.go @@ -1,16 +1,15 @@ package action -type Action struct { - ActionName string `yaml:"action_name"` - ActionArgs string `yaml:"action_args"` -} - -func GetDefaultAction() Action { - return Action{ActionName: "NOOP_ACTION", ActionArgs: "ARG1,ARG2"} -} +import "alertmanager/types" var actionMap = make(ActionLut) func GetActionMap() *ActionLut { return &actionMap } + +type ActionLut map[string]func(types.Action, map[string]interface{}) error + +func (flut ActionLut) Add(fname string, f func(types.Action, map[string]interface{}) error) { + flut[fname] = f +} diff --git a/action/actionLUT.go b/action/actionLUT.go deleted file mode 100644 index 75de5fe..0000000 --- a/action/actionLUT.go +++ /dev/null @@ -1,7 +0,0 @@ -package action - -type ActionLut map[string]func(Action, map[string]interface{}) error - -func (flut ActionLut) Add(fname string, f func(Action, map[string]interface{}) error) { - flut[fname] = f -} diff --git a/action/noopAction.go b/action/noopAction.go index 289bb32..3d350e4 100644 --- a/action/noopAction.go +++ b/action/noopAction.go @@ -2,10 +2,11 @@ package action import ( "alertmanager/logging" + "alertmanager/types" "fmt" ) -func NoopAction(a Action, resultMap map[string]interface{}) error { +func NoopAction(a types.Action, resultMap map[string]interface{}) error { logr := logging.GetLogger() logr.Debug("noop action called") rs := fmt.Sprintf("noop action called \n") diff --git a/alert/alert.go b/alert/alert.go index d5c2c2d..246c1ea 100644 --- a/alert/alert.go +++ b/alert/alert.go @@ -1,8 +1,7 @@ package alert import ( - "alertmanager/action" - "alertmanager/enrichment" + "alertmanager/types" "encoding/json" "fmt" "time" @@ -10,9 +9,9 @@ import ( type Alert struct { // fields we use to process stuff - alertName string `json:"-"` - enrichments []enrichment.Enrichment `json:"-"` - actions []action.Action `json:"-"` + alertName string `json:"-"` + enrichments []types.Enrichment `json:"-"` + actions []types.Action `json:"-"` // fields we get from outside Annotations map[string]string `json:"annotations"` @@ -39,22 +38,6 @@ func (a *Alert) GetAlertName() string { return a.alertName } -func (a *Alert) GetActions() []action.Action { - return a.actions -} - -func (a *Alert) SetActions(actions []action.Action) { - a.actions = actions -} - -func (a *Alert) GetEnrichments() []enrichment.Enrichment { - return a.enrichments -} - -func (a *Alert) SetEnrichments(enrichments []enrichment.Enrichment) { - a.enrichments = enrichments -} - func (c AlertGroup) String() string { s, _ := json.Marshal(c) // we dont need to look at this error as we are marshalling a struct. diff --git a/enrichment/enrichment.go b/enrichment/enrichment.go index b3486e0..9b23c5e 100644 --- a/enrichment/enrichment.go +++ b/enrichment/enrichment.go @@ -1,16 +1,15 @@ package enrichment -type Enrichment struct { - EnrichmentName string `yaml:"enrichment_name"` - EnrichmentArgs string `yaml:"enrichment_args"` -} - -func GetDefaultEnrichment() Enrichment { - return Enrichment{EnrichmentName: "NOOP_ENRICHMENT", EnrichmentArgs: "ARG1,ARG2"} -} +import "alertmanager/types" var enrichmentMap = make(EnrichmentLut) func GetEnrichmentMap() *EnrichmentLut { return &enrichmentMap } + +type EnrichmentLut map[string]func(types.Enrichment) (interface{}, error) + +func (flut EnrichmentLut) Add(fname string, f func(types.Enrichment) (interface{}, error)) { + flut[fname] = f +} diff --git a/enrichment/enrichmentLUT.go b/enrichment/enrichmentLUT.go deleted file mode 100644 index 24226b6..0000000 --- a/enrichment/enrichmentLUT.go +++ /dev/null @@ -1,7 +0,0 @@ -package enrichment - -type EnrichmentLut map[string]func(Enrichment) (interface{}, error) - -func (flut EnrichmentLut) Add(fname string, f func(Enrichment) (interface{}, error)) { - flut[fname] = f -} diff --git a/enrichment/noopEnrichment.go b/enrichment/noopEnrichment.go index e466b48..a04a56d 100644 --- a/enrichment/noopEnrichment.go +++ b/enrichment/noopEnrichment.go @@ -2,10 +2,11 @@ package enrichment import ( "alertmanager/logging" + "alertmanager/types" "fmt" ) -func NoopEnrichment(e Enrichment) (interface{}, error) { +func NoopEnrichment(e types.Enrichment) (interface{}, error) { logr := logging.GetLogger() rs := fmt.Sprint("noop enrichment called with : ", e.EnrichmentArgs) diff --git a/types/action.go b/types/action.go new file mode 100644 index 0000000..5de5249 --- /dev/null +++ b/types/action.go @@ -0,0 +1,10 @@ +package types + +type Action struct { + ActionName string `yaml:"action_name"` + ActionArgs string `yaml:"action_args"` +} + +func GetDefaultAction() Action { + return Action{ActionName: "NOOP_ACTION", ActionArgs: "ARG1,ARG2"} +} diff --git a/types/alertManagerConfig.go b/types/alertManagerConfig.go index e8a99f1..42a14f0 100644 --- a/types/alertManagerConfig.go +++ b/types/alertManagerConfig.go @@ -1,8 +1,6 @@ package types import ( - "alertmanager/action" - "alertmanager/enrichment" "alertmanager/logging" "gopkg.in/yaml.v3" @@ -13,16 +11,16 @@ type AlertManagerConfig struct { } type AlertPipelineConfig struct { - AlertName string `yaml:"alert_name"` - Enrichments []enrichment.Enrichment `yaml:"enrichments"` - Actions []action.Action `yaml:"actions"` + AlertName string `yaml:"alert_name"` + Enrichments []Enrichment `yaml:"enrichments"` + Actions []Action `yaml:"actions"` } func DefaultAlertPipelineConfig() AlertPipelineConfig { return AlertPipelineConfig{ AlertName: "NOOP_ALERT", - Enrichments: []enrichment.Enrichment{enrichment.GetDefaultEnrichment()}, - Actions: []action.Action{action.GetDefaultAction()}, + Enrichments: []Enrichment{GetDefaultEnrichment()}, + Actions: []Action{GetDefaultAction()}, } } diff --git a/types/enrichment.go b/types/enrichment.go new file mode 100644 index 0000000..c66cb1c --- /dev/null +++ b/types/enrichment.go @@ -0,0 +1,10 @@ +package types + +type Enrichment struct { + EnrichmentName string `yaml:"enrichment_name"` + EnrichmentArgs string `yaml:"enrichment_args"` +} + +func GetDefaultEnrichment() Enrichment { + return Enrichment{EnrichmentName: "NOOP_ENRICHMENT", EnrichmentArgs: "ARG1,ARG2"} +}