From fd6597486f1dbc33c05d800abde07fb960c11aa2 Mon Sep 17 00:00:00 2001 From: Sohom Bhattacharjee Date: Fri, 28 Jun 2024 04:53:28 +0530 Subject: [PATCH] got enrichments to work --- alert/processAlert.go | 21 +++++++++++++++++++-- basicWebhookPayload.json | 2 +- cmd/server.go | 6 ++++++ enrichment/enrichment.go | 8 ++++++++ enrichment/noop_enrichment.go | 13 +++++++++++++ utils/fucntionLut.go | 17 +++++++++++++++++ 6 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 enrichment/noop_enrichment.go create mode 100644 utils/fucntionLut.go diff --git a/alert/processAlert.go b/alert/processAlert.go index 5622695..bf0e3d2 100644 --- a/alert/processAlert.go +++ b/alert/processAlert.go @@ -2,7 +2,9 @@ package alert import ( "alertmanager/config" + "alertmanager/enrichment" "alertmanager/logging" + "fmt" ) // Once I have an alertName @@ -18,10 +20,25 @@ func ProcessAlert(a Alert) { amc := config.GetAmConfig() p := amc.GetPipelineForAlert(an) - if p != nil { + if p == nil { logr.Infof("no alert-pipeline configured for %s", an) + return } + logr.Debug("alert pipeline name", (*p)) - logr.Debug("alert pipeline", p) + enrichmentMap := enrichment.GetEnrichmentMap() + + for _, v := range (*p).Enrichments { + logr.Info("processing enrichment : ", v.EnrichmentName) + + if f, ok := (*enrichmentMap)[v.EnrichmentName]; ok { + x, err := f(v.EnrichmentArgs) + if err != nil { + fmt.Println(err) + } + fmt.Print("********** ", x) + } + + } } diff --git a/basicWebhookPayload.json b/basicWebhookPayload.json index 0e41032..65b18ff 100644 --- a/basicWebhookPayload.json +++ b/basicWebhookPayload.json @@ -40,7 +40,7 @@ "summary": "Pod is crash looping." }, "labels": { - "alertname": "KubePodCrashLooping2", + "alertname": "NOOP_ALERT", "cluster": "cluster-main", "container": "test-service", "endpoint": "http", diff --git a/cmd/server.go b/cmd/server.go index c4f4616..c016972 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -2,6 +2,7 @@ package cmd import ( "alertmanager/config" + "alertmanager/enrichment" "alertmanager/logging" "alertmanager/server" "fmt" @@ -28,6 +29,11 @@ func serverCommandRunE(cmd *cobra.Command, args []string) error { return err } + // Initialize the Enrichments + log.Info("Adding NooP Enrichment") + enr := enrichment.GetEnrichmentMap() + enr.Add("NOOP_ENRICHMENT", enrichment.Noop_Enrichment) + b, err := os.ReadFile(cFile) if err != nil { return fmt.Errorf("cannot read config file; %s", err) diff --git a/enrichment/enrichment.go b/enrichment/enrichment.go index 6cac4c6..fb97729 100644 --- a/enrichment/enrichment.go +++ b/enrichment/enrichment.go @@ -1,5 +1,7 @@ package enrichment +import "alertmanager/utils" + type Enrichment struct { EnrichmentName string `yaml:"enrichment_name"` EnrichmentArgs string `yaml:"enrichment_args"` @@ -8,3 +10,9 @@ type Enrichment struct { func GetDefaultEnrichment() Enrichment { return Enrichment{EnrichmentName: "NOOP_ENRICHMENT", EnrichmentArgs: "ARG1,ARG2"} } + +var enrichmentMap = make(utils.FunctionLut) + +func GetEnrichmentMap() *utils.FunctionLut { + return &enrichmentMap +} diff --git a/enrichment/noop_enrichment.go b/enrichment/noop_enrichment.go new file mode 100644 index 0000000..c216fa0 --- /dev/null +++ b/enrichment/noop_enrichment.go @@ -0,0 +1,13 @@ +package enrichment + +import ( + "alertmanager/logging" +) + +func Noop_Enrichment(args string) (string, error) { + logr := logging.GetLogger() + + logr.Info("Noop enrichment Called") + + return "Noop called", nil +} diff --git a/utils/fucntionLut.go b/utils/fucntionLut.go new file mode 100644 index 0000000..fbe3a0a --- /dev/null +++ b/utils/fucntionLut.go @@ -0,0 +1,17 @@ +package utils + +// A lookup table for name -> function +// Functions are pretty generic. +// Accepts a string of args (todo: make this more generic) +// there are no timeouts yet (todo) +// each function is supposed to handle its own arg structure and how to parse it. +// todo +// there has to be a better way to do this with interfaces and gorotines and channels + +type FunctionLut map[string]func(string) (string, error) + +func (flut FunctionLut) Add(fname string, f func(string) (string, error)) FunctionLut { + flut[fname] = f + + return flut +}