@@ -17,15 +17,13 @@ package collector
17
17
import (
18
18
"github.com/go-logr/logr"
19
19
autoscalingv2 "k8s.io/api/autoscaling/v2"
20
- autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
21
20
corev1 "k8s.io/api/core/v1"
22
21
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23
22
"sigs.k8s.io/controller-runtime/pkg/client"
24
23
25
24
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
26
25
"github.com/open-telemetry/opentelemetry-operator/internal/config"
27
26
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
28
- "github.com/open-telemetry/opentelemetry-operator/pkg/autodetect"
29
27
)
30
28
31
29
func HorizontalPodAutoscaler (cfg config.Config , logger logr.Logger , otelcol v1alpha1.OpenTelemetryCollector ) client.Object {
@@ -46,7 +44,6 @@ func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1al
46
44
logger .Info ("hpa field is unset in Spec, skipping autoscaler creation" )
47
45
return nil
48
46
}
49
- autoscalingVersion := cfg .AutoscalingVersion ()
50
47
51
48
if otelcol .Spec .Autoscaler .MaxReplicas == nil {
52
49
otelcol .Spec .Autoscaler .MaxReplicas = otelcol .Spec .MaxReplicas
@@ -60,225 +57,64 @@ func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1al
60
57
}
61
58
}
62
59
63
- if autoscalingVersion == autodetect .AutoscalingVersionV2Beta2 {
64
- metrics := []autoscalingv2beta2.MetricSpec {}
60
+ metrics := []autoscalingv2.MetricSpec {}
65
61
66
- if otelcol .Spec .Autoscaler .TargetMemoryUtilization != nil {
67
- utilizationTarget := autoscalingv2beta2.MetricSpec {
68
- Type : autoscalingv2beta2 .ResourceMetricSourceType ,
69
- Resource : & autoscalingv2beta2.ResourceMetricSource {
70
- Name : corev1 .ResourceMemory ,
71
- Target : autoscalingv2beta2.MetricTarget {
72
- Type : autoscalingv2beta2 .UtilizationMetricType ,
73
- AverageUtilization : otelcol .Spec .Autoscaler .TargetMemoryUtilization ,
74
- },
62
+ if otelcol .Spec .Autoscaler .TargetMemoryUtilization != nil {
63
+ memoryTarget := autoscalingv2.MetricSpec {
64
+ Type : autoscalingv2 .ResourceMetricSourceType ,
65
+ Resource : & autoscalingv2.ResourceMetricSource {
66
+ Name : corev1 .ResourceMemory ,
67
+ Target : autoscalingv2.MetricTarget {
68
+ Type : autoscalingv2 .UtilizationMetricType ,
69
+ AverageUtilization : otelcol .Spec .Autoscaler .TargetMemoryUtilization ,
75
70
},
76
- }
77
- metrics = append (metrics , utilizationTarget )
71
+ },
78
72
}
73
+ metrics = append (metrics , memoryTarget )
74
+ }
79
75
80
- targetCPUUtilization := autoscalingv2beta2.MetricSpec {
81
- Type : autoscalingv2beta2 .ResourceMetricSourceType ,
82
- Resource : & autoscalingv2beta2.ResourceMetricSource {
76
+ if otelcol .Spec .Autoscaler .TargetCPUUtilization != nil {
77
+ cpuTarget := autoscalingv2.MetricSpec {
78
+ Type : autoscalingv2 .ResourceMetricSourceType ,
79
+ Resource : & autoscalingv2.ResourceMetricSource {
83
80
Name : corev1 .ResourceCPU ,
84
- Target : autoscalingv2beta2 .MetricTarget {
85
- Type : autoscalingv2beta2 .UtilizationMetricType ,
81
+ Target : autoscalingv2 .MetricTarget {
82
+ Type : autoscalingv2 .UtilizationMetricType ,
86
83
AverageUtilization : otelcol .Spec .Autoscaler .TargetCPUUtilization ,
87
84
},
88
85
},
89
86
}
90
- metrics = append (metrics , targetCPUUtilization )
91
-
92
- autoscaler := autoscalingv2beta2.HorizontalPodAutoscaler {
93
- ObjectMeta : objectMeta ,
94
- Spec : autoscalingv2beta2.HorizontalPodAutoscalerSpec {
95
- ScaleTargetRef : autoscalingv2beta2.CrossVersionObjectReference {
96
- APIVersion : v1alpha1 .GroupVersion .String (),
97
- Kind : "OpenTelemetryCollector" ,
98
- Name : naming .OpenTelemetryCollector (otelcol .Name ),
99
- },
100
- MinReplicas : otelcol .Spec .Autoscaler .MinReplicas ,
101
- MaxReplicas : * otelcol .Spec .Autoscaler .MaxReplicas ,
102
- Metrics : metrics ,
103
- },
104
- }
105
-
106
- if otelcol .Spec .Autoscaler .Behavior != nil {
107
- behavior := ConvertToV2beta2Behavior (* otelcol .Spec .Autoscaler .Behavior )
108
- autoscaler .Spec .Behavior = & behavior
109
- }
110
-
111
- // check for custom metrics
112
- if len (otelcol .Spec .Autoscaler .Metrics ) > 0 {
113
- metrics := ConvertToV2Beta2PodMetrics (otelcol .Spec .Autoscaler .Metrics )
114
- autoscaler .Spec .Metrics = append (autoscaler .Spec .Metrics , metrics ... )
115
- }
116
-
117
- result = & autoscaler
118
- } else {
119
- metrics := []autoscalingv2.MetricSpec {}
120
-
121
- if otelcol .Spec .Autoscaler .TargetMemoryUtilization != nil {
122
- utilizationTarget := autoscalingv2.MetricSpec {
123
- Type : autoscalingv2 .ResourceMetricSourceType ,
124
- Resource : & autoscalingv2.ResourceMetricSource {
125
- Name : corev1 .ResourceMemory ,
126
- Target : autoscalingv2.MetricTarget {
127
- Type : autoscalingv2 .UtilizationMetricType ,
128
- AverageUtilization : otelcol .Spec .Autoscaler .TargetMemoryUtilization ,
129
- },
130
- },
131
- }
132
- metrics = append (metrics , utilizationTarget )
133
- }
134
-
135
- if otelcol .Spec .Autoscaler .TargetCPUUtilization != nil {
136
- targetCPUUtilization := autoscalingv2.MetricSpec {
137
- Type : autoscalingv2 .ResourceMetricSourceType ,
138
- Resource : & autoscalingv2.ResourceMetricSource {
139
- Name : corev1 .ResourceCPU ,
140
- Target : autoscalingv2.MetricTarget {
141
- Type : autoscalingv2 .UtilizationMetricType ,
142
- AverageUtilization : otelcol .Spec .Autoscaler .TargetCPUUtilization ,
143
- },
144
- },
145
- }
146
- metrics = append (metrics , targetCPUUtilization )
147
- }
148
-
149
- autoscaler := autoscalingv2.HorizontalPodAutoscaler {
150
- ObjectMeta : objectMeta ,
151
- Spec : autoscalingv2.HorizontalPodAutoscalerSpec {
152
- ScaleTargetRef : autoscalingv2.CrossVersionObjectReference {
153
- APIVersion : v1alpha1 .GroupVersion .String (),
154
- Kind : "OpenTelemetryCollector" ,
155
- Name : naming .OpenTelemetryCollector (otelcol .Name ),
156
- },
157
- MinReplicas : otelcol .Spec .Autoscaler .MinReplicas ,
158
- MaxReplicas : * otelcol .Spec .Autoscaler .MaxReplicas ,
159
- Metrics : metrics ,
160
- },
161
- }
162
- if otelcol .Spec .Autoscaler .Behavior != nil {
163
- autoscaler .Spec .Behavior = otelcol .Spec .Autoscaler .Behavior
164
- }
165
-
166
- // convert from v1alpha1.MetricSpec into a autoscalingv2.MetricSpec.
167
- for _ , metric := range otelcol .Spec .Autoscaler .Metrics {
168
- if metric .Type == autoscalingv2 .PodsMetricSourceType {
169
- v2metric := autoscalingv2.MetricSpec {
170
- Type : metric .Type ,
171
- Pods : metric .Pods ,
172
- }
173
- autoscaler .Spec .Metrics = append (autoscaler .Spec .Metrics , v2metric )
174
- } // pod metrics
175
- }
176
- result = & autoscaler
87
+ metrics = append (metrics , cpuTarget )
177
88
}
178
89
179
- return result
180
- }
181
-
182
- func ConvertToV2Beta2PodMetrics (v2metrics []v1alpha1.MetricSpec ) []autoscalingv2beta2.MetricSpec {
183
- metrics := make ([]autoscalingv2beta2.MetricSpec , len (v2metrics ))
184
-
185
- for i , v2metric := range v2metrics {
186
- metrics [i ].Type = autoscalingv2beta2 .MetricSourceType (v2metric .Type )
187
- if v2metric .Pods != nil {
188
- metrics [i ].Pods = & autoscalingv2beta2.PodsMetricSource {
189
- Metric : autoscalingv2beta2.MetricIdentifier {
190
- Name : v2metric .Pods .Metric .Name ,
191
- Selector : v2metric .Pods .Metric .Selector ,
192
- },
193
- Target : autoscalingv2beta2.MetricTarget {
194
- Type : autoscalingv2beta2 .MetricTargetType (v2metric .Pods .Target .Type ),
195
- AverageValue : v2metric .Pods .Target .AverageValue ,
196
- },
197
- }
198
- }
90
+ autoscaler := autoscalingv2.HorizontalPodAutoscaler {
91
+ ObjectMeta : objectMeta ,
92
+ Spec : autoscalingv2.HorizontalPodAutoscalerSpec {
93
+ ScaleTargetRef : autoscalingv2.CrossVersionObjectReference {
94
+ APIVersion : v1alpha1 .GroupVersion .String (),
95
+ Kind : "OpenTelemetryCollector" ,
96
+ Name : naming .OpenTelemetryCollector (otelcol .Name ),
97
+ },
98
+ MinReplicas : otelcol .Spec .Autoscaler .MinReplicas ,
99
+ MaxReplicas : * otelcol .Spec .Autoscaler .MaxReplicas ,
100
+ Metrics : metrics ,
101
+ },
199
102
}
200
-
201
- return metrics
202
- }
203
-
204
- // Create a v2beta2 HorizontalPodAutoscalerBehavior from a v2 instance.
205
- func ConvertToV2beta2Behavior (v2behavior autoscalingv2.HorizontalPodAutoscalerBehavior ) autoscalingv2beta2.HorizontalPodAutoscalerBehavior {
206
- behavior := & autoscalingv2beta2.HorizontalPodAutoscalerBehavior {}
207
-
208
- if v2behavior .ScaleUp != nil {
209
- scaleUpRules := & autoscalingv2beta2.HPAScalingRules {}
210
- scaleUpTime := * v2behavior .ScaleUp .StabilizationWindowSeconds
211
- scaleUpRules .StabilizationWindowSeconds = & scaleUpTime
212
-
213
- if v2behavior .ScaleUp .SelectPolicy != nil {
214
- scaleUpSelectPolicy := ConvertToV2Beta2SelectPolicy (* v2behavior .ScaleUp .SelectPolicy )
215
- scaleUpRules .SelectPolicy = & scaleUpSelectPolicy
216
- }
217
- if v2behavior .ScaleUp .Policies != nil {
218
- scaleUpPolicies := []autoscalingv2beta2.HPAScalingPolicy {}
219
- for _ , policy := range v2behavior .ScaleUp .Policies {
220
- v2beta2policy := ConvertToV2Beta2HPAScalingPolicy (policy )
221
- scaleUpPolicies = append (scaleUpPolicies , v2beta2policy )
222
- }
223
- scaleUpRules .Policies = scaleUpPolicies
224
- }
225
-
226
- behavior .ScaleUp = scaleUpRules
103
+ if otelcol .Spec .Autoscaler .Behavior != nil {
104
+ autoscaler .Spec .Behavior = otelcol .Spec .Autoscaler .Behavior
227
105
}
228
106
229
- if v2behavior .ScaleDown != nil {
230
- scaleDownRules := & autoscalingv2beta2.HPAScalingRules {}
231
- scaleDownTime := * v2behavior .ScaleDown .StabilizationWindowSeconds
232
- scaleDownRules .StabilizationWindowSeconds = & scaleDownTime
233
-
234
- if v2behavior .ScaleDown .SelectPolicy != nil {
235
- scaleDownSelectPolicy := ConvertToV2Beta2SelectPolicy (* v2behavior .ScaleDown .SelectPolicy )
236
- scaleDownRules .SelectPolicy = & scaleDownSelectPolicy
237
- }
238
- if v2behavior .ScaleDown .Policies != nil {
239
- ScaleDownPolicies := []autoscalingv2beta2.HPAScalingPolicy {}
240
- for _ , policy := range v2behavior .ScaleDown .Policies {
241
- v2beta2policy := ConvertToV2Beta2HPAScalingPolicy (policy )
242
- ScaleDownPolicies = append (ScaleDownPolicies , v2beta2policy )
107
+ // convert from v1alpha1.MetricSpec into a autoscalingv2.MetricSpec.
108
+ for _ , metric := range otelcol .Spec .Autoscaler .Metrics {
109
+ if metric .Type == autoscalingv2 .PodsMetricSourceType {
110
+ v2metric := autoscalingv2.MetricSpec {
111
+ Type : metric .Type ,
112
+ Pods : metric .Pods ,
243
113
}
244
- scaleDownRules .Policies = ScaleDownPolicies
245
- }
246
-
247
- behavior .ScaleDown = scaleDownRules
248
- }
249
-
250
- return * behavior
251
- }
252
-
253
- func ConvertToV2Beta2HPAScalingPolicy (v2policy autoscalingv2.HPAScalingPolicy ) autoscalingv2beta2.HPAScalingPolicy {
254
- v2beta2Policy := & autoscalingv2beta2.HPAScalingPolicy {
255
- Value : v2policy .Value ,
256
- PeriodSeconds : v2policy .PeriodSeconds ,
114
+ autoscaler .Spec .Metrics = append (autoscaler .Spec .Metrics , v2metric )
115
+ } // pod metrics
257
116
}
117
+ result = & autoscaler
258
118
259
- switch v2policy .Type {
260
- case autoscalingv2 .PodsScalingPolicy :
261
- v2beta2Policy .Type = autoscalingv2beta2 .PodsScalingPolicy
262
- case autoscalingv2 .PercentScalingPolicy :
263
- v2beta2Policy .Type = autoscalingv2beta2 .PercentScalingPolicy
264
- }
265
-
266
- return * v2beta2Policy
267
- }
268
-
269
- func ConvertToV2Beta2SelectPolicy (scalingPolicy autoscalingv2.ScalingPolicySelect ) autoscalingv2beta2.ScalingPolicySelect {
270
- max := autoscalingv2beta2 .MaxPolicySelect
271
- min := autoscalingv2beta2 .MinPolicySelect
272
- disabled := autoscalingv2beta2 .DisabledPolicySelect
273
-
274
- switch scalingPolicy {
275
- case autoscalingv2 .MaxChangePolicySelect :
276
- return max
277
- case autoscalingv2 .MinChangePolicySelect :
278
- return min
279
- case autoscalingv2 .DisabledPolicySelect :
280
- return disabled
281
- }
282
-
283
- return disabled
119
+ return result
284
120
}
0 commit comments