...

Source file src/k8s.io/kubernetes/pkg/registry/storage/volumeattachment/strategy.go

Documentation: k8s.io/kubernetes/pkg/registry/storage/volumeattachment

     1  /*
     2  Copyright 2017 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 volumeattachment
    18  
    19  import (
    20  	"context"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    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/apis/storage"
    28  	"k8s.io/kubernetes/pkg/apis/storage/validation"
    29  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    30  )
    31  
    32  // volumeAttachmentStrategy implements behavior for VolumeAttachment objects
    33  type volumeAttachmentStrategy struct {
    34  	runtime.ObjectTyper
    35  	names.NameGenerator
    36  }
    37  
    38  // Strategy is the default logic that applies when creating and updating
    39  // VolumeAttachment objects via the REST API.
    40  var Strategy = volumeAttachmentStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
    41  
    42  func (volumeAttachmentStrategy) NamespaceScoped() bool {
    43  	return false
    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 (volumeAttachmentStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
    49  	fields := map[fieldpath.APIVersion]*fieldpath.Set{
    50  		"storage.k8s.io/v1": fieldpath.NewSet(
    51  			fieldpath.MakePathOrDie("status"),
    52  		),
    53  	}
    54  
    55  	return fields
    56  }
    57  
    58  // ResetBeforeCreate clears the Status field which is not allowed to be set by end users on creation.
    59  func (volumeAttachmentStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
    60  	volumeAttachment := obj.(*storage.VolumeAttachment)
    61  	volumeAttachment.Status = storage.VolumeAttachmentStatus{}
    62  }
    63  
    64  func (volumeAttachmentStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
    65  	volumeAttachment := obj.(*storage.VolumeAttachment)
    66  
    67  	errs := validation.ValidateVolumeAttachment(volumeAttachment)
    68  
    69  	// tighten up validation of newly created v1 attachments
    70  	errs = append(errs, validation.ValidateVolumeAttachmentV1(volumeAttachment)...)
    71  	return errs
    72  }
    73  
    74  // WarningsOnCreate returns warnings for the creation of the given object.
    75  func (volumeAttachmentStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
    76  	return nil
    77  }
    78  
    79  // Canonicalize normalizes the object after validation.
    80  func (volumeAttachmentStrategy) Canonicalize(obj runtime.Object) {
    81  }
    82  
    83  func (volumeAttachmentStrategy) AllowCreateOnUpdate() bool {
    84  	return false
    85  }
    86  
    87  // PrepareForUpdate sets the Status fields which is not allowed to be set by an end user updating a VolumeAttachment
    88  func (volumeAttachmentStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
    89  	newVolumeAttachment := obj.(*storage.VolumeAttachment)
    90  	oldVolumeAttachment := old.(*storage.VolumeAttachment)
    91  
    92  	newVolumeAttachment.Status = oldVolumeAttachment.Status
    93  	// No need to increment Generation because we don't allow updates to spec
    94  
    95  }
    96  
    97  func (volumeAttachmentStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
    98  	newVolumeAttachmentObj := obj.(*storage.VolumeAttachment)
    99  	oldVolumeAttachmentObj := old.(*storage.VolumeAttachment)
   100  	errorList := validation.ValidateVolumeAttachment(newVolumeAttachmentObj)
   101  	return append(errorList, validation.ValidateVolumeAttachmentUpdate(newVolumeAttachmentObj, oldVolumeAttachmentObj)...)
   102  }
   103  
   104  // WarningsOnUpdate returns warnings for the given update.
   105  func (volumeAttachmentStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
   106  	return nil
   107  }
   108  
   109  func (volumeAttachmentStrategy) AllowUnconditionalUpdate() bool {
   110  	return false
   111  }
   112  
   113  // volumeAttachmentStatusStrategy implements behavior for VolumeAttachmentStatus subresource
   114  type volumeAttachmentStatusStrategy struct {
   115  	volumeAttachmentStrategy
   116  }
   117  
   118  // StatusStrategy is the default logic that applies when creating and updating
   119  // VolumeAttachmentStatus subresource via the REST API.
   120  var StatusStrategy = volumeAttachmentStatusStrategy{Strategy}
   121  
   122  // GetResetFields returns the set of fields that get reset by the strategy
   123  // and should not be modified by the user.
   124  func (volumeAttachmentStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
   125  	fields := map[fieldpath.APIVersion]*fieldpath.Set{
   126  		"storage.k8s.io/v1": fieldpath.NewSet(
   127  			fieldpath.MakePathOrDie("metadata"),
   128  			fieldpath.MakePathOrDie("spec"),
   129  		),
   130  	}
   131  
   132  	return fields
   133  }
   134  
   135  // PrepareForUpdate sets the Status fields which is not allowed to be set by an end user updating a VolumeAttachment
   136  func (volumeAttachmentStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
   137  	newVolumeAttachment := obj.(*storage.VolumeAttachment)
   138  	oldVolumeAttachment := old.(*storage.VolumeAttachment)
   139  
   140  	newVolumeAttachment.Spec = oldVolumeAttachment.Spec
   141  	metav1.ResetObjectMetaForStatus(&newVolumeAttachment.ObjectMeta, &oldVolumeAttachment.ObjectMeta)
   142  }
   143  

View as plain text