@@ -294,6 +294,10 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v1_HorizontalPodAutoscaler(i
294
294
return err
295
295
}
296
296
297
+ // clear any pre-existing round-trip annotations to make sure the only ones set are ones we produced during conversion
298
+ annotations , copiedAnnotations := autoscaling .DropRoundTripHorizontalPodAutoscalerAnnotations (out .Annotations )
299
+ out .Annotations = annotations
300
+
297
301
otherMetrics := make ([]autoscalingv1.MetricSpec , 0 , len (in .Spec .Metrics ))
298
302
for _ , metric := range in .Spec .Metrics {
299
303
if metric .Type == autoscaling .ResourceMetricSourceType && metric .Resource != nil && metric .Resource .Name == core .ResourceCPU && metric .Resource .Target .AverageUtilization != nil {
@@ -323,19 +327,16 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v1_HorizontalPodAutoscaler(i
323
327
}
324
328
}
325
329
326
- if len (otherMetrics ) > 0 || len (in .Status .CurrentMetrics ) > 0 || len (currentConditions ) > 0 {
327
- old := out .Annotations
328
- out .Annotations = make (map [string ]string , len (old )+ 3 )
329
- for k , v := range old {
330
- out .Annotations [k ] = v
331
- }
332
- }
333
-
334
330
if len (otherMetrics ) > 0 {
335
331
otherMetricsEnc , err := json .Marshal (otherMetrics )
336
332
if err != nil {
337
333
return err
338
334
}
335
+ // copy before mutating
336
+ if ! copiedAnnotations {
337
+ copiedAnnotations = true
338
+ out .Annotations = autoscaling .DeepCopyStringMap (out .Annotations )
339
+ }
339
340
out .Annotations [autoscaling .MetricSpecsAnnotation ] = string (otherMetricsEnc )
340
341
}
341
342
@@ -344,6 +345,11 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v1_HorizontalPodAutoscaler(i
344
345
if err != nil {
345
346
return err
346
347
}
348
+ // copy before mutating
349
+ if ! copiedAnnotations {
350
+ copiedAnnotations = true
351
+ out .Annotations = autoscaling .DeepCopyStringMap (out .Annotations )
352
+ }
347
353
out .Annotations [autoscaling .MetricStatusesAnnotation ] = string (currentMetricsEnc )
348
354
}
349
355
@@ -352,6 +358,11 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v1_HorizontalPodAutoscaler(i
352
358
if err != nil {
353
359
return err
354
360
}
361
+ // copy before mutating
362
+ if ! copiedAnnotations {
363
+ copiedAnnotations = true
364
+ out .Annotations = autoscaling .DeepCopyStringMap (out .Annotations )
365
+ }
355
366
out .Annotations [autoscaling .HorizontalPodAutoscalerConditionsAnnotation ] = string (currentConditionsEnc )
356
367
}
357
368
@@ -365,38 +376,32 @@ func Convert_v1_HorizontalPodAutoscaler_To_autoscaling_HorizontalPodAutoscaler(i
365
376
366
377
if otherMetricsEnc , hasOtherMetrics := out .Annotations [autoscaling .MetricSpecsAnnotation ]; hasOtherMetrics {
367
378
var otherMetrics []autoscalingv1.MetricSpec
368
- if err := json .Unmarshal ([]byte (otherMetricsEnc ), & otherMetrics ); err != nil {
369
- return err
370
- }
371
-
372
- // the normal Spec conversion could have populated out.Spec.Metrics with a single element, so deal with that
373
- outMetrics := make ([]autoscaling.MetricSpec , len (otherMetrics )+ len (out .Spec .Metrics ))
374
- for i , metric := range otherMetrics {
375
- if err := Convert_v1_MetricSpec_To_autoscaling_MetricSpec (& metric , & outMetrics [i ], s ); err != nil {
376
- return err
379
+ if err := json .Unmarshal ([]byte (otherMetricsEnc ), & otherMetrics ); err == nil {
380
+ // the normal Spec conversion could have populated out.Spec.Metrics with a single element, so deal with that
381
+ outMetrics := make ([]autoscaling.MetricSpec , len (otherMetrics )+ len (out .Spec .Metrics ))
382
+ for i , metric := range otherMetrics {
383
+ if err := Convert_v1_MetricSpec_To_autoscaling_MetricSpec (& metric , & outMetrics [i ], s ); err != nil {
384
+ return err
385
+ }
377
386
}
387
+ if out .Spec .Metrics != nil {
388
+ outMetrics [len (otherMetrics )] = out .Spec .Metrics [0 ]
389
+ }
390
+ out .Spec .Metrics = outMetrics
378
391
}
379
- if out .Spec .Metrics != nil {
380
- outMetrics [len (otherMetrics )] = out .Spec .Metrics [0 ]
381
- }
382
- out .Spec .Metrics = outMetrics
383
- delete (out .Annotations , autoscaling .MetricSpecsAnnotation )
384
392
}
385
393
386
394
if currentMetricsEnc , hasCurrentMetrics := out .Annotations [autoscaling .MetricStatusesAnnotation ]; hasCurrentMetrics {
387
395
// ignore any existing status values -- the ones here have more information
388
396
var currentMetrics []autoscalingv1.MetricStatus
389
- if err := json .Unmarshal ([]byte (currentMetricsEnc ), & currentMetrics ); err != nil {
390
- return err
391
- }
392
-
393
- out .Status .CurrentMetrics = make ([]autoscaling.MetricStatus , len (currentMetrics ))
394
- for i , currentMetric := range currentMetrics {
395
- if err := Convert_v1_MetricStatus_To_autoscaling_MetricStatus (& currentMetric , & out .Status .CurrentMetrics [i ], s ); err != nil {
396
- return err
397
+ if err := json .Unmarshal ([]byte (currentMetricsEnc ), & currentMetrics ); err == nil {
398
+ out .Status .CurrentMetrics = make ([]autoscaling.MetricStatus , len (currentMetrics ))
399
+ for i , currentMetric := range currentMetrics {
400
+ if err := Convert_v1_MetricStatus_To_autoscaling_MetricStatus (& currentMetric , & out .Status .CurrentMetrics [i ], s ); err != nil {
401
+ return err
402
+ }
397
403
}
398
404
}
399
- delete (out .Annotations , autoscaling .MetricStatusesAnnotation )
400
405
}
401
406
402
407
// autoscaling/v1 formerly had an implicit default applied in the controller. In v2beta1, we apply it explicitly.
@@ -420,19 +425,19 @@ func Convert_v1_HorizontalPodAutoscaler_To_autoscaling_HorizontalPodAutoscaler(i
420
425
421
426
if currentConditionsEnc , hasCurrentConditions := out .Annotations [autoscaling .HorizontalPodAutoscalerConditionsAnnotation ]; hasCurrentConditions {
422
427
var currentConditions []autoscalingv1.HorizontalPodAutoscalerCondition
423
- if err := json .Unmarshal ([]byte (currentConditionsEnc ), & currentConditions ); err != nil {
424
- return err
425
- }
426
-
427
- out .Status .Conditions = make ([]autoscaling.HorizontalPodAutoscalerCondition , len (currentConditions ))
428
- for i , currentCondition := range currentConditions {
429
- if err := Convert_v1_HorizontalPodAutoscalerCondition_To_autoscaling_HorizontalPodAutoscalerCondition (& currentCondition , & out .Status .Conditions [i ], s ); err != nil {
430
- return err
428
+ if err := json .Unmarshal ([]byte (currentConditionsEnc ), & currentConditions ); err == nil {
429
+ out .Status .Conditions = make ([]autoscaling.HorizontalPodAutoscalerCondition , len (currentConditions ))
430
+ for i , currentCondition := range currentConditions {
431
+ if err := Convert_v1_HorizontalPodAutoscalerCondition_To_autoscaling_HorizontalPodAutoscalerCondition (& currentCondition , & out .Status .Conditions [i ], s ); err != nil {
432
+ return err
433
+ }
431
434
}
432
435
}
433
- delete (out .Annotations , autoscaling .HorizontalPodAutoscalerConditionsAnnotation )
434
436
}
435
437
438
+ // drop round-tripping annotations after converting to internal
439
+ out .Annotations , _ = autoscaling .DropRoundTripHorizontalPodAutoscalerAnnotations (out .Annotations )
440
+
436
441
return nil
437
442
}
438
443
0 commit comments