Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restart claim sync when PVC is updated #113137

Merged
merged 1 commit into from Oct 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions pkg/controller/volume/persistentvolume/pv_controller.go
Expand Up @@ -353,9 +353,14 @@ func (ctrl *PersistentVolumeController) syncUnboundClaim(ctx context.Context, cl

if utilfeature.DefaultFeatureGate.Enabled(features.RetroactiveDefaultStorageClass) {
klog.V(4).Infof("FeatureGate[%s] is enabled, attempting to assign storage class to unbound PersistentVolumeClaim[%s]", features.RetroactiveDefaultStorageClass, claimToClaimKey(claim))
if claim, err = ctrl.assignDefaultStorageClass(claim); err != nil {
updated, err := ctrl.assignDefaultStorageClass(claim)
if err != nil {
return fmt.Errorf("can't update PersistentVolumeClaim[%q]: %w", claimToClaimKey(claim), err)
}
if updated {
klog.V(4).Infof("PersistentVolumeClaim[%q] update successful, restarting claim sync", claimToClaimKey(claim))
return nil
}
}

switch {
Expand Down Expand Up @@ -929,30 +934,31 @@ func (ctrl *PersistentVolumeController) updateVolumePhaseWithEvent(volume *v1.Pe
// assignDefaultStorageClass updates the claim storage class if there is any, the claim is updated to the API server.
// Ignores claims that already have a storage class.
// TODO: if resync is ever changed to a larger period, we might need to change how we set the default class on existing unbound claims
func (ctrl *PersistentVolumeController) assignDefaultStorageClass(claim *v1.PersistentVolumeClaim) (*v1.PersistentVolumeClaim, error) {
func (ctrl *PersistentVolumeController) assignDefaultStorageClass(claim *v1.PersistentVolumeClaim) (bool, error) {
if claim.Spec.StorageClassName != nil {
return claim, nil
return false, nil
}

class, err := util.GetDefaultClass(ctrl.classLister)
if err != nil {
// It is safe to ignore errors here because it means we either could not list SCs or there is more than one default.
// TODO: do not ignore errors after this PR is merged: https://github.com/kubernetes/kubernetes/pull/110559
klog.V(4).Infof("failed to get default storage class: %v", err)
return claim, nil
return false, nil
} else if class == nil {
klog.V(4).Infof("can not assign storage class to PersistentVolumeClaim[%s]: default storage class not found", claimToClaimKey(claim))
return claim, nil
return false, nil
}

klog.V(4).Infof("assigning StorageClass[%s] to PersistentVolumeClaim[%s]", class.Name, claimToClaimKey(claim))
claim.Spec.StorageClassName = &class.Name
newClaim, err := ctrl.kubeClient.CoreV1().PersistentVolumeClaims(claim.GetNamespace()).Update(context.TODO(), claim, metav1.UpdateOptions{})
_, err = ctrl.kubeClient.CoreV1().PersistentVolumeClaims(claim.GetNamespace()).Update(context.TODO(), claim, metav1.UpdateOptions{})
if err != nil {
return claim, err
return false, err
}

return newClaim, nil
klog.V(4).Infof("successfully assigned StorageClass[%s] to PersistentVolumeClaim[%s]", claimToClaimKey(claim), class.Name)
return true, nil
}

// bindVolumeToClaim modifies given volume to be bound to a claim and saves it to
Expand Down