From be9e28a3fde144d998289979ecfbdc0cc45387b9 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Mon, 27 Jan 2025 22:19:18 +0545 Subject: [PATCH] chore: move resource template getter to duty --- db/common.go | 28 ----------------- notification/cel.go | 62 +++---------------------------------- playbook/actions/actions.go | 62 +++---------------------------------- 3 files changed, 8 insertions(+), 144 deletions(-) delete mode 100644 db/common.go diff --git a/db/common.go b/db/common.go deleted file mode 100644 index 1eb844ba4..000000000 --- a/db/common.go +++ /dev/null @@ -1,28 +0,0 @@ -package db - -import ( - "time" - - "github.com/flanksource/duty/context" - "github.com/patrickmn/go-cache" -) - -var distinctTagsCache = cache.New(time.Minute*10, time.Hour) - -func GetDistinctTags(ctx context.Context) ([]string, error) { - if cached, ok := distinctTagsCache.Get("key"); ok { - return cached.([]string), nil - } - - var tags []string - query := ` - SELECT jsonb_object_keys(tags) FROM config_items - UNION - SELECT jsonb_object_keys(tags) FROM playbooks` - if err := ctx.DB().Raw(query).Scan(&tags).Error; err != nil { - return nil, err - } - - distinctTagsCache.SetDefault("key", tags) - return tags, nil -} diff --git a/notification/cel.go b/notification/cel.go index 5eae7f4a8..f024f94d2 100644 --- a/notification/cel.go +++ b/notification/cel.go @@ -4,12 +4,11 @@ import ( "errors" "fmt" - "github.com/flanksource/commons/logger" + "github.com/flanksource/commons/collections" + "github.com/flanksource/duty" "github.com/flanksource/duty/context" "github.com/flanksource/duty/models" "github.com/flanksource/duty/types" - "github.com/flanksource/gomplate/v3" - "github.com/flanksource/incident-commander/db" "github.com/samber/lo" ) @@ -102,61 +101,8 @@ func (t *celVariables) AsMap(ctx context.Context) map[string]any { output["new_state"] = t.NewState } - // Placeholders for commonly used fields of the resource - // If a resource exists, they'll be filled up below - output["name"] = "" - output["status"] = "" - output["health"] = "" - output["labels"] = map[string]string{} - tags := map[string]string{} - - if resource := t.SelectableResource(); resource != nil { - // set the alias name/status/health/labels/tags of the resource - output["name"] = resource.GetName() - if status, err := resource.GetStatus(); err == nil { - output["status"] = status - } - if health, err := resource.GetHealth(); err == nil { - output["health"] = health - } - if table, ok := resource.(models.TaggableModel); ok { - tags = table.GetTags() - } - if table, ok := resource.(models.LabelableModel); ok { - output["labels"] = table.GetLabels() - } - } - - if ctx.DB() != nil { - if tags, err := db.GetDistinctTags(ctx); err != nil { - logger.Errorf("failed to get distinct tags for notification cel variable: %w", err) - } else { - for _, tag := range tags { - if _, ok := output[tag]; !ok { - output[tag] = "" - } - } - } - } - - output["tags"] = tags - - // Inject tags as top level variables - for k, v := range tags { - if !gomplate.IsValidCELIdentifier(k) { - logger.V(9).Infof("skipping tag %s as it is not a valid CEL identifier", k) - continue - } - - if _, ok := output[k]; ok { - logger.V(9).Infof("skipping tag %s as it already exists in the playbook template environment", k) - continue - } - - output[k] = v - } - - return output + resourceContext := duty.GetResourceContext(ctx, t.SelectableResource()) + return collections.MergeMap(resourceContext, output) } func (t *celVariables) SelectableResource() types.ResourceSelectable { diff --git a/playbook/actions/actions.go b/playbook/actions/actions.go index ddb807bf9..462878ee7 100644 --- a/playbook/actions/actions.go +++ b/playbook/actions/actions.go @@ -4,13 +4,12 @@ import ( "encoding/json" "io" - "github.com/flanksource/commons/logger" + "github.com/flanksource/commons/collections" + "github.com/flanksource/duty" "github.com/flanksource/duty/context" "github.com/flanksource/duty/models" "github.com/flanksource/duty/query" "github.com/flanksource/duty/types" - "github.com/flanksource/gomplate/v3" - "github.com/flanksource/incident-commander/db" "github.com/labstack/echo/v4" "github.com/samber/lo" ) @@ -88,61 +87,8 @@ func (t *TemplateEnv) AsMap(ctx context.Context) map[string]any { "request": t.Request, } - // Placeholders for commonly used fields of the resource - // If a resource exists, they'll be filled up below - output["name"] = "" - output["status"] = "" - output["health"] = "" - output["labels"] = map[string]string{} - tags := map[string]string{} - - if resource := t.SelectableResource(); resource != nil { - // set the alias name/status/health/labels/tags of the resource - output["name"] = resource.GetName() - if status, err := resource.GetStatus(); err == nil { - output["status"] = status - } - if health, err := resource.GetHealth(); err == nil { - output["health"] = health - } - if table, ok := resource.(models.TaggableModel); ok { - tags = table.GetTags() - } - if table, ok := resource.(models.LabelableModel); ok { - output["labels"] = table.GetLabels() - } - } - - if ctx.DB() != nil { - if distinctTags, err := db.GetDistinctTags(ctx); err != nil { - logger.Errorf("failed to get distinct tags for notification cel variable: %w", err) - } else { - for _, tag := range distinctTags { - if _, ok := tags[tag]; !ok { - tags[tag] = "" - } - } - } - } - - output["tags"] = tags - - // Inject tags as top level variables - for k, v := range tags { - if !gomplate.IsValidCELIdentifier(k) { - logger.V(9).Infof("skipping tag %s as it is not a valid CEL identifier", k) - continue - } - - if _, ok := output[k]; ok { - logger.V(9).Infof("skipping tag %s as it already exists in the playbook template environment", k) - continue - } - - output[k] = v - } - - return output + resourceContext := duty.GetResourceContext(ctx, t.SelectableResource()) + return collections.MergeMap(resourceContext, output) } func (t *TemplateEnv) JSON(ctx context.Context) string {