-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54838f3
commit 4b21e12
Showing
12 changed files
with
180 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package csicommon | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/container-storage-interface/spec/lib/go/csi" | ||
"google.golang.org/grpc" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
// Defines Non blocking GRPC server interfaces | ||
type NonBlockingGRPCServer interface { | ||
// Start services | ||
Start(endpoint string, ids csi.IdentityServer, cs csi.ControllerServer, ns csi.NodeServer) | ||
// Waits for the service to stop | ||
Wait() | ||
// Stops the service gracefully | ||
Stop() | ||
// Stops the service forcefully | ||
ForceStop() | ||
} | ||
|
||
func NewNonBlockingGRPCServer() NonBlockingGRPCServer { | ||
return &nonBlockingGRPCServer{} | ||
} | ||
|
||
// NonBlocking server | ||
type nonBlockingGRPCServer struct { | ||
wg sync.WaitGroup | ||
server *grpc.Server | ||
} | ||
|
||
func (s *nonBlockingGRPCServer) Start(endpoint string, ids csi.IdentityServer, cs csi.ControllerServer, ns csi.NodeServer) { | ||
s.wg.Add(1) | ||
go s.serve(endpoint, ids, cs, ns) | ||
} | ||
|
||
func (s *nonBlockingGRPCServer) Wait() { | ||
s.wg.Wait() | ||
} | ||
|
||
func (s *nonBlockingGRPCServer) Stop() { | ||
s.server.GracefulStop() | ||
} | ||
|
||
func (s *nonBlockingGRPCServer) ForceStop() { | ||
s.server.Stop() | ||
} | ||
|
||
func (s *nonBlockingGRPCServer) serve(endpoint string, ids csi.IdentityServer, cs csi.ControllerServer, ns csi.NodeServer) { | ||
proto, addr, err := parseEndpoint(endpoint) | ||
if err != nil { | ||
klog.Fatal(err.Error()) | ||
} | ||
|
||
if proto == "unix" { | ||
addr = "/" + addr | ||
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) { | ||
klog.Fatalf("Failed to remove %s, error: %s", addr, err.Error()) | ||
} | ||
} | ||
|
||
listener, err := net.Listen(proto, addr) | ||
if err != nil { | ||
klog.Fatalf("Failed to listen: %v", err) | ||
} | ||
|
||
opts := []grpc.ServerOption{ | ||
grpc.UnaryInterceptor(logGRPC), | ||
} | ||
server := grpc.NewServer(opts...) | ||
s.server = server | ||
|
||
if ids != nil { | ||
csi.RegisterIdentityServer(server, ids) | ||
} | ||
if cs != nil { | ||
csi.RegisterControllerServer(server, cs) | ||
} | ||
if ns != nil { | ||
csi.RegisterNodeServer(server, ns) | ||
} | ||
|
||
klog.Infof("Listening for connections on address: %#v", listener.Addr()) | ||
|
||
err = server.Serve(listener) | ||
if err != nil { | ||
klog.Fatalf("Failed to serve: %v", err) | ||
} | ||
} | ||
|
||
// Helper functions | ||
func parseEndpoint(ep string) (string, string, error) { | ||
if strings.HasPrefix(ep, "unix://") || strings.HasPrefix(ep, "tcp://") { | ||
s := strings.SplitN(ep, "://", 2) | ||
if s[1] != "" { | ||
return s[0], s[1], nil | ||
} | ||
} | ||
return "", "", fmt.Errorf("invalid endpoint: %v", ep) | ||
} | ||
|
||
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
klog.V(3).Infof("GRPC call: %s", info.FullMethod) | ||
return handler(ctx, req) | ||
} | ||
|
||
// CSIDriver object | ||
type CSIDriver struct { | ||
name string | ||
nodeID string | ||
version string | ||
volumeCapabilities []*csi.VolumeCapability_AccessMode | ||
controllerCapabilities []*csi.ControllerServiceCapability | ||
} | ||
|
||
func NewCSIDriver(name string, v string, nodeID string) *CSIDriver { | ||
driver := CSIDriver{ | ||
name: name, | ||
version: v, | ||
nodeID: nodeID, | ||
} | ||
return &driver | ||
} | ||
|
||
func (d *CSIDriver) AddControllerServiceCapabilities(cl []csi.ControllerServiceCapability_RPC_Type) { | ||
var csc []*csi.ControllerServiceCapability | ||
|
||
for _, c := range cl { | ||
csc = append(csc, NewControllerServiceCapability(c)) | ||
} | ||
|
||
d.controllerCapabilities = csc | ||
} | ||
|
||
func (d *CSIDriver) AddVolumeCapabilityAccessModes(vc []csi.VolumeCapability_AccessMode_Mode) { | ||
var vca []*csi.VolumeCapability_AccessMode | ||
for _, c := range vc { | ||
vca = append(vca, NewVolumeCapabilityAccessMode(c)) | ||
} | ||
d.volumeCapabilities = vca | ||
} | ||
|
||
func (d *CSIDriver) GetVolumeCapabilityAccessModes() []*csi.VolumeCapability_AccessMode { | ||
return d.volumeCapabilities | ||
} | ||
|
||
func NewVolumeCapabilityAccessMode(mode csi.VolumeCapability_AccessMode_Mode) *csi.VolumeCapability_AccessMode { | ||
return &csi.VolumeCapability_AccessMode{Mode: mode} | ||
} | ||
|
||
func NewControllerServiceCapability(cap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability { | ||
return &csi.ControllerServiceCapability{ | ||
Type: &csi.ControllerServiceCapability_Rpc{ | ||
Rpc: &csi.ControllerServiceCapability_RPC{ | ||
Type: cap, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters