2018-03-05 11:59:47 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cephfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-04-13 12:49:49 +00:00
|
|
|
"path"
|
2018-03-09 16:05:19 +00:00
|
|
|
)
|
|
|
|
|
2018-03-22 13:01:10 +00:00
|
|
|
const (
|
2018-07-28 08:24:07 +00:00
|
|
|
cephRootPrefix = PluginFolder + "/controller/volumes/root-"
|
|
|
|
cephVolumesRoot = "csi-volumes"
|
2018-04-13 12:49:49 +00:00
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
namespacePrefix = "ns-"
|
2018-03-22 13:01:10 +00:00
|
|
|
)
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
func getCephRootPath_local(volId volumeID) string {
|
|
|
|
return cephRootPrefix + string(volId)
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
2018-03-22 13:01:10 +00:00
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
func getCephRootVolumePath_local(volId volumeID) string {
|
|
|
|
return path.Join(getCephRootPath_local(volId), cephVolumesRoot, string(volId))
|
2018-03-26 13:00:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
func getVolumeRootPath_ceph(volId volumeID) string {
|
|
|
|
return path.Join("/", cephVolumesRoot, string(volId))
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
func getVolumeNamespace(volId volumeID) string {
|
|
|
|
return namespacePrefix + string(volId)
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setVolumeAttribute(root, attrName, attrValue string) error {
|
|
|
|
return execCommandAndValidate("setfattr", "-n", attrName, "-v", attrValue, root)
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
func createVolume(volOptions *volumeOptions, adminCr *credentials, volId volumeID, bytesQuota int64) error {
|
|
|
|
cephRoot := getCephRootPath_local(volId)
|
2018-04-13 12:49:49 +00:00
|
|
|
|
|
|
|
if err := createMountPoint(cephRoot); err != nil {
|
2018-03-26 13:00:28 +00:00
|
|
|
return err
|
2018-03-22 13:01:10 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 12:49:49 +00:00
|
|
|
// RootPath is not set for a dynamically provisioned volume
|
|
|
|
// Access to cephfs's / is required
|
|
|
|
volOptions.RootPath = "/"
|
|
|
|
|
2018-08-14 09:19:41 +00:00
|
|
|
m, err := newMounter(volOptions)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create mounter: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = m.mount(cephRoot, adminCr, volOptions, volId); err != nil {
|
2018-04-13 12:49:49 +00:00
|
|
|
return fmt.Errorf("error mounting ceph root: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
unmountVolume(cephRoot)
|
|
|
|
os.Remove(cephRoot)
|
|
|
|
}()
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
volOptions.RootPath = getVolumeRootPath_ceph(volId)
|
|
|
|
localVolRoot := getCephRootVolumePath_local(volId)
|
2018-04-13 12:49:49 +00:00
|
|
|
|
|
|
|
if err := createMountPoint(localVolRoot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-01 09:39:09 +00:00
|
|
|
if bytesQuota > 0 {
|
|
|
|
if err := setVolumeAttribute(localVolRoot, "ceph.quota.max_bytes", fmt.Sprintf("%d", bytesQuota)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2018-04-13 12:49:49 +00:00
|
|
|
if err := setVolumeAttribute(localVolRoot, "ceph.dir.layout.pool", volOptions.Pool); err != nil {
|
|
|
|
return fmt.Errorf("%v\ncephfs: Does pool '%s' exist?", err, volOptions.Pool)
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
if err := setVolumeAttribute(localVolRoot, "ceph.dir.layout.pool_namespace", getVolumeNamespace(volId)); err != nil {
|
2018-04-13 12:49:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 09:19:41 +00:00
|
|
|
func purgeVolume(volId volumeID, adminCr *credentials, volOptions *volumeOptions) error {
|
2018-04-13 12:49:49 +00:00
|
|
|
var (
|
2018-08-14 09:19:41 +00:00
|
|
|
cephRoot = getCephRootPath_local(volId)
|
2018-07-28 08:24:07 +00:00
|
|
|
volRoot = getCephRootVolumePath_local(volId)
|
2018-06-13 14:23:13 +00:00
|
|
|
volRootDeleting = volRoot + "-deleting"
|
2018-04-13 12:49:49 +00:00
|
|
|
)
|
|
|
|
|
2018-08-14 09:19:41 +00:00
|
|
|
if err := createMountPoint(cephRoot); err != nil {
|
2018-04-13 12:49:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-14 09:19:41 +00:00
|
|
|
// Root path is not set for dynamically provisioned volumes
|
|
|
|
// Access to cephfs's / is required
|
|
|
|
volOptions.RootPath = "/"
|
|
|
|
|
|
|
|
m, err := newMounter(volOptions)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create mounter: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = m.mount(cephRoot, adminCr, volOptions, volId); err != nil {
|
|
|
|
return fmt.Errorf("error mounting ceph root: %v", err)
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
unmountVolume(volRoot)
|
|
|
|
os.Remove(volRoot)
|
|
|
|
}()
|
|
|
|
|
2018-06-13 14:23:13 +00:00
|
|
|
if err := os.Rename(volRoot, volRootDeleting); err != nil {
|
|
|
|
return fmt.Errorf("coudln't mark volume %s for deletion: %v", volId, err)
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 14:23:13 +00:00
|
|
|
if err := os.RemoveAll(volRootDeleting); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete volume %s: %v", volId, err)
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-03-09 16:05:19 +00:00
|
|
|
}
|