From 9453b37bd03be16adcb7cbe92f69d55f887323d9 Mon Sep 17 00:00:00 2001 From: Nanik T Date: Tue, 24 Dec 2019 05:53:57 +1100 Subject: [PATCH] Customizing host path for dynamically provisioned PersistentVolumes 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 --- cmd/storage-provisioner/main.go | 4 +++- .../storage-provisioner.yaml.tmpl | 2 +- deploy/storage-provisioner/Dockerfile | 6 +++--- pkg/storage/storage_provisioner.go | 13 ++++++------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/cmd/storage-provisioner/main.go b/cmd/storage-provisioner/main.go index 6b097496a375..2350ef997bd6 100644 --- a/cmd/storage-provisioner/main.go +++ b/cmd/storage-provisioner/main.go @@ -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 { @@ -33,7 +35,7 @@ func main() { } flag.Parse() - if err := storage.StartStorageProvisioner(); err != nil { + if err := storage.StartStorageProvisioner(pvDir); err != nil { glog.Exit(err) } diff --git a/deploy/addons/storage-provisioner/storage-provisioner.yaml.tmpl b/deploy/addons/storage-provisioner/storage-provisioner.yaml.tmpl index d87939b529ec..e5f2dc28a4ce 100644 --- a/deploy/addons/storage-provisioner/storage-provisioner.yaml.tmpl +++ b/deploy/addons/storage-provisioner/storage-provisioner.yaml.tmpl @@ -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 diff --git a/deploy/storage-provisioner/Dockerfile b/deploy/storage-provisioner/Dockerfile index bef335d5142c..b2f52adbec41 100644 --- a/deploy/storage-provisioner/Dockerfile +++ b/deploy/storage-provisioner/Dockerfile @@ -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"] \ No newline at end of file diff --git a/pkg/storage/storage_provisioner.go b/pkg/storage/storage_provisioner.go index 953b80aba944..3d29cc22b594 100644 --- a/pkg/storage/storage_provisioner.go +++ b/pkg/storage/storage_provisioner.go @@ -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(), } } @@ -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 } @@ -103,8 +103,7 @@ 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") } @@ -112,7 +111,7 @@ func (p *hostPathProvisioner) Delete(volume *core.PersistentVolume) error { } // 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 { @@ -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