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 (
|
2019-08-24 09:14:15 +00:00
|
|
|
"context"
|
2018-01-09 18:57:14 +00:00
|
|
|
"fmt"
|
2019-07-30 13:26:48 +00:00
|
|
|
"runtime/debug"
|
2018-01-09 18:57:14 +00:00
|
|
|
"strings"
|
2019-08-09 17:11:21 +00:00
|
|
|
"sync/atomic"
|
2018-01-09 18:57:14 +00:00
|
|
|
|
2020-04-17 09:23:49 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2020-04-15 03:38:16 +00:00
|
|
|
|
2018-11-26 18:23:56 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2019-01-15 16:20:41 +00:00
|
|
|
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
|
2018-01-09 18:57:14 +00:00
|
|
|
"google.golang.org/grpc"
|
2019-07-30 13:26:48 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2020-07-09 05:14:10 +00:00
|
|
|
klog "k8s.io/klog/v2"
|
2018-01-09 18:57:14 +00:00
|
|
|
)
|
|
|
|
|
2019-02-18 11:30:28 +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
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 11:30:28 +00:00
|
|
|
return "", "", fmt.Errorf("invalid endpoint: %v", ep)
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +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}
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
// NewDefaultNodeServer initializes default node server
|
2020-01-24 16:26:56 +00:00
|
|
|
func NewDefaultNodeServer(d *CSIDriver, t string, topology map[string]string) *DefaultNodeServer {
|
|
|
|
d.topology = topology
|
2018-01-09 18:57:14 +00:00
|
|
|
return &DefaultNodeServer{
|
|
|
|
Driver: d,
|
2019-07-30 06:20:28 +00:00
|
|
|
Type: t,
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
// NewDefaultIdentityServer initializes default identity servier
|
2018-01-09 18:57:14 +00:00
|
|
|
func NewDefaultIdentityServer(d *CSIDriver) *DefaultIdentityServer {
|
|
|
|
return &DefaultIdentityServer{
|
|
|
|
Driver: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
// NewDefaultControllerServer initializes default controller server
|
2018-01-09 18:57:14 +00:00
|
|
|
func NewDefaultControllerServer(d *CSIDriver) *DefaultControllerServer {
|
|
|
|
return &DefaultControllerServer{
|
|
|
|
Driver: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
// NewControllerServiceCapability returns controller capabilities
|
2019-06-10 06:48:41 +00:00
|
|
|
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{
|
2019-06-10 06:48:41 +00:00
|
|
|
Type: ctrlCap,
|
2018-01-09 18:57:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
// RunNodePublishServer starts node server
|
2019-08-21 09:28:02 +00:00
|
|
|
func RunNodePublishServer(endpoint, hstOption string, d *CSIDriver, ns csi.NodeServer, m bool) {
|
2018-01-09 18:57:14 +00:00
|
|
|
ids := NewDefaultIdentityServer(d)
|
|
|
|
|
|
|
|
s := NewNonBlockingGRPCServer()
|
2019-08-21 09:28:02 +00:00
|
|
|
s.Start(endpoint, hstOption, ids, nil, ns, m)
|
2018-01-09 18:57:14 +00:00
|
|
|
s.Wait()
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
// RunControllerPublishServer starts controller server
|
2019-08-21 09:28:02 +00:00
|
|
|
func RunControllerPublishServer(endpoint, hstOption string, d *CSIDriver, cs csi.ControllerServer, m bool) {
|
2018-01-09 18:57:14 +00:00
|
|
|
ids := NewDefaultIdentityServer(d)
|
|
|
|
|
|
|
|
s := NewNonBlockingGRPCServer()
|
2019-08-21 09:28:02 +00:00
|
|
|
s.Start(endpoint, hstOption, ids, cs, nil, m)
|
2018-01-09 18:57:14 +00:00
|
|
|
s.Wait()
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
// RunControllerandNodePublishServer starts both controller and node server
|
2019-08-21 09:28:02 +00:00
|
|
|
func RunControllerandNodePublishServer(endpoint, hstOption string, d *CSIDriver, cs csi.ControllerServer, ns csi.NodeServer, m bool) {
|
2018-01-09 18:57:14 +00:00
|
|
|
ids := NewDefaultIdentityServer(d)
|
|
|
|
|
|
|
|
s := NewNonBlockingGRPCServer()
|
2019-08-21 09:28:02 +00:00
|
|
|
s.Start(endpoint, hstOption, ids, cs, ns, m)
|
2018-01-09 18:57:14 +00:00
|
|
|
s.Wait()
|
|
|
|
}
|
|
|
|
|
2019-09-11 06:04:07 +00:00
|
|
|
func getReqID(req interface{}) string {
|
|
|
|
// if req is nil empty string will be returned
|
|
|
|
reqID := ""
|
|
|
|
switch r := req.(type) {
|
|
|
|
case *csi.CreateVolumeRequest:
|
|
|
|
reqID = r.Name
|
|
|
|
|
|
|
|
case *csi.DeleteVolumeRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
|
|
|
|
case *csi.CreateSnapshotRequest:
|
|
|
|
reqID = r.Name
|
|
|
|
case *csi.DeleteSnapshotRequest:
|
|
|
|
reqID = r.SnapshotId
|
|
|
|
|
|
|
|
case *csi.ControllerExpandVolumeRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
|
|
|
|
case *csi.NodeStageVolumeRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
case *csi.NodeUnstageVolumeRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
|
|
|
|
case *csi.NodePublishVolumeRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
case *csi.NodeUnpublishVolumeRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
|
|
|
|
case *csi.NodeExpandVolumeRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
}
|
|
|
|
return reqID
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:11:21 +00:00
|
|
|
var id uint64
|
|
|
|
|
|
|
|
func contextIDInjector(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
2019-08-14 18:45:30 +00:00
|
|
|
atomic.AddUint64(&id, 1)
|
2019-09-11 04:35:03 +00:00
|
|
|
ctx = context.WithValue(ctx, util.CtxKey, id)
|
2019-09-11 06:04:07 +00:00
|
|
|
reqID := getReqID(req)
|
|
|
|
if reqID != "" {
|
|
|
|
ctx = context.WithValue(ctx, util.ReqID, reqID)
|
|
|
|
}
|
2019-08-09 17:11:21 +00:00
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
2020-07-09 14:48:24 +00:00
|
|
|
util.ExtendedLog(ctx, "GRPC call: %s", info.FullMethod)
|
|
|
|
util.TraceLog(ctx, "GRPC request: %s", protosanitizer.StripSecrets(req))
|
2018-01-09 18:57:14 +00:00
|
|
|
resp, err := handler(ctx, req)
|
|
|
|
if err != nil {
|
2019-08-22 16:57:23 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "GRPC error: %v"), err)
|
2018-01-09 18:57:14 +00:00
|
|
|
} else {
|
2020-07-09 14:48:24 +00:00
|
|
|
util.TraceLog(ctx, "GRPC response: %s", protosanitizer.StripSecrets(resp))
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
2019-07-30 13:26:48 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|