Skip to content
Merged
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
42 changes: 42 additions & 0 deletions pkg/apis/deployment/v1/deployment_metrics_service_monitor_spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// DISCLAIMER
//
// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package v1

type MetricsServiceMonitorSpec struct {
Enabled *bool `json:"enabled,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}

func (m *MetricsServiceMonitorSpec) IsEnabled() bool {
if m == nil || m.Enabled == nil {
return true
}

return *m.Enabled
}

func (m *MetricsServiceMonitorSpec) GetLabels(def map[string]string) map[string]string {
if len(m.Labels) == 0 {
return def
}

return m.Labels
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type MetricsSpec struct {
Mode *MetricsMode `json:"mode,omitempty"`
TLS *bool `json:"tls,omitempty"`

ServiceMonitor *MetricsServiceMonitorSpec `json:"serviceMonitor,omitempty"`

Port *uint16 `json:"port,omitempty"`
}

Expand Down
33 changes: 33 additions & 0 deletions pkg/apis/deployment/v1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// DISCLAIMER
//
// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package v2alpha1

type MetricsServiceMonitorSpec struct {
Enabled *bool `json:"enabled,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}

func (m *MetricsServiceMonitorSpec) IsEnabled() bool {
if m == nil || m.Enabled == nil {
return true
}

return *m.Enabled
}

func (m *MetricsServiceMonitorSpec) GetLabels(def map[string]string) map[string]string {
if len(m.Labels) == 0 {
return def
}

return m.Labels
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type MetricsSpec struct {
Mode *MetricsMode `json:"mode,omitempty"`
TLS *bool `json:"tls,omitempty"`

ServiceMonitor *MetricsServiceMonitorSpec `json:"serviceMonitor,omitempty"`

Port *uint16 `json:"port,omitempty"`
}

Expand Down
33 changes: 33 additions & 0 deletions pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go

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

29 changes: 19 additions & 10 deletions pkg/deployment/resources/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,22 @@ import (
"k8s.io/client-go/rest"
)

func LabelsForExporterServiceMonitor(deploymentName string) map[string]string {
return map[string]string{
k8sutil.LabelKeyArangoDeployment: deploymentName,
k8sutil.LabelKeyApp: k8sutil.AppName,
"context": "metrics",
"metrics": "prometheus",
func LabelsForExporterServiceMonitor(name string, obj deploymentApi.DeploymentSpec) map[string]string {
base := LabelsForExporterServiceMonitorSelector(name)

for k, v := range obj.Metrics.ServiceMonitor.GetLabels(map[string]string{
"context": "metrics",
"metrics": "prometheus",
}) {
base[k] = v
}

return base
}

func LabelsForExporterServiceMonitorSelector(deploymentName string) map[string]string {
func LabelsForExporterServiceMonitorSelector(name string) map[string]string {
return map[string]string{
k8sutil.LabelKeyArangoDeployment: deploymentName,
k8sutil.LabelKeyArangoDeployment: name,
k8sutil.LabelKeyApp: k8sutil.AppName,
}
}
Expand Down Expand Up @@ -122,7 +126,7 @@ func (r *Resources) serviceMonitorSpec() (coreosv1.ServiceMonitorSpec, error) {
endpoint,
},
Selector: metav1.LabelSelector{
MatchLabels: LabelsForExporterServiceMonitorSelector(deploymentName),
MatchLabels: LabelsForExporterServiceMonitorSelector(r.context.GetName()),
},
}, nil
default:
Expand All @@ -147,6 +151,11 @@ func (r *Resources) EnsureServiceMonitor(ctx context.Context) error {
ns := apiObject.GetNamespace()
owner := apiObject.AsOwner()
spec := r.context.GetSpec()

if !spec.Metrics.ServiceMonitor.IsEnabled() || !spec.Metrics.IsEnabled() {
return nil
}

wantMetrics := spec.Metrics.IsEnabled()
serviceMonitorName := k8sutil.CreateExporterClientServiceName(deploymentName)

Expand Down Expand Up @@ -176,7 +185,7 @@ func (r *Resources) EnsureServiceMonitor(ctx context.Context) error {
smon := &coreosv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: serviceMonitorName,
Labels: LabelsForExporterServiceMonitor(deploymentName),
Labels: LabelsForExporterServiceMonitor(r.context.GetName(), r.context.GetSpec()),
OwnerReferences: []metav1.OwnerReference{owner},
},
Spec: spec,
Expand Down