k8s.io/kubernetes@v1.29.3/pkg/api/persistentvolume/util.go (about)

     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 persistentvolume
    18  
    19  import (
    20  	"fmt"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/util/validation/field"
    24  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    25  	nodeapi "k8s.io/kubernetes/pkg/api/node"
    26  	api "k8s.io/kubernetes/pkg/apis/core"
    27  	"k8s.io/kubernetes/pkg/features"
    28  )
    29  
    30  const (
    31  	deprecatedStorageClassAnnotationsMsg = `deprecated since v1.8; use "storageClassName" attribute instead`
    32  )
    33  
    34  // DropDisabledSpecFields removes disabled fields from the pv spec.
    35  // This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a pv spec.
    36  func DropDisabledSpecFields(pvSpec *api.PersistentVolumeSpec, oldPVSpec *api.PersistentVolumeSpec) {
    37  	if !utilfeature.DefaultFeatureGate.Enabled(features.VolumeAttributesClass) {
    38  		if oldPVSpec == nil || oldPVSpec.VolumeAttributesClassName == nil {
    39  			pvSpec.VolumeAttributesClassName = nil
    40  		}
    41  	}
    42  }
    43  
    44  // DropDisabledStatusFields removes disabled fields from the pv status.
    45  // This should be called from PrepareForUpdate for all resources containing a pv status.
    46  func DropDisabledStatusFields(oldStatus, newStatus *api.PersistentVolumeStatus) {
    47  	if !utilfeature.DefaultFeatureGate.Enabled(features.PersistentVolumeLastPhaseTransitionTime) && oldStatus.LastPhaseTransitionTime.IsZero() {
    48  		newStatus.LastPhaseTransitionTime = nil
    49  	}
    50  }
    51  
    52  func GetWarningsForPersistentVolume(pv *api.PersistentVolume) []string {
    53  	if pv == nil {
    54  		return nil
    55  	}
    56  	return warningsForPersistentVolumeSpecAndMeta(nil, &pv.Spec, &pv.ObjectMeta)
    57  }
    58  
    59  func warningsForPersistentVolumeSpecAndMeta(fieldPath *field.Path, pvSpec *api.PersistentVolumeSpec, pvMeta *metav1.ObjectMeta) []string {
    60  	var warnings []string
    61  
    62  	if _, ok := pvMeta.Annotations[api.BetaStorageClassAnnotation]; ok {
    63  		warnings = append(warnings,
    64  			fmt.Sprintf(
    65  				"%s: %s",
    66  				fieldPath.Child("metadata", "annotations").Key(api.BetaStorageClassAnnotation),
    67  				deprecatedStorageClassAnnotationsMsg,
    68  			),
    69  		)
    70  	}
    71  
    72  	if pvSpec.NodeAffinity != nil && pvSpec.NodeAffinity.Required != nil {
    73  		termFldPath := fieldPath.Child("spec", "nodeAffinity", "required", "nodeSelectorTerms")
    74  		// use of deprecated node labels in node affinity
    75  		for i, term := range pvSpec.NodeAffinity.Required.NodeSelectorTerms {
    76  			warnings = append(warnings, nodeapi.GetWarningsForNodeSelectorTerm(term, termFldPath.Index(i))...)
    77  		}
    78  	}
    79  	// If we are on deprecated volume plugin
    80  	if pvSpec.CephFS != nil {
    81  		warnings = append(warnings, fmt.Sprintf("%s: deprecated in v1.28, non-functional in v1.31+", fieldPath.Child("spec", "cephfs")))
    82  	}
    83  	if pvSpec.PhotonPersistentDisk != nil {
    84  		warnings = append(warnings, fmt.Sprintf("%s: deprecated in v1.11, non-functional in v1.16+", fieldPath.Child("spec", "photonPersistentDisk")))
    85  	}
    86  	if pvSpec.ScaleIO != nil {
    87  		warnings = append(warnings, fmt.Sprintf("%s: deprecated in v1.16, non-functional in v1.22+", fieldPath.Child("spec", "scaleIO")))
    88  	}
    89  	if pvSpec.StorageOS != nil {
    90  		warnings = append(warnings, fmt.Sprintf("%s: deprecated in v1.22, non-functional in v1.25+", fieldPath.Child("spec", "storageOS")))
    91  	}
    92  	if pvSpec.Glusterfs != nil {
    93  		warnings = append(warnings, fmt.Sprintf("%s: deprecated in v1.25, non-functional in v1.26+", fieldPath.Child("spec", "glusterfs")))
    94  	}
    95  	if pvSpec.RBD != nil {
    96  		warnings = append(warnings, fmt.Sprintf("%s: deprecated in v1.28, non-functional in v1.31+", fieldPath.Child("spec", "rbd")))
    97  	}
    98  	return warnings
    99  }