From 9b1e026b64de8824024a010c08b4c3065fe6ccb8 Mon Sep 17 00:00:00 2001 From: Rewant Soni Date: Tue, 15 Oct 2024 15:04:07 +0530 Subject: [PATCH] provider: add server side implementation of rpc calls Signed-off-by: Rewant Soni --- .../storageconsumer_controller.go | 1 + controllers/util/util.go | 13 ++++ .../ocs-operator/manifests/provider-role.yaml | 7 +++ .../ocs-operator/v4/controllers/util/util.go | 13 ++++ rbac/provider-role.yaml | 7 +++ services/provider/server/consumer.go | 8 +++ services/provider/server/server.go | 61 +++++++++++++++++++ 7 files changed, 110 insertions(+) diff --git a/controllers/storageconsumer/storageconsumer_controller.go b/controllers/storageconsumer/storageconsumer_controller.go index 53be0e8c4e..484e739e70 100644 --- a/controllers/storageconsumer/storageconsumer_controller.go +++ b/controllers/storageconsumer/storageconsumer_controller.go @@ -51,6 +51,7 @@ const ( StorageProfileLabel = "ocs.openshift.io/storageprofile" ConsumerUUIDLabel = "ocs.openshift.io/storageconsumer-uuid" StorageConsumerNameLabel = "ocs.openshift.io/storageconsumer-name" + MaintenanceModeLabel = "ocs.openshift.io/maintenanceMode" ) // StorageConsumerReconciler reconciles a StorageConsumer object diff --git a/controllers/util/util.go b/controllers/util/util.go index 634ad65245..edc967805b 100644 --- a/controllers/util/util.go +++ b/controllers/util/util.go @@ -89,6 +89,19 @@ func AddLabel(obj metav1.Object, key string, value string) bool { return false } +func RemoveLabel(obj metav1.Object, key string) bool { + labels := obj.GetLabels() + if labels == nil { + return false + } + if _, exists := labels[key]; exists { + delete(labels, key) + obj.SetLabels(labels) + return true + } + return false +} + func CalculateMD5Hash(value any) string { data, err := json.Marshal(value) if err != nil { diff --git a/deploy/ocs-operator/manifests/provider-role.yaml b/deploy/ocs-operator/manifests/provider-role.yaml index 42259a455d..935e2d6233 100644 --- a/deploy/ocs-operator/manifests/provider-role.yaml +++ b/deploy/ocs-operator/manifests/provider-role.yaml @@ -11,6 +11,13 @@ rules: - services verbs: - get + - apiGroups: + - apps + resources: + - deployments + verbs: + - get + - list - apiGroups: - ceph.rook.io resources: diff --git a/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/util.go b/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/util.go index 634ad65245..edc967805b 100644 --- a/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/util.go +++ b/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/util.go @@ -89,6 +89,19 @@ func AddLabel(obj metav1.Object, key string, value string) bool { return false } +func RemoveLabel(obj metav1.Object, key string) bool { + labels := obj.GetLabels() + if labels == nil { + return false + } + if _, exists := labels[key]; exists { + delete(labels, key) + obj.SetLabels(labels) + return true + } + return false +} + func CalculateMD5Hash(value any) string { data, err := json.Marshal(value) if err != nil { diff --git a/rbac/provider-role.yaml b/rbac/provider-role.yaml index 42259a455d..935e2d6233 100644 --- a/rbac/provider-role.yaml +++ b/rbac/provider-role.yaml @@ -11,6 +11,13 @@ rules: - services verbs: - get + - apiGroups: + - apps + resources: + - deployments + verbs: + - get + - list - apiGroups: - ceph.rook.io resources: diff --git a/services/provider/server/consumer.go b/services/provider/server/consumer.go index 37f8b696a9..5b90ab56e9 100644 --- a/services/provider/server/consumer.go +++ b/services/provider/server/consumer.go @@ -228,3 +228,11 @@ func (c *ocsConsumerManager) UpdateConsumerStatus(ctx context.Context, id string klog.Infof("successfully updated Status for StorageConsumer %v", consumerObj.Name) return nil } + +func (c *ocsConsumerManager) Update(ctx context.Context, consumer *ocsv1alpha1.StorageConsumer) error { + if err := c.client.Update(ctx, consumer); err != nil { + return fmt.Errorf("failed to update StorageConsumer %v: %v", consumer.Name, err) + } + klog.Infof("successfully updated StorageConsumer %v", consumer.Name) + return nil +} diff --git a/services/provider/server/server.go b/services/provider/server/server.go index 62349d3b59..40ebf9ade3 100644 --- a/services/provider/server/server.go +++ b/services/provider/server/server.go @@ -11,6 +11,7 @@ import ( "encoding/json" "encoding/pem" "fmt" + appsv1 "k8s.io/api/apps/v1" "math" "net" "slices" @@ -304,6 +305,10 @@ func newScheme() (*runtime.Scheme, error) { if err != nil { return nil, fmt.Errorf("failed to add routev1 to scheme. %v", err) } + err = appsv1.AddToScheme(scheme) + if err != nil { + return nil, fmt.Errorf("failed to add appsv1 to scheme. %v", err) + } return scheme, nil } @@ -1042,3 +1047,59 @@ func (s *OCSProviderServer) PeerStorageCluster(ctx context.Context, req *pb.Peer return &pb.PeerStorageClusterResponse{}, nil } + +func (s *OCSProviderServer) StartMaintenanceMode(ctx context.Context, req *pb.StartMaintenanceModeRequest) (*pb.StartMaintenanceModeResponse, error) { + // Get storage consumer resource using UUID + consumer, err := s.consumerManager.Get(ctx, req.StorageConsumerUUID) + if err != nil { + return nil, status.Errorf(codes.Internal, err.Error()) + } + + util.AddLabel(consumer, controllers.MaintenanceModeLabel, "") + err = s.consumerManager.Update(ctx, consumer) + if err != nil { + return nil, status.Errorf(codes.Internal, err.Error()) + } + + return &pb.StartMaintenanceModeResponse{}, nil +} + +func (s *OCSProviderServer) StopMaintenanceMode(ctx context.Context, req *pb.StopMaintenanceModeRequest) (*pb.StopMaintenanceModeResponse, error) { + // Get storage consumer resource using UUID + consumer, err := s.consumerManager.Get(ctx, req.StorageConsumerUUID) + if err != nil { + return nil, status.Errorf(codes.Internal, err.Error()) + } + + util.RemoveLabel(consumer, controllers.MaintenanceModeLabel) + + if err := s.consumerManager.Update(ctx, consumer); err != nil { + return nil, status.Errorf(codes.Internal, err.Error()) + } + + return &pb.StopMaintenanceModeResponse{}, nil +} + +func (s *OCSProviderServer) GetMaintenanceModeStatus(ctx context.Context, req *pb.GetMaintenanceModeStatusRequest) (*pb.GetMaintenanceModeStatusResponse, error) { + // Get storage consumer resource using UUID + _, err := s.consumerManager.Get(ctx, req.StorageConsumerUUID) + if err != nil { + return nil, status.Errorf(codes.Internal, err.Error()) + } + + rbdMirrorDeployments := &appsv1.DeploymentList{} + err = s.client.List(ctx, rbdMirrorDeployments, client.InNamespace(s.namespace), client.MatchingLabels{"app": "rook-ceph-rbd-mirror"}, client.Limit(1)) + if err != nil { + return nil, status.Errorf(codes.Internal, err.Error()) + } + + if len(rbdMirrorDeployments.Items) == 0 { + return nil, fmt.Errorf("no rbd-mirror deployment found") + } + + replicas := rbdMirrorDeployments.Items[0].Status.Replicas + if replicas == 1 { + return &pb.GetMaintenanceModeStatusResponse{MaintenanceStatus: pb.GetMaintenanceModeStatusResponse_Progressing}, nil + } + return &pb.GetMaintenanceModeStatusResponse{MaintenanceStatus: pb.GetMaintenanceModeStatusResponse_Completed}, nil +}