Skip to content

Commit

Permalink
Verify ServiceMonitor and PodMonitor are installed in prom cr availab…
Browse files Browse the repository at this point in the history
…ility check (#2964)

* Verify ServiceMonitor and PodMonitor are installed in prom cr availability check

* Added changelog
  • Loading branch information
akselleirv authored May 17, 2024
1 parent 96debc4 commit 797f660
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
16 changes: 16 additions & 0 deletions .chloggen/verify-prom-crd-resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: collector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Ensure all Prometheus CRDs are installed

# One or more tracking issues related to the change
issues: [2964]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
21 changes: 20 additions & 1 deletion internal/autodetect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,32 @@ func (a *autoDetect) PrometheusCRsAvailability() (prometheus.Availability, error
return prometheus.NotAvailable, err
}

foundServiceMonitor := false
foundPodMonitor := false
apiGroups := apiList.Groups
for i := 0; i < len(apiGroups); i++ {
if apiGroups[i].Name == "monitoring.coreos.com" {
return prometheus.Available, nil
for _, version := range apiGroups[i].Versions {
resources, err := a.dcl.ServerResourcesForGroupVersion(version.GroupVersion)
if err != nil {
return prometheus.NotAvailable, err
}

for _, resource := range resources.APIResources {
if resource.Kind == "ServiceMonitor" {
foundServiceMonitor = true
} else if resource.Kind == "PodMonitor" {
foundPodMonitor = true
}
}
}
}
}

if foundServiceMonitor && foundPodMonitor {
return prometheus.Available, nil
}

return prometheus.NotAvailable, nil
}

Expand Down
44 changes: 42 additions & 2 deletions internal/autodetect/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,65 @@ func TestDetectPlatformBasedOnAvailableAPIGroups(t *testing.T) {
func TestDetectPlatformBasedOnAvailableAPIGroupsPrometheus(t *testing.T) {
for _, tt := range []struct {
apiGroupList *metav1.APIGroupList
resources *metav1.APIResourceList
expected prometheus.Availability
}{
{
&metav1.APIGroupList{},
&metav1.APIResourceList{},
prometheus.NotAvailable,
},
{
&metav1.APIGroupList{
Groups: []metav1.APIGroup{
{
Name: "monitoring.coreos.com",
Name: "monitoring.coreos.com",
Versions: []metav1.GroupVersionForDiscovery{{GroupVersion: "monitoring.coreos.com/v1"}},
},
},
},
&metav1.APIResourceList{
APIResources: []metav1.APIResource{{Kind: "ServiceMonitor"}},
},
prometheus.NotAvailable,
},
{
&metav1.APIGroupList{
Groups: []metav1.APIGroup{
{
Name: "monitoring.coreos.com",
Versions: []metav1.GroupVersionForDiscovery{{GroupVersion: "monitoring.coreos.com/v1"}},
},
},
},
&metav1.APIResourceList{
APIResources: []metav1.APIResource{{Kind: "PodMonitor"}},
},
prometheus.NotAvailable,
},
{
&metav1.APIGroupList{
Groups: []metav1.APIGroup{
{
Name: "monitoring.coreos.com",
Versions: []metav1.GroupVersionForDiscovery{{GroupVersion: "monitoring.coreos.com/v1"}},
},
},
},
&metav1.APIResourceList{
APIResources: []metav1.APIResource{{Kind: "PodMonitor"}, {Kind: "ServiceMonitor"}},
},
prometheus.Available,
},
} {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
output, err := json.Marshal(tt.apiGroupList)
var output []byte
var err error
if req.URL.Path == "/apis" {
output, err = json.Marshal(tt.apiGroupList)
} else {
output, err = json.Marshal(tt.resources)
}
require.NoError(t, err)

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

0 comments on commit 797f660

Please sign in to comment.