ceph-csi/pkg/csi-common/utils.go

131 lines
3.9 KiB
Go
Raw Normal View History

2018-01-09 18:57:14 +00:00
/*
Copyright 2017 The Kubernetes 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 csicommon
import (
"fmt"
"runtime/debug"
2018-01-09 18:57:14 +00:00
"strings"
2018-11-26 18:23:56 +00:00
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
2018-01-09 18:57:14 +00:00
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog"
2018-01-09 18:57:14 +00:00
)
func parseEndpoint(ep string) (string, string, error) {
2018-01-09 18:57:14 +00:00
if strings.HasPrefix(strings.ToLower(ep), "unix://") || strings.HasPrefix(strings.ToLower(ep), "tcp://") {
s := strings.SplitN(ep, "://", 2)
if s[1] != "" {
return s[0], s[1], nil
}
}
return "", "", fmt.Errorf("invalid endpoint: %v", ep)
2018-01-09 18:57:14 +00:00
}
// NewVolumeCapabilityAccessMode returns volume access mode
2018-01-09 18:57:14 +00:00
func NewVolumeCapabilityAccessMode(mode csi.VolumeCapability_AccessMode_Mode) *csi.VolumeCapability_AccessMode {
return &csi.VolumeCapability_AccessMode{Mode: mode}
}
// NewDefaultNodeServer initializes default node server
func NewDefaultNodeServer(d *CSIDriver, t string) *DefaultNodeServer {
2018-01-09 18:57:14 +00:00
return &DefaultNodeServer{
Driver: d,
Type: t,
2018-01-09 18:57:14 +00:00
}
}
// NewDefaultIdentityServer initializes default identity servier
2018-01-09 18:57:14 +00:00
func NewDefaultIdentityServer(d *CSIDriver) *DefaultIdentityServer {
return &DefaultIdentityServer{
Driver: d,
}
}
// NewDefaultControllerServer initializes default controller server
2018-01-09 18:57:14 +00:00
func NewDefaultControllerServer(d *CSIDriver) *DefaultControllerServer {
return &DefaultControllerServer{
Driver: d,
}
}
// NewControllerServiceCapability returns controller capabilities
func NewControllerServiceCapability(ctrlCap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability {
2018-01-09 18:57:14 +00:00
return &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: ctrlCap,
2018-01-09 18:57:14 +00:00
},
},
}
}
// RunNodePublishServer starts node server
2018-01-09 18:57:14 +00:00
func RunNodePublishServer(endpoint string, d *CSIDriver, ns csi.NodeServer) {
ids := NewDefaultIdentityServer(d)
s := NewNonBlockingGRPCServer()
s.Start(endpoint, ids, nil, ns)
s.Wait()
}
// RunControllerPublishServer starts controller server
2018-01-09 18:57:14 +00:00
func RunControllerPublishServer(endpoint string, d *CSIDriver, cs csi.ControllerServer) {
ids := NewDefaultIdentityServer(d)
s := NewNonBlockingGRPCServer()
s.Start(endpoint, ids, cs, nil)
s.Wait()
}
// RunControllerandNodePublishServer starts both controller and node server
2018-01-09 18:57:14 +00:00
func RunControllerandNodePublishServer(endpoint string, d *CSIDriver, cs csi.ControllerServer, ns csi.NodeServer) {
ids := NewDefaultIdentityServer(d)
s := NewNonBlockingGRPCServer()
s.Start(endpoint, ids, cs, ns)
s.Wait()
}
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
klog.V(3).Infof("GRPC call: %s", info.FullMethod)
klog.V(5).Infof("GRPC request: %s", protosanitizer.StripSecrets(req))
2018-01-09 18:57:14 +00:00
resp, err := handler(ctx, req)
if err != nil {
klog.Errorf("GRPC error: %v", err)
2018-01-09 18:57:14 +00:00
} else {
klog.V(5).Infof("GRPC response: %s", protosanitizer.StripSecrets(resp))
2018-01-09 18:57:14 +00:00
}
return resp, err
}
func panicHandler(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
defer func() {
if r := recover(); r != nil {
klog.Errorf("panic occurred: %v", r)
debug.PrintStack()
err = status.Errorf(codes.Internal, "panic %v", r)
}
}()
return handler(ctx, req)
}