...

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

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

     1  /*
     2  Copyright 2020 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 csistoragecapacity
    18  
    19  import (
    20  	"context"
    21  
    22  	metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
    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  	storageutil "k8s.io/kubernetes/pkg/api/storage"
    28  	"k8s.io/kubernetes/pkg/apis/storage"
    29  	"k8s.io/kubernetes/pkg/apis/storage/validation"
    30  )
    31  
    32  // csiStorageCapacityStrategy implements behavior for CSIStorageCapacity objects
    33  type csiStorageCapacityStrategy struct {
    34  	runtime.ObjectTyper
    35  	names.NameGenerator
    36  }
    37  
    38  // Strategy is the default logic that applies when creating and updating
    39  // CSIStorageCapacity objects via the REST API.
    40  var Strategy = csiStorageCapacityStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
    41  
    42  func (csiStorageCapacityStrategy) NamespaceScoped() bool {
    43  	return true
    44  }
    45  
    46  // PrepareForCreate is currently a NOP.
    47  func (csiStorageCapacityStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
    48  }
    49  
    50  func (csiStorageCapacityStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
    51  	csiStorageCapacity := obj.(*storage.CSIStorageCapacity)
    52  	opts := validation.CSIStorageCapacityValidateOptions{
    53  		AllowInvalidLabelValueInSelector: false,
    54  	}
    55  	errs := validation.ValidateCSIStorageCapacity(csiStorageCapacity, opts)
    56  	return errs
    57  }
    58  
    59  // WarningsOnCreate returns warnings for the creation of the given object.
    60  func (csiStorageCapacityStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
    61  	return storageutil.GetWarningsForCSIStorageCapacity(obj.(*storage.CSIStorageCapacity))
    62  }
    63  
    64  // Canonicalize normalizes the object after validation.
    65  func (csiStorageCapacityStrategy) Canonicalize(obj runtime.Object) {
    66  }
    67  
    68  func (csiStorageCapacityStrategy) AllowCreateOnUpdate() bool {
    69  	return false
    70  }
    71  
    72  // PrepareForUpdate is currently a NOP.
    73  func (csiStorageCapacityStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
    74  }
    75  
    76  func (csiStorageCapacityStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
    77  	newCSIStorageCapacityObj := obj.(*storage.CSIStorageCapacity)
    78  	oldCSIStorageCapacityObj := old.(*storage.CSIStorageCapacity)
    79  	opts := validation.CSIStorageCapacityValidateOptions{
    80  		AllowInvalidLabelValueInSelector: hasInvalidLabelValueInLabelSelector(oldCSIStorageCapacityObj),
    81  	}
    82  	errorList := validation.ValidateCSIStorageCapacity(newCSIStorageCapacityObj, opts)
    83  	return append(errorList, validation.ValidateCSIStorageCapacityUpdate(newCSIStorageCapacityObj, oldCSIStorageCapacityObj)...)
    84  }
    85  
    86  // WarningsOnUpdate returns warnings for the given update.
    87  func (csiStorageCapacityStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
    88  	return storageutil.GetWarningsForCSIStorageCapacity(obj.(*storage.CSIStorageCapacity))
    89  }
    90  
    91  func (csiStorageCapacityStrategy) AllowUnconditionalUpdate() bool {
    92  	return false
    93  }
    94  
    95  func hasInvalidLabelValueInLabelSelector(capacity *storage.CSIStorageCapacity) bool {
    96  	labelSelectorValidationOptions := metav1validation.LabelSelectorValidationOptions{AllowInvalidLabelValueInSelector: false}
    97  	return len(metav1validation.ValidateLabelSelector(capacity.NodeTopology, labelSelectorValidationOptions, nil)) > 0
    98  }
    99  

View as plain text