Skip to content

Commit

Permalink
sidecar: add volume replication service setup
Browse files Browse the repository at this point in the history
adding sidecar service for the volume replication
service.

Signed-off-by: Rakshith R <[email protected]>
  • Loading branch information
Rakshith-R committed Aug 22, 2022
1 parent 34f5e6b commit cfb4fb9
Show file tree
Hide file tree
Showing 5 changed files with 1,594 additions and 0 deletions.
193 changes: 193 additions & 0 deletions internal/sidecar/service/volumereplication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
Copyright 2022 The Kubernetes-CSI-Addons 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 service

import (
"context"

kube "github.com/csi-addons/kubernetes-csi-addons/internal/kubernetes"
"github.com/csi-addons/kubernetes-csi-addons/internal/proto"
csiReplication "github.com/csi-addons/spec/lib/go/replication"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
)

// ReplicationServer struct of sidecar with supported methods of proto
// replication server spec and also containing replication
// controller client to csi driver.
type ReplicationServer struct {
proto.UnimplementedReplicationServer
controllerClient csiReplication.ControllerClient
kubeClient *kubernetes.Clientset
}

// NewReplicationServer creates a new ReplicationServer which handles the proto.Replication
// Service requests.
func NewReplicationServer(c *grpc.ClientConn, kc *kubernetes.Clientset) *ReplicationServer {
return &ReplicationServer{
controllerClient: csiReplication.NewControllerClient(c),
kubeClient: kc,
}
}

// RegisterService registers service with the server.
func (rs *ReplicationServer) RegisterService(server grpc.ServiceRegistrar) {
proto.RegisterReplicationServer(server, rs)
}

// EnableVolumeReplication fetches required information from kubernetes cluster and calls
// CSI-Addons EnableVolumeReplication service.
func (rs *ReplicationServer) EnableVolumeReplication(
ctx context.Context,
req *proto.EnableVolumeReplicationRequest) (*proto.EnableVolumeReplicationResponse, error) {
// Get the secrets from the k8s cluster
data, err := kube.GetSecret(ctx, rs.kubeClient, req.GetSecretName(), req.GetSecretNamespace())
if err != nil {
klog.Errorf("Failed to get secret %s in namespace %s: %v", req.GetSecretName(), req.GetSecretNamespace(), err)
return nil, status.Error(codes.Internal, err.Error())
}

_, err = rs.controllerClient.EnableVolumeReplication(ctx,
&csiReplication.EnableVolumeReplicationRequest{
VolumeId: req.VolumeId,
ReplicationId: req.ReplicationId,
Parameters: req.Parameters,
Secrets: data,
})
if err != nil {
klog.Errorf("Failed to enable volume replication: %v", err)
return nil, err
}

return &proto.EnableVolumeReplicationResponse{}, nil
}

// DisableVolumeReplication fetches required information from kubernetes cluster and calls
// CSI-Addons DisableVolumeReplication service.
func (rs *ReplicationServer) DisableVolumeReplication(
ctx context.Context,
req *proto.DisableVolumeReplicationRequest) (*proto.DisableVolumeReplicationResponse, error) {
// Get the secrets from the k8s cluster
data, err := kube.GetSecret(ctx, rs.kubeClient, req.GetSecretName(), req.GetSecretNamespace())
if err != nil {
klog.Errorf("Failed to get secret %s in namespace %s: %v", req.GetSecretName(), req.GetSecretNamespace(), err)
return nil, status.Error(codes.Internal, err.Error())
}

_, err = rs.controllerClient.DisableVolumeReplication(ctx,
&csiReplication.DisableVolumeReplicationRequest{
VolumeId: req.VolumeId,
ReplicationId: req.ReplicationId,
Parameters: req.Parameters,
Secrets: data,
})
if err != nil {
klog.Errorf("Failed to enable volume replication: %v", err)
return nil, err
}

return &proto.DisableVolumeReplicationResponse{}, nil
}

// PromoteVolume fetches required information from kubernetes cluster and calls
// CSI-Addons PromoteVolume service.
func (rs *ReplicationServer) PromoteVolume(
ctx context.Context,
req *proto.PromoteVolumeRequest) (*proto.PromoteVolumeResponse, error) {
// Get the secrets from the k8s cluster
data, err := kube.GetSecret(ctx, rs.kubeClient, req.GetSecretName(), req.GetSecretNamespace())
if err != nil {
klog.Errorf("Failed to get secret %s in namespace %s: %v", req.GetSecretName(), req.GetSecretNamespace(), err)
return nil, status.Error(codes.Internal, err.Error())
}

_, err = rs.controllerClient.PromoteVolume(ctx,
&csiReplication.PromoteVolumeRequest{
VolumeId: req.VolumeId,
ReplicationId: req.ReplicationId,
Force: req.Force,
Parameters: req.Parameters,
Secrets: data,
})
if err != nil {
klog.Errorf("Failed to enable volume replication: %v", err)
return nil, err
}

return &proto.PromoteVolumeResponse{}, nil
}

// DemoteVolume fetches required information from kubernetes cluster and calls
// CSI-Addons DemoteVolume service.
func (rs *ReplicationServer) DemoteVolume(
ctx context.Context,
req *proto.DemoteVolumeRequest) (*proto.DemoteVolumeResponse, error) {
// Get the secrets from the k8s cluster
data, err := kube.GetSecret(ctx, rs.kubeClient, req.GetSecretName(), req.GetSecretNamespace())
if err != nil {
klog.Errorf("Failed to get secret %s in namespace %s: %v", req.GetSecretName(), req.GetSecretNamespace(), err)
return nil, status.Error(codes.Internal, err.Error())
}

_, err = rs.controllerClient.DemoteVolume(ctx,
&csiReplication.DemoteVolumeRequest{
VolumeId: req.VolumeId,
ReplicationId: req.ReplicationId,
Force: req.Force,
Parameters: req.Parameters,
Secrets: data,
})
if err != nil {
klog.Errorf("Failed to enable volume replication: %v", err)
return nil, err
}

return &proto.DemoteVolumeResponse{}, nil
}

// ResyncVolume fetches required information from kubernetes cluster and calls
// CSI-Addons ResyncVolume service.
func (rs *ReplicationServer) ResyncVolume(
ctx context.Context,
req *proto.ResyncVolumeRequest) (*proto.ResyncVolumeResponse, error) {
// Get the secrets from the k8s cluster
data, err := kube.GetSecret(ctx, rs.kubeClient, req.GetSecretName(), req.GetSecretNamespace())
if err != nil {
klog.Errorf("Failed to get secret %s in namespace %s: %v", req.GetSecretName(), req.GetSecretNamespace(), err)
return nil, status.Error(codes.Internal, err.Error())
}

resp, err := rs.controllerClient.ResyncVolume(ctx,
&csiReplication.ResyncVolumeRequest{
VolumeId: req.VolumeId,
ReplicationId: req.ReplicationId,
Force: req.Force,
Parameters: req.Parameters,
Secrets: data,
})
if err != nil {
klog.Errorf("Failed to enable volume replication: %v", err)
return nil, err
}

return &proto.ResyncVolumeResponse{
Ready: resp.Ready,
}, nil
}
1 change: 1 addition & 0 deletions sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func main() {
sidecarServer.RegisterService(service.NewIdentityServer(csiClient.GetGRPCClient()))
sidecarServer.RegisterService(service.NewReclaimSpaceServer(csiClient.GetGRPCClient(), kubeClient, *stagingPath))
sidecarServer.RegisterService(service.NewNetworkFenceServer(csiClient.GetGRPCClient(), kubeClient))
sidecarServer.RegisterService(service.NewReplicationServer(csiClient.GetGRPCClient(), kubeClient))

sidecarServer.Start()
}
Loading

0 comments on commit cfb4fb9

Please sign in to comment.