k8s.io/kubernetes@v1.29.3/pkg/apis/autoscaling/v2/defaults.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v2
    18  
    19  import (
    20  	autoscalingv2 "k8s.io/api/autoscaling/v2"
    21  	v1 "k8s.io/api/core/v1"
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  	"k8s.io/kubernetes/pkg/apis/autoscaling"
    24  	"k8s.io/utils/pointer"
    25  )
    26  
    27  var (
    28  	// These constants repeats previous HPA behavior
    29  	scaleUpLimitPercent         int32 = 100
    30  	scaleUpLimitMinimumPods     int32 = 4
    31  	scaleUpPeriod               int32 = 15
    32  	scaleUpStabilizationSeconds int32
    33  	maxPolicy                   = autoscalingv2.MaxChangePolicySelect
    34  	defaultHPAScaleUpRules      = autoscalingv2.HPAScalingRules{
    35  		StabilizationWindowSeconds: &scaleUpStabilizationSeconds,
    36  		SelectPolicy:               &maxPolicy,
    37  		Policies: []autoscalingv2.HPAScalingPolicy{
    38  			{
    39  				Type:          autoscalingv2.PodsScalingPolicy,
    40  				Value:         scaleUpLimitMinimumPods,
    41  				PeriodSeconds: scaleUpPeriod,
    42  			},
    43  			{
    44  				Type:          autoscalingv2.PercentScalingPolicy,
    45  				Value:         scaleUpLimitPercent,
    46  				PeriodSeconds: scaleUpPeriod,
    47  			},
    48  		},
    49  	}
    50  	scaleDownPeriod int32 = 15
    51  	// Currently we can set the downscaleStabilizationWindow from the command line
    52  	// So we can not rewrite the command line option from here
    53  	scaleDownLimitPercent    int32 = 100
    54  	defaultHPAScaleDownRules       = autoscalingv2.HPAScalingRules{
    55  		StabilizationWindowSeconds: nil,
    56  		SelectPolicy:               &maxPolicy,
    57  		Policies: []autoscalingv2.HPAScalingPolicy{
    58  			{
    59  				Type:          autoscalingv2.PercentScalingPolicy,
    60  				Value:         scaleDownLimitPercent,
    61  				PeriodSeconds: scaleDownPeriod,
    62  			},
    63  		},
    64  	}
    65  )
    66  
    67  func addDefaultingFuncs(scheme *runtime.Scheme) error {
    68  	return RegisterDefaults(scheme)
    69  }
    70  
    71  func SetDefaults_HorizontalPodAutoscaler(obj *autoscalingv2.HorizontalPodAutoscaler) {
    72  	if obj.Spec.MinReplicas == nil {
    73  		obj.Spec.MinReplicas = pointer.Int32(1)
    74  	}
    75  
    76  	if len(obj.Spec.Metrics) == 0 {
    77  		utilizationDefaultVal := int32(autoscaling.DefaultCPUUtilization)
    78  		obj.Spec.Metrics = []autoscalingv2.MetricSpec{
    79  			{
    80  				Type: autoscalingv2.ResourceMetricSourceType,
    81  				Resource: &autoscalingv2.ResourceMetricSource{
    82  					Name: v1.ResourceCPU,
    83  					Target: autoscalingv2.MetricTarget{
    84  						Type:               autoscalingv2.UtilizationMetricType,
    85  						AverageUtilization: &utilizationDefaultVal,
    86  					},
    87  				},
    88  			},
    89  		}
    90  	}
    91  	SetDefaults_HorizontalPodAutoscalerBehavior(obj)
    92  }
    93  
    94  // SetDefaults_HorizontalPodAutoscalerBehavior fills the behavior if it is not null
    95  func SetDefaults_HorizontalPodAutoscalerBehavior(obj *autoscalingv2.HorizontalPodAutoscaler) {
    96  	// if behavior is specified, we should fill all the 'nil' values with the default ones
    97  	if obj.Spec.Behavior != nil {
    98  		obj.Spec.Behavior.ScaleUp = GenerateHPAScaleUpRules(obj.Spec.Behavior.ScaleUp)
    99  		obj.Spec.Behavior.ScaleDown = GenerateHPAScaleDownRules(obj.Spec.Behavior.ScaleDown)
   100  	}
   101  }
   102  
   103  // GenerateHPAScaleUpRules returns a fully-initialized HPAScalingRules value
   104  // We guarantee that no pointer in the structure will have the 'nil' value
   105  func GenerateHPAScaleUpRules(scalingRules *autoscalingv2.HPAScalingRules) *autoscalingv2.HPAScalingRules {
   106  	defaultScalingRules := defaultHPAScaleUpRules.DeepCopy()
   107  	return copyHPAScalingRules(scalingRules, defaultScalingRules)
   108  }
   109  
   110  // GenerateHPAScaleDownRules returns a fully-initialized HPAScalingRules value
   111  // We guarantee that no pointer in the structure will have the 'nil' value
   112  // EXCEPT StabilizationWindowSeconds, for reasoning check the comment for defaultHPAScaleDownRules
   113  func GenerateHPAScaleDownRules(scalingRules *autoscalingv2.HPAScalingRules) *autoscalingv2.HPAScalingRules {
   114  	defaultScalingRules := defaultHPAScaleDownRules.DeepCopy()
   115  	return copyHPAScalingRules(scalingRules, defaultScalingRules)
   116  }
   117  
   118  // copyHPAScalingRules copies all non-`nil` fields in HPA constraint structure
   119  func copyHPAScalingRules(from, to *autoscalingv2.HPAScalingRules) *autoscalingv2.HPAScalingRules {
   120  	if from == nil {
   121  		return to
   122  	}
   123  	if from.SelectPolicy != nil {
   124  		to.SelectPolicy = from.SelectPolicy
   125  	}
   126  	if from.StabilizationWindowSeconds != nil {
   127  		to.StabilizationWindowSeconds = from.StabilizationWindowSeconds
   128  	}
   129  	if from.Policies != nil {
   130  		to.Policies = from.Policies
   131  	}
   132  	return to
   133  }