Skip to content

Commit

Permalink
Point silence operator to mimir
Browse files Browse the repository at this point in the history
Signed-off-by: QuentinBisson <[email protected]>
  • Loading branch information
QuentinBisson committed Jun 22, 2023
1 parent 2b6ca91 commit 8006b06
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 24 deletions.
3 changes: 2 additions & 1 deletion flag/service/alertmanager/alertmanager.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package alertmanager

type AlertManager struct {
Address string
Address string
TenantId string
}
1 change: 1 addition & 0 deletions helm/silence-operator/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ data:
service:
alertmanager:
address: {{ .Values.alertmanagerAddress }}
tenantId: anonymous
kubernetes:
address: ''
inCluster: true
Expand Down
2 changes: 1 addition & 1 deletion helm/silence-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ image:
name: "giantswarm/silence-operator"
tag: "[[ .Version ]]"

alertmanagerAddress: http://alertmanager-operated.monitoring.svc:9093
alertmanagerAddress: http://mimir-gateway.mimir.svc:8080/alertmanager

pod:
user:
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func mainE(ctx context.Context) error {
daemonCommand.PersistentFlags().String(f.Service.Kubernetes.TLS.KeyFile, "", "Key file path to use to authenticate with Kubernetes.")

daemonCommand.PersistentFlags().String(f.Service.AlertManager.Address, "http://localhost:9093", "Alertmanager address used to create silences.")
daemonCommand.PersistentFlags().String(f.Service.AlertManager.TenantId, "anonymous", "Mimir alertmanager tenant id.")

err = newCommand.CobraCommand().Execute()
if err != nil {
Expand Down
32 changes: 11 additions & 21 deletions pkg/alertmanager/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@ import (
)

type Config struct {
Address string
Address string
TenantId string
}

type AlertManager struct {
address string

httpClient *http.Client
client *httpClient
}

func New(config Config) (*AlertManager, error) {
if config.Address == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.Address must not be empty", config)
}

httpClient := &http.Client{}
client := httpClient{
c: http.Client{},
tenantId: config.TenantId}

return &AlertManager{
address: config.Address,
httpClient: httpClient,
address: config.Address,
client: &client,
}, nil
}

Expand All @@ -58,7 +61,7 @@ func (am *AlertManager) CreateSilence(s *Silence) error {
return microerror.Mask(err)
}

resp, err := am.httpClient.Post(endpoint, "application/json", bytes.NewBuffer(jsonValues))
resp, err := am.client.Post(endpoint, "application/json", bytes.NewBuffer(jsonValues))
if err != nil {
return microerror.Mask(err)
}
Expand Down Expand Up @@ -97,12 +100,7 @@ func (am *AlertManager) ListSilences() ([]Silence, error) {

var silences []Silence

req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, microerror.Mask(err)
}

resp, err := am.httpClient.Do(req)
resp, err := am.client.Get(endpoint)
if err != nil {
return nil, microerror.Mask(err)
}
Expand Down Expand Up @@ -131,17 +129,9 @@ func (am *AlertManager) ListSilences() ([]Silence, error) {
}

func (am *AlertManager) DeleteSilenceByID(id string) error {

endpoint := fmt.Sprintf("%s/api/v2/silence/%s", am.address, id)

req, err := http.NewRequest("DELETE", endpoint, nil)
if err != nil {
return microerror.Mask(err)
}

req.Header.Add("Content-Type", "application/json")

resp, err := am.httpClient.Do(req)
resp, err := am.client.Delete(endpoint, "application/json")
if err != nil {
return microerror.Mask(err)
}
Expand Down
52 changes: 52 additions & 0 deletions pkg/alertmanager/mimirclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package alertmanager

import (
"io"
"net/http"
)

type httpClient struct {
c http.Client
tenantId string
}

func (c *httpClient) Get(url string) (resp *http.Response, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err

}

return c.Do(req)
}

func (c *httpClient) Post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err

}

req.Header.Set("Content-Type", contentType)

return c.Do(req)
}

func (c *httpClient) Delete(url, contentType string) (resp *http.Response, err error) {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return nil, err
}

req.Header.Add("Content-Type", contentType)

return c.Do(req)
}

func (c *httpClient) Do(req *http.Request) (*http.Response, error) {
if c.tenantId != "" {
req.Header.Add("X-Scope-OrgID", c.tenantId)
}
return c.c.Do(req)

}
4 changes: 3 additions & 1 deletion service/controller/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type SilenceConfig struct {
Logger micrologger.Logger

AlertManagerAddress string
AlertManagerTenant string
}

type Silence struct {
Expand Down Expand Up @@ -68,7 +69,8 @@ func newSilenceResources(config SilenceConfig) ([]resource.Interface, error) {
var amClient *alertmanager.AlertManager
{
amConfig := alertmanager.Config{
Address: config.AlertManagerAddress,
Address: config.AlertManagerAddress,
TenantId: config.AlertManagerTenant,
}

amClient, err = alertmanager.New(amConfig)
Expand Down
1 change: 1 addition & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func New(config Config) (*Service, error) {
Logger: config.Logger,

AlertManagerAddress: config.Viper.GetString(config.Flag.Service.AlertManager.Address),
AlertManagerTenant: config.Viper.GetString(config.Flag.Service.AlertManager.TenantId),
}

silenceController, err = controller.NewSilence(c)
Expand Down

0 comments on commit 8006b06

Please sign in to comment.