...

Source file src/k8s.io/kubernetes/pkg/registry/core/podtemplate/strategy.go

Documentation: k8s.io/kubernetes/pkg/registry/core/podtemplate

     1  /*
     2  Copyright 2014 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 podtemplate
    18  
    19  import (
    20  	"context"
    21  
    22  	apiequality "k8s.io/apimachinery/pkg/api/equality"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/apimachinery/pkg/util/validation/field"
    25  	"k8s.io/apiserver/pkg/storage/names"
    26  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    27  	"k8s.io/kubernetes/pkg/api/pod"
    28  	api "k8s.io/kubernetes/pkg/apis/core"
    29  	corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
    30  )
    31  
    32  // podTemplateStrategy implements behavior for PodTemplates
    33  type podTemplateStrategy struct {
    34  	runtime.ObjectTyper
    35  	names.NameGenerator
    36  }
    37  
    38  // Strategy is the default logic that applies when creating and updating PodTemplate
    39  // objects via the REST API.
    40  var Strategy = podTemplateStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
    41  
    42  // NamespaceScoped is true for pod templates.
    43  func (podTemplateStrategy) NamespaceScoped() bool {
    44  	return true
    45  }
    46  
    47  // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
    48  func (podTemplateStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
    49  	template := obj.(*api.PodTemplate)
    50  	template.Generation = 1
    51  	pod.DropDisabledTemplateFields(&template.Template, nil)
    52  }
    53  
    54  // Validate validates a new pod template.
    55  func (podTemplateStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
    56  	template := obj.(*api.PodTemplate)
    57  	opts := pod.GetValidationOptionsFromPodTemplate(&template.Template, nil)
    58  	return corevalidation.ValidatePodTemplate(template, opts)
    59  }
    60  
    61  // WarningsOnCreate returns warnings for the creation of the given object.
    62  func (podTemplateStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
    63  	newPodTemplate := obj.(*api.PodTemplate)
    64  	return pod.GetWarningsForPodTemplate(ctx, field.NewPath("template"), &newPodTemplate.Template, nil)
    65  }
    66  
    67  // Canonicalize normalizes the object after validation.
    68  func (podTemplateStrategy) Canonicalize(obj runtime.Object) {
    69  }
    70  
    71  // AllowCreateOnUpdate is false for pod templates.
    72  func (podTemplateStrategy) AllowCreateOnUpdate() bool {
    73  	return false
    74  }
    75  
    76  // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
    77  func (podTemplateStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
    78  	newTemplate := obj.(*api.PodTemplate)
    79  	oldTemplate := old.(*api.PodTemplate)
    80  
    81  	pod.DropDisabledTemplateFields(&newTemplate.Template, &oldTemplate.Template)
    82  
    83  	// Any changes to the template increment the generation number.
    84  	// See metav1.ObjectMeta description for more information on Generation.
    85  	if !apiequality.Semantic.DeepEqual(newTemplate.Template, oldTemplate.Template) {
    86  		newTemplate.Generation = oldTemplate.Generation + 1
    87  	}
    88  
    89  }
    90  
    91  // ValidateUpdate is the default update validation for an end user.
    92  func (podTemplateStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
    93  	template := obj.(*api.PodTemplate)
    94  	oldTemplate := old.(*api.PodTemplate)
    95  
    96  	// Allow downward api usage of hugepages on pod update if feature is enabled or if the old pod already had used them.
    97  	opts := pod.GetValidationOptionsFromPodTemplate(&template.Template, &oldTemplate.Template)
    98  	return corevalidation.ValidatePodTemplateUpdate(template, oldTemplate, opts)
    99  }
   100  
   101  // WarningsOnUpdate returns warnings for the given update.
   102  func (podTemplateStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
   103  	var warnings []string
   104  	newTemplate := obj.(*api.PodTemplate)
   105  	oldTemplate := old.(*api.PodTemplate)
   106  	if newTemplate.Generation != oldTemplate.Generation {
   107  		warnings = pod.GetWarningsForPodTemplate(ctx, field.NewPath("template"), &newTemplate.Template, &oldTemplate.Template)
   108  	}
   109  	return warnings
   110  }
   111  
   112  func (podTemplateStrategy) AllowUnconditionalUpdate() bool {
   113  	return true
   114  }
   115  

View as plain text