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"
|
2019-01-28 13:59:16 +00:00
|
|
|
|
2019-02-08 08:21:22 +00:00
|
|
|
"k8s.io/klog"
|
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
|
|
|
)
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
func getCephRootPathLocal(volID volumeID) string {
|
|
|
|
return cephRootPrefix + string(volID)
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
2018-03-22 13:01:10 +00:00
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
func getCephRootVolumePathLocal(volID volumeID) string {
|
|
|
|
return path.Join(getCephRootPathLocal(volID), cephVolumesRoot, string(volID))
|
2018-03-26 13:00:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
func getVolumeRootPathCeph(volID volumeID) string {
|
|
|
|
return path.Join("/", cephVolumesRoot, string(volID))
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +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)
|
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
func createVolume(volOptions *volumeOptions, adminCr *credentials, volID volumeID, bytesQuota int64) error {
|
|
|
|
cephRoot := getCephRootPathLocal(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)
|
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:39:54 +00:00
|
|
|
defer unmountAndRemove(cephRoot)
|
2018-04-13 12:49:49 +00:00
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
volOptions.RootPath = getVolumeRootPathCeph(volID)
|
|
|
|
localVolRoot := getCephRootVolumePathLocal(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)
|
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +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
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
func purgeVolume(volID volumeID, adminCr *credentials, volOptions *volumeOptions) error {
|
2018-04-13 12:49:49 +00:00
|
|
|
var (
|
2019-01-17 05:46:32 +00:00
|
|
|
cephRoot = getCephRootPathLocal(volID)
|
|
|
|
volRoot = getCephRootVolumePathLocal(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)
|
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
if err = m.mount(cephRoot, adminCr, volOptions, volID); err != nil {
|
2018-08-14 09:19:41 +00:00
|
|
|
return fmt.Errorf("error mounting ceph root: %v", err)
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 16:39:54 +00:00
|
|
|
defer unmountAndRemove(cephRoot)
|
2018-04-13 12:49:49 +00:00
|
|
|
|
2018-06-13 14:23:13 +00:00
|
|
|
if err := os.Rename(volRoot, volRootDeleting); err != nil {
|
2019-01-17 05:46:32 +00:00
|
|
|
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 {
|
2019-01-17 05:46:32 +00:00
|
|
|
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
|
|
|
}
|
2019-01-28 13:59:16 +00:00
|
|
|
|
2019-02-12 16:39:54 +00:00
|
|
|
func unmountAndRemove(mountPoint string) {
|
2019-01-28 13:59:16 +00:00
|
|
|
var err error
|
|
|
|
if err = unmountVolume(mountPoint); err != nil {
|
2019-02-08 08:21:22 +00:00
|
|
|
klog.Errorf("failed to unmount %s with error %s", mountPoint, err)
|
2019-01-28 13:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Remove(mountPoint); err != nil {
|
2019-02-08 08:21:22 +00:00
|
|
|
klog.Errorf("failed to remove %s with error %s", mountPoint, err)
|
2019-01-28 13:59:16 +00:00
|
|
|
}
|
|
|
|
}
|