Skip to content

Commit

Permalink
Deploy ServiceMonitor when Promethueus operator installed
Browse files Browse the repository at this point in the history
Instead of checking if we are running on an OpenShift cluster, check
if the `monitoring.coreos.com/v1` apis are available. This way,
the operator will configure prometheus depending on the
availability of the Prometheus operator.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>
  • Loading branch information
zeeke committed May 28, 2024
1 parent 68a104e commit e87a82d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bindata/manifests/metrics-exporter/metrics-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
name: sriov-network-metrics
port: {{ .MetricsExporterPort }}
targetPort: {{ .MetricsExporterPort }}
{{ if .IsOpenshift }}
{{ if .IsPrometheusOperatorInstalled }}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
Expand Down
19 changes: 19 additions & 0 deletions controllers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
kscheme "k8s.io/client-go/kubernetes/scheme"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -397,3 +398,21 @@ func updateDaemonsetNodeSelector(obj *uns.Unstructured, nodeSelector map[string]
}
return nil
}

func isPrometheusOperatorInstalled(ctx context.Context, client k8sclient.Reader) bool {
u := &uns.UnstructuredList{}
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "monitoring.coreos.com",
Kind: "ServiceMonitorList",
Version: "v1",
})
err := client.List(ctx, u)
if err != nil {
if errors.IsNotFound(err) {
return false
}
log.Log.WithName("isPrometheusOperatorInstalled").Error(err, "Error while looking for prometheus operator")
return false
}
return true
}
38 changes: 38 additions & 0 deletions controllers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@ package controllers

import (
"context"
"fmt"
"sync"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/google/go-cmp/cmp"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
controllerruntime "sigs.k8s.io/controller-runtime"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
Expand Down Expand Up @@ -327,4 +333,36 @@ var _ = Describe("Helper Validation", Ordered, func() {
Expect(in.Spec.Template.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[0].Key).To(Equal("test1"))
})
})

Context("isPrometheusOperatorInstalled", func() {
It("should return true if ServiceMonitor resource is available in the cluster", func() {
scheme := runtime.NewScheme()
utilruntime.Must(monitoringv1.AddToScheme(scheme))
client := fake.NewClientBuilder().
WithScheme(scheme).
Build()
Expect(isPrometheusOperatorInstalled(context.Background(), client)).To(BeTrue())
})

It("should return false if no monitoring.coreos.com resource is not available in the cluster", func() {
// There is no way to instruct a fake client to return an error while listing unknown resources
// so we use a client that returns an error on get and list
// https://github.com/k8snetworkplumbingwg/sriov-network-operator/blob/8d32f42b42b59a27864043ad22afa3741f197d9a/vendor/sigs.k8s.io/controller-runtime/pkg/client/fake/client.go#L513
client := &errorReturningClient{}
Expect(isPrometheusOperatorInstalled(context.Background(), client)).To(BeFalse())
})

})

})

type errorReturningClient struct {
}

func (e *errorReturningClient) Get(ctx context.Context, key k8sclient.ObjectKey, obj k8sclient.Object, opts ...k8sclient.GetOption) error {
return fmt.Errorf("resource get error")
}

func (e *errorReturningClient) List(ctx context.Context, list k8sclient.ObjectList, opts ...k8sclient.ListOption) error {
return fmt.Errorf("resource list error")
}
1 change: 1 addition & 0 deletions controllers/sriovoperatorconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (r *SriovOperatorConfigReconciler) syncMetricsExporter(ctx context.Context,
data.Data["MetricsExporterPort"] = os.Getenv("METRICS_EXPORTER_PORT")
data.Data["MetricsExporterKubeRbacProxyImage"] = os.Getenv("METRICS_EXPORTER_KUBE_RBAC_PROXY_IMAGE")
data.Data["IsOpenshift"] = r.PlatformHelper.IsOpenshiftCluster()
data.Data["IsPrometheusOperatorInstalled"] = isPrometheusOperatorInstalled(ctx, r.Client)
data.Data["NodeSelectorField"] = GetDefaultNodeSelector()
if dc.Spec.ConfigDaemonNodeSelector != nil {
data.Data["NodeSelectorField"] = dc.Spec.ConfigDaemonNodeSelector
Expand Down
3 changes: 3 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
openshiftconfigv1 "github.com/openshift/api/config/v1"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"

//+kubebuilder:scaffold:imports
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
Expand Down Expand Up @@ -153,6 +154,8 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
err = openshiftconfigv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
err = monitoringv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

vars.Config = cfg
vars.Scheme = scheme.Scheme
Expand Down
1 change: 1 addition & 0 deletions deploy/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rules:
verbs:
- get
- create
- list
- apiGroups:
- apps
resourceNames:
Expand Down
1 change: 1 addition & 0 deletions deployment/sriov-network-operator/templates/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ rules:
verbs:
- get
- create
- list
- apiGroups:
- apps
resourceNames:
Expand Down

0 comments on commit e87a82d

Please sign in to comment.