mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 02:33:34 +00:00
move rbdVolume connection details to utils.ClusterConnection
The shared util.ClusterConnection can be used for rbd.rbdVolume and cephfs.volumeOptions to connect to the Ceph cluster. This will then use the shared ConnPool, and functions for obtaining connection details will be the same across cephfs and rbd packages. The ClusterConnection.Creds credentials are temporarily available until all the functions have been adapted to use go-ceph and the connection from the ConnPool. Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
committed by
mergify[bot]
parent
0991cdf498
commit
01edaf8a71
@ -95,14 +95,14 @@ func (cp *ConnPool) Destroy() {
|
||||
}
|
||||
}
|
||||
|
||||
func (cp *ConnPool) generateUniqueKey(pool, monitors, user, keyfile string) (string, error) {
|
||||
func (cp *ConnPool) generateUniqueKey(monitors, user, keyfile string) (string, error) {
|
||||
// the keyfile can be unique for operations, contents will be the same
|
||||
key, err := ioutil.ReadFile(keyfile) // nolint: gosec, #nosec
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "could not open keyfile %s", keyfile)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s|%s|%s|%s", pool, monitors, user, string(key)), nil
|
||||
return fmt.Sprintf("%s|%s|%s", monitors, user, string(key)), nil
|
||||
}
|
||||
|
||||
// getExisting returns the existing rados.Conn associated with the unique key.
|
||||
@ -118,10 +118,10 @@ func (cp *ConnPool) getConn(unique string) *rados.Conn {
|
||||
}
|
||||
|
||||
// Get returns a rados.Conn for the given arguments. Creates a new rados.Conn in
|
||||
// case there is none in the pool. Use the returned unique string to reduce the
|
||||
// reference count with ConnPool.Put(unique).
|
||||
func (cp *ConnPool) Get(pool, monitors, user, keyfile string) (*rados.Conn, error) {
|
||||
unique, err := cp.generateUniqueKey(pool, monitors, user, keyfile)
|
||||
// case there is none. Use the returned rados.Conn to reduce the reference
|
||||
// count with ConnPool.Put(unique).
|
||||
func (cp *ConnPool) Get(monitors, user, keyfile string) (*rados.Conn, error) {
|
||||
unique, err := cp.generateUniqueKey(monitors, user, keyfile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to generate unique for connection")
|
||||
}
|
||||
|
@ -34,8 +34,8 @@ const (
|
||||
// working Ceph cluster to connect to.
|
||||
//
|
||||
// This is mostly a copy of ConnPool.Get()
|
||||
func (cp *ConnPool) fakeGet(pool, monitors, user, keyfile string) (*rados.Conn, string, error) {
|
||||
unique, err := cp.generateUniqueKey(pool, monitors, user, keyfile)
|
||||
func (cp *ConnPool) fakeGet(monitors, user, keyfile string) (*rados.Conn, string, error) {
|
||||
unique, err := cp.generateUniqueKey(monitors, user, keyfile)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
@ -91,7 +91,7 @@ func TestConnPool(t *testing.T) {
|
||||
var unique string
|
||||
|
||||
t.Run("fakeGet", func(t *testing.T) {
|
||||
conn, unique, err = cp.fakeGet("pool", "monitors", "user", keyfile)
|
||||
conn, unique, err = cp.fakeGet("monitors", "user", keyfile)
|
||||
if err != nil {
|
||||
t.Errorf("failed to get connection: %v", err)
|
||||
}
|
||||
@ -115,7 +115,7 @@ func TestConnPool(t *testing.T) {
|
||||
|
||||
t.Run("doubleFakeGet", func(t *testing.T) {
|
||||
// after a 2nd get, there should still be a single conn in cp.conns
|
||||
_, _, err = cp.fakeGet("pool", "monitors", "user", keyfile)
|
||||
_, _, err = cp.fakeGet("monitors", "user", keyfile)
|
||||
if err != nil {
|
||||
t.Errorf("failed to get connection: %v", err)
|
||||
}
|
||||
|
78
internal/util/connection.go
Normal file
78
internal/util/connection.go
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
Copyright 2020 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 (
|
||||
"time"
|
||||
|
||||
"github.com/ceph/go-ceph/rados"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type ClusterConnection struct {
|
||||
// connection
|
||||
conn *rados.Conn
|
||||
|
||||
// FIXME: temporary reference for credentials. Remove this when go-ceph
|
||||
// is used for operations.
|
||||
Creds *Credentials
|
||||
}
|
||||
|
||||
var (
|
||||
// large interval and timeout, it should be longer than the maximum
|
||||
// time an operation can take (until refcounting of the connections is
|
||||
// available)
|
||||
cpInterval = 15 * time.Minute
|
||||
cpExpiry = 10 * time.Minute
|
||||
connPool = NewConnPool(cpInterval, cpExpiry)
|
||||
)
|
||||
|
||||
// rbdVol.Connect() connects to the Ceph cluster and sets rbdVol.conn for further usage.
|
||||
func (cc *ClusterConnection) Connect(monitors string, cr *Credentials) error {
|
||||
if cc.conn == nil {
|
||||
conn, err := connPool.Get(monitors, cr.ID, cr.KeyFile)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to get connection")
|
||||
}
|
||||
|
||||
cc.conn = conn
|
||||
|
||||
// FIXME: remove .Creds from ClusterConnection
|
||||
cc.Creds = cr
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cc *ClusterConnection) Destroy() {
|
||||
if cc.conn != nil {
|
||||
connPool.Put(cc.conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (cc *ClusterConnection) GetIoctx(pool string) (*rados.IOContext, error) {
|
||||
if cc.conn == nil {
|
||||
return nil, errors.New("cluster is not connected yet")
|
||||
}
|
||||
|
||||
ioctx, err := cc.conn.OpenIOContext(pool)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to open IOContext for pool %s", pool)
|
||||
}
|
||||
|
||||
return ioctx, nil
|
||||
}
|
Reference in New Issue
Block a user