util: implement CreateObject() with go-ceph

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-06-02 10:03:32 +02:00 committed by mergify[bot]
parent 6e24b10364
commit be703d1e42

View File

@ -219,27 +219,31 @@ func RemoveOMapKey(ctx context.Context, monitors string, cr *Credentials, poolNa
// CreateObject creates the object name passed in and returns ErrObjectExists if the provided object
// is already present in rados
func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolName, namespace, objectName string) error {
// Command: "rados <options> create objectName"
args := []string{
"-m", monitors,
"--id", cr.ID,
"--keyfile=" + cr.KeyFile,
"-c", CephConfigPath,
"-p", poolName,
"create", objectName,
conn := ClusterConnection{}
err := conn.Connect(monitors, cr)
if err != nil {
return err
}
defer conn.Destroy()
ioctx, err := conn.GetIoctx(poolName)
if err != nil {
if _, ok := err.(ErrPoolNotFound); ok {
err = ErrObjectNotFound{poolName, err}
}
return err
}
defer ioctx.Destroy()
if namespace != "" {
args = append(args, "--namespace="+namespace)
ioctx.SetNamespace(namespace)
}
_, stderr, err := ExecCommand("rados", args[:]...)
if err != nil {
err = ioctx.Create(objectName, rados.CreateExclusive)
if err == rados.ErrObjectExists {
return ErrObjectExists{objectName, err}
} else if err != nil {
klog.Errorf(Log(ctx, "failed creating omap (%s) in pool (%s): (%v)"), objectName, poolName, err)
if strings.Contains(string(stderr), "error creating "+poolName+"/"+objectName+
": (17) File exists") {
return ErrObjectExists{objectName, err}
}
return err
}