Skip to content

Commit

Permalink
prometheus.operator.*, simplify cluster update logic. (#4852)
Browse files Browse the repository at this point in the history
* prometheus.operator.* simplify logic for cluster updates

* remove uneeded extra variable
  • Loading branch information
captncraig authored Aug 18, 2023
1 parent d1052db commit c913b01
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 48 deletions.
38 changes: 12 additions & 26 deletions component/prometheus/operator/common/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (c *Component) Run(ctx context.Context) error {
}
}()

var runningConfig *operator.Arguments
c.reportHealth(nil)
errChan := make(chan error, 1)
for {
Expand All @@ -64,28 +63,19 @@ func (c *Component) Run(ctx context.Context) error {
case err := <-errChan:
c.reportHealth(err)
case <-c.onUpdate:

c.mut.Lock()
nextConfig := c.config
// only restart crd manager if our config has changed.
// NOT on cluster changes.
if !nextConfig.Equals(runningConfig) {
runningConfig = nextConfig
manager := newCrdManager(c.opts, c.opts.Logger, nextConfig, c.kind)
c.manager = manager
if cancel != nil {
cancel()
}
innerCtx, cancel = context.WithCancel(ctx)
go func() {
if err := manager.Run(innerCtx); err != nil {
level.Error(c.opts.Logger).Log("msg", "error running crd manager", "err", err)
errChan <- err
}
}()
} else {
c.manager.ClusteringUpdated()
manager := newCrdManager(c.opts, c.opts.Logger, c.config, c.kind)
c.manager = manager
if cancel != nil {
cancel()
}
innerCtx, cancel = context.WithCancel(ctx)
go func() {
if err := manager.Run(innerCtx); err != nil {
level.Error(c.opts.Logger).Log("msg", "error running crd manager", "err", err)
errChan <- err
}
}()
c.mut.Unlock()
}
}
Expand Down Expand Up @@ -115,11 +105,7 @@ func (c *Component) NotifyClusterChange() {
return // no-op
}

// Schedule a reload so targets get redistributed.
select {
case c.onUpdate <- struct{}{}:
default:
}
c.manager.ClusteringUpdated()
}

// DebugInfo returns debug information for this component.
Expand Down
5 changes: 0 additions & 5 deletions component/prometheus/operator/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package operator

import (
"reflect"
"time"

"github.com/grafana/agent/component/common/config"
Expand Down Expand Up @@ -30,10 +29,6 @@ type Arguments struct {
RelabelConfigs []*flow_relabel.Config `river:"rule,block,optional"`
}

func (a *Arguments) Equals(b *Arguments) bool {
return reflect.DeepEqual(a, b)
}

// Clustering holds values that configure clustering-specific behavior.
type Clustering struct {
// TODO(@tpaschalis) Move this block to a shared place for all components using clustering.
Expand Down
17 changes: 0 additions & 17 deletions component/prometheus/operator/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,3 @@ func TestRiverUnmarshal(t *testing.T) {
err := river.Unmarshal([]byte(exampleRiverConfig), &args)
require.NoError(t, err)
}

func TestEqual(t *testing.T) {
a := Arguments{
Namespaces: []string{"my-app"},
Clustering: Clustering{Enabled: true},
}
b := Arguments{
Namespaces: []string{"my-app"},
Clustering: Clustering{Enabled: true},
}
c := Arguments{
Namespaces: []string{"my-app", "other-app"},
Clustering: Clustering{Enabled: false},
}
require.True(t, a.Equals(&b))
require.False(t, a.Equals(&c))
}

0 comments on commit c913b01

Please sign in to comment.