2019-04-22 21:35:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Ceph-CSI 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 util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2019-06-10 07:22:13 +00:00
|
|
|
|
|
|
|
"k8s.io/klog"
|
2019-04-22 21:35:39 +00:00
|
|
|
)
|
|
|
|
|
2019-06-10 07:22:13 +00:00
|
|
|
// ExecCommand executes passed in program with args and returns separate stdout and stderr streams
|
2019-04-22 21:35:39 +00:00
|
|
|
func ExecCommand(program string, args ...string) (stdout, stderr []byte, err error) {
|
|
|
|
var (
|
2019-06-08 05:06:03 +00:00
|
|
|
cmd = exec.Command(program, args...) // nolint: gosec
|
|
|
|
sanitizedArgs = StripSecretInArgs(args)
|
|
|
|
stdoutBuf bytes.Buffer
|
|
|
|
stderrBuf bytes.Buffer
|
2019-04-22 21:35:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
cmd.Stdout = &stdoutBuf
|
|
|
|
cmd.Stderr = &stderrBuf
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return stdoutBuf.Bytes(), stderrBuf.Bytes(), fmt.Errorf("an error (%v)"+
|
2019-06-08 05:06:03 +00:00
|
|
|
" occurred while running %s args: %v", err, program, sanitizedArgs)
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stdoutBuf.Bytes(), nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cephStoragePoolSummary strongly typed JSON spec for osd ls pools output
|
|
|
|
type cephStoragePoolSummary struct {
|
|
|
|
Name string `json:"poolname"`
|
|
|
|
Number int64 `json:"poolnum"`
|
|
|
|
}
|
|
|
|
|
2019-06-28 01:10:32 +00:00
|
|
|
// GetPools fetches a list of pools from a cluster
|
|
|
|
func getPools(monitors string, cr *Credentials) ([]cephStoragePoolSummary, error) {
|
2019-04-22 21:35:39 +00:00
|
|
|
// ceph <options> -f json osd lspools
|
|
|
|
// JSON out: [{"poolnum":<int64>,"poolname":<string>}]
|
|
|
|
|
|
|
|
stdout, _, err := ExecCommand(
|
|
|
|
"ceph",
|
|
|
|
"-m", monitors,
|
2019-06-01 21:26:42 +00:00
|
|
|
"--id", cr.ID,
|
2019-06-25 19:29:17 +00:00
|
|
|
"--keyfile="+cr.KeyFile,
|
2019-04-22 21:35:39 +00:00
|
|
|
"-c", CephConfigPath,
|
|
|
|
"-f", "json",
|
|
|
|
"osd", "lspools")
|
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed getting pool list from cluster (%s)", err)
|
2019-06-28 01:10:32 +00:00
|
|
|
return nil, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pools []cephStoragePoolSummary
|
|
|
|
err = json.Unmarshal(stdout, &pools)
|
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed to parse JSON output of pool list from cluster (%s)", err)
|
2019-06-28 01:10:32 +00:00
|
|
|
return nil, fmt.Errorf("unmarshal of pool list failed: %+v. raw buffer response: %s", err, string(stdout))
|
|
|
|
}
|
|
|
|
|
|
|
|
return pools, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPoolID searches a list of pools in a cluster and returns the ID of the pool that matches
|
|
|
|
// the passed in poolName parameter
|
|
|
|
func GetPoolID(monitors string, cr *Credentials, poolName string) (int64, error) {
|
|
|
|
pools, err := getPools(monitors, cr)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range pools {
|
|
|
|
if poolName == p.Name {
|
|
|
|
return p.Number, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, fmt.Errorf("pool (%s) not found in Ceph cluster", poolName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPoolName lists all pools in a ceph cluster, and matches the pool whose pool ID is equal to
|
|
|
|
// the requested poolID parameter
|
2019-06-01 21:26:42 +00:00
|
|
|
func GetPoolName(monitors string, cr *Credentials, poolID int64) (string, error) {
|
2019-06-28 01:10:32 +00:00
|
|
|
pools, err := getPools(monitors, cr)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range pools {
|
|
|
|
if poolID == p.Number {
|
|
|
|
return p.Name, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("pool ID (%d) not found in Ceph cluster", poolID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOMapKeyValue sets the given key and value into the provided Ceph omap name
|
2019-06-01 21:26:42 +00:00
|
|
|
func SetOMapKeyValue(monitors string, cr *Credentials, poolName, namespace, oMapName, oMapKey, keyValue string) error {
|
2019-04-22 21:35:39 +00:00
|
|
|
// Command: "rados <options> setomapval oMapName oMapKey keyValue"
|
2019-05-28 19:03:18 +00:00
|
|
|
args := []string{
|
2019-04-22 21:35:39 +00:00
|
|
|
"-m", monitors,
|
2019-06-01 21:26:42 +00:00
|
|
|
"--id", cr.ID,
|
2019-06-25 19:29:17 +00:00
|
|
|
"--keyfile=" + cr.KeyFile,
|
2019-04-22 21:35:39 +00:00
|
|
|
"-c", CephConfigPath,
|
|
|
|
"-p", poolName,
|
2019-05-28 19:03:18 +00:00
|
|
|
"setomapval", oMapName, oMapKey, keyValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
if namespace != "" {
|
|
|
|
args = append(args, "--namespace="+namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := ExecCommand("rados", args[:]...)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed adding key (%s with value %s), to omap (%s) in "+
|
|
|
|
"pool (%s): (%v)", oMapKey, keyValue, oMapName, poolName, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOMapValue gets the value for the given key from the named omap
|
2019-06-01 21:26:42 +00:00
|
|
|
func GetOMapValue(monitors string, cr *Credentials, poolName, namespace, oMapName, oMapKey string) (string, error) {
|
2019-04-22 21:35:39 +00:00
|
|
|
// Command: "rados <options> getomapval oMapName oMapKey <outfile>"
|
|
|
|
// No such key: replicapool/csi.volumes.directory.default/csi.volname
|
|
|
|
tmpFile, err := ioutil.TempFile("", "omap-get-")
|
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed creating a temporary file for key contents")
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer tmpFile.Close()
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
args := []string{
|
2019-04-22 21:35:39 +00:00
|
|
|
"-m", monitors,
|
2019-06-01 21:26:42 +00:00
|
|
|
"--id", cr.ID,
|
2019-06-25 19:29:17 +00:00
|
|
|
"--keyfile=" + cr.KeyFile,
|
2019-04-22 21:35:39 +00:00
|
|
|
"-c", CephConfigPath,
|
|
|
|
"-p", poolName,
|
2019-05-28 19:03:18 +00:00
|
|
|
"getomapval", oMapName, oMapKey, tmpFile.Name(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if namespace != "" {
|
|
|
|
args = append(args, "--namespace="+namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout, stderr, err := ExecCommand("rados", args[:]...)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
2019-05-14 19:15:01 +00:00
|
|
|
// no logs, as attempting to check for non-existent key/value is done even on
|
|
|
|
// regular call sequences
|
2019-04-22 21:35:39 +00:00
|
|
|
stdoutanderr := strings.Join([]string{string(stdout), string(stderr)}, " ")
|
|
|
|
if strings.Contains(stdoutanderr, "No such key: "+poolName+"/"+oMapName+"/"+oMapKey) {
|
|
|
|
return "", ErrKeyNotFound{poolName + "/" + oMapName + "/" + oMapKey, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(stdoutanderr, "error getting omap value "+
|
|
|
|
poolName+"/"+oMapName+"/"+oMapKey+": (2) No such file or directory") {
|
|
|
|
return "", ErrKeyNotFound{poolName + "/" + oMapName + "/" + oMapKey, err}
|
|
|
|
}
|
|
|
|
|
2019-05-14 19:15:01 +00:00
|
|
|
// log other errors for troubleshooting assistance
|
|
|
|
klog.Errorf("failed getting omap value for key (%s) from omap (%s) in pool (%s): (%v)",
|
|
|
|
oMapKey, oMapName, poolName, err)
|
|
|
|
|
2019-06-10 07:22:13 +00:00
|
|
|
return "", fmt.Errorf("error (%v) occurred, command output streams is (%s)",
|
2019-04-22 21:35:39 +00:00
|
|
|
err.Error(), stdoutanderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyValue, err := ioutil.ReadAll(tmpFile)
|
|
|
|
return string(keyValue), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveOMapKey removes the omap key from the given omap name
|
2019-06-01 21:26:42 +00:00
|
|
|
func RemoveOMapKey(monitors string, cr *Credentials, poolName, namespace, oMapName, oMapKey string) error {
|
2019-04-22 21:35:39 +00:00
|
|
|
// Command: "rados <options> rmomapkey oMapName oMapKey"
|
2019-05-28 19:03:18 +00:00
|
|
|
args := []string{
|
2019-04-22 21:35:39 +00:00
|
|
|
"-m", monitors,
|
2019-06-01 21:26:42 +00:00
|
|
|
"--id", cr.ID,
|
2019-06-25 19:29:17 +00:00
|
|
|
"--keyfile=" + cr.KeyFile,
|
2019-04-22 21:35:39 +00:00
|
|
|
"-c", CephConfigPath,
|
|
|
|
"-p", poolName,
|
2019-05-28 19:03:18 +00:00
|
|
|
"rmomapkey", oMapName, oMapKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
if namespace != "" {
|
|
|
|
args = append(args, "--namespace="+namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := ExecCommand("rados", args[:]...)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
// NOTE: Missing omap key removal does not return an error
|
|
|
|
klog.Errorf("failed removing key (%s), from omap (%s) in "+
|
|
|
|
"pool (%s): (%v)", oMapKey, oMapName, poolName, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateObject creates the object name passed in and returns ErrObjectExists if the provided object
|
|
|
|
// is already present in rados
|
2019-06-01 21:26:42 +00:00
|
|
|
func CreateObject(monitors string, cr *Credentials, poolName, namespace, objectName string) error {
|
2019-04-22 21:35:39 +00:00
|
|
|
// Command: "rados <options> create objectName"
|
2019-05-28 19:03:18 +00:00
|
|
|
args := []string{
|
2019-04-22 21:35:39 +00:00
|
|
|
"-m", monitors,
|
2019-06-01 21:26:42 +00:00
|
|
|
"--id", cr.ID,
|
2019-06-25 19:29:17 +00:00
|
|
|
"--keyfile=" + cr.KeyFile,
|
2019-04-22 21:35:39 +00:00
|
|
|
"-c", CephConfigPath,
|
|
|
|
"-p", poolName,
|
2019-05-28 19:03:18 +00:00
|
|
|
"create", objectName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if namespace != "" {
|
|
|
|
args = append(args, "--namespace="+namespace)
|
|
|
|
}
|
|
|
|
|
2019-07-13 12:00:47 +00:00
|
|
|
_, stderr, err := ExecCommand("rados", args[:]...)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed creating omap (%s) in pool (%s): (%v)", objectName, poolName, err)
|
2019-07-13 12:00:47 +00:00
|
|
|
if strings.Contains(string(stderr), "error creating "+poolName+"/"+objectName+
|
2019-04-22 21:35:39 +00:00
|
|
|
": (17) File exists") {
|
|
|
|
return ErrObjectExists{objectName, err}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveObject removes the entire omap name passed in and returns ErrObjectNotFound is provided omap
|
|
|
|
// is not found in rados
|
2019-06-01 21:26:42 +00:00
|
|
|
func RemoveObject(monitors string, cr *Credentials, poolName, namespace, oMapName string) error {
|
2019-04-22 21:35:39 +00:00
|
|
|
// Command: "rados <options> rm oMapName"
|
2019-05-28 19:03:18 +00:00
|
|
|
args := []string{
|
2019-04-22 21:35:39 +00:00
|
|
|
"-m", monitors,
|
2019-06-01 21:26:42 +00:00
|
|
|
"--id", cr.ID,
|
2019-06-25 19:29:17 +00:00
|
|
|
"--keyfile=" + cr.KeyFile,
|
2019-04-22 21:35:39 +00:00
|
|
|
"-c", CephConfigPath,
|
|
|
|
"-p", poolName,
|
2019-05-28 19:03:18 +00:00
|
|
|
"rm", oMapName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if namespace != "" {
|
|
|
|
args = append(args, "--namespace="+namespace)
|
|
|
|
}
|
|
|
|
|
2019-07-13 12:00:47 +00:00
|
|
|
_, stderr, err := ExecCommand("rados", args[:]...)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed removing omap (%s) in pool (%s): (%v)", oMapName, poolName, err)
|
2019-07-13 12:00:47 +00:00
|
|
|
if strings.Contains(string(stderr), "error removing "+poolName+">"+oMapName+
|
2019-04-22 21:35:39 +00:00
|
|
|
": (2) No such file or directory") {
|
|
|
|
return ErrObjectNotFound{oMapName, err}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|