Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PodMonitor when using sidecar mode #2404

Merged
16 changes: 16 additions & 0 deletions .chloggen/podmonitor-otel-sidecar-mode.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Create PodMonitor when deploying collector in sidecar mode and Prometheus exporters are used.

# One or more tracking issues related to the change
issues: [2306]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
2 changes: 1 addition & 1 deletion apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ type PodDisruptionBudgetSpec struct {

// MetricsConfigSpec defines a metrics config.
type MetricsConfigSpec struct {
// EnableMetrics specifies if ServiceMonitor should be created for the OpenTelemetry Collector and Prometheus Exporters.
// EnableMetrics specifies if ServiceMonitor or PodMonitor(for sidecar mode) should be created for the OpenTelemetry Collector and Prometheus Exporters.
// The operator.observability.prometheus feature gate must be enabled to use this feature.
//
// +optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ metadata:
categories: Logging & Tracing,Monitoring
certified: "false"
containerImage: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator
createdAt: "2023-11-17T13:24:32Z"
createdAt: "2023-12-05T15:43:53Z"
description: Provides the OpenTelemetry components, including the Collector
operators.operatorframework.io/builder: operator-sdk-v1.29.0
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
Expand Down Expand Up @@ -135,9 +135,10 @@ spec:
- description: Metrics defines the metrics configuration for operands.
displayName: Metrics Config
path: observability.metrics
- description: EnableMetrics specifies if ServiceMonitor should be created for
the OpenTelemetry Collector and Prometheus Exporters. The operator.observability.prometheus
feature gate must be enabled to use this feature.
- description: EnableMetrics specifies if ServiceMonitor or PodMonitor(for sidecar
mode) should be created for the OpenTelemetry Collector and Prometheus Exporters.
The operator.observability.prometheus feature gate must be enabled to use
this feature.
displayName: Create ServiceMonitors for OpenTelemetry Collector
path: observability.metrics.enableMetrics
version: v1alpha1
Expand Down Expand Up @@ -233,6 +234,7 @@ spec:
- apiGroups:
- monitoring.coreos.com
resources:
- podmonitors
- servicemonitors
verbs:
- create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3707,9 +3707,9 @@ spec:
description: Metrics defines the metrics configuration for operands.
properties:
enableMetrics:
description: EnableMetrics specifies if ServiceMonitor should
be created for the OpenTelemetry Collector and Prometheus
Exporters. The operator.observability.
description: EnableMetrics specifies if ServiceMonitor or
PodMonitor(for sidecar mode) should be created for the OpenTelemetry
Collector and Prometheus Exporters. The operator.observability.
type: boolean
type: object
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3704,9 +3704,9 @@ spec:
description: Metrics defines the metrics configuration for operands.
properties:
enableMetrics:
description: EnableMetrics specifies if ServiceMonitor should
be created for the OpenTelemetry Collector and Prometheus
Exporters. The operator.observability.
description: EnableMetrics specifies if ServiceMonitor or
PodMonitor(for sidecar mode) should be created for the OpenTelemetry
Collector and Prometheus Exporters. The operator.observability.
type: boolean
type: object
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ spec:
- description: Metrics defines the metrics configuration for operands.
displayName: Metrics Config
path: observability.metrics
- description: EnableMetrics specifies if ServiceMonitor should be created for
the OpenTelemetry Collector and Prometheus Exporters. The operator.observability.prometheus
feature gate must be enabled to use this feature.
- description: EnableMetrics specifies if ServiceMonitor or PodMonitor(for sidecar
mode) should be created for the OpenTelemetry Collector and Prometheus Exporters.
The operator.observability.prometheus feature gate must be enabled to use
this feature.
displayName: Create ServiceMonitors for OpenTelemetry Collector
path: observability.metrics.enableMetrics
version: v1alpha1
Expand Down
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ rules:
- apiGroups:
- monitoring.coreos.com
resources:
- podmonitors
- servicemonitors
verbs:
- create
Expand Down
3 changes: 2 additions & 1 deletion controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewReconciler(p Params) *OpenTelemetryCollectorReconciler {
// +kubebuilder:rbac:groups=autoscaling,resources=horizontalpodautoscalers,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=policy,resources=poddisruptionbudgets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;create;update
// +kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors;podmonitors,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=route.openshift.io,resources=routes;routes/custom-host,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=opentelemetry.io,resources=opentelemetrycollectors,verbs=get;list;watch;update;patch
Expand Down Expand Up @@ -142,6 +142,7 @@ func (r *OpenTelemetryCollectorReconciler) SetupWithManager(mgr ctrl.Manager) er

if featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
builder.Owns(&monitoringv1.ServiceMonitor{})
builder.Owns(&monitoringv1.PodMonitor{})
}

return builder.Complete(r)
Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17287,7 +17287,7 @@ Metrics defines the metrics configuration for operands.
<td><b>enableMetrics</b></td>
<td>boolean</td>
<td>
EnableMetrics specifies if ServiceMonitor should be created for the OpenTelemetry Collector and Prometheus Exporters. The operator.observability.<br/>
EnableMetrics specifies if ServiceMonitor or PodMonitor(for sidecar mode) should be created for the OpenTelemetry Collector and Prometheus Exporters. The operator.observability.<br/>
</td>
<td>false</td>
</tr></tbody>
Expand Down
6 changes: 5 additions & 1 deletion internal/manifests/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ func Build(params manifests.Params) ([]client.Object, error) {
manifests.Factory(Ingress),
}...)
if params.OtelCol.Spec.Observability.Metrics.EnableMetrics && featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
manifestFactories = append(manifestFactories, manifests.Factory(ServiceMonitor))
if params.OtelCol.Spec.Mode == v1alpha1.ModeSidecar {
manifestFactories = append(manifestFactories, manifests.Factory(PodMonitor))
} else {
manifestFactories = append(manifestFactories, manifests.Factory(ServiceMonitor))
}
}
for _, factory := range manifestFactories {
res, err := factory(params)
Expand Down
101 changes: 101 additions & 0 deletions internal/manifests/collector/podmonitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright The OpenTelemetry Authors
//
// 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.

package collector

import (
"fmt"
"strings"

"github.com/go-logr/logr"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/adapters"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)

// ServiceMonitor returns the service monitor for the given instance.
func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) {
if !params.OtelCol.Spec.Observability.Metrics.EnableMetrics {
params.Log.V(2).Info("Metrics disabled for this OTEL Collector",
"params.OtelCol.name", params.OtelCol.Name,
"params.OtelCol.namespace", params.OtelCol.Namespace,
)
return nil, nil
}
var pm monitoringv1.PodMonitor

if params.OtelCol.Spec.Mode != v1alpha1.ModeSidecar {
return nil, nil
}

pm = monitoringv1.PodMonitor{
ObjectMeta: metav1.ObjectMeta{
Namespace: params.OtelCol.Namespace,
Name: naming.PodMonitor(params.OtelCol.Name),
Labels: map[string]string{
"app.kubernetes.io/name": naming.PodMonitor(params.OtelCol.Name),
"app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name),
"app.kubernetes.io/managed-by": "opentelemetry-operator",
},
},
Spec: monitoringv1.PodMonitorSpec{
JobLabel: "app.kubernetes.io/instance",
PodTargetLabels: []string{"app.kubernetes.io/name", "app.kubernetes.io/instance", "app.kubernetes.io/managed-by"},
NamespaceSelector: monitoringv1.NamespaceSelector{
MatchNames: []string{params.OtelCol.Namespace},
},
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/managed-by": "opentelemetry-operator",
"app.kubernetes.io/instance": fmt.Sprintf("%s.%s", params.OtelCol.Namespace, params.OtelCol.Name),
},
},
PodMetricsEndpoints: append(
[]monitoringv1.PodMetricsEndpoint{
{
Port: "monitoring",
},
}, metricsEndpointsFromConfig(params.Log, params.OtelCol)...),
},
}

return &pm, nil
}

func metricsEndpointsFromConfig(logger logr.Logger, otelcol v1alpha1.OpenTelemetryCollector) []monitoringv1.PodMetricsEndpoint {
config, err := adapters.ConfigFromString(otelcol.Spec.Config)
if err != nil {
logger.V(2).Error(err, "Error while parsing the configuration")
return []monitoringv1.PodMetricsEndpoint{}
}
exporterPorts, err := adapters.ConfigToComponentPorts(logger, adapters.ComponentTypeExporter, config)
if err != nil {
logger.Error(err, "couldn't build endpoints to podMonitors from configuration")
return []monitoringv1.PodMetricsEndpoint{}
}
swiatekm marked this conversation as resolved.
Show resolved Hide resolved
metricsEndpoints := []monitoringv1.PodMetricsEndpoint{}
for _, port := range exporterPorts {
if strings.Contains(port.Name, "prometheus") {
e := monitoringv1.PodMetricsEndpoint{
Port: port.Name,
}
metricsEndpoints = append(metricsEndpoints, e)
}
}
return metricsEndpoints
}
59 changes: 59 additions & 0 deletions internal/manifests/collector/podmonitor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright The OpenTelemetry Authors
//
// 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.

package collector

import (
"fmt"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"

"github.com/stretchr/testify/assert"

"testing"
)

func sidecarParams() manifests.Params {
return paramsWithMode(v1alpha1.ModeSidecar)
}

func TestDesiredPodMonitors(t *testing.T) {
params := sidecarParams()

actual, err := PodMonitor(params)
assert.NoError(t, err)
assert.Nil(t, actual)

params.OtelCol.Spec.Observability.Metrics.EnableMetrics = true
actual, err = PodMonitor(params)
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.Equal(t, fmt.Sprintf("%s-collector", params.OtelCol.Name), actual.Name)
assert.Equal(t, params.OtelCol.Namespace, actual.Namespace)
assert.Equal(t, "monitoring", actual.Spec.PodMetricsEndpoints[0].Port)

params, err = newParams("", "testdata/prometheus-exporter.yaml")
assert.NoError(t, err)
params.OtelCol.Spec.Mode = v1alpha1.ModeSidecar
params.OtelCol.Spec.Observability.Metrics.EnableMetrics = true
actual, err = PodMonitor(params)
assert.NoError(t, err)
assert.NotNil(t, actual)
assert.Equal(t, fmt.Sprintf("%s-collector", params.OtelCol.Name), actual.Name)
assert.Equal(t, params.OtelCol.Namespace, actual.Namespace)
assert.Equal(t, "monitoring", actual.Spec.PodMetricsEndpoints[0].Port)
assert.Equal(t, "prometheus-dev", actual.Spec.PodMetricsEndpoints[1].Port)
assert.Equal(t, "prometheus-prod", actual.Spec.PodMetricsEndpoints[2].Port)
}
19 changes: 10 additions & 9 deletions internal/manifests/collector/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro
)
return nil, nil
}
var sm monitoringv1.ServiceMonitor

sm := monitoringv1.ServiceMonitor{
if params.OtelCol.Spec.Mode == v1alpha1.ModeSidecar {
return nil, nil
}
sm = monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Namespace: params.OtelCol.Namespace,
Name: naming.ServiceMonitor(params.OtelCol.Name),
Expand All @@ -49,7 +53,11 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro
},
},
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{},
Endpoints: append([]monitoringv1.Endpoint{
{
Port: "monitoring",
},
}, endpointsFromConfig(params.Log, params.OtelCol)...),
NamespaceSelector: monitoringv1.NamespaceSelector{
MatchNames: []string{params.OtelCol.Namespace},
},
Expand All @@ -62,13 +70,6 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro
},
}

endpoints := []monitoringv1.Endpoint{
{
Port: "monitoring",
},
}

sm.Spec.Endpoints = append(endpoints, endpointsFromConfig(params.Log, params.OtelCol)...)
return &sm, nil
}

Expand Down
11 changes: 11 additions & 0 deletions internal/manifests/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func MutateFuncFor(existing, desired client.Object) controllerutil.MutateFn {
wantSvcMonitor := desired.(*monitoringv1.ServiceMonitor)
mutateServiceMonitor(svcMonitor, wantSvcMonitor)

case *monitoringv1.PodMonitor:
podMonitor := existing.(*monitoringv1.PodMonitor)
wantPodMonitor := desired.(*monitoringv1.PodMonitor)
mutatePodMonitor(podMonitor, wantPodMonitor)

case *networkingv1.Ingress:
ing := existing.(*networkingv1.Ingress)
wantIng := desired.(*networkingv1.Ingress)
Expand Down Expand Up @@ -245,6 +250,12 @@ func mutateServiceMonitor(existing, desired *monitoringv1.ServiceMonitor) {
existing.Spec = desired.Spec
}

func mutatePodMonitor(existing, desired *monitoringv1.PodMonitor) {
existing.Annotations = desired.Annotations
existing.Labels = desired.Labels
existing.Spec = desired.Spec
}

func mutateService(existing, desired *corev1.Service) error {
existing.Spec.Ports = desired.Spec.Ports
if err := mergeWithOverride(&existing.Spec.Selector, desired.Spec.Selector); err != nil {
Expand Down
7 changes: 6 additions & 1 deletion internal/naming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,16 @@ func ServiceAccount(otelcol string) string {
return DNSName(Truncate("%s-collector", 63, otelcol))
}

// ServiceMonitor builds the service account name based on the instance.
// ServiceMonitor builds the service Monitor name based on the instance.
func ServiceMonitor(otelcol string) string {
return DNSName(Truncate("%s-collector", 63, otelcol))
}

// PodMonitor builds the pod Monitor name based on the instance.
func PodMonitor(otelcol string) string {
return DNSName(Truncate("%s-collector", 63, otelcol))
}

// TargetAllocatorServiceAccount returns the TargetAllocator service account resource name.
func TargetAllocatorServiceAccount(otelcol string) string {
return DNSName(Truncate("%s-targetallocator", 63, otelcol))
Expand Down
Loading
Loading