Skip to content

Commit

Permalink
Add Observability field to Target Allocator CRD (#2732)
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm authored Mar 7, 2024
1 parent b27f4e3 commit 1dc43eb
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 2 deletions.
6 changes: 6 additions & 0 deletions apis/v1beta1/targetallocator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ type TargetAllocatorSpec struct {
// PrometheusCR defines the configuration for the retrieval of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1 and podmonitor.monitoring.coreos.com/v1 ).
// +optional
PrometheusCR TargetAllocatorPrometheusCR `json:"prometheusCR,omitempty"`
// ObservabilitySpec defines how telemetry data gets handled.
//
// +optional
// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Observability"
Observability ObservabilitySpec `json:"observability,omitempty"`
}

// TargetAllocatorPrometheusCR configures Prometheus CustomResource handling in the Target Allocator.
Expand Down
1 change: 1 addition & 0 deletions apis/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/manifests/collector/targetallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,22 @@ func TargetAllocator(params manifests.Params) (*v1beta1.TargetAllocator, error)
NodeSelector: taSpec.NodeSelector,
Resources: taSpec.Resources,
ServiceAccount: taSpec.ServiceAccount,
SecurityContext: taSpec.SecurityContext,
PodSecurityContext: taSpec.PodSecurityContext,
Image: taSpec.Image,
Affinity: taSpec.Affinity,
SecurityContext: taSpec.SecurityContext,
PodSecurityContext: taSpec.PodSecurityContext,
TopologySpreadConstraints: taSpec.TopologySpreadConstraints,
Tolerations: taSpec.Tolerations,
Env: taSpec.Env,
PodAnnotations: params.OtelCol.Spec.PodAnnotations,
PodDisruptionBudget: taSpec.PodDisruptionBudget,
},
CollectorSelector: collectorSelector,
AllocationStrategy: taSpec.AllocationStrategy,
FilterStrategy: taSpec.FilterStrategy,
ScrapeConfigs: scrapeConfigs,
PrometheusCR: taSpec.PrometheusCR,
Observability: taSpec.Observability,
},
}, nil
}
Expand Down
217 changes: 217 additions & 0 deletions internal/manifests/collector/targetallocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ package collector
import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
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/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
Expand All @@ -38,6 +42,11 @@ func TestTargetAllocator(t *testing.T) {
"label_key": "label_value",
},
}
replicas := int32(2)
runAsNonRoot := true
privileged := true
runAsUser := int64(1337)
runasGroup := int64(1338)
otelcolConfig := v1beta1.Config{
Receivers: v1beta1.AnyConfig{
Object: map[string]interface{}{
Expand Down Expand Up @@ -88,6 +97,214 @@ func TestTargetAllocator(t *testing.T) {
},
},
},
{
name: "full",
input: v1beta1.OpenTelemetryCollector{
ObjectMeta: objectMetadata,
Spec: v1beta1.OpenTelemetryCollectorSpec{
TargetAllocator: v1beta1.TargetAllocatorEmbedded{
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: v1beta1.TargetAllocatorAllocationStrategyConsistentHashing,
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: v1beta1.TargetAllocatorPrometheusCR{
Enabled: true,
ScrapeInterval: &metav1.Duration{Duration: time.Second},
PodMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"podmonitorkey": "podmonitorvalue"},
},
ServiceMonitorSelector: &metav1.LabelSelector{
MatchLabels: 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: v1beta1.ObservabilitySpec{
Metrics: v1beta1.MetricsConfigSpec{
EnableMetrics: true,
},
},
PodDisruptionBudget: &v1beta1.PodDisruptionBudgetSpec{
MaxUnavailable: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 1,
},
},
},
Config: otelcolConfig,
},
},
want: &v1beta1.TargetAllocator{
ObjectMeta: objectMetadata,
Spec: v1beta1.TargetAllocatorSpec{
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{
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"),
},
},
ServiceAccount: "serviceAccountName",
Image: "custom_image",
Affinity: &v1.Affinity{
NodeAffinity: &v1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{
{
Key: "node",
Operator: v1.NodeSelectorOpIn,
Values: []string{"test-node"},
},
},
},
},
},
},
},
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",
},
},
},
},

PodDisruptionBudget: &v1beta1.PodDisruptionBudgetSpec{
MaxUnavailable: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 1,
},
},
},
CollectorSelector: metav1.LabelSelector{
MatchLabels: manifestutils.SelectorLabels(objectMetadata, ComponentOpenTelemetryCollector),
},
AllocationStrategy: v1beta1.TargetAllocatorAllocationStrategyConsistentHashing,
FilterStrategy: v1beta1.TargetAllocatorFilterStrategyRelabelConfig,
PrometheusCR: v1beta1.TargetAllocatorPrometheusCR{
Enabled: true,
ScrapeInterval: &metav1.Duration{Duration: time.Second},
PodMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"podmonitorkey": "podmonitorvalue"},
},
ServiceMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"servicemonitorkey": "servicemonitorkey"},
},
},
ScrapeConfigs: []v1beta1.AnyConfig{},
Observability: v1beta1.ObservabilitySpec{
Metrics: v1beta1.MetricsConfigSpec{
EnableMetrics: true,
},
},
},
},
},
}

for _, testCase := range testCases {
Expand Down

0 comments on commit 1dc43eb

Please sign in to comment.