Skip to content

Commit 797f660

Browse files
authored
Verify ServiceMonitor and PodMonitor are installed in prom cr availability check (#2964)
* Verify ServiceMonitor and PodMonitor are installed in prom cr availability check * Added changelog
1 parent 96debc4 commit 797f660

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: collector
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Ensure all Prometheus CRDs are installed
9+
10+
# One or more tracking issues related to the change
11+
issues: [2964]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

internal/autodetect/main.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,32 @@ func (a *autoDetect) PrometheusCRsAvailability() (prometheus.Availability, error
6565
return prometheus.NotAvailable, err
6666
}
6767

68+
foundServiceMonitor := false
69+
foundPodMonitor := false
6870
apiGroups := apiList.Groups
6971
for i := 0; i < len(apiGroups); i++ {
7072
if apiGroups[i].Name == "monitoring.coreos.com" {
71-
return prometheus.Available, nil
73+
for _, version := range apiGroups[i].Versions {
74+
resources, err := a.dcl.ServerResourcesForGroupVersion(version.GroupVersion)
75+
if err != nil {
76+
return prometheus.NotAvailable, err
77+
}
78+
79+
for _, resource := range resources.APIResources {
80+
if resource.Kind == "ServiceMonitor" {
81+
foundServiceMonitor = true
82+
} else if resource.Kind == "PodMonitor" {
83+
foundPodMonitor = true
84+
}
85+
}
86+
}
7287
}
7388
}
7489

90+
if foundServiceMonitor && foundPodMonitor {
91+
return prometheus.Available, nil
92+
}
93+
7594
return prometheus.NotAvailable, nil
7695
}
7796

internal/autodetect/main_test.go

+42-2
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,65 @@ func TestDetectPlatformBasedOnAvailableAPIGroups(t *testing.T) {
8585
func TestDetectPlatformBasedOnAvailableAPIGroupsPrometheus(t *testing.T) {
8686
for _, tt := range []struct {
8787
apiGroupList *metav1.APIGroupList
88+
resources *metav1.APIResourceList
8889
expected prometheus.Availability
8990
}{
9091
{
9192
&metav1.APIGroupList{},
93+
&metav1.APIResourceList{},
9294
prometheus.NotAvailable,
9395
},
9496
{
9597
&metav1.APIGroupList{
9698
Groups: []metav1.APIGroup{
9799
{
98-
Name: "monitoring.coreos.com",
100+
Name: "monitoring.coreos.com",
101+
Versions: []metav1.GroupVersionForDiscovery{{GroupVersion: "monitoring.coreos.com/v1"}},
99102
},
100103
},
101104
},
105+
&metav1.APIResourceList{
106+
APIResources: []metav1.APIResource{{Kind: "ServiceMonitor"}},
107+
},
108+
prometheus.NotAvailable,
109+
},
110+
{
111+
&metav1.APIGroupList{
112+
Groups: []metav1.APIGroup{
113+
{
114+
Name: "monitoring.coreos.com",
115+
Versions: []metav1.GroupVersionForDiscovery{{GroupVersion: "monitoring.coreos.com/v1"}},
116+
},
117+
},
118+
},
119+
&metav1.APIResourceList{
120+
APIResources: []metav1.APIResource{{Kind: "PodMonitor"}},
121+
},
122+
prometheus.NotAvailable,
123+
},
124+
{
125+
&metav1.APIGroupList{
126+
Groups: []metav1.APIGroup{
127+
{
128+
Name: "monitoring.coreos.com",
129+
Versions: []metav1.GroupVersionForDiscovery{{GroupVersion: "monitoring.coreos.com/v1"}},
130+
},
131+
},
132+
},
133+
&metav1.APIResourceList{
134+
APIResources: []metav1.APIResource{{Kind: "PodMonitor"}, {Kind: "ServiceMonitor"}},
135+
},
102136
prometheus.Available,
103137
},
104138
} {
105139
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
106-
output, err := json.Marshal(tt.apiGroupList)
140+
var output []byte
141+
var err error
142+
if req.URL.Path == "/apis" {
143+
output, err = json.Marshal(tt.apiGroupList)
144+
} else {
145+
output, err = json.Marshal(tt.resources)
146+
}
107147
require.NoError(t, err)
108148

109149
w.Header().Set("Content-Type", "application/json")

0 commit comments

Comments
 (0)