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

[chore] Move TargetAllocator CRD to v1alpha1 #2904

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 29 additions & 51 deletions apis/v1alpha1/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/conversion"

"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/api/common"
)

var _ conversion.Convertible = &OpenTelemetryCollector{}
Expand Down Expand Up @@ -82,8 +83,8 @@ func tov1beta1(in OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error
Image: in.Status.Image,
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{
ManagementState: v1beta1.ManagementStateType(copy.Spec.ManagementState),
OpenTelemetryCommonFields: common.OpenTelemetryCommonFields{
ManagementState: common.ManagementStateType(copy.Spec.ManagementState),
Resources: copy.Spec.Resources,
NodeSelector: copy.Spec.NodeSelector,
Args: copy.Spec.Args,
Expand Down Expand Up @@ -129,8 +130,8 @@ func tov1beta1(in OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error
},
},
LivenessProbe: tov1beta1Probe(copy.Spec.LivenessProbe),
Observability: v1beta1.ObservabilitySpec{
Metrics: v1beta1.MetricsConfigSpec{
Observability: common.ObservabilitySpec{
Metrics: common.MetricsConfigSpec{
EnableMetrics: copy.Spec.Observability.Metrics.EnableMetrics,
DisablePrometheusAnnotations: copy.Spec.Observability.Metrics.DisablePrometheusAnnotations,
},
Expand All @@ -145,11 +146,11 @@ func tov1beta1(in OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error
}, nil
}

func tov1beta1Ports(in []PortsSpec) []v1beta1.PortsSpec {
var ports []v1beta1.PortsSpec
func tov1beta1Ports(in []PortsSpec) []common.PortsSpec {
var ports []common.PortsSpec

for _, p := range in {
ports = append(ports, v1beta1.PortsSpec{
ports = append(ports, common.PortsSpec{
ServicePort: v1.ServicePort{
Name: p.ServicePort.Name,
Protocol: p.ServicePort.Protocol,
Expand Down Expand Up @@ -192,8 +193,8 @@ func tov1beta1TA(in OpenTelemetryTargetAllocator) v1beta1.TargetAllocatorEmbedde
TopologySpreadConstraints: in.TopologySpreadConstraints,
Tolerations: in.Tolerations,
Env: in.Env,
Observability: v1beta1.ObservabilitySpec{
Metrics: v1beta1.MetricsConfigSpec{
Observability: common.ObservabilitySpec{
Metrics: common.MetricsConfigSpec{
EnableMetrics: in.Observability.Metrics.EnableMetrics,
DisablePrometheusAnnotations: in.Observability.Metrics.DisablePrometheusAnnotations,
},
Expand All @@ -203,17 +204,17 @@ func tov1beta1TA(in OpenTelemetryTargetAllocator) v1beta1.TargetAllocatorEmbedde
}

// The conversion takes into account deprecated v1alpha1 spec.minReplicas and spec.maxReplicas.
func tov1beta1Autoscaler(in *AutoscalerSpec, minReplicas, maxReplicas *int32) *v1beta1.AutoscalerSpec {
func tov1beta1Autoscaler(in *AutoscalerSpec, minReplicas, maxReplicas *int32) *common.AutoscalerSpec {
if in == nil && minReplicas == nil && maxReplicas == nil {
return nil
}
if in == nil {
in = &AutoscalerSpec{}
}

var metrics []v1beta1.MetricSpec
var metrics []common.MetricSpec
for _, m := range in.Metrics {
metrics = append(metrics, v1beta1.MetricSpec{
metrics = append(metrics, common.MetricSpec{
Type: m.Type,
Pods: m.Pods,
})
Expand All @@ -225,7 +226,7 @@ func tov1beta1Autoscaler(in *AutoscalerSpec, minReplicas, maxReplicas *int32) *v
in.MinReplicas = minReplicas
}

return &v1beta1.AutoscalerSpec{
return &common.AutoscalerSpec{
MinReplicas: in.MinReplicas,
MaxReplicas: in.MaxReplicas,
Behavior: in.Behavior,
Expand All @@ -235,11 +236,11 @@ func tov1beta1Autoscaler(in *AutoscalerSpec, minReplicas, maxReplicas *int32) *v
}
}

func tov1beta1PodDisruptionBudget(in *PodDisruptionBudgetSpec) *v1beta1.PodDisruptionBudgetSpec {
func tov1beta1PodDisruptionBudget(in *PodDisruptionBudgetSpec) *common.PodDisruptionBudgetSpec {
if in == nil {
return nil
}
return &v1beta1.PodDisruptionBudgetSpec{
return &common.PodDisruptionBudgetSpec{
MinAvailable: in.MinAvailable,
MaxUnavailable: in.MaxUnavailable,
}
Expand Down Expand Up @@ -270,7 +271,7 @@ func tov1beta1ConfigMaps(in []ConfigMapsSpec) []v1beta1.ConfigMapsSpec {
return mapsSpecs
}

func tov1alpha1Ports(in []v1beta1.PortsSpec) []PortsSpec {
func tov1alpha1Ports(in []common.PortsSpec) []PortsSpec {
var ports []PortsSpec

for _, p := range in {
Expand Down Expand Up @@ -368,7 +369,7 @@ func tov1alpha1(in v1beta1.OpenTelemetryCollector) (*OpenTelemetryCollector, err
}, nil
}

func tov1alpha1PodDisruptionBudget(in *v1beta1.PodDisruptionBudgetSpec) *PodDisruptionBudgetSpec {
func tov1alpha1PodDisruptionBudget(in *common.PodDisruptionBudgetSpec) *PodDisruptionBudgetSpec {
if in == nil {
return nil
}
Expand All @@ -392,7 +393,7 @@ func tov1alpha1Probe(in *v1beta1.Probe) *Probe {
}
}

func tov1alpha1Autoscaler(in *v1beta1.AutoscalerSpec) *AutoscalerSpec {
func tov1alpha1Autoscaler(in *common.AutoscalerSpec) *AutoscalerSpec {
if in == nil {
return nil
}
Expand Down Expand Up @@ -440,8 +441,8 @@ func tov1alpha1TA(in v1beta1.TargetAllocatorEmbedded) OpenTelemetryTargetAllocat
Replicas: in.Replicas,
NodeSelector: in.NodeSelector,
Resources: in.Resources,
AllocationStrategy: tov1alpha1TAAllocationStrategy(in.AllocationStrategy),
FilterStrategy: tov1alpha1TAFilterStrategy(in.FilterStrategy),
AllocationStrategy: Tov1alpha1TAAllocationStrategy(in.AllocationStrategy),
FilterStrategy: Tov1alpha1TAFilterStrategy(in.FilterStrategy),
ServiceAccount: in.ServiceAccount,
Image: in.Image,
Enabled: in.Enabled,
Expand All @@ -467,41 +468,18 @@ func tov1alpha1TA(in v1beta1.TargetAllocatorEmbedded) OpenTelemetryTargetAllocat
}
}

func tov1alpha1TAFilterStrategy(strategy v1beta1.TargetAllocatorFilterStrategy) string {
switch strategy {
case v1beta1.TargetAllocatorFilterStrategyRelabelConfig:
return string(strategy)
}
return ""
func Tov1alpha1TAFilterStrategy(strategy v1beta1.TargetAllocatorFilterStrategy) TargetAllocatorFilterStrategy {
return TargetAllocatorFilterStrategy(strategy)
}

func tov1alpha1TAAllocationStrategy(strategy v1beta1.TargetAllocatorAllocationStrategy) OpenTelemetryTargetAllocatorAllocationStrategy {
switch strategy {
case v1beta1.TargetAllocatorAllocationStrategyConsistentHashing:
return OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing
case v1beta1.TargetAllocatorAllocationStrategyPerNode:
return OpenTelemetryTargetAllocatorAllocationStrategyPerNode
case v1beta1.TargetAllocatorAllocationStrategyLeastWeighted:
return OpenTelemetryTargetAllocatorAllocationStrategyLeastWeighted
}
return ""
func Tov1alpha1TAAllocationStrategy(strategy v1beta1.TargetAllocatorAllocationStrategy) TargetAllocatorAllocationStrategy {
return TargetAllocatorAllocationStrategy(strategy)
}

func tov1beta1TAFilterStrategy(strategy string) v1beta1.TargetAllocatorFilterStrategy {
if strategy == string(v1beta1.TargetAllocatorFilterStrategyRelabelConfig) {
return v1beta1.TargetAllocatorFilterStrategyRelabelConfig
}
return ""
func tov1beta1TAFilterStrategy(strategy TargetAllocatorFilterStrategy) v1beta1.TargetAllocatorFilterStrategy {
return v1beta1.TargetAllocatorFilterStrategy(strategy)
}

func tov1beta1TAAllocationStrategy(strategy OpenTelemetryTargetAllocatorAllocationStrategy) v1beta1.TargetAllocatorAllocationStrategy {
switch strategy {
case OpenTelemetryTargetAllocatorAllocationStrategyPerNode:
return v1beta1.TargetAllocatorAllocationStrategyPerNode
case OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing:
return v1beta1.TargetAllocatorAllocationStrategyConsistentHashing
case OpenTelemetryTargetAllocatorAllocationStrategyLeastWeighted:
return v1beta1.TargetAllocatorAllocationStrategyLeastWeighted
}
return ""
func tov1beta1TAAllocationStrategy(strategy TargetAllocatorAllocationStrategy) v1beta1.TargetAllocatorAllocationStrategy {
return v1beta1.TargetAllocatorAllocationStrategy(strategy)
}
7 changes: 4 additions & 3 deletions apis/v1alpha1/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/api/common"
)

const collectorCfg = `---
Expand Down Expand Up @@ -427,7 +428,7 @@ func createTA() OpenTelemetryTargetAllocator {
v1.ResourceMemory: resource.MustParse("128Mi"),
},
},
AllocationStrategy: OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing,
AllocationStrategy: TargetAllocatorAllocationStrategyConsistentHashing,
FilterStrategy: "relabel-config",
ServiceAccount: "serviceAccountName",
Image: "custom_image",
Expand Down Expand Up @@ -527,7 +528,7 @@ func TestConvertTo(t *testing.T) {
Name: "otel",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{
OpenTelemetryCommonFields: common.OpenTelemetryCommonFields{
ServiceAccount: "otelcol",
},
TargetAllocator: v1beta1.TargetAllocatorEmbedded{
Expand All @@ -549,7 +550,7 @@ func TestConvertFrom(t *testing.T) {
Name: "otel",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{
OpenTelemetryCommonFields: common.OpenTelemetryCommonFields{
ServiceAccount: "otelcol",
},
},
Expand Down
4 changes: 2 additions & 2 deletions apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,13 @@ type OpenTelemetryTargetAllocator struct {
// WARNING: The per-node strategy currently ignores targets without a Node, like control plane components.
// +optional
// +kubebuilder:default:=consistent-hashing
AllocationStrategy OpenTelemetryTargetAllocatorAllocationStrategy `json:"allocationStrategy,omitempty"`
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 string `json:"filterStrategy,omitempty"`
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
Expand Down
117 changes: 117 additions & 0 deletions apis/v1alpha1/targetallocator_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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 v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-operator/internal/api/common"
)

func init() {
SchemeBuilder.Register(&TargetAllocator{}, &TargetAllocatorList{})
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// TargetAllocator is the Schema for the targetallocators API.
type TargetAllocator struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec TargetAllocatorSpec `json:"spec,omitempty"`
Status TargetAllocatorStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// TargetAllocatorList contains a list of TargetAllocator.
type TargetAllocatorList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []OpenTelemetryCollector `json:"items"`
}

// TargetAllocatorStatus defines the observed state of Target Allocator.
type TargetAllocatorStatus struct {
// Version of the managed Target Allocator (operand)
// +optional
Version string `json:"version,omitempty"`

// Image indicates the container image to use for the Target Allocator.
// +optional
Image string `json:"image,omitempty"`
}

// TargetAllocatorSpec defines the desired state of TargetAllocator.
type TargetAllocatorSpec struct {
// Common defines fields that are common to all OpenTelemetry CRD workloads.
common.OpenTelemetryCommonFields `json:",inline"`
// CollectorSelector is the selector for Collector Pods the target allocator will allocate targets to.
CollectorSelector metav1.LabelSelector `json:"collectorSelector,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.
// WARNING: The per-node strategy currently ignores targets without a Node, like control plane components.
// +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"`
// ScrapeConfigs define static Prometheus scrape configurations for the target allocator.
// To use dynamic configurations from ServiceMonitors and PodMonitors, see the PrometheusCR section.
// For the exact format, see https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config.
// +optional
// +listType=atomic
// +kubebuilder:pruning:PreserveUnknownFields
ScrapeConfigs []common.AnyConfig `json:"scrapeConfigs,omitempty"`
// 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 common.ObservabilitySpec `json:"observability,omitempty"`
}

type (
// TargetAllocatorPrometheusCR configures Prometheus CustomResource handling in the Target Allocator.
TargetAllocatorPrometheusCR common.TargetAllocatorPrometheusCR
// TargetAllocatorAllocationStrategy represent a strategy Target Allocator uses to distribute targets to each collector.
TargetAllocatorAllocationStrategy common.TargetAllocatorAllocationStrategy
// TargetAllocatorFilterStrategy represent a filtering strategy for targets before they are assigned to collectors.
TargetAllocatorFilterStrategy common.TargetAllocatorFilterStrategy
)

const (
// TargetAllocatorAllocationStrategyLeastWeighted targets will be distributed to collector with fewer targets currently assigned.
TargetAllocatorAllocationStrategyLeastWeighted = TargetAllocatorAllocationStrategy(common.TargetAllocatorAllocationStrategyLeastWeighted)

// TargetAllocatorAllocationStrategyConsistentHashing targets will be consistently added to collectors, which allows a high-availability setup.
TargetAllocatorAllocationStrategyConsistentHashing = TargetAllocatorAllocationStrategy(common.TargetAllocatorAllocationStrategyConsistentHashing)

// TargetAllocatorAllocationStrategyPerNode targets will be assigned to the collector on the node they reside on (use only with daemon set).
TargetAllocatorAllocationStrategyPerNode TargetAllocatorAllocationStrategy = TargetAllocatorAllocationStrategy(common.TargetAllocatorAllocationStrategyPerNode)

// TargetAllocatorFilterStrategyRelabelConfig targets will be consistently drops targets based on the relabel_config.
TargetAllocatorFilterStrategyRelabelConfig = TargetAllocatorFilterStrategy(common.TargetAllocatorFilterStrategyRelabelConfig)
)
Loading
Loading