From e069e9ab5d308601f136437e39e6a80845e257a4 Mon Sep 17 00:00:00 2001 From: Rewant Soni Date: Mon, 4 Nov 2024 21:11:02 +0530 Subject: [PATCH] add server side implementation Signed-off-by: Rewant Soni add generated changes Signed-off-by: Rewant Soni --- .../ocs-operator/manifests/provider-role.yaml | 1 + rbac/provider-role.yaml | 1 + services/provider/server/server.go | 103 +++++++++++++++++- 3 files changed, 100 insertions(+), 5 deletions(-) diff --git a/deploy/ocs-operator/manifests/provider-role.yaml b/deploy/ocs-operator/manifests/provider-role.yaml index 42259a455d..b8980ab515 100644 --- a/deploy/ocs-operator/manifests/provider-role.yaml +++ b/deploy/ocs-operator/manifests/provider-role.yaml @@ -16,6 +16,7 @@ rules: resources: - cephfilesystemsubvolumegroups - cephblockpoolradosnamespaces + - cephblockpools verbs: - get - list diff --git a/rbac/provider-role.yaml b/rbac/provider-role.yaml index 42259a455d..b8980ab515 100644 --- a/rbac/provider-role.yaml +++ b/rbac/provider-role.yaml @@ -16,6 +16,7 @@ rules: resources: - cephfilesystemsubvolumegroups - cephblockpoolradosnamespaces + - cephblockpools verbs: - get - list diff --git a/services/provider/server/server.go b/services/provider/server/server.go index 5a96ffdc43..eed1afa69a 100644 --- a/services/provider/server/server.go +++ b/services/provider/server/server.go @@ -52,11 +52,12 @@ import ( ) const ( - TicketAnnotation = "ocs.openshift.io/provider-onboarding-ticket" - ProviderCertsMountPoint = "/mnt/cert" - onboardingTicketKeySecret = "onboarding-ticket-key" - storageRequestNameLabel = "ocs.openshift.io/storagerequest-name" - notAvailable = "N/A" + TicketAnnotation = "ocs.openshift.io/provider-onboarding-ticket" + ProviderCertsMountPoint = "/mnt/cert" + onboardingTicketKeySecret = "onboarding-ticket-key" + storageRequestNameLabel = "ocs.openshift.io/storagerequest-name" + notAvailable = "N/A" + rbdMirrorBootstrapPeerSecretName = "rbdMirrorBootstrapPeerSecretName" ) const ( @@ -966,3 +967,95 @@ func (s *OCSProviderServer) PeerStorageCluster(ctx context.Context, req *pb.Peer return &pb.PeerStorageClusterResponse{}, nil } + +func (s *OCSProviderServer) GetClientsInfo(ctx context.Context, req *pb.GetClientsInfoRequest) (*pb.GetClientsInfoResponse, error) { + storageCluster, err := util.GetStorageClusterInNamespace(ctx, s.client, s.namespace) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to get storageCluster. %v", err) + } + + if storageCluster.UID != types.UID(req.StorageClusterUID) { + return nil, status.Errorf(codes.InvalidArgument, "storageClusterUID specified on the req does not match any existing storage cluster") + } + + var clientsInfo []*pb.ClientInfo + + for i := range req.ClientIDs { + consumer, err := s.consumerManager.Get(ctx, req.ClientIDs[i]) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to get consumer. %v", err) + } + rnsList := &rookCephv1.CephBlockPoolRadosNamespaceList{} + err = s.client.List(ctx, rnsList, client.InNamespace(s.namespace), client.MatchingLabels{controllers.StorageConsumerNameLabel: consumer.Name}, client.Limit(1)) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to list rados namespace. %v", err) + } + if len(rnsList.Items) == 0 { + return nil, status.Errorf(codes.Internal, "rados namespace for the client not found: %v", consumer.UID) + } + clientsInfo = append(clientsInfo, &pb.ClientInfo{ClientID: req.ClientIDs[i], RadosNamespace: rnsList.Items[0].Name}) + } + + return &pb.GetClientsInfoResponse{ClientsInfo: clientsInfo}, nil +} + +func (s *OCSProviderServer) GetBlockPoolsInfo(ctx context.Context, req *pb.GetBlockPoolsInfoRequest) (*pb.GetBlockPoolsInfoResponse, error) { + storageCluster, err := util.GetStorageClusterInNamespace(ctx, s.client, s.namespace) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to get storageCluster. %v", err) + } + + if storageCluster.UID != types.UID(req.StorageClusterUID) { + return nil, status.Errorf(codes.InvalidArgument, "storageClusterUID specified on the req does not match any existing storage cluster") + } + + var blockPoolsInfo []*pb.BlockPoolInfo + for i := range req.BlockPoolNames { + cephBlockPool := &rookCephv1.CephBlockPool{} + cephBlockPool.Name = req.BlockPoolNames[i] + cephBlockPool.Namespace = s.namespace + err = s.client.Get(ctx, client.ObjectKeyFromObject(cephBlockPool), cephBlockPool) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to get CephBlockPool: %v. %v", cephBlockPool.Name, err) + } + + // if the blockPool mirroring is not enabled return an error + if !cephBlockPool.Spec.Mirroring.Enabled { + errMsg := fmt.Sprintf("Mirroring is not enabled for CephBlockPool: %v", cephBlockPool.Name) + klog.Error(errMsg) + return nil, status.Errorf(codes.Internal, errMsg) + } + + if cephBlockPool.Status.Info != nil && + cephBlockPool.Status.Info[rbdMirrorBootstrapPeerSecretName] != "" { + secret := &corev1.Secret{} + secret.Name = cephBlockPool.Status.Info[rbdMirrorBootstrapPeerSecretName] + secret.Namespace = s.namespace + err := s.client.Get(ctx, client.ObjectKeyFromObject(secret), secret) + if err != nil { + errMsg := fmt.Sprintf("Error fetching bootstrap secret %s for CephBlockPool %s: %v", + cephBlockPool.Status.Info[rbdMirrorBootstrapPeerSecretName], + cephBlockPool.Name, + err) + klog.Error(errMsg) + return nil, status.Errorf(codes.Internal, errMsg) + } + if _, ok := secret.Data["token"]; !ok { + return nil, status.Errorf(codes.Internal, "error fetching mirroring token for CephBlockPool %s", cephBlockPool.Name) + } + + blockPoolsInfo = append(blockPoolsInfo, &pb.BlockPoolInfo{ + BlockPoolName: cephBlockPool.Name, + MirroringToken: string(secret.Data["token"]), + BlockPoolID: strconv.Itoa(cephBlockPool.Status.PoolID), + }) + } else { + errMsg := fmt.Sprintf("Mirroring Token for CephBlockPool %s is not generated", cephBlockPool.Name) + klog.Error(errMsg) + return nil, status.Errorf(codes.Internal, errMsg) + } + + } + + return &pb.GetBlockPoolsInfoResponse{BlockPoolsInfo: blockPoolsInfo}, nil +}