|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package operatormetrics |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + |
| 22 | + monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 27 | + "k8s.io/client-go/rest" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + // namespaceFile is the path to the namespace file for the service account. |
| 33 | + namespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" |
| 34 | + |
| 35 | + // caBundleConfigMap declares the name of the config map for the CA bundle. |
| 36 | + caBundleConfigMap = "serving-certs-ca-bundle" |
| 37 | + |
| 38 | + // prometheusCAFile declares the path for prometheus CA file for service monitors in OpenShift. |
| 39 | + prometheusCAFile = fmt.Sprintf("/etc/prometheus/configmaps/%s/service-ca.crt", caBundleConfigMap) |
| 40 | + |
| 41 | + // nolint #nosec |
| 42 | + // bearerTokenFile declares the path for bearer token file for service monitors. |
| 43 | + bearerTokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token" |
| 44 | + |
| 45 | + // openshiftInClusterMonitoringNamespace declares the namespace for the OpenShift in-cluster monitoring. |
| 46 | + openshiftInClusterMonitoringNamespace = "openshift-monitoring" |
| 47 | +) |
| 48 | + |
| 49 | +type OperatorMetrics struct { |
| 50 | + kubeClient client.Client |
| 51 | +} |
| 52 | + |
| 53 | +func NewOperatorMetrics(config *rest.Config, scheme *runtime.Scheme) (OperatorMetrics, error) { |
| 54 | + kubeClient, err := client.New(config, client.Options{Scheme: scheme}) |
| 55 | + if err != nil { |
| 56 | + return OperatorMetrics{}, err |
| 57 | + } |
| 58 | + |
| 59 | + return OperatorMetrics{ |
| 60 | + kubeClient: kubeClient, |
| 61 | + }, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (om OperatorMetrics) Start(ctx context.Context) error { |
| 65 | + rawNamespace, err := os.ReadFile(namespaceFile) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("error reading namespace file: %w", err) |
| 68 | + } |
| 69 | + namespace := string(rawNamespace) |
| 70 | + |
| 71 | + var tlsConfig *monitoringv1.TLSConfig |
| 72 | + |
| 73 | + if om.caConfigMapExists() { |
| 74 | + serviceName := fmt.Sprintf("opentelemetry-operator-controller-manager-metrics-service.%s.svc", namespace) |
| 75 | + |
| 76 | + tlsConfig = &monitoringv1.TLSConfig{ |
| 77 | + CAFile: prometheusCAFile, |
| 78 | + SafeTLSConfig: monitoringv1.SafeTLSConfig{ |
| 79 | + ServerName: &serviceName, |
| 80 | + }, |
| 81 | + } |
| 82 | + } else { |
| 83 | + t := true |
| 84 | + tlsConfig = &monitoringv1.TLSConfig{ |
| 85 | + SafeTLSConfig: monitoringv1.SafeTLSConfig{ |
| 86 | + // kube-rbac-proxy uses a self-signed cert by default |
| 87 | + InsecureSkipVerify: &t, |
| 88 | + }, |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + sm := monitoringv1.ServiceMonitor{ |
| 93 | + ObjectMeta: metav1.ObjectMeta{ |
| 94 | + Name: "opentelemetry-operator-metrics-monitor", |
| 95 | + Namespace: namespace, |
| 96 | + Labels: map[string]string{ |
| 97 | + "app.kubernetes.io/name": "opentelemetry-operator", |
| 98 | + "app.kubernetes.io/part-of": "opentelemetry-operator", |
| 99 | + "control-plane": "controller-manager", |
| 100 | + }, |
| 101 | + }, |
| 102 | + Spec: monitoringv1.ServiceMonitorSpec{ |
| 103 | + Selector: metav1.LabelSelector{ |
| 104 | + MatchLabels: map[string]string{ |
| 105 | + "app.kubernetes.io/name": "opentelemetry-operator", |
| 106 | + }, |
| 107 | + }, |
| 108 | + Endpoints: []monitoringv1.Endpoint{ |
| 109 | + { |
| 110 | + BearerTokenFile: bearerTokenFile, |
| 111 | + Interval: "30s", |
| 112 | + Path: "/metrics", |
| 113 | + Scheme: "https", |
| 114 | + ScrapeTimeout: "10s", |
| 115 | + TargetPort: &intstr.IntOrString{IntVal: 8443}, |
| 116 | + TLSConfig: tlsConfig, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + err = om.kubeClient.Create(ctx, &sm) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("error creating service monitor: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + <-ctx.Done() |
| 128 | + |
| 129 | + return om.kubeClient.Delete(ctx, &sm) |
| 130 | +} |
| 131 | + |
| 132 | +func (om OperatorMetrics) NeedLeaderElection() bool { |
| 133 | + return true |
| 134 | +} |
| 135 | + |
| 136 | +func (om OperatorMetrics) caConfigMapExists() bool { |
| 137 | + return om.kubeClient.Get(context.Background(), client.ObjectKey{ |
| 138 | + Name: caBundleConfigMap, |
| 139 | + Namespace: openshiftInClusterMonitoringNamespace, |
| 140 | + }, &corev1.ConfigMap{}, |
| 141 | + ) == nil |
| 142 | +} |
0 commit comments