-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prometheus.operator.* - Fix issue with missing targets when one monit…
…or's name is a prefix of another (#5862) Co-authored-by: Paul Bormans <[email protected]>
- Loading branch information
1 parent
84344fb
commit f232fb4
Showing
4 changed files
with
224 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
component/prometheus/operator/common/crdmanager_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/exp/maps" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/grafana/agent/component" | ||
"github.com/grafana/agent/component/prometheus/operator" | ||
"github.com/grafana/agent/service/cluster" | ||
"github.com/grafana/agent/service/labelstore" | ||
"github.com/prometheus/prometheus/config" | ||
"github.com/prometheus/prometheus/discovery" | ||
"github.com/prometheus/prometheus/discovery/targetgroup" | ||
"github.com/prometheus/prometheus/scrape" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
|
||
promopv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestClearConfigsSameNsSamePrefix(t *testing.T) { | ||
logger := log.NewNopLogger() | ||
m := newCrdManager( | ||
component.Options{ | ||
Logger: logger, | ||
GetServiceData: func(name string) (interface{}, error) { return nil, nil }, | ||
}, | ||
cluster.Mock(), | ||
logger, | ||
&operator.DefaultArguments, | ||
KindServiceMonitor, | ||
labelstore.New(logger), | ||
) | ||
|
||
m.discoveryManager = newMockDiscoveryManager() | ||
m.scrapeManager = newMockScrapeManager() | ||
|
||
targetPort := intstr.FromInt(9090) | ||
m.onAddServiceMonitor(&promopv1.ServiceMonitor{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "monitoring", | ||
Name: "svcmonitor", | ||
}, | ||
Spec: promopv1.ServiceMonitorSpec{ | ||
Selector: metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"group": "my-group", | ||
}, | ||
}, | ||
Endpoints: []promopv1.Endpoint{ | ||
{ | ||
TargetPort: &targetPort, | ||
ScrapeTimeout: "5s", | ||
Interval: "10s", | ||
}, | ||
}, | ||
}, | ||
}) | ||
m.onAddServiceMonitor(&promopv1.ServiceMonitor{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "monitoring", | ||
Name: "svcmonitor-another", | ||
}, | ||
Spec: promopv1.ServiceMonitorSpec{ | ||
Selector: metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"group": "my-group", | ||
}, | ||
}, | ||
Endpoints: []promopv1.Endpoint{ | ||
{ | ||
TargetPort: &targetPort, | ||
ScrapeTimeout: "5s", | ||
Interval: "10s", | ||
}, | ||
}, | ||
}}) | ||
|
||
require.ElementsMatch(t, []string{"serviceMonitor/monitoring/svcmonitor-another/0", "serviceMonitor/monitoring/svcmonitor/0"}, maps.Keys(m.discoveryConfigs)) | ||
m.clearConfigs("monitoring", "svcmonitor") | ||
require.ElementsMatch(t, []string{"monitoring/svcmonitor", "monitoring/svcmonitor-another"}, maps.Keys(m.crdsToMapKeys)) | ||
require.ElementsMatch(t, []string{"serviceMonitor/monitoring/svcmonitor-another/0"}, maps.Keys(m.discoveryConfigs)) | ||
require.ElementsMatch(t, []string{"serviceMonitor/monitoring/svcmonitor-another"}, maps.Keys(m.debugInfo)) | ||
} | ||
|
||
func TestClearConfigsProbe(t *testing.T) { | ||
logger := log.NewNopLogger() | ||
m := newCrdManager( | ||
component.Options{ | ||
Logger: logger, | ||
GetServiceData: func(name string) (interface{}, error) { return nil, nil }, | ||
}, | ||
cluster.Mock(), | ||
logger, | ||
&operator.DefaultArguments, | ||
KindProbe, | ||
labelstore.New(logger), | ||
) | ||
|
||
m.discoveryManager = newMockDiscoveryManager() | ||
m.scrapeManager = newMockScrapeManager() | ||
|
||
m.onAddProbe(&promopv1.Probe{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "monitoring", | ||
Name: "probe", | ||
}, | ||
Spec: promopv1.ProbeSpec{}, | ||
}) | ||
m.onAddProbe(&promopv1.Probe{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "monitoring", | ||
Name: "probe-another", | ||
}, | ||
Spec: promopv1.ProbeSpec{}}) | ||
|
||
require.ElementsMatch(t, []string{"probe/monitoring/probe-another", "probe/monitoring/probe"}, maps.Keys(m.discoveryConfigs)) | ||
m.clearConfigs("monitoring", "probe") | ||
require.ElementsMatch(t, []string{"monitoring/probe", "monitoring/probe-another"}, maps.Keys(m.crdsToMapKeys)) | ||
require.ElementsMatch(t, []string{"probe/monitoring/probe-another"}, maps.Keys(m.discoveryConfigs)) | ||
require.ElementsMatch(t, []string{"probe/monitoring/probe-another"}, maps.Keys(m.debugInfo)) | ||
} | ||
|
||
type mockDiscoveryManager struct { | ||
} | ||
|
||
func newMockDiscoveryManager() *mockDiscoveryManager { | ||
return &mockDiscoveryManager{} | ||
} | ||
|
||
func (m *mockDiscoveryManager) Run() error { | ||
return nil | ||
} | ||
|
||
func (m *mockDiscoveryManager) SyncCh() <-chan map[string][]*targetgroup.Group { | ||
return nil | ||
} | ||
|
||
func (m *mockDiscoveryManager) ApplyConfig(cfg map[string]discovery.Configs) error { | ||
return nil | ||
} | ||
|
||
type mockScrapeManager struct { | ||
} | ||
|
||
func newMockScrapeManager() *mockScrapeManager { | ||
return &mockScrapeManager{} | ||
} | ||
|
||
func (m *mockScrapeManager) Run(tsets <-chan map[string][]*targetgroup.Group) error { | ||
return nil | ||
} | ||
|
||
func (m *mockScrapeManager) Stop() { | ||
|
||
} | ||
|
||
func (m *mockScrapeManager) TargetsActive() map[string][]*scrape.Target { | ||
return nil | ||
} | ||
|
||
func (m *mockScrapeManager) ApplyConfig(cfg *config.Config) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/prometheus/prometheus/config" | ||
"github.com/prometheus/prometheus/discovery" | ||
"github.com/prometheus/prometheus/discovery/targetgroup" | ||
"github.com/prometheus/prometheus/scrape" | ||
) | ||
|
||
// discoveryManager is an interface around discovery.Manager | ||
type discoveryManager interface { | ||
Run() error | ||
SyncCh() <-chan map[string][]*targetgroup.Group | ||
ApplyConfig(cfg map[string]discovery.Configs) error | ||
} | ||
|
||
// scrapeManager is an interface around scrape.Manager | ||
type scrapeManager interface { | ||
Run(tsets <-chan map[string][]*targetgroup.Group) error | ||
Stop() | ||
TargetsActive() map[string][]*scrape.Target | ||
ApplyConfig(cfg *config.Config) error | ||
} |