Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Adds archiveOnDelete parameter to nfs-client provisioner. #905

Merged
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
5 changes: 3 additions & 2 deletions nfs-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
39 changes: 39 additions & 0 deletions nfs-client/cmd/nfs-client-provisioner/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -141,6 +179,7 @@ func main() {
}

clientNFSProvisioner := &nfsProvisioner{
client: clientset,
server: server,
path: path,
}
Expand Down
2 changes: 2 additions & 0 deletions nfs-client/deploy/class.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"