forked from rook/rook
mgr: update default rook_cluster of ServiceMonitor
When deploying a cluster with Helm Chart, if the namespace is not the default value `rook-ceph`, and if the monitoring feature is enabled, then the generated ServiceMonitor's `rook_cluster` selector now follows the namespace, not the hard-coded value `rook-ceph`. Signed-off-by: Ho Kim <ho.kim@ulagbulag.io>
This commit is contained in:
@@ -19,7 +19,6 @@ package mgr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"github.com/banzaicloud/k8s-objectmatcher/patch"
|
||||
@@ -503,22 +502,16 @@ func wellKnownModule(name string) bool {
|
||||
|
||||
// EnableServiceMonitor add a servicemonitor that allows prometheus to scrape from the monitoring endpoint of the cluster
|
||||
func (c *Cluster) EnableServiceMonitor() error {
|
||||
serviceMonitor, err := k8sutil.GetServiceMonitor(path.Join(monitoringPath, serviceMonitorFile))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "service monitor could not be enabled")
|
||||
}
|
||||
serviceMonitor.SetName(AppName)
|
||||
serviceMonitor.SetNamespace(c.clusterInfo.Namespace)
|
||||
serviceMonitor := k8sutil.GetServiceMonitor(AppName, c.clusterInfo.Namespace)
|
||||
cephv1.GetMonitoringLabels(c.spec.Labels).OverwriteApplyToObjectMeta(&serviceMonitor.ObjectMeta)
|
||||
|
||||
if c.spec.External.Enable {
|
||||
serviceMonitor.Spec.Endpoints[0].Port = controller.ServiceExternalMetricName
|
||||
}
|
||||
err = c.clusterInfo.OwnerInfo.SetControllerReference(serviceMonitor)
|
||||
err := c.clusterInfo.OwnerInfo.SetControllerReference(serviceMonitor)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to set owner reference to service monitor %q", serviceMonitor.Name)
|
||||
}
|
||||
serviceMonitor.Spec.NamespaceSelector.MatchNames = []string{c.clusterInfo.Namespace}
|
||||
|
||||
applyMonitoringLabels(c, serviceMonitor)
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ package nodedaemon
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -221,19 +220,13 @@ func MakeCephExporterMetricsService(cephCluster cephv1.CephCluster, servicePortM
|
||||
|
||||
// EnableCephExporterServiceMonitor add a servicemonitor that allows prometheus to scrape from the monitoring endpoint of the exporter
|
||||
func EnableCephExporterServiceMonitor(cephCluster cephv1.CephCluster, scheme *runtime.Scheme, opManagerContext context.Context) error {
|
||||
serviceMonitor, err := k8sutil.GetServiceMonitor(path.Join(monitoringPath, serviceMonitorFile))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "service monitor could not be enabled")
|
||||
}
|
||||
serviceMonitor.SetName(cephExporterAppName)
|
||||
serviceMonitor.SetNamespace(cephCluster.Namespace)
|
||||
serviceMonitor := k8sutil.GetServiceMonitor(cephExporterAppName, cephCluster.Namespace)
|
||||
cephv1.GetCephExporterLabels(cephCluster.Spec.Labels).OverwriteApplyToObjectMeta(&serviceMonitor.ObjectMeta)
|
||||
|
||||
err = controllerutil.SetControllerReference(&cephCluster, serviceMonitor, scheme)
|
||||
err := controllerutil.SetControllerReference(&cephCluster, serviceMonitor, scheme)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to set owner reference to service monitor %q", serviceMonitor.Name)
|
||||
}
|
||||
serviceMonitor.Spec.NamespaceSelector.MatchNames = []string{cephCluster.Namespace}
|
||||
serviceMonitor.Spec.Selector.MatchLabels = controller.AppLabels(cephExporterAppName, cephCluster.Namespace)
|
||||
applyCephExporterLabels(cephCluster, serviceMonitor)
|
||||
|
||||
|
||||
@@ -18,17 +18,13 @@ limitations under the License.
|
||||
package k8sutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
|
||||
monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
|
||||
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
k8sYAML "k8s.io/apimachinery/pkg/util/yaml"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
@@ -44,18 +40,37 @@ func getMonitoringClient() (*monitoringclient.Clientset, error) {
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// GetServiceMonitor returns servicemonitor or an error
|
||||
func GetServiceMonitor(filePath string) (*monitoringv1.ServiceMonitor, error) {
|
||||
file, err := os.ReadFile(filepath.Clean(filePath))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("servicemonitor file could not be fetched. %v", err)
|
||||
// GetServiceMonitor creates serviceMonitor object template
|
||||
func GetServiceMonitor(name string, namespace string) *monitoringv1.ServiceMonitor {
|
||||
return &monitoringv1.ServiceMonitor{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{
|
||||
"team": "rook",
|
||||
},
|
||||
},
|
||||
Spec: monitoringv1.ServiceMonitorSpec{
|
||||
NamespaceSelector: monitoringv1.NamespaceSelector{
|
||||
MatchNames: []string{
|
||||
namespace,
|
||||
},
|
||||
},
|
||||
Selector: metav1.LabelSelector{
|
||||
MatchLabels: map[string]string{
|
||||
"app": name,
|
||||
"rook_cluster": namespace,
|
||||
},
|
||||
},
|
||||
Endpoints: []monitoringv1.Endpoint{
|
||||
{
|
||||
Port: "http-metrics",
|
||||
Path: "/metrics",
|
||||
Interval: "5s",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
var servicemonitor monitoringv1.ServiceMonitor
|
||||
err = k8sYAML.NewYAMLOrJSONDecoder(bytes.NewBufferString(string(file)), 1000).Decode(&servicemonitor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("servicemonitor could not be decoded. %v", err)
|
||||
}
|
||||
return &servicemonitor, nil
|
||||
}
|
||||
|
||||
// CreateOrUpdateServiceMonitor creates serviceMonitor object or an error
|
||||
|
||||
@@ -18,21 +18,19 @@ limitations under the License.
|
||||
package k8sutil
|
||||
|
||||
import (
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/rook/rook/pkg/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetServiceMonitor(t *testing.T) {
|
||||
projectRoot := util.PathToProjectRoot()
|
||||
filePath := path.Join(projectRoot, "/deploy/examples/monitoring/service-monitor.yaml")
|
||||
servicemonitor, err := GetServiceMonitor(filePath)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "rook-ceph-mgr", servicemonitor.GetName())
|
||||
assert.Equal(t, "rook-ceph", servicemonitor.GetNamespace())
|
||||
name := "rook-ceph-mgr"
|
||||
namespace := "rook-ceph"
|
||||
servicemonitor := GetServiceMonitor(name, namespace)
|
||||
assert.Equal(t, name, servicemonitor.GetName())
|
||||
assert.Equal(t, namespace, servicemonitor.GetNamespace())
|
||||
assert.NotNil(t, servicemonitor.GetLabels())
|
||||
assert.NotNil(t, servicemonitor.Spec.NamespaceSelector.MatchNames)
|
||||
assert.NotNil(t, servicemonitor.Spec.Selector.MatchLabels)
|
||||
assert.NotNil(t, servicemonitor.Spec.Endpoints)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user