From d248a1e200d2f14d9131ce6e90f90695296c0d8c Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Tue, 9 Jul 2024 17:27:08 +0200 Subject: [PATCH] rbd: add Manager interface for using Volumes and VolumeGroups Signed-off-by: Niels de Vos --- internal/rbd/manager.go | 89 +++++++++++++++++++++++++++++++++++ internal/rbd/types/manager.go | 44 +++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 internal/rbd/manager.go create mode 100644 internal/rbd/types/manager.go diff --git a/internal/rbd/manager.go b/internal/rbd/manager.go new file mode 100644 index 000000000..518a914d0 --- /dev/null +++ b/internal/rbd/manager.go @@ -0,0 +1,89 @@ +/* +Copyright 2024 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 rbd + +import ( + "context" + "errors" + + "github.com/ceph/ceph-csi/internal/rbd/types" + "github.com/ceph/ceph-csi/internal/util" +) + +var _ types.Manager = &rbdManager{} + +type rbdManager struct { + parameters map[string]string + secrets map[string]string + + creds *util.Credentials +} + +// NewManager returns a new manager for handling Volume and Volume Group +// operations, combining the requests for RBD and the journalling in RADOS. +func NewManager(parameters, secrets map[string]string) types.Manager { + return &rbdManager{ + parameters: parameters, + secrets: secrets, + } +} + +func (mgr *rbdManager) Destroy(ctx context.Context) { + if mgr.creds != nil { + mgr.creds.DeleteCredentials() + mgr.creds = nil + } +} + +// connect sets up credentials and connects to the journal. +func (mgr *rbdManager) connect() error { + if mgr.creds == nil { + creds, err := util.NewUserCredentials(mgr.secrets) + if err != nil { + return err + } + + mgr.creds = creds + } + + return nil +} + +func (mgr *rbdManager) GetVolumeByID(ctx context.Context, id string) (types.Volume, error) { + if err := mgr.connect(); err != nil { + return nil, err + } + + volume, err := GenVolFromVolID(ctx, id, mgr.creds, mgr.secrets) + if err != nil { + return nil, err + } + + return volume, nil +} + +func (mgr *rbdManager) GetVolumeGroupByID(ctx context.Context, id string) (types.VolumeGroup, error) { + return nil, errors.New("rbdManager.GetVolumeGroupByID() is not implemented yet") +} + +func (mgr *rbdManager) CreateVolumeGroup(ctx context.Context, name string) (types.VolumeGroup, error) { + return nil, errors.New("rbdManager.CreateVolumeGroup() is not implemented yet") +} + +func (mgr *rbdManager) DeleteVolumeGroup(ctx context.Context, vg types.VolumeGroup) error { + return errors.New("rbdManager.CreateVolumeGroup() is not implemented yet") +} diff --git a/internal/rbd/types/manager.go b/internal/rbd/types/manager.go new file mode 100644 index 000000000..7744eb18a --- /dev/null +++ b/internal/rbd/types/manager.go @@ -0,0 +1,44 @@ +/* +Copyright 2024 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 types + +import ( + "context" +) + +// Manager provides a way for other packages to get Volumes and VolumeGroups. +// It handles the operations on the backend, and makes sure the journal +// reflects the expected state. +type Manager interface { + // Destroy frees all resources that the Manager allocated. + Destroy(ctx context.Context) + + // GetVolumeByID uses the CSI VolumeId to resolve the returned Volume. + GetVolumeByID(ctx context.Context, id string) (Volume, error) + + // GetVolumeGroupByID uses the CSI-Addons VolumeGroupId to resolve the + // returned VolumeGroup. + GetVolumeGroupByID(ctx context.Context, id string) (VolumeGroup, error) + + // CreateVolumeGroup allocates a new VolumeGroup in the backend storage + // and records details about it in the journal. + CreateVolumeGroup(ctx context.Context, name string) (VolumeGroup, error) + + // DeleteVolumeGroup removes VolumeGroup from the backend storage and + // any details from the journal. + DeleteVolumeGroup(ctx context.Context, vg VolumeGroup) error +}