diff --git a/apis/v1alpha2/allocation_strategy.go b/apis/v1alpha2/allocation_strategy.go index cc1ac4c1e8..9b710920de 100644 --- a/apis/v1alpha2/allocation_strategy.go +++ b/apis/v1alpha2/allocation_strategy.go @@ -31,5 +31,5 @@ const ( TargetAllocatorAllocationStrategyConsistentHashing TargetAllocatorAllocationStrategy = "consistent-hashing" // TargetAllocatorFilterStrategyRelabelConfig targets will be consistently drops targets based on the relabel_config. - TargetAllocatorFilterStrategyRelabelConfig TargetAllocatorFilterStrategy = "consistent-hashing" + TargetAllocatorFilterStrategyRelabelConfig TargetAllocatorFilterStrategy = "relabel-config" ) diff --git a/apis/v1alpha2/opentelemetrycollector_types.go b/apis/v1alpha2/opentelemetrycollector_types.go index 74d844da1b..3521afcdaf 100644 --- a/apis/v1alpha2/opentelemetrycollector_types.go +++ b/apis/v1alpha2/opentelemetrycollector_types.go @@ -18,6 +18,7 @@ package v1alpha2 import ( appsv1 "k8s.io/api/apps/v1" + v1 "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -120,7 +121,7 @@ type OpenTelemetryCollectorSpec struct { OpenTelemetryCommonFields `json:",inline"` // TargetAllocator indicates a value which determines whether to spawn a target allocation resource or not. // +optional - TargetAllocator v1alpha1.OpenTelemetryTargetAllocator `json:"targetAllocator,omitempty"` + TargetAllocator TargetAllocatorEmbedded `json:"targetAllocator,omitempty"` // Mode represents how the collector should be deployed (deployment, daemonset, statefulset or sidecar) // +optional Mode Mode `json:"mode,omitempty"` @@ -165,6 +166,84 @@ type OpenTelemetryCollectorSpec struct { DeploymentUpdateStrategy appsv1.DeploymentStrategy `json:"deploymentUpdateStrategy,omitempty"` } +// TargetAllocatorEmbedded defines the configuration for the Prometheus target allocator, embedded in the +// OpenTelemetryCollector spec. +type TargetAllocatorEmbedded struct { + // Replicas is the number of pod instances for the underlying TargetAllocator. This should only be set to a value + // other than 1 if a strategy that allows for high availability is chosen. Currently, the only allocation strategy + // that can be run in a high availability mode is consistent-hashing. + // +optional + Replicas *int32 `json:"replicas,omitempty"` + // NodeSelector to schedule OpenTelemetry TargetAllocator pods. + // +optional + NodeSelector map[string]string `json:"nodeSelector,omitempty"` + // Resources to set on the OpenTelemetryTargetAllocator containers. + // +optional + Resources v1.ResourceRequirements `json:"resources,omitempty"` + // AllocationStrategy determines which strategy the target allocator should use for allocation. + // The current options are least-weighted, consistent-hashing and per-node. The default is + // consistent-hashing. + // +optional + // +kubebuilder:default:=consistent-hashing + AllocationStrategy TargetAllocatorAllocationStrategy `json:"allocationStrategy,omitempty"` + // FilterStrategy determines how to filter targets before allocating them among the collectors. + // The only current option is relabel-config (drops targets based on prom relabel_config). + // The default is relabel-config. + // +optional + // +kubebuilder:default:=relabel-config + FilterStrategy TargetAllocatorFilterStrategy `json:"filterStrategy,omitempty"` + // ServiceAccount indicates the name of an existing service account to use with this instance. When set, + // the operator will not automatically create a ServiceAccount for the TargetAllocator. + // +optional + ServiceAccount string `json:"serviceAccount,omitempty"` + // Image indicates the container image to use for the OpenTelemetry TargetAllocator. + // +optional + Image string `json:"image,omitempty"` + // Enabled indicates whether to use a target allocation mechanism for Prometheus targets or not. + // +optional + Enabled bool `json:"enabled,omitempty"` + // If specified, indicates the pod's scheduling constraints + // +optional + Affinity *v1.Affinity `json:"affinity,omitempty"` + // PrometheusCR defines the configuration for the retrieval of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1 and podmonitor.monitoring.coreos.com/v1 ) retrieval. + // All CR instances which the ServiceAccount has access to will be retrieved. This includes other namespaces. + // +optional + PrometheusCR TargetAllocatorPrometheusCR `json:"prometheusCR,omitempty"` + // SecurityContext configures the container security context for + // the targetallocator. + // +optional + SecurityContext *v1.SecurityContext `json:"securityContext,omitempty"` + // PodSecurityContext configures the pod security context for the + // targetallocator. + // +optional + PodSecurityContext *v1.PodSecurityContext `json:"podSecurityContext,omitempty"` + // TopologySpreadConstraints embedded kubernetes pod configuration option, + // controls how pods are spread across your cluster among failure-domains + // such as regions, zones, nodes, and other user-defined topology domains + // https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/ + // +optional + TopologySpreadConstraints []v1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"` + // Toleration embedded kubernetes pod configuration option, + // controls how pods can be scheduled with matching taints + // +optional + Tolerations []v1.Toleration `json:"tolerations,omitempty"` + // ENV vars to set on the OpenTelemetry TargetAllocator's Pods. These can then in certain cases be + // consumed in the config file for the TargetAllocator. + // +optional + Env []v1.EnvVar `json:"env,omitempty"` + // ObservabilitySpec defines how telemetry data gets handled. + // + // +optional + // +kubebuilder:validation:Optional + // +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Observability" + Observability v1alpha1.ObservabilitySpec `json:"observability,omitempty"` + // PodDisruptionBudget specifies the pod disruption budget configuration to use + // for the target allocator workload. + // + // +optional + PodDisruptionBudget *PodDisruptionBudgetSpec `json:"podDisruptionBudget,omitempty"` +} + // OpenTelemetryCollectorStatus defines the observed state of OpenTelemetryCollector. type OpenTelemetryCollectorStatus struct { // Scale is the OpenTelemetryCollector's scale subresource status. diff --git a/apis/v1alpha2/zz_generated.deepcopy.go b/apis/v1alpha2/zz_generated.deepcopy.go index afa8717bbd..3ceafb521c 100644 --- a/apis/v1alpha2/zz_generated.deepcopy.go +++ b/apis/v1alpha2/zz_generated.deepcopy.go @@ -934,6 +934,77 @@ func (in *TargetAllocator) DeepCopyObject() runtime.Object { return nil } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TargetAllocatorEmbedded) DeepCopyInto(out *TargetAllocatorEmbedded) { + *out = *in + if in.Replicas != nil { + in, out := &in.Replicas, &out.Replicas + *out = new(int32) + **out = **in + } + if in.NodeSelector != nil { + in, out := &in.NodeSelector, &out.NodeSelector + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + in.Resources.DeepCopyInto(&out.Resources) + if in.Affinity != nil { + in, out := &in.Affinity, &out.Affinity + *out = new(v1.Affinity) + (*in).DeepCopyInto(*out) + } + in.PrometheusCR.DeepCopyInto(&out.PrometheusCR) + if in.SecurityContext != nil { + in, out := &in.SecurityContext, &out.SecurityContext + *out = new(v1.SecurityContext) + (*in).DeepCopyInto(*out) + } + if in.PodSecurityContext != nil { + in, out := &in.PodSecurityContext, &out.PodSecurityContext + *out = new(v1.PodSecurityContext) + (*in).DeepCopyInto(*out) + } + if in.TopologySpreadConstraints != nil { + in, out := &in.TopologySpreadConstraints, &out.TopologySpreadConstraints + *out = make([]v1.TopologySpreadConstraint, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.Tolerations != nil { + in, out := &in.Tolerations, &out.Tolerations + *out = make([]v1.Toleration, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.Env != nil { + in, out := &in.Env, &out.Env + *out = make([]v1.EnvVar, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + out.Observability = in.Observability + if in.PodDisruptionBudget != nil { + in, out := &in.PodDisruptionBudget, &out.PodDisruptionBudget + *out = new(PodDisruptionBudgetSpec) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TargetAllocatorEmbedded. +func (in *TargetAllocatorEmbedded) DeepCopy() *TargetAllocatorEmbedded { + if in == nil { + return nil + } + out := new(TargetAllocatorEmbedded) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TargetAllocatorList) DeepCopyInto(out *TargetAllocatorList) { *out = *in diff --git a/controllers/builder_test.go b/controllers/builder_test.go index 81c8b8acf1..7b69e16962 100644 --- a/controllers/builder_test.go +++ b/controllers/builder_test.go @@ -1150,10 +1150,10 @@ service: }, Mode: "statefulset", Config: goodConfig, - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, FilterStrategy: "relabel-config", - PrometheusCR: v1alpha1.OpenTelemetryTargetAllocatorPrometheusCR{ + PrometheusCR: v1alpha2.TargetAllocatorPrometheusCR{ Enabled: true, }, }, @@ -1371,12 +1371,8 @@ label_selector: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/part-of: opentelemetry prometheus_cr: - pod_monitor_selector: - matchlabels: {} - matchexpressions: [] - service_monitor_selector: - matchlabels: {} - matchexpressions: [] + pod_monitor_selector: null + service_monitor_selector: null `, }, }, @@ -1409,7 +1405,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "51477b182d2c9e7c0db27a2cbc9c7d35b24895b1cf0774d51a41b8d1753696ed", + "opentelemetry-targetallocator-config/hash": "59307aaa5652c8723f7803aa2d2b631389d1a0267444a4a8dc559878b5c4aa2c", }, }, Spec: corev1.PodSpec{ @@ -1546,9 +1542,9 @@ prometheus_cr: }, Mode: "statefulset", Config: goodConfig, - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, - PrometheusCR: v1alpha1.OpenTelemetryTargetAllocatorPrometheusCR{ + PrometheusCR: v1alpha2.TargetAllocatorPrometheusCR{ Enabled: true, }, FilterStrategy: "relabel-config", @@ -1772,12 +1768,8 @@ label_selector: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/part-of: opentelemetry prometheus_cr: - pod_monitor_selector: - matchlabels: {} - matchexpressions: [] - service_monitor_selector: - matchlabels: {} - matchexpressions: [] + pod_monitor_selector: null + service_monitor_selector: null `, }, }, @@ -1810,7 +1802,7 @@ prometheus_cr: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "51477b182d2c9e7c0db27a2cbc9c7d35b24895b1cf0774d51a41b8d1753696ed", + "opentelemetry-targetallocator-config/hash": "59307aaa5652c8723f7803aa2d2b631389d1a0267444a4a8dc559878b5c4aa2c", }, }, Spec: corev1.PodSpec{ diff --git a/controllers/reconcile_test.go b/controllers/reconcile_test.go index 92357129d0..1e97ab8397 100644 --- a/controllers/reconcile_test.go +++ b/controllers/reconcile_test.go @@ -468,6 +468,8 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) { taConfig["config"] = promConfig["config"] taConfig["allocation_strategy"] = "consistent-hashing" taConfig["filter_strategy"] = "relabel-config" + taConfig["pod_monitor_selector"] = map[string]string{} + taConfig["service_monitor_selector"] = map[string]string{} taConfig["prometheus_cr"] = map[string]any{ "scrape_interval": "30s", "pod_monitor_selector": &metav1.LabelSelector{}, diff --git a/internal/api/convert/v1alpha.go b/internal/api/convert/v1alpha.go index 1a6ed18313..4c5331086b 100644 --- a/internal/api/convert/v1alpha.go +++ b/internal/api/convert/v1alpha.go @@ -18,6 +18,7 @@ import ( "errors" "gopkg.in/yaml.v3" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" @@ -93,30 +94,7 @@ func V1Alpha1to2(in v1alpha1.OpenTelemetryCollector) (v1alpha2.OpenTelemetryColl out.Spec.OpenTelemetryCommonFields.InitContainers = copy.Spec.InitContainers out.Spec.OpenTelemetryCommonFields.AdditionalContainers = copy.Spec.AdditionalContainers - out.Spec.TargetAllocator.Replicas = copy.Spec.TargetAllocator.Replicas - out.Spec.TargetAllocator.NodeSelector = copy.Spec.TargetAllocator.NodeSelector - out.Spec.TargetAllocator.Resources = copy.Spec.TargetAllocator.Resources - out.Spec.TargetAllocator.AllocationStrategy = copy.Spec.TargetAllocator.AllocationStrategy - out.Spec.TargetAllocator.FilterStrategy = copy.Spec.TargetAllocator.FilterStrategy - out.Spec.TargetAllocator.ServiceAccount = copy.Spec.TargetAllocator.ServiceAccount - out.Spec.TargetAllocator.Image = copy.Spec.TargetAllocator.Image - out.Spec.TargetAllocator.Enabled = copy.Spec.TargetAllocator.Enabled - out.Spec.TargetAllocator.Affinity = copy.Spec.TargetAllocator.Affinity - out.Spec.TargetAllocator.PrometheusCR.Enabled = copy.Spec.TargetAllocator.PrometheusCR.Enabled - out.Spec.TargetAllocator.PrometheusCR.ScrapeInterval = copy.Spec.TargetAllocator.PrometheusCR.ScrapeInterval - out.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector = copy.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector - out.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector = copy.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector - out.Spec.TargetAllocator.SecurityContext = copy.Spec.TargetAllocator.SecurityContext - out.Spec.TargetAllocator.PodSecurityContext = copy.Spec.TargetAllocator.PodSecurityContext - out.Spec.TargetAllocator.TopologySpreadConstraints = copy.Spec.TargetAllocator.TopologySpreadConstraints - out.Spec.TargetAllocator.Tolerations = copy.Spec.TargetAllocator.Tolerations - out.Spec.TargetAllocator.Env = copy.Spec.TargetAllocator.Env - out.Spec.TargetAllocator.Observability = v1alpha1.ObservabilitySpec{ - Metrics: v1alpha1.MetricsConfigSpec{ - EnableMetrics: copy.Spec.TargetAllocator.Observability.Metrics.EnableMetrics, - }, - } - out.Spec.TargetAllocator.PodDisruptionBudget = copy.Spec.TargetAllocator.PodDisruptionBudget + out.Spec.TargetAllocator = TargetAllocatorEmbedded(copy.Spec.TargetAllocator) out.Spec.Mode = v1alpha2.Mode(copy.Spec.Mode) out.Spec.UpgradeStrategy = v1alpha2.UpgradeStrategy(copy.Spec.UpgradeStrategy) @@ -148,3 +126,42 @@ func V1Alpha1to2(in v1alpha1.OpenTelemetryCollector) (v1alpha2.OpenTelemetryColl return out, nil } + +func TargetAllocatorEmbedded(in v1alpha1.OpenTelemetryTargetAllocator) v1alpha2.TargetAllocatorEmbedded { + out := v1alpha2.TargetAllocatorEmbedded{} + out.Replicas = in.Replicas + out.NodeSelector = in.NodeSelector + out.Resources = in.Resources + out.AllocationStrategy = v1alpha2.TargetAllocatorAllocationStrategy(in.AllocationStrategy) + out.FilterStrategy = v1alpha2.TargetAllocatorFilterStrategy(in.FilterStrategy) + out.ServiceAccount = in.ServiceAccount + out.Image = in.Image + out.Enabled = in.Enabled + out.Affinity = in.Affinity + out.PrometheusCR.Enabled = in.PrometheusCR.Enabled + out.PrometheusCR.ScrapeInterval = in.PrometheusCR.ScrapeInterval + out.SecurityContext = in.SecurityContext + out.PodSecurityContext = in.PodSecurityContext + out.TopologySpreadConstraints = in.TopologySpreadConstraints + out.Tolerations = in.Tolerations + out.Env = in.Env + out.Observability = v1alpha1.ObservabilitySpec{ + Metrics: v1alpha1.MetricsConfigSpec{ + EnableMetrics: in.Observability.Metrics.EnableMetrics, + }, + } + + out.PrometheusCR.PodMonitorSelector = &metav1.LabelSelector{ + MatchLabels: in.PrometheusCR.PodMonitorSelector, + } + out.PrometheusCR.ServiceMonitorSelector = &metav1.LabelSelector{ + MatchLabels: in.PrometheusCR.ServiceMonitorSelector, + } + if in.PodDisruptionBudget != nil { + out.PodDisruptionBudget = &v1alpha2.PodDisruptionBudgetSpec{ + MinAvailable: in.PodDisruptionBudget.MinAvailable, + MaxUnavailable: in.PodDisruptionBudget.MaxUnavailable, + } + } + return out +} diff --git a/internal/api/convert/v1alpha_test.go b/internal/api/convert/v1alpha_test.go index 8d3bac8297..3d7f959336 100644 --- a/internal/api/convert/v1alpha_test.go +++ b/internal/api/convert/v1alpha_test.go @@ -16,11 +16,17 @@ package convert import ( "testing" + "time" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" ) func Test_V1Alpha1to2(t *testing.T) { @@ -69,3 +75,136 @@ service: assert.ErrorContains(t, err, "could not convert config json to v1alpha2.Config") }) } + +func Test_TargetAllocator(t *testing.T) { + replicas := int32(2) + runAsNonRoot := true + privileged := true + runAsUser := int64(1337) + runasGroup := int64(1338) + input := v1alpha1.OpenTelemetryTargetAllocator{ + Replicas: &replicas, + NodeSelector: map[string]string{"key": "value"}, + Resources: v1.ResourceRequirements{ + Limits: v1.ResourceList{ + v1.ResourceCPU: resource.MustParse("500m"), + v1.ResourceMemory: resource.MustParse("128Mi"), + }, + Requests: v1.ResourceList{ + v1.ResourceCPU: resource.MustParse("500m"), + v1.ResourceMemory: resource.MustParse("128Mi"), + }, + }, + AllocationStrategy: v1alpha1.OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing, + FilterStrategy: "relabel-config", + ServiceAccount: "serviceAccountName", + Image: "custom_image", + Enabled: true, + Affinity: &v1.Affinity{ + NodeAffinity: &v1.NodeAffinity{ + RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{ + NodeSelectorTerms: []v1.NodeSelectorTerm{ + { + MatchExpressions: []v1.NodeSelectorRequirement{ + { + Key: "node", + Operator: v1.NodeSelectorOpIn, + Values: []string{"test-node"}, + }, + }, + }, + }, + }, + }, + }, + PrometheusCR: v1alpha1.OpenTelemetryTargetAllocatorPrometheusCR{ + Enabled: true, + ScrapeInterval: &metav1.Duration{Duration: time.Second}, + PodMonitorSelector: map[string]string{"podmonitorkey": "podmonitorvalue"}, + ServiceMonitorSelector: map[string]string{"servicemonitorkey": "servicemonitorkey"}, + }, + PodSecurityContext: &v1.PodSecurityContext{ + RunAsNonRoot: &runAsNonRoot, + RunAsUser: &runAsUser, + RunAsGroup: &runasGroup, + }, + SecurityContext: &v1.SecurityContext{ + RunAsUser: &runAsUser, + Privileged: &privileged, + }, + TopologySpreadConstraints: []v1.TopologySpreadConstraint{ + { + MaxSkew: 1, + TopologyKey: "kubernetes.io/hostname", + WhenUnsatisfiable: "DoNotSchedule", + LabelSelector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "foo": "bar", + }, + }, + }, + }, + Tolerations: []v1.Toleration{ + { + Key: "hii", + Value: "greeting", + Effect: "NoSchedule", + }, + }, + Env: []v1.EnvVar{ + { + Name: "POD_NAME", + ValueFrom: &v1.EnvVarSource{ + FieldRef: &v1.ObjectFieldSelector{ + FieldPath: "metadata.name", + }, + }, + }, + }, + Observability: v1alpha1.ObservabilitySpec{ + Metrics: v1alpha1.MetricsConfigSpec{ + EnableMetrics: true, + }, + }, + PodDisruptionBudget: &v1alpha1.PodDisruptionBudgetSpec{ + MaxUnavailable: &intstr.IntOrString{ + Type: intstr.Int, + IntVal: 1, + }, + }, + } + + expected := v1alpha2.TargetAllocatorEmbedded{ + Replicas: input.Replicas, + NodeSelector: input.NodeSelector, + Resources: input.Resources, + AllocationStrategy: v1alpha2.TargetAllocatorAllocationStrategyConsistentHashing, + FilterStrategy: v1alpha2.TargetAllocatorFilterStrategyRelabelConfig, + ServiceAccount: input.ServiceAccount, + Image: input.Image, + Enabled: input.Enabled, + Affinity: input.Affinity, + PrometheusCR: v1alpha2.TargetAllocatorPrometheusCR{ + Enabled: input.PrometheusCR.Enabled, + ScrapeInterval: input.PrometheusCR.ScrapeInterval, + PodMonitorSelector: &metav1.LabelSelector{ + MatchLabels: input.PrometheusCR.PodMonitorSelector, + }, + ServiceMonitorSelector: &metav1.LabelSelector{ + MatchLabels: input.PrometheusCR.ServiceMonitorSelector, + }, + }, + SecurityContext: input.SecurityContext, + PodSecurityContext: input.PodSecurityContext, + TopologySpreadConstraints: input.TopologySpreadConstraints, + Tolerations: input.Tolerations, + Env: input.Env, + Observability: input.Observability, + PodDisruptionBudget: &v1alpha2.PodDisruptionBudgetSpec{ + MinAvailable: input.PodDisruptionBudget.MinAvailable, + MaxUnavailable: input.PodDisruptionBudget.MaxUnavailable, + }, + } + + assert.Equal(t, expected, TargetAllocatorEmbedded(input)) +} diff --git a/internal/manifests/collector/suite_test.go b/internal/manifests/collector/suite_test.go index 9ed61939bf..88975a4fdb 100644 --- a/internal/manifests/collector/suite_test.go +++ b/internal/manifests/collector/suite_test.go @@ -26,7 +26,6 @@ import ( "k8s.io/client-go/tools/record" logf "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift" "github.com/open-telemetry/opentelemetry-operator/internal/config" @@ -147,7 +146,7 @@ func newParams(taContainerImage string, file string) (manifests.Params, error) { Replicas: &replicas, }, Mode: v1alpha2.ModeStatefulSet, - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, Image: taContainerImage, }, diff --git a/internal/manifests/targetallocator/configmap.go b/internal/manifests/targetallocator/configmap.go index 5c6c4f27ea..e4ddb5182a 100644 --- a/internal/manifests/targetallocator/configmap.go +++ b/internal/manifests/targetallocator/configmap.go @@ -19,7 +19,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" "github.com/open-telemetry/opentelemetry-operator/internal/manifests" "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector" "github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils" @@ -64,7 +64,7 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) { if len(params.OtelCol.Spec.TargetAllocator.AllocationStrategy) > 0 { taConfig["allocation_strategy"] = params.OtelCol.Spec.TargetAllocator.AllocationStrategy } else { - taConfig["allocation_strategy"] = v1alpha1.OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing + taConfig["allocation_strategy"] = v1alpha2.TargetAllocatorAllocationStrategyConsistentHashing } taConfig["filter_strategy"] = params.OtelCol.Spec.TargetAllocator.FilterStrategy @@ -73,22 +73,20 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) { prometheusCRConfig["scrape_interval"] = params.OtelCol.Spec.TargetAllocator.PrometheusCR.ScrapeInterval.Duration } - prometheusCRConfig["service_monitor_selector"] = &metav1.LabelSelector{ - MatchLabels: params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector, - } + prometheusCRConfig["service_monitor_selector"] = params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector + // The below instruction is here for compatibility with the previous target allocator version // TODO: Drop it after 3 more versions if params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector != nil { - taConfig["service_monitor_selector"] = ¶ms.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector + taConfig["service_monitor_selector"] = ¶ms.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector.MatchLabels } - prometheusCRConfig["pod_monitor_selector"] = &metav1.LabelSelector{ - MatchLabels: params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector, - } + prometheusCRConfig["pod_monitor_selector"] = params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector + // The below instruction is here for compatibility with the previous target allocator version // TODO: Drop it after 3 more versions if params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector != nil { - taConfig["pod_monitor_selector"] = ¶ms.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector + taConfig["pod_monitor_selector"] = ¶ms.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector.MatchLabels } if len(prometheusCRConfig) > 0 { diff --git a/internal/manifests/targetallocator/configmap_test.go b/internal/manifests/targetallocator/configmap_test.go index 0ea684bf1a..ee097d8660 100644 --- a/internal/manifests/targetallocator/configmap_test.go +++ b/internal/manifests/targetallocator/configmap_test.go @@ -61,12 +61,8 @@ label_selector: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/part-of: opentelemetry prometheus_cr: - pod_monitor_selector: - matchlabels: {} - matchexpressions: [] - service_monitor_selector: - matchlabels: {} - matchexpressions: [] + pod_monitor_selector: null + service_monitor_selector: null `, } instance := collectorInstance() @@ -126,11 +122,15 @@ service_monitor_selector: `, } instance := collectorInstance() - instance.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector = map[string]string{ - "release": "my-instance", + instance.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector = &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "release": "my-instance", + }, } - instance.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector = map[string]string{ - "release": "my-instance", + instance.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector = &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "release": "my-instance", + }, } cfg := config.New() params := manifests.Params{ @@ -173,13 +173,9 @@ label_selector: app.kubernetes.io/managed-by: opentelemetry-operator app.kubernetes.io/part-of: opentelemetry prometheus_cr: - pod_monitor_selector: - matchlabels: {} - matchexpressions: [] + pod_monitor_selector: null scrape_interval: 30s - service_monitor_selector: - matchlabels: {} - matchexpressions: [] + service_monitor_selector: null `, } diff --git a/internal/manifests/targetallocator/container_test.go b/internal/manifests/targetallocator/container_test.go index 5cdb91db3c..9957740c9a 100644 --- a/internal/manifests/targetallocator/container_test.go +++ b/internal/manifests/targetallocator/container_test.go @@ -25,7 +25,6 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" logf "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" "github.com/open-telemetry/opentelemetry-operator/internal/config" "github.com/open-telemetry/opentelemetry-operator/internal/naming" @@ -49,7 +48,7 @@ func TestContainerWithImageOverridden(t *testing.T) { // prepare otelcol := v1alpha2.OpenTelemetryCollector{ Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, Image: "overridden-image", }, @@ -68,7 +67,7 @@ func TestContainerPorts(t *testing.T) { // prepare otelcol := v1alpha2.OpenTelemetryCollector{ Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, Image: "default-image", }, @@ -89,7 +88,7 @@ func TestContainerVolumes(t *testing.T) { // prepare otelcol := v1alpha2.OpenTelemetryCollector{ Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, Image: "default-image", }, @@ -108,7 +107,7 @@ func TestContainerVolumes(t *testing.T) { func TestContainerResourceRequirements(t *testing.T) { otelcol := v1alpha2.OpenTelemetryCollector{ Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Resources: corev1.ResourceRequirements{ Limits: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("100m"), @@ -146,7 +145,7 @@ func TestContainerHasEnvVars(t *testing.T) { // prepare otelcol := v1alpha2.OpenTelemetryCollector{ Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, Env: []corev1.EnvVar{ { @@ -231,7 +230,7 @@ func TestContainerHasProxyEnvVars(t *testing.T) { // prepare otelcol := v1alpha2.OpenTelemetryCollector{ Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, Env: []corev1.EnvVar{ { @@ -257,7 +256,7 @@ func TestContainerDoesNotOverrideEnvVars(t *testing.T) { // prepare otelcol := v1alpha2.OpenTelemetryCollector{ Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, Env: []corev1.EnvVar{ { @@ -323,7 +322,7 @@ func TestContainerDoesNotOverrideEnvVars(t *testing.T) { func TestReadinessProbe(t *testing.T) { otelcol := v1alpha2.OpenTelemetryCollector{ Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, }, }, @@ -348,7 +347,7 @@ func TestLivenessProbe(t *testing.T) { // prepare otelcol := v1alpha2.OpenTelemetryCollector{ Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, }, }, @@ -378,7 +377,7 @@ func TestSecurityContext(t *testing.T) { // prepare otelcol := v1alpha2.OpenTelemetryCollector{ Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Enabled: true, SecurityContext: securityContext, }, diff --git a/internal/manifests/targetallocator/deployment_test.go b/internal/manifests/targetallocator/deployment_test.go index fffce41a2b..61efa1550f 100644 --- a/internal/manifests/targetallocator/deployment_test.go +++ b/internal/manifests/targetallocator/deployment_test.go @@ -24,7 +24,6 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" "github.com/open-telemetry/opentelemetry-operator/internal/config" "github.com/open-telemetry/opentelemetry-operator/internal/manifests" @@ -104,7 +103,7 @@ func TestDeploymentSecurityContext(t *testing.T) { Name: "my-instance-securitycontext", }, Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ PodSecurityContext: testSecurityContextValue, }, }, @@ -196,7 +195,7 @@ func collectorInstance() v1alpha2.OpenTelemetryCollector { Image: "ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator:0.47.0", }, Config: cfg, - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Image: "ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-targetallocator:0.47.0", FilterStrategy: "relabel-config", }, @@ -229,7 +228,7 @@ func TestDeploymentNodeSelector(t *testing.T) { Name: "my-instance-nodeselector", }, Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ NodeSelector: map[string]string{ "node-key": "node-value", }, @@ -274,7 +273,7 @@ func TestDeploymentAffinity(t *testing.T) { Name: "my-instance-affinity", }, Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Affinity: testAffinityValue, }, }, @@ -318,7 +317,7 @@ func TestDeploymentTolerations(t *testing.T) { Name: "my-instance-toleration", }, Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ Tolerations: testTolerationValues, }, }, @@ -363,7 +362,7 @@ func TestDeploymentTopologySpreadConstraints(t *testing.T) { Name: "my-instance-topologyspreadconstraint", }, Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ TopologySpreadConstraints: testTopologySpreadConstraintValue, }, }, diff --git a/internal/manifests/targetallocator/poddisruptionbudget.go b/internal/manifests/targetallocator/poddisruptionbudget.go index f7d5d21a5e..ed4a441500 100644 --- a/internal/manifests/targetallocator/poddisruptionbudget.go +++ b/internal/manifests/targetallocator/poddisruptionbudget.go @@ -17,7 +17,7 @@ package targetallocator import ( "fmt" - "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" "github.com/open-telemetry/opentelemetry-operator/internal/manifests" "github.com/open-telemetry/opentelemetry-operator/internal/naming" @@ -35,7 +35,7 @@ func PodDisruptionBudget(params manifests.Params) (*policyV1.PodDisruptionBudget // defaulter doesn't set PodDisruptionBudget if the strategy isn't valid, // if PodDisruptionBudget != nil and stategy isn't correct, users have set // it wrongly - if params.OtelCol.Spec.TargetAllocator.AllocationStrategy != v1alpha1.OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing { + if params.OtelCol.Spec.TargetAllocator.AllocationStrategy != v1alpha2.TargetAllocatorAllocationStrategyConsistentHashing { params.Log.V(4).Info("current allocation strategy not compatible, skipping podDisruptionBudget creation") return nil, fmt.Errorf("target allocator pdb has been configured but the allocation strategy isn't not compatible") } diff --git a/internal/manifests/targetallocator/poddisruptionbudget_test.go b/internal/manifests/targetallocator/poddisruptionbudget_test.go index e2ccbdb04a..88a3097a1c 100644 --- a/internal/manifests/targetallocator/poddisruptionbudget_test.go +++ b/internal/manifests/targetallocator/poddisruptionbudget_test.go @@ -21,7 +21,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" - "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" "github.com/open-telemetry/opentelemetry-operator/internal/config" "github.com/open-telemetry/opentelemetry-operator/internal/manifests" @@ -72,12 +71,12 @@ func TestPDBWithValidStrategy(t *testing.T) { Name: "my-instance", }, Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ - PodDisruptionBudget: &v1alpha1.PodDisruptionBudgetSpec{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ + PodDisruptionBudget: &v1alpha2.PodDisruptionBudgetSpec{ MinAvailable: test.MinAvailable, MaxUnavailable: test.MaxUnavailable, }, - AllocationStrategy: v1alpha1.OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing, + AllocationStrategy: v1alpha2.TargetAllocatorAllocationStrategyConsistentHashing, }, }, } @@ -106,12 +105,12 @@ func TestPDBWithNotValidStrategy(t *testing.T) { Name: "my-instance", }, Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ - PodDisruptionBudget: &v1alpha1.PodDisruptionBudgetSpec{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ + PodDisruptionBudget: &v1alpha2.PodDisruptionBudgetSpec{ MinAvailable: test.MinAvailable, MaxUnavailable: test.MaxUnavailable, }, - AllocationStrategy: v1alpha1.OpenTelemetryTargetAllocatorAllocationStrategyLeastWeighted, + AllocationStrategy: v1alpha2.TargetAllocatorAllocationStrategyLeastWeighted, }, }, } @@ -135,8 +134,8 @@ func TestNoPDB(t *testing.T) { Name: "my-instance", }, Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ - AllocationStrategy: v1alpha1.OpenTelemetryTargetAllocatorAllocationStrategyLeastWeighted, + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ + AllocationStrategy: v1alpha2.TargetAllocatorAllocationStrategyLeastWeighted, }, }, } diff --git a/internal/manifests/targetallocator/serviceaccount_test.go b/internal/manifests/targetallocator/serviceaccount_test.go index 953a9b7770..bf31c21422 100644 --- a/internal/manifests/targetallocator/serviceaccount_test.go +++ b/internal/manifests/targetallocator/serviceaccount_test.go @@ -21,7 +21,6 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" "github.com/open-telemetry/opentelemetry-operator/internal/manifests" ) @@ -48,7 +47,7 @@ func TestServiceAccountOverrideName(t *testing.T) { Name: "my-instance", }, Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ ServiceAccount: "my-special-sa", }, }, @@ -92,7 +91,7 @@ func TestServiceAccountOverride(t *testing.T) { Name: "my-instance", }, Spec: v1alpha2.OpenTelemetryCollectorSpec{ - TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{ + TargetAllocator: v1alpha2.TargetAllocatorEmbedded{ ServiceAccount: "my-special-sa", }, },