From c8a748a6973aef442e9965402836a7dba68f3b9e Mon Sep 17 00:00:00 2001 From: Patrick Oyarzun Date: Fri, 9 Dec 2022 17:04:12 -0600 Subject: [PATCH] Fix linter --- component/mimir/rules/diff.go | 14 ++++------- component/mimir/rules/diff_test.go | 1 - component/mimir/rules/events.go | 9 ++++---- component/mimir/rules/rules.go | 37 ++++++++++++++++++++++-------- pkg/mimir/client/client.go | 4 ++-- pkg/mimir/client/client_test.go | 1 - pkg/mimir/client/rules_test.go | 1 - 7 files changed, 39 insertions(+), 28 deletions(-) diff --git a/component/mimir/rules/diff.go b/component/mimir/rules/diff.go index 1a29233f221e..3d8f30e3897c 100644 --- a/component/mimir/rules/diff.go +++ b/component/mimir/rules/diff.go @@ -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 @@ -48,10 +45,7 @@ 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 } @@ -59,7 +53,7 @@ func diffRuleState(desired map[string][]mimirClient.RuleGroup, actual map[string 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{} @@ -100,7 +94,7 @@ desiredGroups: }) } - return diff, nil + return diff } func equalRuleGroups(a, b mimirClient.RuleGroup) bool { diff --git a/component/mimir/rules/diff_test.go b/component/mimir/rules/diff_test.go index 54da1a4bd8a0..517e0bb2a7da 100644 --- a/component/mimir/rules/diff_test.go +++ b/component/mimir/rules/diff_test.go @@ -158,7 +158,6 @@ func requireEqualRuleDiffs(t *testing.T, expected, actual map[string][]ruleGroup t.Logf("actual diff: %s", summarizeDiff(actualDiff)) t.Fail() } - } } } diff --git a/component/mimir/rules/events.go b/component/mimir/rules/events.go index 20ad767577b5..51a9678e2c8a 100644 --- a/component/mimir/rules/events.go +++ b/component/mimir/rules/events.go @@ -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 { @@ -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. diff --git a/component/mimir/rules/rules.go b/component/mimir/rules/rules.go index 4df1a5dcd361..63544a73b390 100644 --- a/component/mimir/rules/rules.go +++ b/component/mimir/rules/rules.go @@ -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" @@ -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, @@ -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 { @@ -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 @@ -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() { diff --git a/pkg/mimir/client/client.go b/pkg/mimir/client/client.go index 6e430004cc37..b7b2ce63466d 100644 --- a/pkg/mimir/client/client.go +++ b/pkg/mimir/client/client.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "errors" "fmt" "io" "net/http" @@ -11,7 +12,6 @@ import ( "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" @@ -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 diff --git a/pkg/mimir/client/client_test.go b/pkg/mimir/client/client_test.go index 0777def46276..5ef8a373519b 100644 --- a/pkg/mimir/client/client_test.go +++ b/pkg/mimir/client/client_test.go @@ -91,5 +91,4 @@ func TestBuildURL(t *testing.T) { require.Equal(t, tt.resultURL, req.URL.String()) }) } - } diff --git a/pkg/mimir/client/rules_test.go b/pkg/mimir/client/rules_test.go index fb22b15a7f84..4bd8f6ddf369 100644 --- a/pkg/mimir/client/rules_test.go +++ b/pkg/mimir/client/rules_test.go @@ -72,5 +72,4 @@ func TestMimirClient_X(t *testing.T) { require.Equal(t, tc.expURLPath, req.URL.EscapedPath()) }) } - }