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
|
|
|
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-03-13 05:09:58 +00:00
|
|
|
var (
|
|
|
|
cephRootPrefix = PluginFolder + "/controller/volumes/root-"
|
|
|
|
)
|
|
|
|
|
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 {
|
2019-02-14 10:47:16 +00:00
|
|
|
return execCommandErr("setfattr", "-n", attrName, "-v", attrValue, root)
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
func createVolume(volOptions *volumeOptions, adminCr *credentials, volID volumeID, bytesQuota int64) error {
|
2019-02-26 10:06:16 +00:00
|
|
|
if err := mountCephRoot(volID, volOptions, adminCr); err != nil {
|
2018-03-26 13:00:28 +00:00
|
|
|
return err
|
2018-03-22 13:01:10 +00:00
|
|
|
}
|
2019-02-26 10:06:16 +00:00
|
|
|
defer unmountCephRoot(volID)
|
2018-03-22 13:01:10 +00:00
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
var (
|
|
|
|
volRoot = getCephRootVolumePathLocal(volID)
|
|
|
|
volRootCreating = volRoot + "-creating"
|
|
|
|
)
|
2018-08-14 09:19:41 +00:00
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
if pathExists(volRoot) {
|
|
|
|
klog.V(4).Infof("cephfs: volume %s already exists, skipping creation", volID)
|
|
|
|
return nil
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
if err := createMountPoint(volRootCreating); err != nil {
|
2018-04-13 12:49:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-01 09:39:09 +00:00
|
|
|
if bytesQuota > 0 {
|
2019-02-26 10:06:16 +00:00
|
|
|
if err := setVolumeAttribute(volRootCreating, "ceph.quota.max_bytes", fmt.Sprintf("%d", bytesQuota)); err != nil {
|
2018-12-01 09:39:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
if err := setVolumeAttribute(volRootCreating, "ceph.dir.layout.pool", volOptions.Pool); err != nil {
|
2018-04-13 12:49:49 +00:00
|
|
|
return fmt.Errorf("%v\ncephfs: Does pool '%s' exist?", err, volOptions.Pool)
|
|
|
|
}
|
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
if err := setVolumeAttribute(volRootCreating, "ceph.dir.layout.pool_namespace", getVolumeNamespace(volID)); err != nil {
|
2018-04-13 12:49:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
if err := os.Rename(volRootCreating, volRoot); err != nil {
|
|
|
|
return fmt.Errorf("couldn't mark volume %s as created: %v", volID, err)
|
|
|
|
}
|
|
|
|
|
2018-04-13 12:49:49 +00:00
|
|
|
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 {
|
2019-02-26 10:06:16 +00:00
|
|
|
if err := mountCephRoot(volID, volOptions, adminCr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer unmountCephRoot(volID)
|
|
|
|
|
2018-04-13 12:49:49 +00:00
|
|
|
var (
|
2019-01-17 05:46:32 +00:00
|
|
|
volRoot = getCephRootVolumePathLocal(volID)
|
2018-06-13 14:23:13 +00:00
|
|
|
volRootDeleting = volRoot + "-deleting"
|
2018-04-13 12:49:49 +00:00
|
|
|
)
|
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
if pathExists(volRoot) {
|
|
|
|
if err := os.Rename(volRoot, volRootDeleting); err != nil {
|
|
|
|
return fmt.Errorf("couldn't mark volume %s for deletion: %v", volID, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !pathExists(volRootDeleting) {
|
|
|
|
klog.V(4).Infof("cephfs: volume %s not found, assuming it to be already deleted", volID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mountCephRoot(volID volumeID, volOptions *volumeOptions, adminCr *credentials) error {
|
|
|
|
cephRoot := getCephRootPathLocal(volID)
|
|
|
|
|
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 = "/"
|
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
if err := createMountPoint(cephRoot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-03-04 04:10:44 +00:00
|
|
|
if err = m.mount(cephRoot, adminCr, volOptions); 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
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-03-09 16:05:19 +00:00
|
|
|
}
|
2019-01-28 13:59:16 +00:00
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
func unmountCephRoot(volID volumeID) {
|
|
|
|
cephRoot := getCephRootPathLocal(volID)
|
|
|
|
|
|
|
|
if err := unmountVolume(cephRoot); err != nil {
|
|
|
|
klog.Errorf("failed to unmount %s with error %s", cephRoot, err)
|
2019-02-26 13:46:21 +00:00
|
|
|
} else {
|
|
|
|
if err := os.Remove(cephRoot); err != nil {
|
|
|
|
klog.Errorf("failed to remove %s with error %s", cephRoot, err)
|
|
|
|
}
|
2019-01-28 13:59:16 +00:00
|
|
|
}
|
|
|
|
}
|