k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/storage/csidriver/strategy.go (about)

     1  /*
     2  Copyright 2019 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 csidriver
    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  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    27  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    28  	"k8s.io/kubernetes/pkg/apis/storage"
    29  	"k8s.io/kubernetes/pkg/apis/storage/validation"
    30  	"k8s.io/kubernetes/pkg/features"
    31  )
    32  
    33  // csiDriverStrategy implements behavior for CSIDriver objects
    34  type csiDriverStrategy struct {
    35  	runtime.ObjectTyper
    36  	names.NameGenerator
    37  }
    38  
    39  // Strategy is the default logic that applies when creating and updating
    40  // CSIDriver objects via the REST API.
    41  var Strategy = csiDriverStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
    42  
    43  func (csiDriverStrategy) NamespaceScoped() bool {
    44  	return false
    45  }
    46  
    47  // PrepareForCreate clears the fields for which the corresponding feature is disabled.
    48  func (csiDriverStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
    49  	csiDriver := obj.(*storage.CSIDriver)
    50  	if !utilfeature.DefaultFeatureGate.Enabled(features.SELinuxMountReadWriteOncePod) {
    51  		csiDriver.Spec.SELinuxMount = nil
    52  	}
    53  }
    54  
    55  func (csiDriverStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
    56  	csiDriver := obj.(*storage.CSIDriver)
    57  
    58  	return validation.ValidateCSIDriver(csiDriver)
    59  }
    60  
    61  // WarningsOnCreate returns warnings for the creation of the given object.
    62  func (csiDriverStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
    63  	return nil
    64  }
    65  
    66  // Canonicalize normalizes the object after validation.
    67  func (csiDriverStrategy) Canonicalize(obj runtime.Object) {
    68  }
    69  
    70  func (csiDriverStrategy) AllowCreateOnUpdate() bool {
    71  	return false
    72  }
    73  
    74  // PrepareForUpdate clears the fields for which the corresponding feature is disabled and
    75  // existing object does not already have that field set. This allows the field to remain when
    76  // downgrading to a version that has the feature disabled.
    77  func (csiDriverStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
    78  	newCSIDriver := obj.(*storage.CSIDriver)
    79  	oldCSIDriver := old.(*storage.CSIDriver)
    80  
    81  	if oldCSIDriver.Spec.SELinuxMount == nil &&
    82  		!utilfeature.DefaultFeatureGate.Enabled(features.SELinuxMountReadWriteOncePod) {
    83  		newCSIDriver.Spec.SELinuxMount = nil
    84  	}
    85  
    86  	// Any changes to the spec increment the generation number.
    87  	if !apiequality.Semantic.DeepEqual(oldCSIDriver.Spec, newCSIDriver.Spec) {
    88  		newCSIDriver.Generation = oldCSIDriver.Generation + 1
    89  	}
    90  }
    91  
    92  func (csiDriverStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
    93  	newCSIDriverObj := obj.(*storage.CSIDriver)
    94  	oldCSIDriverObj := old.(*storage.CSIDriver)
    95  	return validation.ValidateCSIDriverUpdate(newCSIDriverObj, oldCSIDriverObj)
    96  }
    97  
    98  // WarningsOnUpdate returns warnings for the given update.
    99  func (csiDriverStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
   100  	return nil
   101  }
   102  
   103  func (csiDriverStrategy) AllowUnconditionalUpdate() bool {
   104  	return false
   105  }