Skip to content

Commit

Permalink
Customizing host path for dynamically provisioned PersistentVolumes
Browse files Browse the repository at this point in the history
This fix contains few things:

* Used k8s.gcr.io/debian-base-amd64:v2.0.0 as base image to build storage-provisioner
* Modify RBAC permission used to cluster-admin
  • Loading branch information
nanikjava committed Jan 15, 2020
1 parent 6c45d66 commit 9453b37
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cmd/storage-provisioner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"k8s.io/minikube/pkg/storage"
)

var pvDir = "/tmp/hostpath-provisioner"

func main() {
// Glog requires that /tmp exists.
if err := os.MkdirAll("/tmp", 0755); err != nil {
Expand All @@ -33,7 +35,7 @@ func main() {
}
flag.Parse()

if err := storage.StartStorageProvisioner(); err != nil {
if err := storage.StartStorageProvisioner(pvDir); err != nil {
glog.Exit(err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ metadata:
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:persistent-volume-provisioner
name: cluster-admin
subjects:
- kind: ServiceAccount
name: storage-provisioner
Expand Down
6 changes: 3 additions & 3 deletions deploy/storage-provisioner/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM scratch
COPY out/storage-provisioner storage-provisioner
CMD ["/storage-provisioner"]
FROM k8s.gcr.io/debian-base-amd64:v2.0.0
COPY out/storage-provisioner /storage-provisioner
CMD ["/storage-provisioner"]
13 changes: 6 additions & 7 deletions pkg/storage/storage_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ type hostPathProvisioner struct {
}

// NewHostPathProvisioner creates a new Provisioner using host paths
func NewHostPathProvisioner() controller.Provisioner {
func NewHostPathProvisioner(pvDir string) controller.Provisioner {
return &hostPathProvisioner{
pvDir: "/tmp/hostpath-provisioner",
pvDir: pvDir,
identity: uuid.NewUUID(),
}
}
Expand All @@ -57,7 +57,7 @@ var _ controller.Provisioner = &hostPathProvisioner{}
// Provision creates a storage asset and returns a PV object representing it.
func (p *hostPathProvisioner) Provision(options controller.ProvisionOptions) (*core.PersistentVolume, error) {
glog.Infof("Provisioning volume %v", options)
path := path.Join(p.pvDir, options.PVName)
path := path.Join(p.pvDir, options.PVC.Name)
if err := os.MkdirAll(path, 0777); err != nil {
return nil, err
}
Expand Down Expand Up @@ -103,16 +103,15 @@ func (p *hostPathProvisioner) Delete(volume *core.PersistentVolume) error {
return &controller.IgnoredError{Reason: "identity annotation on PV does not match ours"}
}

path := path.Join(p.pvDir, volume.Name)
if err := os.RemoveAll(path); err != nil {
if err := os.RemoveAll(volume.Spec.PersistentVolumeSource.HostPath.Path); err != nil {
return errors.Wrap(err, "removing hostpath PV")
}

return nil
}

// StartStorageProvisioner will start storage provisioner server
func StartStorageProvisioner() error {
func StartStorageProvisioner(pvDir string) error {
glog.Infof("Initializing the Minikube storage provisioner...")
config, err := rest.InClusterConfig()
if err != nil {
Expand All @@ -132,7 +131,7 @@ func StartStorageProvisioner() error {

// Create the provisioner: it implements the Provisioner interface expected by
// the controller
hostPathProvisioner := NewHostPathProvisioner()
hostPathProvisioner := NewHostPathProvisioner(pvDir)

// Start the provision controller which will dynamically provision hostPath
// PVs
Expand Down

0 comments on commit 9453b37

Please sign in to comment.