@@ -260,6 +260,10 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v1_HorizontalPodAutoscaler(i
260
260
return err
261
261
}
262
262
263
+ // clear any pre-existing round-trip annotations to make sure the only ones set are ones we produced during conversion
264
+ annotations , copiedAnnotations := autoscaling .DropRoundTripHorizontalPodAutoscalerAnnotations (out .Annotations )
265
+ out .Annotations = annotations
266
+
263
267
otherMetrics := make ([]autoscalingv1.MetricSpec , 0 , len (in .Spec .Metrics ))
264
268
for _ , metric := range in .Spec .Metrics {
265
269
if metric .Type == autoscaling .ResourceMetricSourceType && metric .Resource != nil && metric .Resource .Name == core .ResourceCPU && metric .Resource .Target .AverageUtilization != nil {
@@ -289,19 +293,16 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v1_HorizontalPodAutoscaler(i
289
293
}
290
294
}
291
295
292
- if len (otherMetrics ) > 0 || len (in .Status .CurrentMetrics ) > 0 || len (currentConditions ) > 0 || in .Spec .Behavior != nil {
293
- old := out .Annotations
294
- out .Annotations = make (map [string ]string , len (old )+ 4 )
295
- for k , v := range old {
296
- out .Annotations [k ] = v
297
- }
298
- }
299
-
300
296
if len (otherMetrics ) > 0 {
301
297
otherMetricsEnc , err := json .Marshal (otherMetrics )
302
298
if err != nil {
303
299
return err
304
300
}
301
+ // copy before mutating
302
+ if ! copiedAnnotations {
303
+ copiedAnnotations = true
304
+ out .Annotations = autoscaling .DeepCopyStringMap (out .Annotations )
305
+ }
305
306
out .Annotations [autoscaling .MetricSpecsAnnotation ] = string (otherMetricsEnc )
306
307
}
307
308
@@ -310,14 +311,25 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v1_HorizontalPodAutoscaler(i
310
311
if err != nil {
311
312
return err
312
313
}
314
+ // copy before mutating
315
+ if ! copiedAnnotations {
316
+ copiedAnnotations = true
317
+ out .Annotations = autoscaling .DeepCopyStringMap (out .Annotations )
318
+ }
313
319
out .Annotations [autoscaling .MetricStatusesAnnotation ] = string (currentMetricsEnc )
314
320
}
315
321
316
322
if in .Spec .Behavior != nil {
323
+ // TODO: this is marshaling an internal type. Fix this without breaking backwards compatibility.
317
324
behaviorEnc , err := json .Marshal (in .Spec .Behavior )
318
325
if err != nil {
319
326
return err
320
327
}
328
+ // copy before mutating
329
+ if ! copiedAnnotations {
330
+ copiedAnnotations = true
331
+ out .Annotations = autoscaling .DeepCopyStringMap (out .Annotations )
332
+ }
321
333
out .Annotations [autoscaling .BehaviorSpecsAnnotation ] = string (behaviorEnc )
322
334
}
323
335
@@ -326,6 +338,11 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v1_HorizontalPodAutoscaler(i
326
338
if err != nil {
327
339
return err
328
340
}
341
+ // copy before mutating
342
+ if ! copiedAnnotations {
343
+ copiedAnnotations = true
344
+ out .Annotations = autoscaling .DeepCopyStringMap (out .Annotations )
345
+ }
329
346
out .Annotations [autoscaling .HorizontalPodAutoscalerConditionsAnnotation ] = string (currentConditionsEnc )
330
347
}
331
348
@@ -339,47 +356,40 @@ func Convert_v1_HorizontalPodAutoscaler_To_autoscaling_HorizontalPodAutoscaler(i
339
356
340
357
if otherMetricsEnc , hasOtherMetrics := out .Annotations [autoscaling .MetricSpecsAnnotation ]; hasOtherMetrics {
341
358
var otherMetrics []autoscalingv1.MetricSpec
342
- if err := json .Unmarshal ([]byte (otherMetricsEnc ), & otherMetrics ); err != nil {
343
- return err
344
- }
345
-
346
- // the normal Spec conversion could have populated out.Spec.Metrics with a single element, so deal with that
347
- outMetrics := make ([]autoscaling.MetricSpec , len (otherMetrics )+ len (out .Spec .Metrics ))
348
- for i , metric := range otherMetrics {
349
- if err := Convert_v1_MetricSpec_To_autoscaling_MetricSpec (& metric , & outMetrics [i ], s ); err != nil {
350
- return err
359
+ if err := json .Unmarshal ([]byte (otherMetricsEnc ), & otherMetrics ); err == nil {
360
+ // the normal Spec conversion could have populated out.Spec.Metrics with a single element, so deal with that
361
+ outMetrics := make ([]autoscaling.MetricSpec , len (otherMetrics )+ len (out .Spec .Metrics ))
362
+ for i , metric := range otherMetrics {
363
+ if err := Convert_v1_MetricSpec_To_autoscaling_MetricSpec (& metric , & outMetrics [i ], s ); err != nil {
364
+ return err
365
+ }
351
366
}
367
+ if out .Spec .Metrics != nil {
368
+ outMetrics [len (otherMetrics )] = out .Spec .Metrics [0 ]
369
+ }
370
+ out .Spec .Metrics = outMetrics
352
371
}
353
- if out .Spec .Metrics != nil {
354
- outMetrics [len (otherMetrics )] = out .Spec .Metrics [0 ]
355
- }
356
- out .Spec .Metrics = outMetrics
357
- delete (out .Annotations , autoscaling .MetricSpecsAnnotation )
358
372
}
359
373
360
374
if behaviorEnc , hasConstraints := out .Annotations [autoscaling .BehaviorSpecsAnnotation ]; hasConstraints {
375
+ // TODO: this is unmarshaling an internal type. Fix this without breaking backwards compatibility.
361
376
var behavior autoscaling.HorizontalPodAutoscalerBehavior
362
- if err := json .Unmarshal ([]byte (behaviorEnc ), & behavior ); err != nil {
363
- return err
377
+ if err := json .Unmarshal ([]byte (behaviorEnc ), & behavior ); err == nil && behavior != (autoscaling. HorizontalPodAutoscalerBehavior {}) {
378
+ out . Spec . Behavior = & behavior
364
379
}
365
- out .Spec .Behavior = & behavior
366
- delete (out .Annotations , autoscaling .BehaviorSpecsAnnotation )
367
380
}
368
381
369
382
if currentMetricsEnc , hasCurrentMetrics := out .Annotations [autoscaling .MetricStatusesAnnotation ]; hasCurrentMetrics {
370
383
// ignore any existing status values -- the ones here have more information
371
384
var currentMetrics []autoscalingv1.MetricStatus
372
- if err := json .Unmarshal ([]byte (currentMetricsEnc ), & currentMetrics ); err != nil {
373
- return err
374
- }
375
-
376
- out .Status .CurrentMetrics = make ([]autoscaling.MetricStatus , len (currentMetrics ))
377
- for i , currentMetric := range currentMetrics {
378
- if err := Convert_v1_MetricStatus_To_autoscaling_MetricStatus (& currentMetric , & out .Status .CurrentMetrics [i ], s ); err != nil {
379
- return err
385
+ if err := json .Unmarshal ([]byte (currentMetricsEnc ), & currentMetrics ); err == nil {
386
+ out .Status .CurrentMetrics = make ([]autoscaling.MetricStatus , len (currentMetrics ))
387
+ for i , currentMetric := range currentMetrics {
388
+ if err := Convert_v1_MetricStatus_To_autoscaling_MetricStatus (& currentMetric , & out .Status .CurrentMetrics [i ], s ); err != nil {
389
+ return err
390
+ }
380
391
}
381
392
}
382
- delete (out .Annotations , autoscaling .MetricStatusesAnnotation )
383
393
}
384
394
385
395
// autoscaling/v1 formerly had an implicit default applied in the controller. In v2beta1, we apply it explicitly.
@@ -403,19 +413,19 @@ func Convert_v1_HorizontalPodAutoscaler_To_autoscaling_HorizontalPodAutoscaler(i
403
413
404
414
if currentConditionsEnc , hasCurrentConditions := out .Annotations [autoscaling .HorizontalPodAutoscalerConditionsAnnotation ]; hasCurrentConditions {
405
415
var currentConditions []autoscalingv1.HorizontalPodAutoscalerCondition
406
- if err := json .Unmarshal ([]byte (currentConditionsEnc ), & currentConditions ); err != nil {
407
- return err
408
- }
409
-
410
- out .Status .Conditions = make ([]autoscaling.HorizontalPodAutoscalerCondition , len (currentConditions ))
411
- for i , currentCondition := range currentConditions {
412
- if err := Convert_v1_HorizontalPodAutoscalerCondition_To_autoscaling_HorizontalPodAutoscalerCondition (& currentCondition , & out .Status .Conditions [i ], s ); err != nil {
413
- return err
416
+ if err := json .Unmarshal ([]byte (currentConditionsEnc ), & currentConditions ); err == nil {
417
+ out .Status .Conditions = make ([]autoscaling.HorizontalPodAutoscalerCondition , len (currentConditions ))
418
+ for i , currentCondition := range currentConditions {
419
+ if err := Convert_v1_HorizontalPodAutoscalerCondition_To_autoscaling_HorizontalPodAutoscalerCondition (& currentCondition , & out .Status .Conditions [i ], s ); err != nil {
420
+ return err
421
+ }
414
422
}
415
423
}
416
- delete (out .Annotations , autoscaling .HorizontalPodAutoscalerConditionsAnnotation )
417
424
}
418
425
426
+ // drop round-tripping annotations after converting to internal
427
+ out .Annotations , _ = autoscaling .DropRoundTripHorizontalPodAutoscalerAnnotations (out .Annotations )
428
+
419
429
return nil
420
430
}
421
431
0 commit comments