Skip to content
Open
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
3 changes: 3 additions & 0 deletions api/doris/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type DorisClusterSpec struct {
// the password key is `password`. the username defaults to `root` and is omitempty.
AuthSecret string `json:"authSecret,omitempty"`

// EnableServiceMonitor creates a Secret with prometheus-additional.yaml for Prometheus additionalScrapeConfigs. Default false.
EnableServiceMonitor bool `json:"enableServiceMonitor,omitempty"`

// EnableRestartWhenConfigChange configmap monitoring, default is false.
// When EnableRestartWhenConfigChange is true, changing the doris core configmap will cause a rolling restart of the corresponding node
EnableRestartWhenConfigChange bool `json:"enableRestartWhenConfigChange,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7096,6 +7096,10 @@ spec:
EnableRestartWhenConfigChange configmap monitoring, default is false.
When EnableRestartWhenConfigChange is true, changing the doris core configmap will cause a rolling restart of the corresponding node
type: boolean
enableServiceMonitor:
description: EnableServiceMonitor creates a Secret with prometheus-additional.yaml
for Prometheus additionalScrapeConfigs. Default false.
type: boolean
feSpec:
description: defines the fe cluster state that will be created by
operator.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/doris.apache.com_dorisclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7096,6 +7096,10 @@ spec:
EnableRestartWhenConfigChange configmap monitoring, default is false.
When EnableRestartWhenConfigChange is true, changing the doris core configmap will cause a rolling restart of the corresponding node
type: boolean
enableServiceMonitor:
description: EnableServiceMonitor creates a Secret with prometheus-additional.yaml
for Prometheus additionalScrapeConfigs. Default false.
type: boolean
feSpec:
description: defines the fe cluster state that will be created by
operator.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/doris.selectdb.com_dorisclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7096,6 +7096,10 @@ spec:
EnableRestartWhenConfigChange configmap monitoring, default is false.
When EnableRestartWhenConfigChange is true, changing the doris core configmap will cause a rolling restart of the corresponding node
type: boolean
enableServiceMonitor:
description: EnableServiceMonitor creates a Secret with prometheus-additional.yaml
for Prometheus additionalScrapeConfigs. Default false.
type: boolean
feSpec:
description: defines the fe cluster state that will be created by
operator.
Expand Down
4 changes: 4 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ rules:
- get
- list
- watch
- create
- update
- patch
- delete
- apiGroups:
- ""
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7096,6 +7096,10 @@ spec:
EnableRestartWhenConfigChange configmap monitoring, default is false.
When EnableRestartWhenConfigChange is true, changing the doris core configmap will cause a rolling restart of the corresponding node
type: boolean
enableServiceMonitor:
description: EnableServiceMonitor creates a Secret with prometheus-additional.yaml
for Prometheus additionalScrapeConfigs. Default false.
type: boolean
feSpec:
description: defines the fe cluster state that will be created by
operator.
Expand Down
3 changes: 3 additions & 0 deletions helm-charts/doris-operator/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ rules:
- update
- list
- watch
- create
- patch
- delete
- apiGroups:
- "admissionregistration.k8s.io"
resources:
Expand Down
1 change: 1 addition & 0 deletions helm-charts/doris/templates/doriscluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ metadata:
{{- toYaml .Values.dorisCluster.annotations | nindent 4 }}
{{- end }}
spec:
enableServiceMonitor: {{ .Values.dorisCluster.enableServiceMonitor | default false }}
{{- if .Values.dorisCluster.adminUser }}
adminUser:
{{- toYaml .Values.dorisCluster.adminUser | nindent 4 }}
Expand Down
2 changes: 2 additions & 0 deletions helm-charts/doris/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dorisCluster:
enabledCn: false
# specify the broker deploy or not.
enabledBroker: false
# generate a Secret with prometheus-additional.yaml for Prometheus additionalScrapeConfigs.
enableServiceMonitor: false
# specify the doriscluster adminuser or not.
# set adminUser will record the password in clear text. This method has been abandoned and is not recommended.
# When adding adminuser. should follow the steps:
Expand Down
202 changes: 202 additions & 0 deletions pkg/common/utils/resource/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package resource

import (
"bytes"
"fmt"

dorisv1 "github.com/apache/doris-operator/api/doris/v1"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

const (
// PrometheusAdditionalConfigKey is the Secret key referenced by Prometheus additionalScrapeConfigs.
PrometheusAdditionalConfigKey = "prometheus-additional.yaml"
prometheusAdditionalSuffix = "-prometheus-additional"
)

type prometheusScrapeConfig struct {
JobName string `yaml:"job_name"`
MetricsPath string `yaml:"metrics_path"`
KubernetesSDConfigs []prometheusK8sSD `yaml:"kubernetes_sd_configs"`
RelabelConfigs []prometheusRelabel `yaml:"relabel_configs"`
}

type prometheusK8sSD struct {
Role string `yaml:"role"`
Namespaces prometheusSDNamespaces `yaml:"namespaces"`
}

type prometheusSDNamespaces struct {
Names []string `yaml:"names"`
}

type prometheusRelabel struct {
SourceLabels []string `yaml:"source_labels,omitempty"`
TargetLabel string `yaml:"target_label,omitempty"`
Regex string `yaml:"regex,omitempty"`
Action string `yaml:"action"`
}

type prometheusTarget struct {
jobName string
namespace string
serviceName string
portName string
path string
}

type prometheusEndpoint struct {
// jobSuffix distinguishes multiple endpoints of one component, e.g. "-brpc".
jobSuffix string
portKey string
path string
}

type prometheusComponent struct {
component dorisv1.ComponentType
enabled func(*dorisv1.DorisCluster) bool
endpoints []prometheusEndpoint
}

// prometheusComponents defines the components and endpoints scraped by Prometheus.
var prometheusComponents = []prometheusComponent{
{
component: dorisv1.Component_FE,
enabled: func(dcr *dorisv1.DorisCluster) bool { return dcr.Spec.FeSpec != nil },
endpoints: []prometheusEndpoint{
{portKey: HTTP_PORT, path: "/metrics"},
},
},
{
component: dorisv1.Component_BE,
enabled: func(dcr *dorisv1.DorisCluster) bool { return dcr.Spec.BeSpec != nil },
endpoints: []prometheusEndpoint{
{portKey: WEBSERVER_PORT, path: "/metrics"},
{jobSuffix: "-brpc", portKey: BRPC_PORT, path: "/brpc_metrics"},
},
},
}

// PrometheusAdditionalSecretName returns the name of the generated scrape config Secret.
func PrometheusAdditionalSecretName(clusterName string) string {
return clusterName + prometheusAdditionalSuffix
}

// BuildPrometheusAdditionalSecret builds the Secret consumed by Prometheus additionalScrapeConfigs for a DorisCluster.
func BuildPrometheusAdditionalSecret(dcr *dorisv1.DorisCluster) (*corev1.Secret, error) {
targets := make([]prometheusTarget, 0, 3)
for _, component := range prometheusComponents {
if component.enabled(dcr) {
targets = append(targets, component.prometheusTargets(dcr)...)
}
}

labels := map[string]string{dorisv1.OwnerReference: dcr.Name}
owner := prometheusOwnerReference(dorisv1.GroupVersion.String(), "DorisCluster", dcr.Name, dcr.UID)
return buildPrometheusAdditionalSecret(dcr.Name, dcr.Namespace, labels, owner, targets)
}

func (c prometheusComponent) prometheusTargets(dcr *dorisv1.DorisCluster) []prometheusTarget {
serviceName := dorisv1.GenerateExternalServiceName(dcr, c.component)
targets := make([]prometheusTarget, 0, len(c.endpoints))
for _, endpoint := range c.endpoints {
jobComponent := string(c.component) + endpoint.jobSuffix
targets = append(targets, newPrometheusTarget(dcr.Namespace, dcr.Name, jobComponent, serviceName, GetPortKey(endpoint.portKey), endpoint.path))
}
return targets
}

func newPrometheusTarget(namespace, clusterName, component, serviceName, portName, path string) prometheusTarget {
return prometheusTarget{
jobName: fmt.Sprintf("%s/%s/%s", namespace, clusterName, component),
namespace: namespace,
serviceName: serviceName,
portName: portName,
path: path,
}
}

func prometheusOwnerReference(apiVersion, kind, name string, uid types.UID) metav1.OwnerReference {
controller := true
return metav1.OwnerReference{APIVersion: apiVersion, Kind: kind, Name: name, UID: uid, Controller: &controller}
}

func buildPrometheusAdditionalSecret(clusterName, namespace string, labels map[string]string, owner metav1.OwnerReference, targets []prometheusTarget) (*corev1.Secret, error) {
configs := make([]prometheusScrapeConfig, 0, len(targets))
for _, target := range targets {
configs = append(configs, target.scrapeConfig())
}
var buffer bytes.Buffer
encoder := yaml.NewEncoder(&buffer)
encoder.SetIndent(2)
if err := encoder.Encode(configs); err != nil {
return nil, err
}
if err := encoder.Close(); err != nil {
return nil, err
}
data := buffer.Bytes()

return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: PrometheusAdditionalSecretName(clusterName),
Namespace: namespace,
Labels: labels,
OwnerReferences: []metav1.OwnerReference{owner},
},
Data: map[string][]byte{PrometheusAdditionalConfigKey: data},
}, nil
}

func (t prometheusTarget) scrapeConfig() prometheusScrapeConfig {
return prometheusScrapeConfig{
JobName: t.jobName,
MetricsPath: t.path,
KubernetesSDConfigs: []prometheusK8sSD{{
Role: "endpoints",
Namespaces: prometheusSDNamespaces{Names: []string{t.namespace}},
}},
RelabelConfigs: []prometheusRelabel{
{
SourceLabels: []string{"__meta_kubernetes_service_name"},
Regex: t.serviceName,
Action: "keep",
},
{
SourceLabels: []string{"__meta_kubernetes_endpoint_port_name"},
Regex: t.portName,
Action: "keep",
},
{
SourceLabels: []string{"__meta_kubernetes_namespace"},
TargetLabel: "namespace",
Action: "replace",
},
{
SourceLabels: []string{"__meta_kubernetes_pod_name"},
TargetLabel: "pod",
Action: "replace",
},
},
}
}
101 changes: 101 additions & 0 deletions pkg/common/utils/resource/prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package resource

import (
"testing"

dorisv1 "github.com/apache/doris-operator/api/doris/v1"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Test_BuildPrometheusAdditionalSecret(t *testing.T) {
dcr := &dorisv1.DorisCluster{
ObjectMeta: metav1.ObjectMeta{Name: "demo", Namespace: "doris"},
Spec: dorisv1.DorisClusterSpec{
FeSpec: &dorisv1.FeSpec{},
BeSpec: &dorisv1.BeSpec{},
},
}

secret, err := BuildPrometheusAdditionalSecret(dcr)
if err != nil {
t.Fatalf("build secret failed: %s", err)
}
if secret.Name != "demo-prometheus-additional" || secret.Namespace != "doris" {
t.Fatalf("unexpected secret metadata: %s/%s", secret.Namespace, secret.Name)
}
if len(secret.OwnerReferences) != 1 || secret.OwnerReferences[0].Name != "demo" {
t.Fatalf("unexpected owner references: %#v", secret.OwnerReferences)
}

jobs := scrapeJobs(t, secret)
assertTarget(t, jobs, "doris/demo/fe", "demo-fe-service", "http-port", "/metrics")
assertTarget(t, jobs, "doris/demo/be", "demo-be-service", "webserver-port", "/metrics")
assertTarget(t, jobs, "doris/demo/be-brpc", "demo-be-service", "brpc-port", "/brpc_metrics")
if len(jobs) != 3 {
t.Fatalf("expected 3 scrape jobs, got %d", len(jobs))
}
}

func scrapeJobs(t *testing.T, secret *corev1.Secret) map[string]prometheusScrapeConfig {
t.Helper()
data, ok := secret.Data[PrometheusAdditionalConfigKey]
if !ok {
t.Fatalf("secret missing key %s", PrometheusAdditionalConfigKey)
}
var configs []prometheusScrapeConfig
if err := yaml.Unmarshal(data, &configs); err != nil {
t.Fatalf("unmarshal scrape configs failed: %s", err)
}
jobs := make(map[string]prometheusScrapeConfig, len(configs))
for _, config := range configs {
jobs[config.JobName] = config
}
return jobs
}

func assertTarget(t *testing.T, jobs map[string]prometheusScrapeConfig, jobName, serviceName, portName, path string) {
t.Helper()
job, ok := jobs[jobName]
if !ok {
t.Fatalf("job %s not found", jobName)
}
if job.MetricsPath != path {
t.Errorf("job %s metrics_path = %s, want %s", jobName, job.MetricsPath, path)
}
if len(job.KubernetesSDConfigs) != 1 || len(job.KubernetesSDConfigs[0].Namespaces.Names) != 1 ||
job.KubernetesSDConfigs[0].Namespaces.Names[0] != "doris" {
t.Errorf("job %s unexpected kubernetes_sd_configs: %#v", jobName, job.KubernetesSDConfigs)
}

relabels := map[string]string{}
for _, relabel := range job.RelabelConfigs {
if len(relabel.SourceLabels) == 1 && relabel.Action == "keep" {
relabels[relabel.SourceLabels[0]] = relabel.Regex
}
}
if relabels["__meta_kubernetes_service_name"] != serviceName {
t.Errorf("job %s service selector = %s, want %s", jobName, relabels["__meta_kubernetes_service_name"], serviceName)
}
if relabels["__meta_kubernetes_endpoint_port_name"] != portName {
t.Errorf("job %s port selector = %s, want %s", jobName, relabels["__meta_kubernetes_endpoint_port_name"], portName)
}
}
Loading