...

Source file src/k8s.io/kubernetes/pkg/apis/autoscaling/v2beta2/defaults.go

Documentation: k8s.io/kubernetes/pkg/apis/autoscaling/v2beta2

     1  /*
     2  Copyright 2018 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 v2beta2
    18  
    19  import (
    20  	autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
    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                   = autoscalingv2beta2.MaxPolicySelect
    34  	defaultHPAScaleUpRules      = autoscalingv2beta2.HPAScalingRules{
    35  		StabilizationWindowSeconds: &scaleUpStabilizationSeconds,
    36  		SelectPolicy:               &maxPolicy,
    37  		Policies: []autoscalingv2beta2.HPAScalingPolicy{
    38  			{
    39  				Type:          autoscalingv2beta2.PodsScalingPolicy,
    40  				Value:         scaleUpLimitMinimumPods,
    41  				PeriodSeconds: scaleUpPeriod,
    42  			},
    43  			{
    44  				Type:          autoscalingv2beta2.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  	scaleDownStabilizationSeconds *int32 = nil
    54  	scaleDownLimitPercent         int32  = 100
    55  	defaultHPAScaleDownRules             = autoscalingv2beta2.HPAScalingRules{
    56  		StabilizationWindowSeconds: scaleDownStabilizationSeconds,
    57  		SelectPolicy:               &maxPolicy,
    58  		Policies: []autoscalingv2beta2.HPAScalingPolicy{
    59  			{
    60  				Type:          autoscalingv2beta2.PercentScalingPolicy,
    61  				Value:         scaleDownLimitPercent,
    62  				PeriodSeconds: scaleDownPeriod,
    63  			},
    64  		},
    65  	}
    66  )
    67  
    68  func addDefaultingFuncs(scheme *runtime.Scheme) error {
    69  	return RegisterDefaults(scheme)
    70  }
    71  
    72  func SetDefaults_HorizontalPodAutoscaler(obj *autoscalingv2beta2.HorizontalPodAutoscaler) {
    73  	if obj.Spec.MinReplicas == nil {
    74  		obj.Spec.MinReplicas = pointer.Int32(1)
    75  	}
    76  
    77  	if len(obj.Spec.Metrics) == 0 {
    78  		utilizationDefaultVal := int32(autoscaling.DefaultCPUUtilization)
    79  		obj.Spec.Metrics = []autoscalingv2beta2.MetricSpec{
    80  			{
    81  				Type: autoscalingv2beta2.ResourceMetricSourceType,
    82  				Resource: &autoscalingv2beta2.ResourceMetricSource{
    83  					Name: v1.ResourceCPU,
    84  					Target: autoscalingv2beta2.MetricTarget{
    85  						Type:               autoscalingv2beta2.UtilizationMetricType,
    86  						AverageUtilization: &utilizationDefaultVal,
    87  					},
    88  				},
    89  			},
    90  		}
    91  	}
    92  	SetDefaults_HorizontalPodAutoscalerBehavior(obj)
    93  }
    94  
    95  // SetDefaults_HorizontalPodAutoscalerBehavior fills the behavior if it is not null
    96  func SetDefaults_HorizontalPodAutoscalerBehavior(obj *autoscalingv2beta2.HorizontalPodAutoscaler) {
    97  	// if behavior is specified, we should fill all the 'nil' values with the default ones
    98  	if obj.Spec.Behavior != nil {
    99  		obj.Spec.Behavior.ScaleUp = GenerateHPAScaleUpRules(obj.Spec.Behavior.ScaleUp)
   100  		obj.Spec.Behavior.ScaleDown = GenerateHPAScaleDownRules(obj.Spec.Behavior.ScaleDown)
   101  	}
   102  }
   103  
   104  // GenerateHPAScaleUpRules returns a fully-initialized HPAScalingRules value
   105  // We guarantee that no pointer in the structure will have the 'nil' value
   106  func GenerateHPAScaleUpRules(scalingRules *autoscalingv2beta2.HPAScalingRules) *autoscalingv2beta2.HPAScalingRules {
   107  	defaultScalingRules := defaultHPAScaleUpRules.DeepCopy()
   108  	return copyHPAScalingRules(scalingRules, defaultScalingRules)
   109  }
   110  
   111  // GenerateHPAScaleDownRules returns a fully-initialized HPAScalingRules value
   112  // We guarantee that no pointer in the structure will have the 'nil' value
   113  // EXCEPT StabilizationWindowSeconds, for reasoning check the comment for defaultHPAScaleDownRules
   114  func GenerateHPAScaleDownRules(scalingRules *autoscalingv2beta2.HPAScalingRules) *autoscalingv2beta2.HPAScalingRules {
   115  	defaultScalingRules := defaultHPAScaleDownRules.DeepCopy()
   116  	return copyHPAScalingRules(scalingRules, defaultScalingRules)
   117  }
   118  
   119  // copyHPAScalingRules copies all non-`nil` fields in HPA constraint structure
   120  func copyHPAScalingRules(from, to *autoscalingv2beta2.HPAScalingRules) *autoscalingv2beta2.HPAScalingRules {
   121  	if from == nil {
   122  		return to
   123  	}
   124  	if from.SelectPolicy != nil {
   125  		to.SelectPolicy = from.SelectPolicy
   126  	}
   127  	if from.StabilizationWindowSeconds != nil {
   128  		to.StabilizationWindowSeconds = from.StabilizationWindowSeconds
   129  	}
   130  	if from.Policies != nil {
   131  		to.Policies = from.Policies
   132  	}
   133  	return to
   134  }
   135  

View as plain text