pvc bind pv with vac

This commit is contained in:
carlory
2023-11-15 17:20:58 +08:00
parent 2ad04a0505
commit 3a6a4830df
12 changed files with 534 additions and 37 deletions

View File

@@ -26,6 +26,7 @@ import (
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/util/slice"
"k8s.io/utils/ptr"
v1 "k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1"
@@ -274,6 +275,20 @@ func checkVolumeSatisfyClaim(volume *v1.PersistentVolume, claim *v1.PersistentVo
return fmt.Errorf("storageClassName does not match")
}
if utilfeature.DefaultFeatureGate.Enabled(features.VolumeAttributesClass) {
requestedVAC := ptr.Deref(claim.Spec.VolumeAttributesClassName, "")
volumeVAC := ptr.Deref(volume.Spec.VolumeAttributesClassName, "")
if requestedVAC != volumeVAC {
return fmt.Errorf("volumeAttributesClassName does not match")
}
} else {
requestedVAC := ptr.Deref(claim.Spec.VolumeAttributesClassName, "")
volumeVAC := ptr.Deref(volume.Spec.VolumeAttributesClassName, "")
if requestedVAC != "" || volumeVAC != "" {
return fmt.Errorf("volumeAttributesClassName is not supported when the feature-gate VolumeAttributesClass is disabled")
}
}
if storagehelpers.CheckVolumeModeMismatches(&claim.Spec, &volume.Spec) {
return fmt.Errorf("incompatible volumeMode")
}
@@ -764,7 +779,7 @@ func (ctrl *PersistentVolumeController) syncVolume(ctx context.Context, volume *
//
// claim - claim to update
// phase - phase to set
// volume - volume which Capacity is set into claim.Status.Capacity
// volume - volume which Capacity is set into claim.Status.Capacity and VolumeAttributesClassName is set into claim.Status.CurrentVolumeAttributesClassName
func (ctrl *PersistentVolumeController) updateClaimStatus(ctx context.Context, claim *v1.PersistentVolumeClaim, phase v1.PersistentVolumeClaimPhase, volume *v1.PersistentVolume) (*v1.PersistentVolumeClaim, error) {
logger := klog.FromContext(ctx)
logger.V(4).Info("Updating PersistentVolumeClaim status", "PVC", klog.KObj(claim), "setPhase", phase)
@@ -778,7 +793,7 @@ func (ctrl *PersistentVolumeController) updateClaimStatus(ctx context.Context, c
}
if volume == nil {
// Need to reset AccessModes and Capacity
// Need to reset AccessModes, Capacity and CurrentVolumeAttributesClassName
if claim.Status.AccessModes != nil {
claimClone.Status.AccessModes = nil
dirty = true
@@ -787,8 +802,12 @@ func (ctrl *PersistentVolumeController) updateClaimStatus(ctx context.Context, c
claimClone.Status.Capacity = nil
dirty = true
}
if claim.Status.CurrentVolumeAttributesClassName != nil {
claimClone.Status.CurrentVolumeAttributesClassName = nil
dirty = true
}
} else {
// Need to update AccessModes and Capacity
// Need to update AccessModes, Capacity and CurrentVolumeAttributesClassName
if !reflect.DeepEqual(claim.Status.AccessModes, volume.Spec.AccessModes) {
claimClone.Status.AccessModes = volume.Spec.AccessModes
dirty = true
@@ -821,6 +840,20 @@ func (ctrl *PersistentVolumeController) updateClaimStatus(ctx context.Context, c
dirty = true
}
}
if utilfeature.DefaultFeatureGate.Enabled(features.VolumeAttributesClass) {
// There are two components to updating the current vac name, this controller and external-resizer.
// The controller ensures that the field is set properly when the volume is statically provisioned.
// It is safer for the controller to only set this field during binding, but not after. Without this
// constraint, there is a potential race condition where the resizer sets the field after the controller
// has set it, or vice versa. Afterwards, it should be handled only by the resizer, or if an admin wants
// to override.
if claim.Status.Phase == v1.ClaimPending && phase == v1.ClaimBound &&
!reflect.DeepEqual(claim.Status.CurrentVolumeAttributesClassName, volume.Spec.VolumeAttributesClassName) {
claimClone.Status.CurrentVolumeAttributesClassName = volume.Spec.VolumeAttributesClassName
dirty = true
}
}
}
if !dirty {
@@ -850,7 +883,7 @@ func (ctrl *PersistentVolumeController) updateClaimStatus(ctx context.Context, c
//
// claim - claim to update
// phase - phase to set
// volume - volume which Capacity is set into claim.Status.Capacity
// volume - volume which Capacity is set into claim.Status.Capacity and VolumeAttributesClassName is set into claim.Status.CurrentVolumeAttributesClassName
// eventtype, reason, message - event to send, see EventRecorder.Event()
func (ctrl *PersistentVolumeController) updateClaimStatusWithEvent(ctx context.Context, claim *v1.PersistentVolumeClaim, phase v1.PersistentVolumeClaimPhase, volume *v1.PersistentVolume, eventtype, reason, message string) (*v1.PersistentVolumeClaim, error) {
logger := klog.FromContext(ctx)