Skip to content
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
2 changes: 2 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions deploy/kubernetes/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ metadata:
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "update"]
verbs: ["get", "list", "watch", "update", "patch"]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need update?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to have it there, just in case :-)
(btw, I'd prefer if there were just generic "read" and "write" permissions)

- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "list", "watch"]
Expand All @@ -33,7 +33,7 @@ rules:
verbs: ["get", "list", "watch"]
- apiGroups: ["storage.k8s.io"]
resources: ["volumeattachments"]
verbs: ["get", "list", "watch", "update"]
verbs: ["get", "list", "watch", "update", "patch"]
#Secret permission is optional.
#Enable it if you need value from secret.
#For example, you have key `csi.storage.k8s.io/controller-publish-secret-name` in StorageClass.parameters
Expand Down
57 changes: 43 additions & 14 deletions pkg/controller/csi_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
storage "k8s.io/api/storage/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
storagelisters "k8s.io/client-go/listers/storage/v1beta1"
Expand Down Expand Up @@ -191,9 +192,8 @@ func (h *csiHandler) prepareVANodeID(va *storage.VolumeAttachment, nodeID string
return clone, true
}

func (h *csiHandler) saveVA(va *storage.VolumeAttachment) (*storage.VolumeAttachment, error) {
// TODO: #158 #157: use patch to save us from VersionError
newVA, err := h.client.StorageV1beta1().VolumeAttachments().Update(va)
func (h *csiHandler) saveVA(va *storage.VolumeAttachment, patch []byte) (*storage.VolumeAttachment, error) {
newVA, err := h.client.StorageV1beta1().VolumeAttachments().Patch(va.Name, types.MergePatchType, patch)
if err != nil {
return va, err
}
Expand All @@ -215,11 +215,12 @@ func (h *csiHandler) addPVFinalizer(pv *v1.PersistentVolume) (*v1.PersistentVolu
klog.V(4).Infof("Adding finalizer to PV %q", pv.Name)
clone := pv.DeepCopy()
clone.Finalizers = append(clone.Finalizers, finalizerName)
// TODO: #158 #157: use patch to save us from VersionError
newPV, err := h.client.CoreV1().PersistentVolumes().Update(clone)

newPV, err := h.patchPV(pv, clone)
if err != nil {
return pv, err
}

klog.V(4).Infof("PV finalizer added to %q", pv.Name)
return newPV, nil
}
Expand Down Expand Up @@ -327,10 +328,9 @@ func (h *csiHandler) csiAttach(va *storage.VolumeAttachment) (*storage.VolumeAtt
originalVA := va
va, finalizerAdded := h.prepareVAFinalizer(va)
va, nodeIDAdded := h.prepareVANodeID(va, nodeID)

if finalizerAdded || nodeIDAdded {
va, err = h.saveVA(va)
if err != nil {
// va modification failed, return the original va that's still on API server
if va, err = h.patchVA(originalVA, va); err != nil {
return originalVA, nil, fmt.Errorf("could not save VolumeAttachment: %s", err)
}
}
Expand Down Expand Up @@ -415,8 +415,9 @@ func (h *csiHandler) saveAttachError(va *storage.VolumeAttachment, err error) (*
Message: err.Error(),
Time: metav1.Now(),
}
newVa, err := h.client.StorageV1beta1().VolumeAttachments().Update(clone)
if err != nil {

var newVa *storage.VolumeAttachment
if newVa, err = h.patchVA(va, clone); err != nil {
return va, err
}
klog.V(4).Infof("Saved attach error to %q", va.Name)
Expand All @@ -430,8 +431,9 @@ func (h *csiHandler) saveDetachError(va *storage.VolumeAttachment, err error) (*
Message: err.Error(),
Time: metav1.Now(),
}
newVa, err := h.client.StorageV1beta1().VolumeAttachments().Update(clone)
if err != nil {

var newVa *storage.VolumeAttachment
if newVa, err = h.patchVA(va, clone); err != nil {
return va, err
}
klog.V(4).Infof("Saved detach error to %q", va.Name)
Expand Down Expand Up @@ -496,12 +498,13 @@ func (h *csiHandler) SyncNewOrUpdatedPersistentVolume(pv *v1.PersistentVolume) {
newFinalizers = nil
}
clone.Finalizers = newFinalizers
_, err = h.client.CoreV1().PersistentVolumes().Update(clone)
if err != nil {

if _, err = h.patchPV(pv, clone); err != nil {
klog.Errorf("Failed to remove finalizer from PV %q: %s", pv.Name, err.Error())
h.pvQueue.AddRateLimited(pv.Name)
return
}

klog.V(2).Infof("Removed finalizer from PV %q", pv.Name)
h.pvQueue.Forget(pv.Name)

Expand Down Expand Up @@ -564,3 +567,29 @@ func (h *csiHandler) getNodeID(driver string, nodeName string, va *storage.Volum
// return nodeLister.Get error
return "", err
}

func (h *csiHandler) patchVA(va, clone *storage.VolumeAttachment) (*storage.VolumeAttachment, error) {
patch, err := createMergePatch(va, clone)
if err != nil {
return va, err
}

newVa, err := h.client.StorageV1beta1().VolumeAttachments().Patch(va.Name, types.MergePatchType, patch)
if err != nil {
return va, err
}
return newVa, nil
}

func (h *csiHandler) patchPV(pv, clone *v1.PersistentVolume) (*v1.PersistentVolume, error) {
patch, err := createMergePatch(pv, clone)
if err != nil {
return pv, err
}

newPV, err := h.client.CoreV1().PersistentVolumes().Patch(pv.Name, types.MergePatchType, patch)
if err != nil {
return pv, err
}
return newPV, nil
}
Loading