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

use patch method instead when update finalizer to PV #164

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -37,7 +38,9 @@ import (
storagebeta "k8s.io/api/storage/v1beta1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
Expand Down Expand Up @@ -1169,8 +1172,7 @@ func (ctrl *ProvisionController) handleProtectionFinalizer(ctx context.Context,
}

if modified {
volume.ObjectMeta.Finalizers = volumeFinalizers
newVolume, err := ctrl.updatePersistentVolume(ctx, volume)
newVolume, err := ctrl.patchPersistentVolumeWithFinalizers(ctx, volume, volumeFinalizers)
if err != nil {
return volume, fmt.Errorf("failed to modify finalizers to %+v on volume %s err: %+v", volumeFinalizers, volume.Name, err)
}
Expand Down Expand Up @@ -1319,12 +1321,29 @@ func (ctrl *ProvisionController) updateDeleteStats(volume *v1.PersistentVolume,
}
}

func (ctrl *ProvisionController) updatePersistentVolume(ctx context.Context, volume *v1.PersistentVolume) (*v1.PersistentVolume, error) {
newVolume, err := ctrl.client.CoreV1().PersistentVolumes().Update(ctx, volume, metav1.UpdateOptions{})
// patchPersistentVolumeWithFinalizers patches the PersistentVolume with the given finalizers
func (ctrl *ProvisionController) patchPersistentVolumeWithFinalizers(ctx context.Context, volume *v1.PersistentVolume, finalizers []string) (*v1.PersistentVolume, error) {
oldData, err := json.Marshal(volume)
if err != nil {
return volume, err
return nil, err
}

volumeCopy := volume.DeepCopy()
volumeCopy.ObjectMeta.Finalizers = finalizers
newData, err := json.Marshal(volumeCopy)
if err != nil {
return nil, err
}

patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, &v1.PersistentVolume{})
if err != nil {
return nil, err
}
pv, err := ctrl.client.CoreV1().PersistentVolumes().Patch(ctx, volume.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{})
if err != nil {
return nil, err
}
return newVolume, nil
return pv, nil
}

// rescheduleProvisioning signal back to the scheduler to retry dynamic provisioning
Expand Down Expand Up @@ -1556,8 +1575,7 @@ func (ctrl *ProvisionController) deleteVolumeOperation(ctx context.Context, volu

// Only update the finalizers if we actually removed something
if modified {
newVolume.ObjectMeta.Finalizers = finalizers
if _, err = ctrl.client.CoreV1().PersistentVolumes().Update(ctx, newVolume, metav1.UpdateOptions{}); err != nil {
if _, err = ctrl.patchPersistentVolumeWithFinalizers(ctx, newVolume, finalizers); err != nil {
if !apierrs.IsNotFound(err) {
// Couldn't remove finalizer and the object still exists, the controller may
// try to remove the finalizer again on the next update
Expand Down
2 changes: 1 addition & 1 deletion examples/hostpath-provisioner/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ metadata:
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
verbs: ["get", "list", "watch", "create", "patch", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch"]
Expand Down