Skip to content

Commit

Permalink
feat: encrich namespace labels when sync runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdh committed Oct 21, 2023
1 parent e3f6e4c commit 2dbc037
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 131 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME="github.com/goto/siren"
LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_TAG := "$(shell git rev-list --tags --max-count=1)"
APP_VERSION := "$(shell git describe --tags ${LAST_TAG})-next"
PROTON_COMMIT := "534a703026bd70fa464c3bedc6a9aef3f8fcdd19"
PROTON_COMMIT := "8bae419bea0ab170472a0d07bf01ef8fe11d3f1b"

.PHONY: all build test clean dist vet proto install

Expand Down
28 changes: 20 additions & 8 deletions core/namespace/mocks/config_syncer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Repository interface {
Create(context.Context, *EncryptedNamespace) error
Get(context.Context, uint64) (*EncryptedNamespace, error)
Update(context.Context, *EncryptedNamespace) error
UpdateLabels(context.Context, map[string]string) error
Delete(context.Context, uint64) error
}

Expand Down
2 changes: 1 addition & 1 deletion core/namespace/provider_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import (

//go:generate mockery --name=ConfigSyncer -r --case underscore --with-expecter --structname ConfigSyncer --filename config_syncer.go --output=./mocks
type ConfigSyncer interface {
SyncRuntimeConfig(ctx context.Context, namespaceID uint64, namespaceURN string, prov provider.Provider) error
SyncRuntimeConfig(ctx context.Context, namespaceID uint64, namespaceURN string, prov provider.Provider) (map[string]string, error)
}
17 changes: 13 additions & 4 deletions core/namespace/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,18 @@ func (s *Service) Create(ctx context.Context, ns *Namespace) error {
return err
}

if err := pluginService.SyncRuntimeConfig(ctx, encryptedNamespace.ID, ns.URN, *prov); err != nil {
labels, err := pluginService.SyncRuntimeConfig(ctx, encryptedNamespace.ID, ns.URN, ns.Provider)
if err != nil {
if err := s.repository.Rollback(ctx, err); err != nil {
return err
}
return err
}

for k, v := range labels {
encryptedNamespace.Labels[k] = v
}

if err := s.repository.Commit(ctx); err != nil {
return err
}
Expand Down Expand Up @@ -142,8 +147,7 @@ func (s *Service) Update(ctx context.Context, ns *Namespace) error {
}

ctx = s.repository.WithTransaction(ctx)
err = s.repository.Update(ctx, encryptedNamespace)
if err != nil {
if err = s.repository.Update(ctx, encryptedNamespace); err != nil {
if err := s.repository.Rollback(ctx, err); err != nil {
return err
}
Expand All @@ -159,13 +163,18 @@ func (s *Service) Update(ctx context.Context, ns *Namespace) error {
return err
}

if err := pluginService.SyncRuntimeConfig(ctx, encryptedNamespace.ID, ns.URN, ns.Provider); err != nil {
labels, err := pluginService.SyncRuntimeConfig(ctx, encryptedNamespace.ID, ns.URN, ns.Provider)
if err != nil {
if err := s.repository.Rollback(ctx, err); err != nil {
return err
}
return err
}

for k, v := range labels {
encryptedNamespace.Labels[k] = v
}

if err := s.repository.Commit(ctx); err != nil {
return err
}
Expand Down
24 changes: 23 additions & 1 deletion internal/store/postgres/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ WHERE id = $1
RETURNING *
`

const namespaceUpdateLabelQuery = `
UPDATE namespaces SET labels=$2, updated_at=now()
WHERE id = $1
RETURNING *
`

var namespaceListQueryBuilder = sq.Select(`
n.id as id,
n.urn as urn,
Expand Down Expand Up @@ -168,12 +174,28 @@ func (r NamespaceRepository) Update(ctx context.Context, ns *namespace.Encrypted
return nil
}

func (r NamespaceRepository) UpdateLabels(ctx context.Context, id uint64, labels map[string]string) error {
if len(labels) == 0 {
return nil
}
rows, err := r.client.QueryxContext(ctx, pgc.OpUpdate, r.tableName, namespaceUpdateLabelQuery, id, labels)
if err != nil {
err = pgc.CheckError(err)
if errors.Is(err, sql.ErrNoRows) {
return namespace.NotFoundError{ID: id}
}
return err
}
defer rows.Close()
return nil
}

func (r NamespaceRepository) Delete(ctx context.Context, id uint64) error {
rows, err := r.client.QueryxContext(ctx, pgc.OpDelete, r.tableName, namespaceDeleteQuery, id)
if err != nil {
return err
}
rows.Close()
defer rows.Close()
return nil
}

Expand Down
40 changes: 40 additions & 0 deletions internal/store/postgres/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package postgres_test

import (
"context"
"errors"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -401,6 +402,45 @@ func (s *NamespaceRepositoryTestSuite) TestUpdate() {
}
}

func (s *NamespaceRepositoryTestSuite) TestUpdateLabels() {
type testCase struct {
Description string
ID uint64
Labels map[string]string
ExpectedLabels map[string]string
Err error
}

var testCases = []testCase{
{
Description: "should update existing namespace label",
ID: 1,
Labels: map[string]string{
"k": "v",
},
Err: nil,
},
{
Description: "should return error not found if id not found",
ID: 1000,
Labels: map[string]string{},
Err: errors.New("namespace with id 1000 not found"),
},
{
ID: 1,
Description: "should return nil if label is empty",
Err: nil,
},
}

for _, tc := range testCases {
s.Run(tc.Description, func() {
err := s.repository.UpdateLabels(s.ctx, tc.ID, tc.Labels)
s.Assert().Equal(tc.Err, err)
})
}
}

func (s *NamespaceRepositoryTestSuite) TestDelete() {
type testCase struct {
Description string
Expand Down
10 changes: 5 additions & 5 deletions plugins/providers/cortex/v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s *PluginService) TransformToAlerts(ctx context.Context, providerID uint64
}

// SyncRuntimeConfig synchronizes runtime configuration of provider
func (s *PluginService) SyncRuntimeConfig(ctx context.Context, namespaceID uint64, namespaceURN string, prov provider.Provider) error {
func (s *PluginService) SyncRuntimeConfig(ctx context.Context, namespaceID uint64, namespaceURN string, prov provider.Provider) (map[string]string, error) {
webhookURL := fmt.Sprintf("%s/%d/%d", s.cfg.WebhookBaseAPI, prov.ID, namespaceID)

tmplConfig := TemplateConfig{
Expand All @@ -167,21 +167,21 @@ func (s *PluginService) SyncRuntimeConfig(ctx context.Context, namespaceID uint6

cfg, err := s.generateAlertmanagerConfig(tmplConfig)
if err != nil {
return err
return nil, err
}
templates := map[string]string{
"helper.tmpl": s.helperTemplate,
}

cortexClient, err := s.getCortexClient(prov.Host, namespaceURN)
if err != nil {
return err
return nil, err
}

if err = cortexClient.CreateAlertmanagerConfig(ctx, cfg, templates); err != nil {
return err
return nil, err
}
return nil
return make(map[string]string), nil
}

// UpsertRule manages upsert logic to cortex ruler. Cortex client API granularity is on the rule-group.
Expand Down
11 changes: 9 additions & 2 deletions plugins/providerv1beta1.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

type ProviderV1beta1 interface {
SyncRuntimeConfig(ctx context.Context, namespaceID uint64, namespaceURN string, prov provider.Provider) error
SyncRuntimeConfig(ctx context.Context, namespaceID uint64, namespaceURN string, prov provider.Provider) (map[string]string, error)
UpsertRule(ctx context.Context, ns namespace.Namespace, prov provider.Provider, rl *rule.Rule, templateToUpdate *template.Template) error
SetConfig(ctx context.Context, configRaw string) error
TransformToAlerts(ctx context.Context, providerID uint64, namespaceID uint64, body map[string]any) ([]alert.Alert, int, error)
Expand Down Expand Up @@ -132,7 +132,14 @@ func (s *GRPCServer) SyncRuntimeConfig(ctx context.Context, req *sirenproviderv1
return nil, errors.New("error parsing namespace ID")
}

return &sirenproviderv1beta1.SyncRuntimeConfigResponse{}, s.service.SyncRuntimeConfig(ctx, namespaceIDUint64, req.GetNamespaceUrn(), prov)
encrichedLabels, err := s.service.SyncRuntimeConfig(ctx, namespaceIDUint64, req.GetNamespaceUrn(), prov)
if err != nil {
return nil, err
}

return &sirenproviderv1beta1.SyncRuntimeConfigResponse{
Labels: encrichedLabels,
}, nil
}

func (s *GRPCServer) UpsertRule(ctx context.Context, req *sirenproviderv1beta1.UpsertRuleRequest) (*sirenproviderv1beta1.UpsertRuleResponse, error) {
Expand Down
Loading

0 comments on commit 2dbc037

Please sign in to comment.