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

Skip to content

Commit afde280

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

Some content is hidden

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

42 files changed

+2096
-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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,19 @@ func Convert_autoscaling_HorizontalPodAutoscaler_To_v1_HorizontalPodAutoscaler(i
355355
out.Annotations[autoscaling.BehaviorSpecsAnnotation] = string(behaviorEnc)
356356
}
357357

358+
if in.Spec.SelectionStrategy != nil {
359+
selectionStrategyEnc, err := json.Marshal(in.Spec.SelectionStrategy)
360+
if err != nil {
361+
return err
362+
}
363+
// copy before mutating
364+
if !copiedAnnotations {
365+
copiedAnnotations = true
366+
out.Annotations = autoscaling.DeepCopyStringMap(out.Annotations)
367+
}
368+
out.Annotations[autoscaling.SelectionStrategyAnnotation] = string(selectionStrategyEnc)
369+
}
370+
358371
if len(in.Status.Conditions) > 0 {
359372
currentConditionsEnc, err := json.Marshal(currentConditions)
360373
if err != nil {

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/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.

0 commit comments

Comments
 (0)
0