Skip to content

Commit 093db06

Browse files
authored
Merge pull request #1592 from achetronic/feat/add-tenant-mimir-capabilities
feat: Add Mimir tenant capabilities
2 parents 385b1de + fd86772 commit 093db06

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

main.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var CLI struct {
6868
TLSCertFile string `default:"" help:"File containing the default x509 Certificate for HTTPS."`
6969
TLSPrivateKeyFile string `default:"" help:"File containing the default x509 private key matching --tls-cert-file."`
7070
TLSClientCAFile string `default:"" help:"File containing the CA certificate for the client"`
71-
MimirTenantIDs []string `default:"" help:"Mimir tenant IDs to query if multi-tenancy is enabled."`
71+
MimirOrgID string `default:"" help:"Mimir tenant ID to query if multi-tenancy is enabled."`
7272
} `cmd:"" help:"Runs Pyrra's API and UI."`
7373
Filesystem struct {
7474
ConfigFiles string `default:"/etc/pyrra/*.yaml" help:"The folder where Pyrra finds the config files to use. Any non yaml files will be ignored."`
@@ -86,8 +86,10 @@ var CLI struct {
8686
MimirURL *url.URL `default:"" help:"The URL to the Mimir API. If specified provisions rules via Mimir instead of Prometheus"`
8787
MimirPrometheusPrefix string `default:"prometheus" help:"The prefix for the Prometheus API in Mimir"`
8888
MimirBasicAuthUsername string `default:"" help:"The HTTP basic authentication username"`
89-
MimirBasicAuthPassword string `default:"" help:"The HTTP basic authentication password"`
9089
MimirWriteAlertingRules bool `default:"false" help:"If alerting rules should be provisioned to the Mimir Ruler."`
90+
MimirBasicAuthPassword string `default:"" help:"The HTTP basic authentication password"`
91+
MimirOrgID string `default:"" help:"Mimir tenant ID to query if multi-tenancy is enabled."`
92+
MimirDeploymentMode string `default:"standalone" help:"Mimir deployment mode. Possible values: standalone (default), distributed"`
9193
} `cmd:"" help:"Runs Pyrra's Kubernetes operator and backend for the API."`
9294
Generate struct {
9395
ConfigFiles string `default:"/etc/pyrra/*.yaml" help:"The folder where Pyrra finds the config files to use."`
@@ -132,12 +134,12 @@ func main() {
132134
if CLI.API.TLSClientCAFile != "" {
133135
clientConfig.TLSConfig = promconfig.TLSConfig{CAFile: CLI.API.TLSClientCAFile}
134136
}
135-
if len(CLI.API.MimirTenantIDs) > 0 {
136-
mimirHeaderValue := strings.Join(CLI.API.MimirTenantIDs, "|")
137+
138+
if strings.EqualFold(CLI.API.MimirOrgID, "") {
137139
clientConfig.HTTPHeaders = &promconfig.Headers{
138140
Headers: map[string]promconfig.Header{
139-
"X-Scope-OrgID": {
140-
Values: []string{mimirHeaderValue},
141+
mimir.TenantHeaderName: {
142+
Values: []string{CLI.API.MimirOrgID},
141143
},
142144
},
143145
}
@@ -190,6 +192,8 @@ func main() {
190192
PrometheusPrefix: CLI.Kubernetes.MimirPrometheusPrefix,
191193
BasicAuthUsername: CLI.Kubernetes.MimirBasicAuthUsername,
192194
BasicAuthPassword: CLI.Kubernetes.MimirBasicAuthPassword,
195+
OrgID: CLI.Kubernetes.MimirOrgID,
196+
DeploymentMode: CLI.Kubernetes.MimirDeploymentMode,
193197
}
194198

195199
mimirClient, err = mimir.NewClient(mimirConfig)

mimir/client.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import (
88
"net/url"
99
)
1010

11+
const TenantHeaderName = "X-Scope-OrgID"
12+
1113
// Client is a simple client for the required Mimir API resources.
1214
type Client struct {
1315
client http.Client
1416
address *url.URL
1517
prometheusPrefix string
18+
orgID string
19+
deploymentMode string
1620
}
1721

1822
// Config is used to configure the client.
@@ -21,6 +25,8 @@ type Config struct {
2125
PrometheusPrefix string
2226
BasicAuthUsername string
2327
BasicAuthPassword string
28+
OrgID string
29+
DeploymentMode string
2430
}
2531

2632
// NewClient creates a new client with the given configuration.
@@ -48,6 +54,8 @@ func NewClient(config Config) (*Client, error) {
4854
client: httpClient,
4955
address: addr,
5056
prometheusPrefix: config.PrometheusPrefix,
57+
orgID: config.OrgID,
58+
deploymentMode: config.DeploymentMode,
5159
}, nil
5260
}
5361

@@ -68,11 +76,19 @@ func (t *BasicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error
6876
func (c *Client) Ready(ctx context.Context) error {
6977
path := c.address.JoinPath("/ready")
7078

79+
if c.deploymentMode == "distributed" {
80+
path = c.address.JoinPath("/api/v1/status/buildinfo")
81+
}
82+
7183
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
7284
if err != nil {
7385
return err
7486
}
7587

88+
if c.orgID != "" {
89+
req.Header.Set(TenantHeaderName, c.orgID)
90+
}
91+
7692
resp, err := c.client.Do(req)
7793
if err != nil {
7894
return err

mimir/rulegroup.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func (c *Client) SetRuleGroup(ctx context.Context, namespace string, ruleGroup r
2727

2828
req.Header.Set("Content-Type", "application/yaml")
2929

30+
if c.orgID != "" {
31+
req.Header.Set(TenantHeaderName, c.orgID)
32+
}
33+
3034
resp, err := c.client.Do(req)
3135
if err != nil {
3236
return err
@@ -49,6 +53,10 @@ func (c *Client) DeleteNamespace(ctx context.Context, namespace string) error {
4953
return err
5054
}
5155

56+
if c.orgID != "" {
57+
req.Header.Set(TenantHeaderName, c.orgID)
58+
}
59+
5260
resp, err := c.client.Do(req)
5361
if err != nil {
5462
return err

0 commit comments

Comments
 (0)