github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/ruler/rulestore/configdb/store.go (about)

     1  package configdb
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/cortexproject/cortex/pkg/configs/client"
     8  	"github.com/cortexproject/cortex/pkg/configs/userconfig"
     9  	"github.com/cortexproject/cortex/pkg/ruler/rulespb"
    10  )
    11  
    12  const (
    13  	Name = "configdb"
    14  )
    15  
    16  // ConfigRuleStore is a concrete implementation of RuleStore that sources rules from the config service
    17  type ConfigRuleStore struct {
    18  	configClient  client.Client
    19  	since         userconfig.ID
    20  	ruleGroupList map[string]rulespb.RuleGroupList
    21  }
    22  
    23  func (c *ConfigRuleStore) SupportsModifications() bool {
    24  	return false
    25  }
    26  
    27  // NewConfigRuleStore constructs a ConfigRuleStore
    28  func NewConfigRuleStore(c client.Client) *ConfigRuleStore {
    29  	return &ConfigRuleStore{
    30  		configClient:  c,
    31  		since:         0,
    32  		ruleGroupList: make(map[string]rulespb.RuleGroupList),
    33  	}
    34  }
    35  
    36  func (c *ConfigRuleStore) ListAllUsers(ctx context.Context) ([]string, error) {
    37  	m, err := c.ListAllRuleGroups(ctx)
    38  
    39  	// TODO: this should be optimized, if possible.
    40  	result := []string(nil)
    41  	for u := range m {
    42  		result = append(result, u)
    43  	}
    44  
    45  	return result, err
    46  }
    47  
    48  // ListAllRuleGroups implements RuleStore
    49  func (c *ConfigRuleStore) ListAllRuleGroups(ctx context.Context) (map[string]rulespb.RuleGroupList, error) {
    50  	configs, err := c.configClient.GetRules(ctx, c.since)
    51  
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	for user, cfg := range configs {
    57  		userRules := rulespb.RuleGroupList{}
    58  		if cfg.IsDeleted() {
    59  			delete(c.ruleGroupList, user)
    60  			continue
    61  		}
    62  		rMap, err := cfg.Config.ParseFormatted()
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  		for file, rgs := range rMap {
    67  			for _, rg := range rgs.Groups {
    68  				userRules = append(userRules, rulespb.ToProto(user, file, rg))
    69  			}
    70  		}
    71  		c.ruleGroupList[user] = userRules
    72  	}
    73  
    74  	c.since = getLatestConfigID(configs, c.since)
    75  
    76  	return c.ruleGroupList, nil
    77  }
    78  
    79  // getLatestConfigID gets the latest configs ID.
    80  // max [latest, max (map getID cfgs)]
    81  func getLatestConfigID(cfgs map[string]userconfig.VersionedRulesConfig, latest userconfig.ID) userconfig.ID {
    82  	ret := latest
    83  	for _, config := range cfgs {
    84  		if config.ID > ret {
    85  			ret = config.ID
    86  		}
    87  	}
    88  	return ret
    89  }
    90  
    91  func (c *ConfigRuleStore) ListRuleGroupsForUserAndNamespace(ctx context.Context, userID string, namespace string) (rulespb.RuleGroupList, error) {
    92  	r, err := c.ListAllRuleGroups(ctx)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	if namespace == "" {
    98  		return r[userID], nil
    99  	}
   100  
   101  	list := r[userID]
   102  	for ix := 0; ix < len(list); {
   103  		if list[ix].GetNamespace() != namespace {
   104  			list = append(list[:ix], list[ix+1:]...)
   105  		} else {
   106  			ix++
   107  		}
   108  	}
   109  
   110  	return list, nil
   111  }
   112  
   113  func (c *ConfigRuleStore) LoadRuleGroups(ctx context.Context, groupsToLoad map[string]rulespb.RuleGroupList) error {
   114  	// Since ConfigRuleStore already Loads the rules in the List methods, there is nothing left to do here.
   115  	return nil
   116  }
   117  
   118  // GetRuleGroup is not implemented
   119  func (c *ConfigRuleStore) GetRuleGroup(ctx context.Context, userID, namespace, group string) (*rulespb.RuleGroupDesc, error) {
   120  	return nil, errors.New("not implemented by the config service rule store")
   121  }
   122  
   123  // SetRuleGroup is not implemented
   124  func (c *ConfigRuleStore) SetRuleGroup(ctx context.Context, userID, namespace string, group *rulespb.RuleGroupDesc) error {
   125  	return errors.New("not implemented by the config service rule store")
   126  }
   127  
   128  // DeleteRuleGroup is not implemented
   129  func (c *ConfigRuleStore) DeleteRuleGroup(ctx context.Context, userID, namespace string, group string) error {
   130  	return errors.New("not implemented by the config service rule store")
   131  }
   132  
   133  // DeleteNamespace is not implemented
   134  func (c *ConfigRuleStore) DeleteNamespace(ctx context.Context, userID, namespace string) error {
   135  	return errors.New("not implemented by the config service rule store")
   136  }