diff --git a/pkg/apis/deployment/v1/deployment_metrics_service_monitor_spec.go b/pkg/apis/deployment/v1/deployment_metrics_service_monitor_spec.go new file mode 100644 index 000000000..10c2e4ba0 --- /dev/null +++ b/pkg/apis/deployment/v1/deployment_metrics_service_monitor_spec.go @@ -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 +} diff --git a/pkg/apis/deployment/v1/metrics_spec.go b/pkg/apis/deployment/v1/deployment_metrics_spec.go similarity index 98% rename from pkg/apis/deployment/v1/metrics_spec.go rename to pkg/apis/deployment/v1/deployment_metrics_spec.go index 4b17dc9ef..6fc1676c2 100644 --- a/pkg/apis/deployment/v1/metrics_spec.go +++ b/pkg/apis/deployment/v1/deployment_metrics_spec.go @@ -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"` } diff --git a/pkg/apis/deployment/v1/monitoring_spec.go b/pkg/apis/deployment/v1/sync_monitoring_spec.go similarity index 100% rename from pkg/apis/deployment/v1/monitoring_spec.go rename to pkg/apis/deployment/v1/sync_monitoring_spec.go diff --git a/pkg/apis/deployment/v1/monitoring_spec_test.go b/pkg/apis/deployment/v1/sync_monitoring_spec_test.go similarity index 100% rename from pkg/apis/deployment/v1/monitoring_spec_test.go rename to pkg/apis/deployment/v1/sync_monitoring_spec_test.go diff --git a/pkg/apis/deployment/v1/zz_generated.deepcopy.go b/pkg/apis/deployment/v1/zz_generated.deepcopy.go index e94c2fdc6..043e115db 100644 --- a/pkg/apis/deployment/v1/zz_generated.deepcopy.go +++ b/pkg/apis/deployment/v1/zz_generated.deepcopy.go @@ -1236,6 +1236,34 @@ func (in *MetricsAuthenticationSpec) DeepCopy() *MetricsAuthenticationSpec { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *MetricsServiceMonitorSpec) DeepCopyInto(out *MetricsServiceMonitorSpec) { + *out = *in + if in.Enabled != nil { + in, out := &in.Enabled, &out.Enabled + *out = new(bool) + **out = **in + } + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new MetricsServiceMonitorSpec. +func (in *MetricsServiceMonitorSpec) DeepCopy() *MetricsServiceMonitorSpec { + if in == nil { + return nil + } + out := new(MetricsServiceMonitorSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *MetricsSpec) DeepCopyInto(out *MetricsSpec) { *out = *in @@ -1261,6 +1289,11 @@ func (in *MetricsSpec) DeepCopyInto(out *MetricsSpec) { *out = new(bool) **out = **in } + if in.ServiceMonitor != nil { + in, out := &in.ServiceMonitor, &out.ServiceMonitor + *out = new(MetricsServiceMonitorSpec) + (*in).DeepCopyInto(*out) + } if in.Port != nil { in, out := &in.Port, &out.Port *out = new(uint16) diff --git a/pkg/apis/deployment/v2alpha1/deployment_metrics_service_monitor_spec.go b/pkg/apis/deployment/v2alpha1/deployment_metrics_service_monitor_spec.go new file mode 100644 index 000000000..4a2874f2e --- /dev/null +++ b/pkg/apis/deployment/v2alpha1/deployment_metrics_service_monitor_spec.go @@ -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 +} diff --git a/pkg/apis/deployment/v2alpha1/metrics_spec.go b/pkg/apis/deployment/v2alpha1/deployment_metrics_spec.go similarity index 98% rename from pkg/apis/deployment/v2alpha1/metrics_spec.go rename to pkg/apis/deployment/v2alpha1/deployment_metrics_spec.go index 5d373caf3..c1fdf0b67 100644 --- a/pkg/apis/deployment/v2alpha1/metrics_spec.go +++ b/pkg/apis/deployment/v2alpha1/deployment_metrics_spec.go @@ -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"` } diff --git a/pkg/apis/deployment/v2alpha1/monitoring_spec.go b/pkg/apis/deployment/v2alpha1/sync_monitoring_spec.go similarity index 100% rename from pkg/apis/deployment/v2alpha1/monitoring_spec.go rename to pkg/apis/deployment/v2alpha1/sync_monitoring_spec.go diff --git a/pkg/apis/deployment/v2alpha1/monitoring_spec_test.go b/pkg/apis/deployment/v2alpha1/sync_monitoring_spec_test.go similarity index 100% rename from pkg/apis/deployment/v2alpha1/monitoring_spec_test.go rename to pkg/apis/deployment/v2alpha1/sync_monitoring_spec_test.go diff --git a/pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go b/pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go index 82f6a9d5a..dce7902a2 100644 --- a/pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go @@ -1236,6 +1236,34 @@ func (in *MetricsAuthenticationSpec) DeepCopy() *MetricsAuthenticationSpec { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *MetricsServiceMonitorSpec) DeepCopyInto(out *MetricsServiceMonitorSpec) { + *out = *in + if in.Enabled != nil { + in, out := &in.Enabled, &out.Enabled + *out = new(bool) + **out = **in + } + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new MetricsServiceMonitorSpec. +func (in *MetricsServiceMonitorSpec) DeepCopy() *MetricsServiceMonitorSpec { + if in == nil { + return nil + } + out := new(MetricsServiceMonitorSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *MetricsSpec) DeepCopyInto(out *MetricsSpec) { *out = *in @@ -1261,6 +1289,11 @@ func (in *MetricsSpec) DeepCopyInto(out *MetricsSpec) { *out = new(bool) **out = **in } + if in.ServiceMonitor != nil { + in, out := &in.ServiceMonitor, &out.ServiceMonitor + *out = new(MetricsServiceMonitorSpec) + (*in).DeepCopyInto(*out) + } if in.Port != nil { in, out := &in.Port, &out.Port *out = new(uint16) diff --git a/pkg/deployment/resources/servicemonitor.go b/pkg/deployment/resources/servicemonitor.go index 7ab22965a..a510c21fc 100644 --- a/pkg/deployment/resources/servicemonitor.go +++ b/pkg/deployment/resources/servicemonitor.go @@ -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, } } @@ -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: @@ -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) @@ -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,