8000 KEP 5325 - Improve pod selection accuracy across workload types · kubernetes/kubernetes@e0f0de6 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0f0de6

Browse files
committed
KEP 5325 - Improve pod selection accuracy across workload types
Signed-off-by: Omer Aplatony <omerap12@gmail.com>
1 parent 3f0a1c6 commit e0f0de6

File tree

43 files changed

+2110
-217
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2110
-217
lines changed

api/openapi-spec/swagger.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/openapi-spec/v3/apis__autoscaling__v2_openapi.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/kube-controller-manager/app/autoscaling.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ func startHPAControllerWithMetricsClient(ctx context.Context, controllerContext
7575
if err != nil {
7676
return nil, false, err
7777
}
78+
dynamicClient, err := dynamic.NewForConfig(hpaClientConfig)
79+
if err != nil {
80+
return nil, false, err
81+
}
7882

7983
go podautoscaler.NewHorizontalController(
8084
ctx,
@@ -90,6 +94,7 @@ func startHPAControllerWithMetricsClient(ctx context.Context, controllerContext
9094
controllerContext.ComponentConfig.HPAController.HorizontalPodAutoscalerTolerance,
9195
controllerContext.ComponentConfig.HPAController.HorizontalPodAutoscalerCPUInitializationPeriod.Duration,
9296
controllerContext.ComponentConfig.HPAController.HorizontalPodAutoscalerInitialReadinessDelay.Duration,
97+
dynamicClient,
9398
).Run(ctx, int(controllerContext.ComponentConfig.HPAController.ConcurrentHorizontalPodAutoscalerSyncs))
9499
return nil, true, nil
95100
}

pkg/apis/autoscaling/annotations.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ const ToleranceScaleDownAnnotation = "autoscaling.alpha.kubernetes.io/scale-down
4444
// ToleranceScaleUpAnnotation is the annotation which holds the HPA tolerance specs
4545
// when converting the `ScaleUp.Tolerance` field from autoscaling/v2
4646
const ToleranceScaleUpAnnotation = "autoscaling.alpha.kubernetes.io/scale-up-tolerance"
47+
48+
// SelectionStrategyAnnotation is the annotation which holds the pod selection strategy
49+
// when converting the `PodSelectionStrategy` field from autoscaling/v2
50+
const SelectionStrategyAnnotation = "autoscaling.alpha.kubernetes.io/pod-selection-strategy"

pkg/apis/autoscaling/helpers.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,23 @@ func DropRoundTripHorizontalPodAutoscalerAnnotations(in map[string]string) (out
4040
_, hasToleranceScaleUp := in[ToleranceScaleUpAnnotation]
4141
_, hasMetricsStatuses := in[MetricStatusesAnnotation]
4242
_, hasConditions := in[HorizontalPodAutoscalerConditionsAnnotation]
43-
if hasMetricsSpecs || hasBehaviorSpecs || hasToleranceScaleDown || hasToleranceScaleUp || hasMetricsStatuses || hasConditions {
43+
_, hasSelectionStrategy := in[SelectionStrategyAnnotation]
44+
45+
if hasMetricsSpecs || hasBehaviorSpecs || hasToleranceScaleDown ||
46+
hasToleranceScaleUp || hasMetricsStatuses || hasConditions ||
47+
hasSelectionStrategy {
48+
4449
out = DeepCopyStringMap(in)
4550
delete(out, MetricSpecsAnnotation)
4651
delete(out, BehaviorSpecsAnnotation)
4752
delete(out, ToleranceScaleDownAnnotation)
4853
delete(out, ToleranceScaleUpAnnotation)
4954
delete(out, MetricStatusesAnnotation)
5055
delete(out, HorizontalPodAutoscalerConditionsAnnotation)
56+
delete(out, SelectionStrategyAnnotation)
5157
return out, true
5258
}
59+
5360
return in, false
5461
}
5562

pkg/apis/autoscaling/types.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ type CrossVersionObjectReference struct {
7373
APIVersion string
7474
}
7575

76+
// SelectionStrategy defines how pods are selected for metrics collection and scaling decisions
77+
type SelectionStrategy string
78+
79+
const (
80+
// LabelSelector selects pods based on the label selector specified in the HPA's scale target
81+
LabelSelector SelectionStrategy = "LabelSelector"
82+
83+
// OwnerReferences selects pods based on the ownership chain to the HPA's scale target
84+
OwnerReferences SelectionStrategy = "OwnerReferences"
85+
)
86+
7687
// HorizontalPodAutoscalerSpec describes the desired functionality of the HorizontalPodAutoscaler.
7788
type HorizontalPodAutoscalerSpec struct {
7889
// scaleTargetRef points to the target resource to scale, and is used to the pods for which metrics
@@ -106,6 +117,25 @@ type HorizontalPodAutoscalerSpec struct {
106117
// If not set, the default HPAScalingRules for scale up and scale down are used.
107118
// +optional
108119
Behavior *HorizontalPodAutoscalerBehavior
120+
121+
// SelectionStrategy determines how pods are selected for metrics collection
122+
// and scaling decisions. Valid values are:
123+
// - "LabelSelector": uses the label selector from the scale target (default)
124+
// - "OwnerReferences": only considers pods owned by the scale target
125+
//
126+
// If not set, the default value "LabelSelector" is used, which maintains
127+
// backward compatibility with existing behavior.
128+
//
129+
// For example, when using "OwnerReferences" with a Deployment target,
130+
// only pods directly owned by the Deployment's ReplicaSets will be considered,
131+
// even if other pods match the label selector.
132+
//
133+
// This is an alpha field and requires enabling the HPASelectionStrategy
134+
// feature gate.
135+
//
136+
// +featureGate=HPASelectionStrategy
137+
// +optional
138+
SelectionStrategy *SelectionStrategy
109139
}
110140

111141
// HorizontalPodAutoscalerBehavior configures a scaling behavior for Up and Down direction

pkg/apis/autoscaling/v1/conversion.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v1_HorizontalPodAutoscaler(i
355355
out.Annotations[autoscaling.BehaviorSpecsAnnotation] = string(behaviorEnc)
356356
}
357357

358+
if in.Spec.SelectionStrategy != nil {
359+
// copy before mutating
360+
if !copiedAnnotations {
361+
copiedAnnotations = true
362+
out.Annotations = autoscaling.DeepCopyStringMap(out.Annotations)
363+
}
364+
out.Annotations[autoscaling.SelectionStrategyAnnotation] = string(*in.Spec.SelectionStrategy)
365+
}
366+
358367
if len(in.Status.Conditions) > 0 {
359368
currentConditionsEnc, err := json.Marshal(currentConditions)
360369
if err != nil {
@@ -446,6 +455,11 @@ func Convert_v1_HorizontalPodAutoscaler_To_autoscaling_HorizontalPodAutoscaler(i
446455
}
447456
}
448457

458+
if strategyStr, ok := out.Annotations[autoscaling.SelectionStrategyAnnotation]; ok {
459+
strategy := autoscaling.SelectionStrategy(strategyStr)
460+
out.Spec.SelectionStrategy = &strategy
461+
}
462+
449463
// drop round-tripping annotations after converting to internal
450464
out.Annotations, _ = autoscaling.DropRoundTripHorizontalPodAutoscalerAnnotations(out.Annotations)
451465

pkg/apis/autoscaling/v1/zz_generated.conversion.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/autoscaling/v2/zz_generated.conversion.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/autoscaling/v2beta1/conversion.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v2beta1_HorizontalPodAutosca
291291
annotations, copiedAnnotations := autoscaling.DropRoundTripHorizontalPodAutoscalerAnnotations(out.Annotations)
292292
out.Annotations = annotations
293293

294+
if in.Spec.SelectionStrategy != nil {
295+
if !copiedAnnotations {
296+
copiedAnnotations = true
297+
out.Annotations = autoscaling.DeepCopyStringMap(out.Annotations)
298+
}
299+
out.Annotations[autoscaling.SelectionStrategyAnnotation] = string(*in.Spec.SelectionStrategy)
300+
}
301+
294302
if in.Spec.Behavior != nil {
295303
// TODO: this is marshaling an internal type. Fix this without breaking backwards compatibility with n-1 API servers.
296304
behaviorEnc, err := json.Marshal(in.Spec.Behavior)
@@ -323,6 +331,11 @@ func Convert_v2beta1_HorizontalPodAutoscaler_To_autoscaling_HorizontalPodAutosca
323331
}
324332
}
325333

334+
if strategyStr, ok := out.Annotations[autoscaling.SelectionStrategyAnnotation]; ok {
335+
strategy := autoscaling.SelectionStrategy(strategyStr)
336+
out.Spec.SelectionStrategy = &strategy
337+
}
338+
326339
// drop round-tripping annotations after converting to internal
327340
out.Annotations, _ = autoscaling.DropRoundTripHorizontalPodAutoscalerAnnotations(out.Annotations)
328341

0 commit comments

Comments
 (0)
0