Adds archiveOnDelete parameter to nfs-client provisioner
This commit is contained in:
		
							parent
							
								
									9debe1e4d3
								
							
						
					
					
						commit
						2b11b498c5
					
				|  | @ -6,7 +6,6 @@ | ||||||
| `nfs-client` is an automatic provisioner that used your *already configured* NFS server, automatically creating Persistent Volumes. | `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 are provisioned as ${namespace}-${pvcName}-${pvName} | ||||||
| - Persistent volumes which are recycled as archieved-${namespace}-${pvcName}-${pvName} |  | ||||||
| 
 | 
 | ||||||
| # How to deploy nfs-client to your cluster. | # How to deploy nfs-client to your cluster. | ||||||
| 
 | 
 | ||||||
|  | @ -41,6 +40,8 @@ kind: StorageClass | ||||||
| metadata: | metadata: | ||||||
|   name: managed-nfs-storage |   name: managed-nfs-storage | ||||||
| provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME' | 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 | 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 | 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 | 4. Deploying your own PersistentVolumeClaim | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -22,11 +22,15 @@ import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
|  | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
|  | 	"k8s.io/kubernetes/pkg/apis/core/v1/helper" | ||||||
|  | 
 | ||||||
| 	"github.com/golang/glog" | 	"github.com/golang/glog" | ||||||
| 	"github.com/kubernetes-incubator/external-storage/lib/controller" | 	"github.com/kubernetes-incubator/external-storage/lib/controller" | ||||||
| 	"k8s.io/api/core/v1" | 	"k8s.io/api/core/v1" | ||||||
|  | 	storage "k8s.io/api/storage/v1" | ||||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
| 	"k8s.io/apimachinery/pkg/util/wait" | 	"k8s.io/apimachinery/pkg/util/wait" | ||||||
| 	"k8s.io/client-go/kubernetes" | 	"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) | 		glog.Warningf("path %s does not exist, deletion skipped", oldPath) | ||||||
| 		return nil | 		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) | 	archivePath := filepath.Join(mountPath, "archived-"+pvName) | ||||||
| 	glog.V(4).Infof("archiving path %s to %s", oldPath, archivePath) | 	glog.V(4).Infof("archiving path %s to %s", oldPath, archivePath) | ||||||
| 	return os.Rename(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() { | func main() { | ||||||
|  | @ -141,6 +179,7 @@ func main() { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	clientNFSProvisioner := &nfsProvisioner{ | 	clientNFSProvisioner := &nfsProvisioner{ | ||||||
|  | 		client: clientset, | ||||||
| 		server: server, | 		server: server, | ||||||
| 		path:   path, | 		path:   path, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -3,3 +3,5 @@ kind: StorageClass | ||||||
| metadata: | metadata: | ||||||
|   name: managed-nfs-storage |   name: managed-nfs-storage | ||||||
| provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME' | provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME' | ||||||
|  | parameters: | ||||||
|  |   archiveOnDelete: "false" | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue