Skip to content

Commit

Permalink
Fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Logiraptor committed Dec 13, 2022
1 parent a012337 commit c8a748a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 28 deletions.
14 changes: 4 additions & 10 deletions component/mimir/rules/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ func diffRuleState(desired map[string][]mimirClient.RuleGroup, actual map[string
seen[namespace] = true

actualRuleGroups := actual[namespace]
subDiff, err := diffRuleNamespaceState(desiredRuleGroups, actualRuleGroups)
if err != nil {
return nil, err
}
subDiff := diffRuleNamespaceState(desiredRuleGroups, actualRuleGroups)

if len(subDiff) == 0 {
continue
Expand All @@ -48,18 +45,15 @@ func diffRuleState(desired map[string][]mimirClient.RuleGroup, actual map[string
continue
}

subDiff, err := diffRuleNamespaceState(nil, actualRuleGroups)
if err != nil {
return nil, err
}
subDiff := diffRuleNamespaceState(nil, actualRuleGroups)

diff[namespace] = subDiff
}

return diff, nil
}

func diffRuleNamespaceState(desired []mimirClient.RuleGroup, actual []mimirClient.RuleGroup) ([]ruleGroupDiff, error) {
func diffRuleNamespaceState(desired []mimirClient.RuleGroup, actual []mimirClient.RuleGroup) []ruleGroupDiff {
var diff []ruleGroupDiff

seenGroups := map[string]bool{}
Expand Down Expand Up @@ -100,7 +94,7 @@ desiredGroups:
})
}

return diff, nil
return diff
}

func equalRuleGroups(a, b mimirClient.RuleGroup) bool {
Expand Down
1 change: 0 additions & 1 deletion component/mimir/rules/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ func requireEqualRuleDiffs(t *testing.T, expected, actual map[string][]ruleGroup
t.Logf("actual diff: %s", summarizeDiff(actualDiff))
t.Fail()
}

}
}
}
9 changes: 5 additions & 4 deletions component/mimir/rules/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ func (c *Component) reconcileState(ctx context.Context) error {
defer cancel()

desiredState, err := c.loadStateFromK8s()
if err != nil {
return err
}

diffs, err := diffRuleState(desiredState, c.currentState)
if err != nil {
Expand Down Expand Up @@ -238,16 +241,14 @@ func (c *Component) applyChanges(ctx context.Context, namespace string, diffs []
}

// resync mimir state after applying changes
c.syncMimir(ctx)

return nil
return c.syncMimir(ctx)
}

// mimirNamespaceForRuleCRD returns the namespace that the rule CRD should be
// stored in mimir. This function, along with isManagedNamespace, is used to
// determine if a rule CRD is managed by the agent.
func mimirNamespaceForRuleCRD(prefix string, pr *promv1.PrometheusRule) string {
return fmt.Sprintf("agent/%s/%s/%s", pr.Namespace, pr.Name, pr.UID)
return fmt.Sprintf("%s/%s/%s/%s", prefix, pr.Namespace, pr.Name, pr.UID)
}

// isManagedMimirNamespace returns true if the namespace is managed by the agent.
Expand Down
37 changes: 28 additions & 9 deletions component/mimir/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/go-kit/log/level"
"github.com/grafana/agent/component"
mimirClient "github.com/grafana/agent/pkg/mimir/client"
"github.com/pkg/errors"
promListers "github.com/prometheus-operator/prometheus-operator/pkg/client/listers/monitoring/v1"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/common/instrument"
Expand Down Expand Up @@ -131,7 +130,10 @@ var _ component.HealthComponent = (*Component)(nil)

func NewComponent(o component.Options, args Arguments) (*Component, error) {
metrics := newMetrics()
metrics.Register(o.Registerer)
err := metrics.Register(o.Registerer)
if err != nil {
return nil, fmt.Errorf("registering metrics failed: %w", err)
}

c := &Component{
log: o.Logger,
Expand All @@ -142,16 +144,20 @@ func NewComponent(o component.Options, args Arguments) (*Component, error) {
metrics: metrics,
}

err := c.init()
err = c.init()
if err != nil {
return nil, errors.Wrap(err, "initializing component")
return nil, fmt.Errorf("initializing component failed: %w", err)
}

return c, nil
}

func (c *Component) Run(ctx context.Context) error {
c.startup(ctx)
err := c.startup(ctx)
if err != nil {
level.Error(c.log).Log("msg", "starting up component failed", "err", err)
c.reportUnhealthy(err)
}

for {
select {
Expand All @@ -161,13 +167,22 @@ func (c *Component) Run(ctx context.Context) error {

c.args = update.args
err := c.init()
update.err <- err
if err != nil {
level.Error(c.log).Log("msg", "updating configuration failed", "err", err)
c.reportUnhealthy(err)
update.err <- err
continue
}

c.startup(ctx)
err = c.startup(ctx)
if err != nil {
level.Error(c.log).Log("msg", "updating configuration failed", "err", err)
c.reportUnhealthy(err)
update.err <- err
continue
}

update.err <- nil
case <-ctx.Done():
c.shutdown()
return nil
Expand All @@ -180,14 +195,18 @@ func (c *Component) Run(ctx context.Context) error {
}

// startup launches the informers and starts the event loop.
func (c *Component) startup(ctx context.Context) {
func (c *Component) startup(ctx context.Context) error {
c.queue = workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "mimir.rules.kubernetes")
c.informerStopChan = make(chan struct{})

c.startNamespaceInformer()
c.startRuleInformer()
c.syncMimir(ctx)
err := c.syncMimir(ctx)
if err != nil {
return err
}
go c.eventLoop(ctx)
return nil
}

func (c *Component) shutdown() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/mimir/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"

log "github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/config"
weaveworksClient "github.com/weaveworks/common/http/client"
Expand Down Expand Up @@ -98,7 +98,7 @@ func (r *MimirClient) doRequest(operation, path, method string, payload []byte)

if err := checkResponse(resp); err != nil {
_ = resp.Body.Close()
return nil, errors.Wrapf(err, "%s request to %s failed", req.Method, req.URL.String())
return nil, fmt.Errorf("error %s %s: %w", method, path, err)
}

return resp, nil
Expand Down
1 change: 0 additions & 1 deletion pkg/mimir/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,4 @@ func TestBuildURL(t *testing.T) {
require.Equal(t, tt.resultURL, req.URL.String())
})
}

}
1 change: 0 additions & 1 deletion pkg/mimir/client/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,4 @@ func TestMimirClient_X(t *testing.T) {
require.Equal(t, tc.expURLPath, req.URL.EscapedPath())
})
}

}

0 comments on commit c8a748a

Please sign in to comment.