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 13596cb commit 2bddf42
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
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
10 changes: 7 additions & 3 deletions controllers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func updateDaemonsetNodeSelector(obj *uns.Unstructured, nodeSelector map[string]
return nil
}

func isPrometheusOperatorInstalled(ctx context.Context, client k8sclient.Client) (bool, error) {
func isPrometheusOperatorInstalled(ctx context.Context, client k8sclient.Reader) bool {
u := &unstructured.UnstructuredList{}
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "monitoring.coreos.com",
Expand All @@ -409,7 +409,11 @@ func isPrometheusOperatorInstalled(ctx context.Context, client k8sclient.Client)
})
err := client.List(ctx, u)
if err != nil {
return false, err
if errors.IsNotFound(err) {
return false
}
log.Log.WithName("isPrometheusOperatorInstalled").Error(err, "Error while looking for prometheus operator")
return false
}
return false, nil
return true
}
28 changes: 14 additions & 14 deletions controllers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ 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"
Expand All @@ -36,7 +38,6 @@ import (

sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/typed/monitoring/v1"
)

func TestNodeSelectorMerge(t *testing.T) {
Expand Down Expand Up @@ -342,26 +343,25 @@ var _ = Describe("Helper Validation", Ordered, func() {
Expect(isPrometheusOperatorInstalled(context.Background(), client)).To(BeTrue())
})

It("should return false if no monitoring.coreos.com resource is available in the cluster", func() {
scheme := runtime.NewScheme()
client := fake.NewClientBuilder().
WithScheme(scheme).
Build()
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())
})

})

})

func TestIsPrometheusOperatorSupported(t *testing.T) {
scheme := runtime.NewScheme()
fake.NewClientBuilder().
WithScheme(scheme).WithObjects(&nodeState).
Build()

assert
type errorReturningClient struct {
}

utilruntime.Must(sriovnetworkv1.AddToScheme(scheme))
func (e *errorReturningClient) Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error {

Check failure on line 361 in controllers/helper_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

undefined: ObjectKey

Check failure on line 361 in controllers/helper_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

undefined: Object

Check failure on line 361 in controllers/helper_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

undefined: GetOption

Check failure on line 361 in controllers/helper_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: ObjectKey

Check failure on line 361 in controllers/helper_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: Object

Check failure on line 361 in controllers/helper_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: GetOption
return fmt.Errorf("resource get error")
}

func (e *errorReturningClient) List(ctx context.Context, list ObjectList, opts ...ListOption) error {

Check failure on line 365 in controllers/helper_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

undefined: ObjectList

Check failure on line 365 in controllers/helper_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

undefined: ListOption (typecheck)

Check failure on line 365 in controllers/helper_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: ObjectList

Check failure on line 365 in controllers/helper_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: ListOption
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

0 comments on commit 2bddf42

Please sign in to comment.