...

Source file src/k8s.io/kubernetes/pkg/registry/autoscaling/horizontalpodautoscaler/strategy.go

Documentation: k8s.io/kubernetes/pkg/registry/autoscaling/horizontalpodautoscaler

     1  /*
     2  Copyright 2015 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 horizontalpodautoscaler
    18  
    19  import (
    20  	"context"
    21  
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  	"k8s.io/apimachinery/pkg/util/validation/field"
    24  	"k8s.io/apiserver/pkg/storage/names"
    25  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    26  	"k8s.io/kubernetes/pkg/apis/autoscaling"
    27  	"k8s.io/kubernetes/pkg/apis/autoscaling/validation"
    28  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    29  )
    30  
    31  // autoscalerStrategy implements behavior for HorizontalPodAutoscalers
    32  type autoscalerStrategy struct {
    33  	runtime.ObjectTyper
    34  	names.NameGenerator
    35  }
    36  
    37  // Strategy is the default logic that applies when creating and updating HorizontalPodAutoscaler
    38  // objects via the REST API.
    39  var Strategy = autoscalerStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
    40  
    41  // NamespaceScoped is true for autoscaler.
    42  func (autoscalerStrategy) NamespaceScoped() bool {
    43  	return true
    44  }
    45  
    46  // GetResetFields returns the set of fields that get reset by the strategy
    47  // and should not be modified by the user.
    48  func (autoscalerStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
    49  	fields := map[fieldpath.APIVersion]*fieldpath.Set{
    50  		"autoscaling/v1": fieldpath.NewSet(
    51  			fieldpath.MakePathOrDie("status"),
    52  		),
    53  		"autoscaling/v2": fieldpath.NewSet(
    54  			fieldpath.MakePathOrDie("status"),
    55  		),
    56  		"autoscaling/v2beta1": fieldpath.NewSet(
    57  			fieldpath.MakePathOrDie("status"),
    58  		),
    59  		"autoscaling/v2beta2": fieldpath.NewSet(
    60  			fieldpath.MakePathOrDie("status"),
    61  		),
    62  	}
    63  
    64  	return fields
    65  }
    66  
    67  // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
    68  func (autoscalerStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
    69  	newHPA := obj.(*autoscaling.HorizontalPodAutoscaler)
    70  
    71  	// create cannot set status
    72  	newHPA.Status = autoscaling.HorizontalPodAutoscalerStatus{}
    73  }
    74  
    75  // Validate validates a new autoscaler.
    76  func (autoscalerStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
    77  	autoscaler := obj.(*autoscaling.HorizontalPodAutoscaler)
    78  	return validation.ValidateHorizontalPodAutoscaler(autoscaler)
    79  }
    80  
    81  // WarningsOnCreate returns warnings for the creation of the given object.
    82  func (autoscalerStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
    83  	return nil
    84  }
    85  
    86  // Canonicalize normalizes the object after validation.
    87  func (autoscalerStrategy) Canonicalize(obj runtime.Object) {
    88  }
    89  
    90  // AllowCreateOnUpdate is false for autoscalers.
    91  func (autoscalerStrategy) AllowCreateOnUpdate() bool {
    92  	return false
    93  }
    94  
    95  // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
    96  func (autoscalerStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
    97  	newHPA := obj.(*autoscaling.HorizontalPodAutoscaler)
    98  	oldHPA := old.(*autoscaling.HorizontalPodAutoscaler)
    99  	// Update is not allowed to set status
   100  	newHPA.Status = oldHPA.Status
   101  }
   102  
   103  // ValidateUpdate is the default update validation for an end user.
   104  func (autoscalerStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
   105  	return validation.ValidateHorizontalPodAutoscalerUpdate(obj.(*autoscaling.HorizontalPodAutoscaler), old.(*autoscaling.HorizontalPodAutoscaler))
   106  }
   107  
   108  // WarningsOnUpdate returns warnings for the given update.
   109  func (autoscalerStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
   110  	return nil
   111  }
   112  
   113  func (autoscalerStrategy) AllowUnconditionalUpdate() bool {
   114  	return true
   115  }
   116  
   117  type autoscalerStatusStrategy struct {
   118  	autoscalerStrategy
   119  }
   120  
   121  // StatusStrategy is the default logic invoked when updating object status.
   122  var StatusStrategy = autoscalerStatusStrategy{Strategy}
   123  
   124  // GetResetFields returns the set of fields that get reset by the strategy
   125  // and should not be modified by the user.
   126  func (autoscalerStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
   127  	fields := map[fieldpath.APIVersion]*fieldpath.Set{
   128  		"autoscaling/v1": fieldpath.NewSet(
   129  			fieldpath.MakePathOrDie("spec"),
   130  		),
   131  		"autoscaling/v2": fieldpath.NewSet(
   132  			fieldpath.MakePathOrDie("spec"),
   133  		),
   134  		"autoscaling/v2beta1": fieldpath.NewSet(
   135  			fieldpath.MakePathOrDie("spec"),
   136  		),
   137  		"autoscaling/v2beta2": fieldpath.NewSet(
   138  			fieldpath.MakePathOrDie("spec"),
   139  		),
   140  	}
   141  
   142  	return fields
   143  }
   144  
   145  func (autoscalerStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
   146  	newAutoscaler := obj.(*autoscaling.HorizontalPodAutoscaler)
   147  	oldAutoscaler := old.(*autoscaling.HorizontalPodAutoscaler)
   148  	// status changes are not allowed to update spec
   149  	newAutoscaler.Spec = oldAutoscaler.Spec
   150  }
   151  
   152  func (autoscalerStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
   153  	return validation.ValidateHorizontalPodAutoscalerStatusUpdate(obj.(*autoscaling.HorizontalPodAutoscaler), old.(*autoscaling.HorizontalPodAutoscaler))
   154  }
   155  
   156  // WarningsOnUpdate returns warnings for the given update.
   157  func (autoscalerStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
   158  	return nil
   159  }
   160  

View as plain text