|
| 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 convert |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + |
| 20 | + appsv1 "k8s.io/api/apps/v1" |
| 21 | + "sigs.k8s.io/yaml" |
| 22 | + |
| 23 | + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" |
| 24 | + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" |
| 25 | +) |
| 26 | + |
| 27 | +func V1Alpha1to2(in v1alpha1.OpenTelemetryCollector) v1alpha2.OpenTelemetryCollector { |
| 28 | + copy := in.DeepCopy() |
| 29 | + out := v1alpha2.OpenTelemetryCollector{ |
| 30 | + TypeMeta: copy.TypeMeta, |
| 31 | + ObjectMeta: copy.ObjectMeta, |
| 32 | + } |
| 33 | + |
| 34 | + collectorJson, err := yaml.YAMLToJSON([]byte(in.Spec.Config)) |
| 35 | + if err != nil { |
| 36 | + return v1alpha2.OpenTelemetryCollector{} |
| 37 | + } |
| 38 | + |
| 39 | + cfg := &v1alpha2.Config{} |
| 40 | + if err := json.Unmarshal(collectorJson, cfg); err != nil { |
| 41 | + return v1alpha2.OpenTelemetryCollector{} |
| 42 | + } |
| 43 | + out.Spec.Config = *cfg |
| 44 | + |
| 45 | + out.Spec.OpenTelemetryCommonFields.ManagementState = v1alpha2.ManagementStateType(copy.Spec.ManagementState) |
| 46 | + out.Spec.OpenTelemetryCommonFields.Resources = copy.Spec.Resources |
| 47 | + out.Spec.OpenTelemetryCommonFields.NodeSelector = copy.Spec.NodeSelector |
| 48 | + out.Spec.OpenTelemetryCommonFields.Args = copy.Spec.NodeSelector |
| 49 | + out.Spec.OpenTelemetryCommonFields.Replicas = copy.Spec.Replicas |
| 50 | + |
| 51 | + if copy.Spec.Autoscaler != nil { |
| 52 | + metrics := make([]v1alpha2.MetricSpec, len(copy.Spec.Autoscaler.Metrics)) |
| 53 | + for i, m := range copy.Spec.Autoscaler.Metrics { |
| 54 | + metrics[i] = v1alpha2.MetricSpec{ |
| 55 | + Type: m.Type, |
| 56 | + Pods: m.Pods, |
| 57 | + } |
| 58 | + } |
| 59 | + out.Spec.OpenTelemetryCommonFields.Autoscaler = &v1alpha2.AutoscalerSpec{ |
| 60 | + MinReplicas: copy.Spec.Autoscaler.MinReplicas, |
| 61 | + MaxReplicas: copy.Spec.Autoscaler.MaxReplicas, |
| 62 | + Behavior: copy.Spec.Autoscaler.Behavior, |
| 63 | + Metrics: metrics, |
| 64 | + TargetCPUUtilization: copy.Spec.Autoscaler.TargetCPUUtilization, |
| 65 | + TargetMemoryUtilization: copy.Spec.Autoscaler.TargetMemoryUtilization, |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if copy.Spec.PodDisruptionBudget != nil { |
| 70 | + out.Spec.OpenTelemetryCommonFields.PodDisruptionBudget = &v1alpha2.PodDisruptionBudgetSpec{ |
| 71 | + MinAvailable: copy.Spec.PodDisruptionBudget.MinAvailable, |
| 72 | + MaxUnavailable: copy.Spec.PodDisruptionBudget.MaxUnavailable, |
| 73 | + } |
| 74 | + } |
| 75 | + if copy.Spec.SecurityContext != nil { |
| 76 | + out.Spec.OpenTelemetryCommonFields.SecurityContext = copy.Spec.SecurityContext |
| 77 | + } |
| 78 | + if copy.Spec.PodSecurityContext != nil { |
| 79 | + out.Spec.OpenTelemetryCommonFields.PodSecurityContext = copy.Spec.PodSecurityContext |
| 80 | + } |
| 81 | + out.Spec.OpenTelemetryCommonFields.PodAnnotations = copy.Spec.PodAnnotations |
| 82 | + out.Spec.OpenTelemetryCommonFields.ServiceAccount = copy.Spec.ServiceAccount |
| 83 | + out.Spec.OpenTelemetryCommonFields.Image = copy.Spec.Image |
| 84 | + out.Spec.OpenTelemetryCommonFields.ImagePullPolicy = copy.Spec.ImagePullPolicy |
| 85 | + out.Spec.OpenTelemetryCommonFields.VolumeMounts = copy.Spec.VolumeMounts |
| 86 | + out.Spec.OpenTelemetryCommonFields.Ports = copy.Spec.Ports |
| 87 | + out.Spec.OpenTelemetryCommonFields.Env = copy.Spec.Env |
| 88 | + out.Spec.OpenTelemetryCommonFields.EnvFrom = copy.Spec.EnvFrom |
| 89 | + out.Spec.OpenTelemetryCommonFields.VolumeClaimTemplates = copy.Spec.VolumeClaimTemplates |
| 90 | + out.Spec.OpenTelemetryCommonFields.Tolerations = copy.Spec.Tolerations |
| 91 | + out.Spec.OpenTelemetryCommonFields.Volumes = copy.Spec.Volumes |
| 92 | + out.Spec.OpenTelemetryCommonFields.Affinity = copy.Spec.Affinity |
| 93 | + out.Spec.OpenTelemetryCommonFields.Lifecycle = copy.Spec.Lifecycle |
| 94 | + out.Spec.OpenTelemetryCommonFields.TerminationGracePeriodSeconds = copy.Spec.TerminationGracePeriodSeconds |
| 95 | + out.Spec.OpenTelemetryCommonFields.TopologySpreadConstraints = copy.Spec.TopologySpreadConstraints |
| 96 | + out.Spec.OpenTelemetryCommonFields.HostNetwork = copy.Spec.HostNetwork |
| 97 | + out.Spec.OpenTelemetryCommonFields.ShareProcessNamespace = copy.Spec.ShareProcessNamespace |
| 98 | + out.Spec.OpenTelemetryCommonFields.PriorityClassName = copy.Spec.PriorityClassName |
| 99 | + out.Spec.OpenTelemetryCommonFields.InitContainers = copy.Spec.InitContainers |
| 100 | + out.Spec.OpenTelemetryCommonFields.AdditionalContainers = copy.Spec.AdditionalContainers |
| 101 | + |
| 102 | + out.Spec.TargetAllocator.Replicas = copy.Spec.TargetAllocator.Replicas |
| 103 | + out.Spec.TargetAllocator.NodeSelector = copy.Spec.TargetAllocator.NodeSelector |
| 104 | + out.Spec.TargetAllocator.Resources = copy.Spec.TargetAllocator.Resources |
| 105 | + out.Spec.TargetAllocator.AllocationStrategy = copy.Spec.TargetAllocator.AllocationStrategy |
| 106 | + out.Spec.TargetAllocator.FilterStrategy = copy.Spec.TargetAllocator.FilterStrategy |
| 107 | + out.Spec.TargetAllocator.ServiceAccount = copy.Spec.TargetAllocator.ServiceAccount |
| 108 | + out.Spec.TargetAllocator.Image = copy.Spec.TargetAllocator.Image |
| 109 | + out.Spec.TargetAllocator.Enabled = copy.Spec.TargetAllocator.Enabled |
| 110 | + out.Spec.TargetAllocator.Affinity = copy.Spec.TargetAllocator.Affinity |
| 111 | + out.Spec.TargetAllocator.PrometheusCR.Enabled = copy.Spec.TargetAllocator.PrometheusCR.Enabled |
| 112 | + out.Spec.TargetAllocator.PrometheusCR.ScrapeInterval = copy.Spec.TargetAllocator.PrometheusCR.ScrapeInterval |
| 113 | + out.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector = copy.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector |
| 114 | + out.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector = copy.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector |
| 115 | + out.Spec.TargetAllocator.SecurityContext = copy.Spec.TargetAllocator.SecurityContext |
| 116 | + out.Spec.TargetAllocator.PodSecurityContext = copy.Spec.TargetAllocator.PodSecurityContext |
| 117 | + out.Spec.TargetAllocator.TopologySpreadConstraints = copy.Spec.TargetAllocator.TopologySpreadConstraints |
| 118 | + out.Spec.TargetAllocator.Tolerations = copy.Spec.TargetAllocator.Tolerations |
| 119 | + out.Spec.TargetAllocator.Env = copy.Spec.TargetAllocator.Env |
| 120 | + out.Spec.TargetAllocator.Observability = v1alpha1.ObservabilitySpec{ |
| 121 | + Metrics: v1alpha1.MetricsConfigSpec{ |
| 122 | + EnableMetrics: copy.Spec.TargetAllocator.Observability.Metrics.EnableMetrics, |
| 123 | + }, |
| 124 | + } |
| 125 | + out.Spec.TargetAllocator.PodDisruptionBudget = copy.Spec.TargetAllocator.PodDisruptionBudget |
| 126 | + |
| 127 | + out.Spec.Mode = v1alpha2.Mode(copy.Spec.Mode) |
| 128 | + out.Spec.UpgradeStrategy = v1alpha2.UpgradeStrategy(copy.Spec.UpgradeStrategy) |
| 129 | + out.Spec.Ingress.Type = v1alpha2.IngressType(copy.Spec.Ingress.Type) |
| 130 | + out.Spec.Ingress.Annotations = copy.Spec.Ingress.Annotations |
| 131 | + out.Spec.Ingress.TLS = copy.Spec.Ingress.TLS |
| 132 | + out.Spec.Ingress.IngressClassName = copy.Spec.Ingress.IngressClassName |
| 133 | + out.Spec.Ingress.Route.Termination = v1alpha2.TLSRouteTerminationType(copy.Spec.Ingress.Route.Termination) |
| 134 | + |
| 135 | + if copy.Spec.LivenessProbe != nil { |
| 136 | + out.Spec.LivenessProbe = &v1alpha2.Probe{ |
| 137 | + InitialDelaySeconds: copy.Spec.LivenessProbe.InitialDelaySeconds, |
| 138 | + TimeoutSeconds: copy.Spec.LivenessProbe.TimeoutSeconds, |
| 139 | + PeriodSeconds: copy.Spec.LivenessProbe.PeriodSeconds, |
| 140 | + SuccessThreshold: copy.Spec.LivenessProbe.SuccessThreshold, |
| 141 | + FailureThreshold: copy.Spec.LivenessProbe.FailureThreshold, |
| 142 | + TerminationGracePeriodSeconds: copy.Spec.LivenessProbe.TerminationGracePeriodSeconds, |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + out.Spec.Observability.Metrics.EnableMetrics = copy.Spec.Observability.Metrics.EnableMetrics |
| 147 | + |
| 148 | + out.Spec.ConfigMaps = copy.Spec.ConfigMaps |
| 149 | + out.Spec.DaemonSetUpdateStrategy = appsv1.DaemonSetUpdateStrategy{} // NOTE: N/A in v1alpha1 |
| 150 | + out.Spec.DeploymentUpdateStrategy.Type = copy.Spec.DeploymentUpdateStrategy.Type |
| 151 | + out.Spec.DeploymentUpdateStrategy.RollingUpdate = copy.Spec.DeploymentUpdateStrategy.RollingUpdate |
| 152 | + |
| 153 | + return out |
| 154 | +} |
0 commit comments