ceph-csi/internal/util/connection.go
Niels de Vos 01edaf8a71 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>
2020-05-04 13:21:03 +00:00

79 lines
1.9 KiB
Go

/*
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
}