Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify ServiceMonitor and PodMonitor are installed in prom cr availability check #2964

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading