Skip to content

Commit

Permalink
small bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandreLamarre committed Oct 21, 2022
1 parent 21a105b commit c068a64
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
5 changes: 3 additions & 2 deletions pkg/alerting/backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package backend

import (
"fmt"
"time"

corev1 "github.com/rancher/opni/pkg/apis/core/v1"
alertingv1alpha "github.com/rancher/opni/plugins/alerting/pkg/apis/common"
"time"
)

// PostableAlert : corresponds to the data AlertManager API
Expand Down Expand Up @@ -82,7 +83,7 @@ func (p *PostableSilence) WithCondition(conditionId string) {
if p.Matchers == nil {
p.Matchers = make([]Matcher, 0)
}
p.Matchers = append(p.Matchers, Matcher{Name: conditionId})
p.Matchers = append(p.Matchers, Matcher{Name: "conditionId", Value: conditionId})
}

func (p *PostableSilence) WithDuration(dur time.Duration) {
Expand Down
9 changes: 6 additions & 3 deletions pkg/alerting/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func WithLogger(logger *zap.SugaredLogger) AlertManagerApiOption {
func WithPostSilenceBody(conditionId string, duration time.Duration, silenceId *string) AlertManagerApiOption {
return func(o *AlertManagerApiOptions) {
p := &PostableSilence{}
p.CreatedBy = "opni admin"
p.Comment = "silence created by opni admin"
p.WithCondition(conditionId)
p.WithDuration(duration)
if err := p.Must(); err != nil {
Expand Down Expand Up @@ -204,6 +206,7 @@ func (a *AlertManagerAPI) doRequest() error {
)
return err
}
defer resp.Body.Close()
if err := a.expectClosure(resp); err != nil {
lg.Error(
zap.Error(err),
Expand Down Expand Up @@ -306,7 +309,7 @@ func NewAlertManagerPostSilenceClient(endpoint string, ctx context.Context, opts
return (&AlertManagerAPI{
AlertManagerApiOptions: options,
Endpoint: endpoint,
Route: "/silence",
Route: "/silences",
Verb: POST,
ctx: ctx,
}).WithAPIV2()
Expand All @@ -318,7 +321,7 @@ func NewAlertManagerGetSilenceClient(endpoint string, ctx context.Context, opts
return (&AlertManagerAPI{
AlertManagerApiOptions: options,
Endpoint: endpoint,
Route: "/silence",
Route: "/silences",
Verb: GET,
ctx: ctx,
}).WithAPIV2()
Expand All @@ -333,7 +336,7 @@ func NewAlertManagerDeleteSilenceClient(endpoint, silenceId string, ctx context.
return (&AlertManagerAPI{
AlertManagerApiOptions: options,
Endpoint: endpoint,
Route: fmt.Sprintf("/silence/%s", silenceId),
Route: fmt.Sprintf("/silences/%s", silenceId),
Verb: DELETE,
ctx: ctx,
}).WithAPIV2()
Expand Down
1 change: 1 addition & 0 deletions plugins/alerting/pkg/alerting/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (p *Plugin) TriggerAlerts(ctx context.Context, req *alertingv1alpha.Trigger
ctx,
backend.WithLogger(lg),
backend.WithExpectClosure(backend.NewExpectStatusOk()),
backend.WithPostAlertBody(req.ConditionId.Id, req.Annotations),
)
err = apiNode.DoRequest()
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions plugins/alerting/pkg/alerting/api_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/rancher/opni/plugins/alerting/pkg/apis/common"
"io/ioutil"
"io"
"math/rand"
"net/http"
"time"

"github.com/rancher/opni/plugins/alerting/pkg/apis/common"

"github.com/prometheus/alertmanager/api/v2/models"
"github.com/rancher/opni/pkg/alerting/backend"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -186,7 +187,8 @@ func (p *Plugin) AlertConditionStatus(ctx context.Context, ref *corev1.Reference
if err != nil {
return nil, err
}
var result gjson.Result
//FIXME : doesn't update because it's not a pointer
var resultBytes []byte
apiNode := backend.NewAlertManagerGetAlertsClient(
availableEndpoint,
ctx,
Expand All @@ -195,19 +197,20 @@ func (p *Plugin) AlertConditionStatus(ctx context.Context, ref *corev1.Reference
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
lg.Errorf("failed to read body : %s", err)
return err
}
result = gjson.Get(string(b), "")
resultBytes = b
return nil
}),
)
err = apiNode.DoRequest()
if err != nil {
return nil, err
}
result := gjson.Get(string(resultBytes), "")
if !result.Exists() {
return defaultState, nil
}
Expand Down

0 comments on commit c068a64

Please sign in to comment.