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

internal/manifests/collector: switch internally to v1alpha2 #2532

Merged
merged 19 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
14 changes: 14 additions & 0 deletions apis/v1alpha2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package v1alpha2

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"sort"

"gopkg.in/yaml.v3"
)

// AnyConfig represent parts of the config.
Expand Down Expand Up @@ -84,6 +87,17 @@ type Config struct {
Service Service `json:"service" yaml:"service"`
}

// Yaml encodes the current object and returns it as a string.
func (c Config) Yaml() (string, error) {
var buf bytes.Buffer
yamlEncoder := yaml.NewEncoder(&buf)
yamlEncoder.SetIndent(2)
if err := yamlEncoder.Encode(&c); err != nil {
return "", err
}
return buf.String(), nil
}

type Service struct {
Extensions *[]string `json:"extensions,omitempty" yaml:"extensions,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Expand Down
74 changes: 74 additions & 0 deletions apis/v1alpha2/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,77 @@ func TestNullObjects_go_yaml(t *testing.T) {
nullObjects := cfg.nullObjects()
assert.Equal(t, []string{"connectors.spanmetrics:", "exporters.otlp.endpoint:", "extensions.health_check:", "processors.batch:", "receivers.otlp.protocols.grpc:", "receivers.otlp.protocols.http:"}, nullObjects)
}

func TestConfigYaml(t *testing.T) {
cfg := &Config{
Receivers: AnyConfig{
Object: map[string]interface{}{
"otlp": nil,
},
},
Processors: &AnyConfig{
Object: map[string]interface{}{
"modify_2000": "enabled",
},
},
Exporters: AnyConfig{
Object: map[string]interface{}{
"otlp/exporter": nil,
},
},
Connectors: &AnyConfig{
Object: map[string]interface{}{
"con": "magic",
},
},
Extensions: &AnyConfig{
Object: map[string]interface{}{
"addon": "option1",
},
},
Service: Service{
Extensions: &[]string{"addon"},
Telemetry: &AnyConfig{
Object: map[string]interface{}{
"insights": "yeah!",
},
},
Pipelines: AnyConfig{
Object: map[string]interface{}{
"receivers": []string{"otlp"},
"processors": []string{"modify_2000"},
"exporters": []string{"otlp/exporter", "con"},
},
},
},
}
yamlCollector, err := cfg.Yaml()
require.NoError(t, err)

const expected = `receivers:
otlp: null
exporters:
otlp/exporter: null
processors:
modify_2000: enabled
connectors:
con: magic
extensions:
addon: option1
service:
extensions:
- addon
telemetry:
insights: yeah!
pipelines:
exporters:
- otlp/exporter
- con
processors:
- modify_2000
receivers:
- otlp
`

assert.Equal(t, expected, yamlCollector)
}
30 changes: 15 additions & 15 deletions controllers/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand All @@ -147,7 +147,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand Down Expand Up @@ -232,7 +232,7 @@ service:
Annotations: nil,
},
Data: map[string]string{
"collector.yaml": "receivers:\n examplereceiver:\n endpoint: \"0.0.0.0:12345\"\nexporters:\n logging:\nservice:\n pipelines:\n metrics:\n receivers: [examplereceiver]\n exporters: [logging]\n",
"collector.yaml": "receivers:\n examplereceiver:\n endpoint: 0.0.0.0:12345\nexporters:\n logging: null\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - examplereceiver\n",
},
},
&corev1.ServiceAccount{
Expand Down Expand Up @@ -369,7 +369,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand All @@ -391,7 +391,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand Down Expand Up @@ -476,7 +476,7 @@ service:
Annotations: nil,
},
Data: map[string]string{
"collector.yaml": "receivers:\n examplereceiver:\n endpoint: \"0.0.0.0:12345\"\nexporters:\n logging:\nservice:\n pipelines:\n metrics:\n receivers: [examplereceiver]\n exporters: [logging]\n",
"collector.yaml": "receivers:\n examplereceiver:\n endpoint: 0.0.0.0:12345\nexporters:\n logging: null\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - examplereceiver\n",
},
},
&corev1.ServiceAccount{
Expand Down Expand Up @@ -646,7 +646,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand All @@ -668,7 +668,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "8188c85abd4aa5e9c798874b4f47d37f08d4778933154e3f7d60246510ed9dd2",
"opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand Down Expand Up @@ -753,7 +753,7 @@ service:
Annotations: nil,
},
Data: map[string]string{
"collector.yaml": "receivers:\n examplereceiver:\n endpoint: \"0.0.0.0:12345\"\nexporters:\n logging:\nservice:\n pipelines:\n metrics:\n receivers: [examplereceiver]\n exporters: [logging]\n",
"collector.yaml": "receivers:\n examplereceiver:\n endpoint: 0.0.0.0:12345\nexporters:\n logging: null\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - examplereceiver\n",
},
},
&corev1.Service{
Expand Down Expand Up @@ -1157,7 +1157,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "5fe4d7d7faf3247bd7ec88688c9f73618f9ab8e170362bd7203c54e7a2f5cec0",
"opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand All @@ -1180,7 +1180,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "5fe4d7d7faf3247bd7ec88688c9f73618f9ab8e170362bd7203c54e7a2f5cec0",
"opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand Down Expand Up @@ -1265,7 +1265,7 @@ service:
Annotations: nil,
},
Data: map[string]string{
"collector.yaml": "exporters:\n logging: null\nreceivers:\n prometheus:\n config: {}\n target_allocator:\n collector_id: ${POD_NAME}\n endpoint: http://test-targetallocator:80\n interval: 30s\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - prometheus\n",
"collector.yaml": "exporters:\n logging: null\nreceivers:\n prometheus:\n config: {}\n target_allocator:\n collector_id: ${POD_NAME}\n endpoint: http://test-targetallocator:80\n interval: 30s\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - prometheus\n",
},
},
&corev1.ServiceAccount{
Expand Down Expand Up @@ -1556,7 +1556,7 @@ prometheus_cr:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "5fe4d7d7faf3247bd7ec88688c9f73618f9ab8e170362bd7203c54e7a2f5cec0",
"opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand All @@ -1579,7 +1579,7 @@ prometheus_cr:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "5fe4d7d7faf3247bd7ec88688c9f73618f9ab8e170362bd7203c54e7a2f5cec0",
"opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand Down Expand Up @@ -1664,7 +1664,7 @@ prometheus_cr:
Annotations: nil,
},
Data: map[string]string{
"collector.yaml": "exporters:\n logging: null\nreceivers:\n prometheus:\n config: {}\n target_allocator:\n collector_id: ${POD_NAME}\n endpoint: http://test-targetallocator:80\n interval: 30s\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - prometheus\n",
"collector.yaml": "exporters:\n logging: null\nreceivers:\n prometheus:\n config: {}\n target_allocator:\n collector_id: ${POD_NAME}\n endpoint: http://test-targetallocator:80\n interval: 30s\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - prometheus\n",
},
},
&corev1.ServiceAccount{
Expand Down
150 changes: 150 additions & 0 deletions internal/api/convert/v1alpha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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 convert

import (
"errors"

"gopkg.in/yaml.v3"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2"
)

func V1Alpha1to2(in v1alpha1.OpenTelemetryCollector) (v1alpha2.OpenTelemetryCollector, error) {
frzifus marked this conversation as resolved.
Show resolved Hide resolved
copy := in.DeepCopy()
out := v1alpha2.OpenTelemetryCollector{
TypeMeta: copy.TypeMeta,
ObjectMeta: copy.ObjectMeta,
}

cfg := &v1alpha2.Config{}
if err := yaml.Unmarshal([]byte(in.Spec.Config), cfg); err != nil {
return v1alpha2.OpenTelemetryCollector{}, errors.New("could not convert config json to v1alpha2.Config")
}
out.Spec.Config = *cfg

out.Spec.OpenTelemetryCommonFields.ManagementState = v1alpha2.ManagementStateType(copy.Spec.ManagementState)
out.Spec.OpenTelemetryCommonFields.Resources = copy.Spec.Resources
out.Spec.OpenTelemetryCommonFields.NodeSelector = copy.Spec.NodeSelector
out.Spec.OpenTelemetryCommonFields.Args = copy.Spec.NodeSelector
out.Spec.OpenTelemetryCommonFields.Replicas = copy.Spec.Replicas

if copy.Spec.Autoscaler != nil {
metrics := make([]v1alpha2.MetricSpec, len(copy.Spec.Autoscaler.Metrics))
for i, m := range copy.Spec.Autoscaler.Metrics {
metrics[i] = v1alpha2.MetricSpec{
Type: m.Type,
Pods: m.Pods,
}
}
out.Spec.OpenTelemetryCommonFields.Autoscaler = &v1alpha2.AutoscalerSpec{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to worry about the deprecated min and max replica fields?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did say no. Since you are still able to set min/max in the AutoscalerSpec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since they are deprecated and this is a version bump... I would say this is the perfect moment to remove them

Copy link
Member Author

@frzifus frzifus Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep the change as minimal as possible. We can add/remove/change behavior afterwards.

MinReplicas: copy.Spec.Autoscaler.MinReplicas,
MaxReplicas: copy.Spec.Autoscaler.MaxReplicas,
Behavior: copy.Spec.Autoscaler.Behavior,
Metrics: metrics,
TargetCPUUtilization: copy.Spec.Autoscaler.TargetCPUUtilization,
TargetMemoryUtilization: copy.Spec.Autoscaler.TargetMemoryUtilization,
}
}

if copy.Spec.PodDisruptionBudget != nil {
out.Spec.OpenTelemetryCommonFields.PodDisruptionBudget = &v1alpha2.PodDisruptionBudgetSpec{
MinAvailable: copy.Spec.PodDisruptionBudget.MinAvailable,
MaxUnavailable: copy.Spec.PodDisruptionBudget.MaxUnavailable,
}
}
if copy.Spec.SecurityContext != nil {
out.Spec.OpenTelemetryCommonFields.SecurityContext = copy.Spec.SecurityContext
}
if copy.Spec.PodSecurityContext != nil {
out.Spec.OpenTelemetryCommonFields.PodSecurityContext = copy.Spec.PodSecurityContext
}
out.Spec.OpenTelemetryCommonFields.PodAnnotations = copy.Spec.PodAnnotations
out.Spec.OpenTelemetryCommonFields.ServiceAccount = copy.Spec.ServiceAccount
out.Spec.OpenTelemetryCommonFields.Image = copy.Spec.Image
out.Spec.OpenTelemetryCommonFields.ImagePullPolicy = copy.Spec.ImagePullPolicy
out.Spec.OpenTelemetryCommonFields.VolumeMounts = copy.Spec.VolumeMounts
out.Spec.OpenTelemetryCommonFields.Ports = copy.Spec.Ports
out.Spec.OpenTelemetryCommonFields.Env = copy.Spec.Env
out.Spec.OpenTelemetryCommonFields.EnvFrom = copy.Spec.EnvFrom
out.Spec.OpenTelemetryCommonFields.VolumeClaimTemplates = copy.Spec.VolumeClaimTemplates
out.Spec.OpenTelemetryCommonFields.Tolerations = copy.Spec.Tolerations
out.Spec.OpenTelemetryCommonFields.Volumes = copy.Spec.Volumes
out.Spec.OpenTelemetryCommonFields.Affinity = copy.Spec.Affinity
out.Spec.OpenTelemetryCommonFields.Lifecycle = copy.Spec.Lifecycle
out.Spec.OpenTelemetryCommonFields.TerminationGracePeriodSeconds = copy.Spec.TerminationGracePeriodSeconds
out.Spec.OpenTelemetryCommonFields.TopologySpreadConstraints = copy.Spec.TopologySpreadConstraints
out.Spec.OpenTelemetryCommonFields.HostNetwork = copy.Spec.HostNetwork
out.Spec.OpenTelemetryCommonFields.ShareProcessNamespace = copy.Spec.ShareProcessNamespace
out.Spec.OpenTelemetryCommonFields.PriorityClassName = copy.Spec.PriorityClassName
out.Spec.OpenTelemetryCommonFields.InitContainers = copy.Spec.InitContainers
out.Spec.OpenTelemetryCommonFields.AdditionalContainers = copy.Spec.AdditionalContainers

out.Spec.TargetAllocator.Replicas = copy.Spec.TargetAllocator.Replicas
frzifus marked this conversation as resolved.
Show resolved Hide resolved
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.Mode = v1alpha2.Mode(copy.Spec.Mode)
out.Spec.UpgradeStrategy = v1alpha2.UpgradeStrategy(copy.Spec.UpgradeStrategy)
out.Spec.Ingress.Type = v1alpha2.IngressType(copy.Spec.Ingress.Type)
out.Spec.Ingress.RuleType = v1alpha2.IngressRuleType(copy.Spec.Ingress.RuleType)
out.Spec.Ingress.Hostname = copy.Spec.Ingress.Hostname
out.Spec.Ingress.Annotations = copy.Spec.Ingress.Annotations
out.Spec.Ingress.TLS = copy.Spec.Ingress.TLS
out.Spec.Ingress.IngressClassName = copy.Spec.Ingress.IngressClassName
out.Spec.Ingress.Route.Termination = v1alpha2.TLSRouteTerminationType(copy.Spec.Ingress.Route.Termination)

if copy.Spec.LivenessProbe != nil {
out.Spec.LivenessProbe = &v1alpha2.Probe{
InitialDelaySeconds: copy.Spec.LivenessProbe.InitialDelaySeconds,
TimeoutSeconds: copy.Spec.LivenessProbe.TimeoutSeconds,
PeriodSeconds: copy.Spec.LivenessProbe.PeriodSeconds,
SuccessThreshold: copy.Spec.LivenessProbe.SuccessThreshold,
FailureThreshold: copy.Spec.LivenessProbe.FailureThreshold,
TerminationGracePeriodSeconds: copy.Spec.LivenessProbe.TerminationGracePeriodSeconds,
}
}

out.Spec.Observability.Metrics.EnableMetrics = copy.Spec.Observability.Metrics.EnableMetrics

out.Spec.ConfigMaps = copy.Spec.ConfigMaps
out.Spec.DaemonSetUpdateStrategy = copy.Spec.UpdateStrategy
out.Spec.DeploymentUpdateStrategy.Type = copy.Spec.DeploymentUpdateStrategy.Type
out.Spec.DeploymentUpdateStrategy.RollingUpdate = copy.Spec.DeploymentUpdateStrategy.RollingUpdate
frzifus marked this conversation as resolved.
Show resolved Hide resolved

return out, nil
}
Loading
Loading