@@ -267,6 +267,8 @@ func (c *Service) CreateHPA(ctx context.Context, tortoise *autoscalingv1beta3.To
267267 },
268268 }
269269
270+ c .copyLabelsFromTortoiseToHPA (tortoise , hpa )
271+
270272 hpa , tortoise , _ = c .syncHPAMetricsWithTortoiseAutoscalingPolicy (ctx , tortoise , hpa , now )
271273
272274 tortoise .Status .Targets .HorizontalPodAutoscaler = hpa .Name
@@ -275,6 +277,63 @@ func (c *Service) CreateHPA(ctx context.Context, tortoise *autoscalingv1beta3.To
275277 return hpa .DeepCopy (), tortoise , err
276278}
277279
280+ func (c * Service ) copyLabelsFromTortoiseToHPA (tortoise * autoscalingv1beta3.Tortoise , hpa * v2.HorizontalPodAutoscaler ) {
281+ if len (tortoise .ObjectMeta .Labels ) == 0 {
282+ hpa .ObjectMeta .Labels = nil
283+ return
284+ }
285+
286+ hpa .ObjectMeta .Labels = make (map [string ]string , len (tortoise .ObjectMeta .Labels ))
287+ for k , v := range tortoise .ObjectMeta .Labels {
288+ hpa .ObjectMeta .Labels [k ] = v
289+ }
290+ }
291+
292+ // UpdateHPA updates HPA labels from Tortoise.
293+ // Note that it will not perform any update if the UpdateMode is Off, user specified the existing HPA, or there is no horizontal policy.
294+ func (c * Service ) UpdateHPALabelsFromTortoise (ctx context.Context , tortoise * autoscalingv1beta3.Tortoise ) error {
295+ if tortoise .Spec .UpdateMode == autoscalingv1beta3 .UpdateModeOff {
296+ // When UpdateMode is Off, we don't update HPA.
297+ return nil
298+ }
299+ if ! HasHorizontal (tortoise ) {
300+ // no need to handle when there is no horizontal policy.
301+ return nil
302+ }
303+ if tortoise .Spec .TargetRefs .HorizontalPodAutoscalerName != nil {
304+ // no need to handle when the user specified existing HPA.
305+ return nil
306+ }
307+
308+ retryNumber := - 1
309+ updateFn := func () error {
310+ retryNumber ++
311+ hpa := & v2.HorizontalPodAutoscaler {}
312+ if err := c .c .Get (ctx , types.NamespacedName {Namespace : tortoise .Namespace , Name : tortoise .Status .Targets .HorizontalPodAutoscaler }, hpa ); err != nil {
313+ return fmt .Errorf ("failed to get hpa on tortoise: %w" , err )
314+ }
315+ hpa = hpa .DeepCopy ()
316+
317+ if reflect .DeepEqual (hpa .ObjectMeta .Labels , tortoise .ObjectMeta .Labels ) {
318+ // Labels are already in sync, no need to update.
319+ return nil
320+ }
321+ c .copyLabelsFromTortoiseToHPA (tortoise , hpa )
322+
323+ if err := c .c .Update (ctx , hpa ); err != nil {
324+ return fmt .Errorf ("failed to update hpa: %w" , err )
325+ }
326+
327+ return nil
328+ }
329+
330+ if err := retry .RetryOnConflict (retry .DefaultRetry , updateFn ); err != nil {
331+ return fmt .Errorf ("update hpa labels: %w (%v times retried)" , err , retryNumber )
332+ }
333+
334+ return nil
335+ }
336+
278337func (c * Service ) GetHPAOnTortoiseSpec (ctx context.Context , tortoise * autoscalingv1beta3.Tortoise ) (* v2.HorizontalPodAutoscaler , error ) {
279338 if tortoise .Spec .TargetRefs .HorizontalPodAutoscalerName == nil {
280339 return nil , nil
0 commit comments