|
| 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 collector |
| 16 | + |
| 17 | +import ( |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + |
| 20 | + "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1" |
| 21 | + "github.com/open-telemetry/opentelemetry-operator/internal/manifests" |
| 22 | + "github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils" |
| 23 | + "github.com/open-telemetry/opentelemetry-operator/internal/manifests/targetallocator/adapters" |
| 24 | +) |
| 25 | + |
| 26 | +// TargetAllocator builds the TargetAllocator CR for the given instance. |
| 27 | +func TargetAllocator(params manifests.Params) (*v1beta1.TargetAllocator, error) { |
| 28 | + |
| 29 | + taSpec := params.OtelCol.Spec.TargetAllocator |
| 30 | + if !taSpec.Enabled { |
| 31 | + return nil, nil |
| 32 | + } |
| 33 | + |
| 34 | + collectorSelector := metav1.LabelSelector{ |
| 35 | + MatchLabels: manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector), |
| 36 | + } |
| 37 | + |
| 38 | + configStr, err := params.OtelCol.Spec.Config.Yaml() |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + scrapeConfigs, err := getScrapeConfigs(configStr) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + return &v1beta1.TargetAllocator{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: params.OtelCol.Name, |
| 50 | + Namespace: params.OtelCol.Namespace, |
| 51 | + Annotations: params.OtelCol.Annotations, |
| 52 | + Labels: params.OtelCol.Labels, |
| 53 | + }, |
| 54 | + Spec: v1beta1.TargetAllocatorSpec{ |
| 55 | + OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{ |
| 56 | + Replicas: taSpec.Replicas, |
| 57 | + NodeSelector: taSpec.NodeSelector, |
| 58 | + Resources: taSpec.Resources, |
| 59 | + ServiceAccount: taSpec.ServiceAccount, |
| 60 | + SecurityContext: taSpec.SecurityContext, |
| 61 | + PodSecurityContext: taSpec.PodSecurityContext, |
| 62 | + Image: taSpec.Image, |
| 63 | + Affinity: taSpec.Affinity, |
| 64 | + TopologySpreadConstraints: taSpec.TopologySpreadConstraints, |
| 65 | + Tolerations: taSpec.Tolerations, |
| 66 | + Env: taSpec.Env, |
| 67 | + PodAnnotations: params.OtelCol.Spec.PodAnnotations, |
| 68 | + }, |
| 69 | + CollectorSelector: collectorSelector, |
| 70 | + AllocationStrategy: taSpec.AllocationStrategy, |
| 71 | + FilterStrategy: taSpec.FilterStrategy, |
| 72 | + ScrapeConfigs: scrapeConfigs, |
| 73 | + PrometheusCR: taSpec.PrometheusCR, |
| 74 | + }, |
| 75 | + }, nil |
| 76 | +} |
| 77 | + |
| 78 | +func getScrapeConfigs(otelcolConfig string) ([]v1beta1.ScrapeConfig, error) { |
| 79 | + // Collector supports environment variable substitution, but the TA does not. |
| 80 | + // TA Scrape Configs should have a single "$", as it does not support env var substitution |
| 81 | + prometheusReceiverConfig, err := adapters.UnescapeDollarSignsInPromConfig(otelcolConfig) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + scrapeConfigs, err := adapters.GetScrapeConfigsFromPromConfig(prometheusReceiverConfig) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + v1beta1scrapeConfigs := make([]v1beta1.ScrapeConfig, len(scrapeConfigs)) |
| 92 | + |
| 93 | + for i, config := range scrapeConfigs { |
| 94 | + v1beta1scrapeConfigs[i] = config |
| 95 | + } |
| 96 | + |
| 97 | + return v1beta1scrapeConfigs, nil |
| 98 | +} |
0 commit comments