Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use commons http.Client for alertmanager #2355

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ ginkgo:
go install github.com/onsi/ginkgo/v2/ginkgo

.bin/controller-gen: .bin
GOBIN=$(PWD)/.bin go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.14.0
GOBIN=$(PWD)/.bin go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.16.5
CONTROLLER_GEN=$(GOBIN)/controller-gen

.bin/yq: .bin
Expand Down
47 changes: 28 additions & 19 deletions checks/alertmanager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package checks

import (
gocontext "context"
"encoding/json"
"fmt"
"net/url"
"strings"
Expand All @@ -10,7 +10,7 @@ import (
"github.com/flanksource/canary-checker/api/external"
v1 "github.com/flanksource/canary-checker/api/v1"
"github.com/flanksource/canary-checker/pkg"
alertmanagerClient "github.com/prometheus/alertmanager/api/v2/client"
"github.com/flanksource/commons/http"
alertmanagerAlert "github.com/prometheus/alertmanager/api/v2/client/alert"
)

Expand Down Expand Up @@ -39,37 +39,46 @@ func (c *AlertManagerChecker) Check(ctx *context.Context, extConfig external.Che
return results.Failf("error getting connection: %v", err)
}

parsedURL, err := url.Parse(connection.URL)
path, err := url.JoinPath(connection.URL, "/api/v2/alerts")
if err != nil {
return results.Failf("error parsing url: %v", err)
}
client := alertmanagerClient.NewHTTPClientWithConfig(nil, &alertmanagerClient.TransportConfig{
Host: parsedURL.Host,
Schemes: []string{parsedURL.Scheme},
BasePath: alertmanagerClient.DefaultBasePath,
})
var filters []string
results.ErrorMessage(fmt.Errorf("error joining url path: %v", err))
return results
}

req := http.NewClient().R(ctx)
for k, v := range check.Filters {
filters = append(filters, fmt.Sprintf("%s=~%s", k, v))
req.QueryParamAdd("filter", fmt.Sprintf("%s=~%s", k, v))
}
for _, alert := range check.Alerts {
filters = append(filters, fmt.Sprintf("alertname=~%s", alert))
req.QueryParamAdd("filter", fmt.Sprintf("alertname=~%s", alert))
}
for _, ignore := range check.Ignore {
filters = append(filters, fmt.Sprintf("alertname!~%s", ignore))
req.QueryParamAdd("filter", fmt.Sprintf("alertname!~%s", ignore))
}
for k, v := range check.ExcludeFilters {
filters = append(filters, fmt.Sprintf("%s!=%s", k, v))
req.QueryParamAdd("filter", fmt.Sprintf("%s!=%s", k, v))
}

alerts, err := client.Alert.GetAlerts(&alertmanagerAlert.GetAlertsParams{
Context: gocontext.Background(),
Filter: filters,
})
var alerts alertmanagerAlert.GetAlertsOK
resp, err := req.Get(path)
if err != nil {
results.ErrorMessage(fmt.Errorf("error fetching from alertmanager: %v", err))
return results
}
if !resp.IsOK() {
results.ErrorMessage(fmt.Errorf("received non 2xx from alertmanager: %v", err))
return results
}
respStr, err := resp.AsString()
if err != nil {
results.ErrorMessage(fmt.Errorf("error reading response from alertmanager: %v", err))
return results
}

if err := json.Unmarshal([]byte(respStr), &alerts.Payload); err != nil {
results.ErrorMessage(fmt.Errorf("error casting alertmanager response: %v", err))
return results
}

type Alerts struct {
Alerts []map[string]interface{} `json:"alerts,omitempty"`
Expand Down
16 changes: 11 additions & 5 deletions config/deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.16.5
name: canaries.canaries.flanksource.com
spec:
group: canaries.flanksource.com
Expand Down Expand Up @@ -4505,10 +4505,13 @@ spec:
description: The key to select.
type: string
name:
default: ""
description: |-
Name of the referent.
This field is effectively required, but due to backwards compatibility is
allowed to be empty. Instances of this type with an empty value here are
almost certainly wrong.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?
type: string
optional:
description: Specify whether the ConfigMap or its key must be defined
Expand Down Expand Up @@ -4539,10 +4542,13 @@ spec:
description: The key of the secret to select from. Must be a valid secret key.
type: string
name:
default: ""
description: |-
Name of the referent.
This field is effectively required, but due to backwards compatibility is
allowed to be empty. Instances of this type with an empty value here are
almost certainly wrong.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?
type: string
optional:
description: Specify whether the Secret or its key must be defined
Expand Down Expand Up @@ -13501,7 +13507,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.16.5
name: components.canaries.flanksource.com
spec:
group: canaries.flanksource.com
Expand Down Expand Up @@ -13959,7 +13965,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.16.5
name: topologies.canaries.flanksource.com
spec:
group: canaries.flanksource.com
Expand Down
16 changes: 11 additions & 5 deletions config/deploy/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.16.5
name: canaries.canaries.flanksource.com
spec:
group: canaries.flanksource.com
Expand Down Expand Up @@ -4504,10 +4504,13 @@ spec:
description: The key to select.
type: string
name:
default: ""
description: |-
Name of the referent.
This field is effectively required, but due to backwards compatibility is
allowed to be empty. Instances of this type with an empty value here are
almost certainly wrong.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?
type: string
optional:
description: Specify whether the ConfigMap or its key must be defined
Expand Down Expand Up @@ -4538,10 +4541,13 @@ spec:
description: The key of the secret to select from. Must be a valid secret key.
type: string
name:
default: ""
description: |-
Name of the referent.
This field is effectively required, but due to backwards compatibility is
allowed to be empty. Instances of this type with an empty value here are
almost certainly wrong.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?
type: string
optional:
description: Specify whether the Secret or its key must be defined
Expand Down Expand Up @@ -13769,7 +13775,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.16.5
name: components.canaries.flanksource.com
spec:
group: canaries.flanksource.com
Expand Down Expand Up @@ -14227,7 +14233,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.16.5
name: topologies.canaries.flanksource.com
spec:
group: canaries.flanksource.com
Expand Down
Loading
Loading