Skip to content

Commit

Permalink
provider: add server side implementation of rpc calls
Browse files Browse the repository at this point in the history
Signed-off-by: Rewant Soni <[email protected]>
  • Loading branch information
rewantsoni committed Nov 13, 2024
1 parent 2ef217f commit 9b1e026
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions controllers/storageconsumer/storageconsumer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions controllers/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions deploy/ocs-operator/manifests/provider-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ rules:
- services
verbs:
- get
- apiGroups:
- apps
resources:
- deployments
verbs:
- get
- list
- apiGroups:
- ceph.rook.io
resources:
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions rbac/provider-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ rules:
- services
verbs:
- get
- apiGroups:
- apps
resources:
- deployments
verbs:
- get
- list
- apiGroups:
- ceph.rook.io
resources:
Expand Down
8 changes: 8 additions & 0 deletions services/provider/server/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
61 changes: 61 additions & 0 deletions services/provider/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
appsv1 "k8s.io/api/apps/v1"
"math"
"net"
"slices"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 9b1e026

Please sign in to comment.