非常简单的provisioner,我们可以学习下如何实现一个provisioner
    https://github.com/kubernetes-retired/external-storage/blob/master/nfs-client/cmd/nfs-client-provisioner/provisioner.go
    核心函数

    1. const (
    2. mountPath = "/persistentvolumes"
    3. )
    4. func (p *nfsProvisioner) Provision(options controller.VolumeOptions) (*v1.PersistentVolume, error) {
    5. if options.PVC.Spec.Selector != nil {
    6. return nil, fmt.Errorf("claim Selector is not supported")
    7. }
    8. glog.V(4).Infof("nfs provisioner: VolumeOptions %v", options)
    9. pvcNamespace := options.PVC.Namespace
    10. pvcName := options.PVC.Name
    11. pvName := strings.Join([]string{pvcNamespace, pvcName, options.PVName}, "-")
    12. fullPath := filepath.Join(mountPath, pvName)
    13. glog.V(4).Infof("creating path %s", fullPath)
    14. if err := os.MkdirAll(fullPath, 0777); err != nil {
    15. return nil, errors.New("unable to create directory to provision new pv: " + err.Error())
    16. }
    17. os.Chmod(fullPath, 0777)
    18. path := filepath.Join(p.path, pvName)
    19. pv := &v1.PersistentVolume{
    20. ObjectMeta: metav1.ObjectMeta{
    21. Name: options.PVName,
    22. },
    23. Spec: v1.PersistentVolumeSpec{
    24. PersistentVolumeReclaimPolicy: options.PersistentVolumeReclaimPolicy,
    25. AccessModes: options.PVC.Spec.AccessModes,
    26. MountOptions: options.MountOptions,
    27. Capacity: v1.ResourceList{
    28. v1.ResourceName(v1.ResourceStorage): options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)],
    29. },
    30. PersistentVolumeSource: v1.PersistentVolumeSource{
    31. NFS: &v1.NFSVolumeSource{
    32. Server: p.server,
    33. Path: path,
    34. ReadOnly: false,
    35. },
    36. },
    37. },
    38. }
    39. return pv, nil
    40. }

    非常简洁,判断下目录是否存在,,如果不存在就把目录创建出来,然后把元信息返回出去就行了。