|
| 1 | +package convert |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + appsv1 "k8s.io/api/apps/v1" |
| 8 | + "sigs.k8s.io/yaml" |
| 9 | + |
| 10 | + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" |
| 11 | + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha2" |
| 12 | +) |
| 13 | + |
| 14 | +func V1Alpha1to2(in v1alpha1.OpenTelemetryCollector) v1alpha2.OpenTelemetryCollector { |
| 15 | + copy := in.DeepCopy() |
| 16 | + out := v1alpha2.OpenTelemetryCollector{ |
| 17 | + TypeMeta: copy.TypeMeta, |
| 18 | + ObjectMeta: copy.ObjectMeta, |
| 19 | + } |
| 20 | + |
| 21 | + collectorJson, err := yaml.YAMLToJSON([]byte(in.Spec.Config)) |
| 22 | + if err != nil { |
| 23 | + panic(fmt.Sprintf("could not convert v1alpha1 yaml config to json, err: %s", err)) |
| 24 | + } |
| 25 | + |
| 26 | + cfg := v1alpha2.Config{} |
| 27 | + if err := json.Unmarshal(collectorJson, &cfg); err != nil { |
| 28 | + panic(fmt.Sprintf("could not convert v1alpha1 config to v1alpha2, err: %s", err)) |
| 29 | + } |
| 30 | + out.Spec.Config = cfg |
| 31 | + |
| 32 | + out.Spec.OpenTelemetryCommonFields.ManagementState = v1alpha2.ManagementStateType(copy.Spec.ManagementState) |
| 33 | + out.Spec.OpenTelemetryCommonFields.Resources = copy.Spec.Resources |
| 34 | + out.Spec.OpenTelemetryCommonFields.NodeSelector = copy.Spec.NodeSelector |
| 35 | + out.Spec.OpenTelemetryCommonFields.Args = copy.Spec.NodeSelector |
| 36 | + out.Spec.OpenTelemetryCommonFields.Replicas = copy.Spec.Replicas |
| 37 | + |
| 38 | + if copy.Spec.Autoscaler != nil { |
| 39 | + metrics := make([]v1alpha2.MetricSpec, len(copy.Spec.Autoscaler.Metrics)) |
| 40 | + for i, m := range copy.Spec.Autoscaler.Metrics { |
| 41 | + metrics[i] = v1alpha2.MetricSpec{ |
| 42 | + Type: m.Type, |
| 43 | + Pods: m.Pods, |
| 44 | + } |
| 45 | + } |
| 46 | + out.Spec.OpenTelemetryCommonFields.Autoscaler = &v1alpha2.AutoscalerSpec{ |
| 47 | + MinReplicas: copy.Spec.Autoscaler.MinReplicas, |
| 48 | + MaxReplicas: copy.Spec.Autoscaler.MaxReplicas, |
| 49 | + Behavior: copy.Spec.Autoscaler.Behavior, |
| 50 | + Metrics: metrics, |
| 51 | + TargetCPUUtilization: copy.Spec.Autoscaler.TargetCPUUtilization, |
| 52 | + TargetMemoryUtilization: copy.Spec.Autoscaler.TargetMemoryUtilization, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if copy.Spec.PodDisruptionBudget != nil { |
| 57 | + out.Spec.OpenTelemetryCommonFields.PodDisruptionBudget = &v1alpha2.PodDisruptionBudgetSpec{ |
| 58 | + MinAvailable: copy.Spec.PodDisruptionBudget.MinAvailable, |
| 59 | + MaxUnavailable: copy.Spec.PodDisruptionBudget.MaxUnavailable, |
| 60 | + } |
| 61 | + } |
| 62 | + if copy.Spec.SecurityContext != nil { |
| 63 | + out.Spec.OpenTelemetryCommonFields.SecurityContext = copy.Spec.SecurityContext |
| 64 | + } |
| 65 | + if copy.Spec.PodSecurityContext != nil { |
| 66 | + out.Spec.OpenTelemetryCommonFields.PodSecurityContext = copy.Spec.PodSecurityContext |
| 67 | + } |
| 68 | + out.Spec.OpenTelemetryCommonFields.PodAnnotations = copy.Spec.PodAnnotations |
| 69 | + out.Spec.OpenTelemetryCommonFields.ServiceAccount = copy.Spec.ServiceAccount |
| 70 | + out.Spec.OpenTelemetryCommonFields.Image = copy.Spec.Image |
| 71 | + out.Spec.OpenTelemetryCommonFields.ImagePullPolicy = copy.Spec.ImagePullPolicy |
| 72 | + out.Spec.OpenTelemetryCommonFields.VolumeMounts = copy.Spec.VolumeMounts |
| 73 | + out.Spec.OpenTelemetryCommonFields.Ports = copy.Spec.Ports |
| 74 | + out.Spec.OpenTelemetryCommonFields.Env = copy.Spec.Env |
| 75 | + out.Spec.OpenTelemetryCommonFields.EnvFrom = copy.Spec.EnvFrom |
| 76 | + out.Spec.OpenTelemetryCommonFields.VolumeClaimTemplates = copy.Spec.VolumeClaimTemplates |
| 77 | + out.Spec.OpenTelemetryCommonFields.Tolerations = copy.Spec.Tolerations |
| 78 | + out.Spec.OpenTelemetryCommonFields.Volumes = copy.Spec.Volumes |
| 79 | + out.Spec.OpenTelemetryCommonFields.Affinity = copy.Spec.Affinity |
| 80 | + out.Spec.OpenTelemetryCommonFields.Lifecycle = copy.Spec.Lifecycle |
| 81 | + out.Spec.OpenTelemetryCommonFields.TerminationGracePeriodSeconds = copy.Spec.TerminationGracePeriodSeconds |
| 82 | + out.Spec.OpenTelemetryCommonFields.TopologySpreadConstraints = copy.Spec.TopologySpreadConstraints |
| 83 | + out.Spec.OpenTelemetryCommonFields.HostNetwork = copy.Spec.HostNetwork |
| 84 | + out.Spec.OpenTelemetryCommonFields.PriorityClassName = copy.Spec.PriorityClassName |
| 85 | + out.Spec.OpenTelemetryCommonFields.InitContainers = copy.Spec.InitContainers |
| 86 | + out.Spec.OpenTelemetryCommonFields.AdditionalContainers = copy.Spec.AdditionalContainers |
| 87 | + |
| 88 | + // TODO: migrate Target Allocator fields |
| 89 | + // out.Spec.TargetAllocator = ... |
| 90 | + |
| 91 | + out.Spec.Mode = v1alpha2.Mode(copy.Spec.Mode) |
| 92 | + out.Spec.UpgradeStrategy = v1alpha2.UpgradeStrategy(copy.Spec.UpgradeStrategy) |
| 93 | + out.Spec.Ingress.Type = v1alpha2.IngressType(copy.Spec.Ingress.Type) |
| 94 | + out.Spec.Ingress.Annotations = copy.Spec.Ingress.Annotations |
| 95 | + out.Spec.Ingress.TLS = copy.Spec.Ingress.TLS |
| 96 | + out.Spec.Ingress.IngressClassName = copy.Spec.Ingress.IngressClassName |
| 97 | + out.Spec.Ingress.Route.Termination = v1alpha2.TLSRouteTerminationType(copy.Spec.Ingress.Route.Termination) |
| 98 | + |
| 99 | + if copy.Spec.LivenessProbe != nil { |
| 100 | + out.Spec.LivenessProbe = &v1alpha2.Probe{ |
| 101 | + InitialDelaySeconds: copy.Spec.LivenessProbe.InitialDelaySeconds, |
| 102 | + TimeoutSeconds: copy.Spec.LivenessProbe.TimeoutSeconds, |
| 103 | + PeriodSeconds: copy.Spec.LivenessProbe.PeriodSeconds, |
| 104 | + SuccessThreshold: copy.Spec.LivenessProbe.SuccessThreshold, |
| 105 | + FailureThreshold: copy.Spec.LivenessProbe.FailureThreshold, |
| 106 | + TerminationGracePeriodSeconds: copy.Spec.LivenessProbe.TerminationGracePeriodSeconds, |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + out.Spec.Observability.Metrics.EnableMetrics = copy.Spec.Observability.Metrics.EnableMetrics |
| 111 | + |
| 112 | + out.Spec.ConfigMaps = copy.Spec.ConfigMaps |
| 113 | + out.Spec.DaemonSetUpdateStrategy = appsv1.DaemonSetUpdateStrategy{} // NOTE: N/A in v1alpha1 |
| 114 | + out.Spec.DeploymentUpdateStrategy.Type = copy.Spec.DeploymentUpdateStrategy.Type |
| 115 | + out.Spec.DeploymentUpdateStrategy.RollingUpdate = copy.Spec.DeploymentUpdateStrategy.RollingUpdate |
| 116 | + |
| 117 | + return out |
| 118 | +} |
0 commit comments