Skip to content

Commit

Permalink
feat: add prometheus alerts in support bundle
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Yu <jack.yu@suse.com>
  • Loading branch information
Yu-Jack committed Jan 25, 2024
1 parent a242356 commit 924ca40
Show file tree
Hide file tree
Showing 11 changed files with 1,388 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ require (
github.com/pborman/uuid v1.2.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_golang v1.12.1
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9q
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -555,6 +556,7 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mvdan/xurls v1.1.0/go.mod h1:tQlNn3BED8bE/15hnSL2HLkDeLWpNPAwtw7wkEq44oU=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J1GEMiLbxo1LJaP8RfCpH6pymGZus=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
Expand Down
49 changes: 46 additions & 3 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manager
import (
"archive/zip"
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
Expand All @@ -12,15 +13,14 @@ import (
"time"

"github.com/pkg/errors"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"

"github.com/rancher/wrangler/pkg/signals"

"github.com/rancher/support-bundle-kit/pkg/manager/client"
"github.com/rancher/support-bundle-kit/pkg/types"
"github.com/rancher/support-bundle-kit/pkg/utils"
Expand Down Expand Up @@ -119,6 +119,10 @@ func (m *SupportBundleManager) Run() error {
types.ManagerPhaseClusterBundle,
m.phaseCollectClusterBundle,
},
{
types.ManagerPhasePrometheusBundle,
m.phaseCollectPrometheusBundle,
},
{
types.ManagerPhaseNodeBundle,
m.phaseCollectNodeBundles,
Expand Down Expand Up @@ -209,6 +213,45 @@ func (m *SupportBundleManager) phaseCollectClusterBundle() error {
return nil
}

func (m *SupportBundleManager) phaseCollectPrometheusBundle() error {
pods, err := m.k8s.GetPodsListByLabels("cattle-monitoring-system", "app.kubernetes.io/name=prometheus")
if err != nil {
if apierrors.IsNotFound(err) {
logrus.Info("prometheus pods not found")
return nil
}

return errors.Wrap(err, "failed to get prometheus pods")
}

if len(pods.Items) != 1 {
return errors.Wrap(err, "multiple prometheus pods found")
}

targetPod := pods.Items[0]
p, err := utils.NewPrometheus(targetPod.Status.PodIP)
if err != nil {
logrus.Debugf("host: %s, port: %d", targetPod.Status.PodIP, utils.PrometheusPort)
return errors.Wrap(err, "failed to new prometheus")
}

alerts, err := p.GetAlerts(m.context)
if err != nil {
return errors.Wrap(err, "failed to get prometheus alert")
}

b, err := json.MarshalIndent(alerts, "", "\t")
if err != nil {
return errors.Wrap(err, "failed to marshal prometheus alert")
}

if err := os.WriteFile(fmt.Sprintf("%s/prometheus-alerts.json", m.getWorkingDir()), b, 0644); err != nil {
return errors.Wrap(err, "failed to write prometheus alert")
}

return nil
}

func (m *SupportBundleManager) phaseCollectNodeBundles() error {
err := m.collectNodeBundles()
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ const (
type ManagerPhase string

const (
ManagerPhaseInit = ManagerPhase("init")
ManagerPhaseClusterBundle = ManagerPhase("cluster bundle")
ManagerPhaseNodeBundle = ManagerPhase("node bundle")
ManagerPhasePackaging = ManagerPhase("package")
ManagerPhaseDone = ManagerPhase("done")
ManagerPhaseInit = ManagerPhase("init")
ManagerPhaseClusterBundle = ManagerPhase("cluster bundle")
ManagerPhasePrometheusBundle = ManagerPhase("prometheus bundle")
ManagerPhaseNodeBundle = ManagerPhase("node bundle")
ManagerPhasePackaging = ManagerPhase("package")
ManagerPhaseDone = ManagerPhase("done")
)

type ManagerStatus struct {
Expand Down
37 changes: 37 additions & 0 deletions pkg/utils/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package utils

import (
"context"
"fmt"

"github.com/prometheus/client_golang/api"
"github.com/prometheus/client_golang/api/prometheus/v1"
)

const PrometheusPort = 9090

type Prometheus struct {
api v1.API
}

func NewPrometheus(host string) (*Prometheus, error) {
client, err := api.NewClient(api.Config{
Address: fmt.Sprintf("http://%s:%d", host, PrometheusPort),
})

if err != nil {
return nil, err
}

return &Prometheus{api: v1.NewAPI(client)}, nil
}

func (p *Prometheus) GetAlerts(ctx context.Context) ([]v1.Alert, error) {
result, err := p.api.Alerts(ctx)

if err != nil {
return []v1.Alert{}, err
}

return result.Alerts, nil
}
131 changes: 131 additions & 0 deletions vendor/github.com/prometheus/client_golang/api/client.go

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

Loading

0 comments on commit 924ca40

Please sign in to comment.