From 2b11b498c5497158bc02dd2ac273e2f97157c95e Mon Sep 17 00:00:00 2001 From: Gary Morse Date: Thu, 9 Aug 2018 11:32:54 -0400 Subject: [PATCH] Adds archiveOnDelete parameter to nfs-client provisioner --- README.md | 5 +-- cmd/nfs-client-provisioner/provisioner.go | 39 +++++++++++++++++++++++ deploy/class.yaml | 2 ++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cfb6db42..0041773f 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ `nfs-client` is an automatic provisioner that used your *already configured* NFS server, automatically creating Persistent Volumes. - Persistent volumes are provisioned as ${namespace}-${pvcName}-${pvName} -- Persistent volumes which are recycled as archieved-${namespace}-${pvcName}-${pvName} # How to deploy nfs-client to your cluster. @@ -41,6 +40,8 @@ kind: StorageClass metadata: name: managed-nfs-storage provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME' +parameters: + archiveOnDelete: "false" # When set to "false" your PVs will not be archived by the provisioner upon deletion of the PVC. ``` 2. Authorization @@ -82,7 +83,7 @@ Now check your NFS Server for the file `SUCCESS`. kubectl delete -f deploy/test-pod.yaml -f deploy/test-claim.yaml ``` -Now check the folder renamed to `archived-???`. +Now check the folder has been deleted. 4. Deploying your own PersistentVolumeClaim diff --git a/cmd/nfs-client-provisioner/provisioner.go b/cmd/nfs-client-provisioner/provisioner.go index 85ba2a77..eab741f0 100644 --- a/cmd/nfs-client-provisioner/provisioner.go +++ b/cmd/nfs-client-provisioner/provisioner.go @@ -22,11 +22,15 @@ import ( "fmt" "os" "path/filepath" + "strconv" "strings" + "k8s.io/kubernetes/pkg/apis/core/v1/helper" + "github.com/golang/glog" "github.com/kubernetes-incubator/external-storage/lib/controller" "k8s.io/api/core/v1" + storage "k8s.io/api/storage/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" @@ -100,9 +104,43 @@ func (p *nfsProvisioner) Delete(volume *v1.PersistentVolume) error { glog.Warningf("path %s does not exist, deletion skipped", oldPath) return nil } + // Get the storage class for this volume. + storageClass, err := p.getClassForVolume(volume) + if err != nil { + return err + } + // Determine if the "archiveOnDelete" parameter exists. + // If it exists and has a falsey value, delete the directory. + // Otherwise, archive it. + archiveOnDelete, exists := storageClass.Parameters["archiveOnDelete"] + archiveBool, err := strconv.ParseBool(archiveOnDelete) + if err != nil { + return err + } + if exists && !archiveBool { + return os.RemoveAll(oldPath) + } + archivePath := filepath.Join(mountPath, "archived-"+pvName) glog.V(4).Infof("archiving path %s to %s", oldPath, archivePath) return os.Rename(oldPath, archivePath) + +} + +// getClassForVolume returns StorageClass +func (p *nfsProvisioner) getClassForVolume(pv *v1.PersistentVolume) (*storage.StorageClass, error) { + if p.client == nil { + return nil, fmt.Errorf("Cannot get kube client") + } + className := helper.GetPersistentVolumeClass(pv) + if className == "" { + return nil, fmt.Errorf("Volume has no storage class") + } + class, err := p.client.StorageV1().StorageClasses().Get(className, metav1.GetOptions{}) + if err != nil { + return nil, err + } + return class, nil } func main() { @@ -141,6 +179,7 @@ func main() { } clientNFSProvisioner := &nfsProvisioner{ + client: clientset, server: server, path: path, } diff --git a/deploy/class.yaml b/deploy/class.yaml index b53fe002..4d3b4805 100644 --- a/deploy/class.yaml +++ b/deploy/class.yaml @@ -3,3 +3,5 @@ kind: StorageClass metadata: name: managed-nfs-storage provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME' +parameters: + archiveOnDelete: "false"