Skip to content

Commit

Permalink
refactor: generate podmonitor configuration (#88)
Browse files Browse the repository at this point in the history
* refactor: Configuring generate prometheus podmonitor

* feat: define http port for meta container and service
  • Loading branch information
daviderli614 authored Apr 3, 2023
1 parent a1ee3b3 commit 933a78e
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 59 deletions.
30 changes: 28 additions & 2 deletions apis/v1alpha1/greptimedbcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,7 @@ type GreptimeDBClusterSpec struct {
EnableInfluxDBProtocol bool `json:"enableInfluxDBProtocol,omitempty"`

// +optional
// +kubebuilder:validation:Enum:={true, false}
EnablePrometheusMonitor bool `json:"enablePrometheusMonitor,omitempty"`
PrometheusMonitor *PrometheusMonitor `json:"prometheusMonitor,omitempty"`

// +optional
// The version of greptimedb.
Expand All @@ -454,6 +453,33 @@ type GreptimeDBClusterSpec struct {
// More cluster settings can be added here.
}

// PrometheusMonitor defines the PodMonitor configuration.
type PrometheusMonitor struct {
// Enable a prometheus PodMonitor
// +optional
Enabled bool `json:"enabled,omitempty"`

// HTTP path to scrape for metrics.
// +optional
Path string `json:"path,omitempty"`

// Name of the pod port this endpoint refers to. Mutually exclusive with targetPort.
// +optional
Port string `json:"port,omitempty"`

// Interval at which metrics should be scraped
// +optional
Interval string `json:"interval,omitempty"`

// HonorLabels chooses the metrics labels on collisions with target labels.
// +optional
HonorLabels bool `json:"honorLabels,omitempty"`

// Prometheus PodMonitor selector.
// +optional
LabelsSelector map[string]string `json:"labelsSelector,omitempty"`
}

// GreptimeDBClusterStatus defines the observed state of GreptimeDBCluster
type GreptimeDBClusterStatus struct {
Frontend FrontendStatus `json:"frontend,omitempty"`
Expand Down
27 changes: 27 additions & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions config/crd/bases/greptime.io_greptimedbclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3918,11 +3918,6 @@ spec:
type: object
enableInfluxDBProtocol:
type: boolean
enablePrometheusMonitor:
enum:
- true
- false
type: boolean
frontend:
properties:
config:
Expand Down Expand Up @@ -7836,6 +7831,23 @@ spec:
postgresServicePort:
format: int32
type: integer
prometheusMonitor:
properties:
enabled:
type: boolean
honorLabels:
type: boolean
interval:
type: string
labelsSelector:
additionalProperties:
type: string
type: object
path:
type: string
port:
type: string
type: object
storage:
properties:
local:
Expand Down
39 changes: 39 additions & 0 deletions config/samples/db-prometheus-monitor/cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2022 Greptime Team
#
# Licensed 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.

apiVersion: greptime.io/v1alpha1
kind: GreptimeDBCluster
metadata:
name: basic
namespace: default
spec:
base:
main:
image: greptime/greptimedb:0.1.2
frontend:
replicas: 1
meta:
replicas: 1
etcdEndpoints:
- "etcd.default:2379"
datanode:
replicas: 3
prometheusMonitor:
enabled: true
path: "/metrics"
port: "http"
interval: "20s"
honorLabels: true
labelsSelector:
release: prometheus
4 changes: 0 additions & 4 deletions controllers/greptimedbcluster/deployers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ const (
)

var (
DefaultMetricPortName = "metrics"
DefaultMetricPath = "/metrics"
DefaultScapeInterval = "30s"

DefaultConfigPath = "/etc/greptimedb"
)

Expand Down
21 changes: 12 additions & 9 deletions controllers/greptimedbcluster/deployers/datanode.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ func (d *DatanodeDeployer) Render(crdObject client.Object) ([]client.Object, err
}
}

if cluster.Spec.EnablePrometheusMonitor {
pm, err := d.generatePodMonitor(cluster)
if err != nil {
return nil, err
if cluster.Spec.PrometheusMonitor != nil {
if cluster.Spec.PrometheusMonitor.Enabled {
pm, err := d.generatePodMonitor(cluster)
if err != nil {
return nil, err
}
renderObjects = append(renderObjects, pm)
}
renderObjects = append(renderObjects, pm)
}
}

Expand Down Expand Up @@ -283,14 +285,15 @@ func (d *DatanodeDeployer) generatePodMonitor(cluster *v1alpha1.GreptimeDBCluste
ObjectMeta: metav1.ObjectMeta{
Name: d.ResourceName(cluster.Name, v1alpha1.DatanodeComponentKind),
Namespace: cluster.Namespace,
Labels: cluster.Spec.PrometheusMonitor.LabelsSelector,
},
Spec: monitoringv1.PodMonitorSpec{
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
{
Path: DefaultMetricPath,
Port: DefaultMetricPortName,
Interval: DefaultScapeInterval,
HonorLabels: true,
Path: cluster.Spec.PrometheusMonitor.Path,
Port: cluster.Spec.PrometheusMonitor.Port,
Interval: cluster.Spec.PrometheusMonitor.Interval,
HonorLabels: cluster.Spec.PrometheusMonitor.HonorLabels,
},
},
Selector: metav1.LabelSelector{
Expand Down
21 changes: 12 additions & 9 deletions controllers/greptimedbcluster/deployers/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ func (d *FrontendDeployer) Render(crdObject client.Object) ([]client.Object, err
}
}

if cluster.Spec.EnablePrometheusMonitor {
pm, err := d.generatePodMonitor(cluster)
if err != nil {
return nil, err
if cluster.Spec.PrometheusMonitor != nil {
if cluster.Spec.PrometheusMonitor.Enabled {
pm, err := d.generatePodMonitor(cluster)
if err != nil {
return nil, err
}
renderObjects = append(renderObjects, pm)
}
renderObjects = append(renderObjects, pm)
}
}

Expand Down Expand Up @@ -245,14 +247,15 @@ func (d *FrontendDeployer) generatePodMonitor(cluster *v1alpha1.GreptimeDBCluste
ObjectMeta: metav1.ObjectMeta{
Name: d.ResourceName(cluster.Name, v1alpha1.FrontendComponentKind),
Namespace: cluster.Namespace,
Labels: cluster.Spec.PrometheusMonitor.LabelsSelector,
},
Spec: monitoringv1.PodMonitorSpec{
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
{
Path: DefaultMetricPath,
Port: DefaultMetricPortName,
Interval: DefaultScapeInterval,
HonorLabels: true,
Path: cluster.Spec.PrometheusMonitor.Path,
Port: cluster.Spec.PrometheusMonitor.Port,
Interval: cluster.Spec.PrometheusMonitor.Interval,
HonorLabels: cluster.Spec.PrometheusMonitor.HonorLabels,
},
},
Selector: metav1.LabelSelector{
Expand Down
31 changes: 22 additions & 9 deletions controllers/greptimedbcluster/deployers/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@ func (d *MetaDeployer) Render(crdObject client.Object) ([]client.Object, error)
}
}

if cluster.Spec.EnablePrometheusMonitor {
pm, err := d.generatePodMonitor(cluster)
if err != nil {
return nil, err
if cluster.Spec.PrometheusMonitor != nil {
if cluster.Spec.PrometheusMonitor.Enabled {
pm, err := d.generatePodMonitor(cluster)
if err != nil {
return nil, err
}
renderObjects = append(renderObjects, pm)
}
renderObjects = append(renderObjects, pm)
}
}

Expand Down Expand Up @@ -188,6 +190,11 @@ func (d *MetaDeployer) generateSvc(cluster *v1alpha1.GreptimeDBCluster) (*corev1
Protocol: corev1.ProtocolTCP,
Port: cluster.Spec.Meta.ServicePort,
},
{
Name: "http",
Protocol: corev1.ProtocolTCP,
Port: cluster.Spec.HTTPServicePort,
},
},
},
}
Expand Down Expand Up @@ -236,14 +243,15 @@ func (d *MetaDeployer) generatePodMonitor(cluster *v1alpha1.GreptimeDBCluster) (
ObjectMeta: metav1.ObjectMeta{
Name: d.ResourceName(cluster.Name, v1alpha1.MetaComponentKind),
Namespace: cluster.Namespace,
Labels: cluster.Spec.PrometheusMonitor.LabelsSelector,
},
Spec: monitoringv1.PodMonitorSpec{
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
{
Path: DefaultMetricPath,
Port: DefaultMetricPortName,
Interval: DefaultScapeInterval,
HonorLabels: true,
Path: cluster.Spec.PrometheusMonitor.Path,
Port: cluster.Spec.PrometheusMonitor.Port,
Interval: cluster.Spec.PrometheusMonitor.Interval,
HonorLabels: cluster.Spec.PrometheusMonitor.HonorLabels,
},
},
Selector: metav1.LabelSelector{
Expand Down Expand Up @@ -354,6 +362,11 @@ func (d *MetaDeployer) generatePodTemplateSpec(cluster *v1alpha1.GreptimeDBClust
Protocol: corev1.ProtocolTCP,
ContainerPort: cluster.Spec.Meta.ServicePort,
},
{
Name: "http",
Protocol: corev1.ProtocolTCP,
ContainerPort: cluster.Spec.HTTPServicePort,
},
}

podTemplateSpec.ObjectMeta.Labels = deployer.MergeStringMap(podTemplateSpec.ObjectMeta.Labels, map[string]string{
Expand Down
24 changes: 13 additions & 11 deletions controllers/greptimedbcluster/greptimedbcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,19 @@ func (r *Reconciler) validate(ctx context.Context, cluster *v1alpha1.GreptimeDBC
}

// To detect if the CRD of podmonitor is installed.
if cluster.Spec.EnablePrometheusMonitor {
// CheckPodMonitorCRDInstall is used to check if the CRD of podmonitor is installed, it is not used to create the podmonitor.
err := r.checkPodMonitorCRDInstall(ctx, metav1.GroupKind{
Group: "monitoring.coreos.com",
Kind: "PodMonitor",
})
if err != nil {
if errors.IsNotFound(err) {
return fmt.Errorf("the crd podmonitors.monitoring.coreos.com is not installed")
} else {
return fmt.Errorf("check crd of podmonitors.monitoring.coreos.com is installed error: %v", err)
if cluster.Spec.PrometheusMonitor != nil {
if cluster.Spec.PrometheusMonitor.Enabled {
// CheckPodMonitorCRDInstall is used to check if the CRD of podmonitor is installed, it is not used to create the podmonitor.
err := r.checkPodMonitorCRDInstall(ctx, metav1.GroupKind{
Group: "monitoring.coreos.com",
Kind: "PodMonitor",
})
if err != nil {
if errors.IsNotFound(err) {
return fmt.Errorf("the crd podmonitors.monitoring.coreos.com is not installed")
} else {
return fmt.Errorf("check crd of podmonitors.monitoring.coreos.com is installed error: %v", err)
}
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions manifests/greptimedb-operator-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3917,11 +3917,6 @@ spec:
type: object
enableInfluxDBProtocol:
type: boolean
enablePrometheusMonitor:
enum:
- true
- false
type: boolean
frontend:
properties:
config:
Expand Down Expand Up @@ -7835,6 +7830,23 @@ spec:
postgresServicePort:
format: int32
type: integer
prometheusMonitor:
properties:
enabled:
type: boolean
honorLabels:
type: boolean
interval:
type: string
labelsSelector:
additionalProperties:
type: string
type: object
path:
type: string
port:
type: string
type: object
storage:
properties:
local:
Expand Down
Loading

0 comments on commit 933a78e

Please sign in to comment.