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"
|
2021-01-11 08:02:41 +00:00
|
|
|
"os"
|
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"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
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"
|
2021-04-06 06:46:35 +00:00
|
|
|
rp "github.com/csi-addons/replication-lib-utils/protosanitizer"
|
|
|
|
"github.com/csi-addons/spec/lib/go/replication"
|
2021-12-21 17:54:13 +00:00
|
|
|
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
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"
|
2021-03-24 13:13:56 +00:00
|
|
|
"k8s.io/klog/v2"
|
2021-01-11 08:02:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
return "", "", fmt.Errorf("invalid endpoint: %v", ep)
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +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}
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +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
|
2021-07-22 05:45:17 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 10:49:37 +00:00
|
|
|
// NewDefaultIdentityServer initializes default identity server.
|
2018-01-09 18:57:14 +00:00
|
|
|
func NewDefaultIdentityServer(d *CSIDriver) *DefaultIdentityServer {
|
|
|
|
return &DefaultIdentityServer{
|
|
|
|
Driver: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// NewDefaultControllerServer initializes default controller server.
|
2018-01-09 18:57:14 +00:00
|
|
|
func NewDefaultControllerServer(d *CSIDriver) *DefaultControllerServer {
|
|
|
|
return &DefaultControllerServer{
|
|
|
|
Driver: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +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
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 08:20:09 +00:00
|
|
|
// Add replication request names to the list when we implement more API's.
|
|
|
|
func isReplicationRequest(req interface{}) bool {
|
|
|
|
isReplicationRequest := true
|
|
|
|
switch req.(type) {
|
|
|
|
case *replication.EnableVolumeReplicationRequest:
|
|
|
|
case *replication.DisableVolumeReplicationRequest:
|
|
|
|
case *replication.PromoteVolumeRequest:
|
|
|
|
case *replication.DemoteVolumeRequest:
|
|
|
|
case *replication.ResyncVolumeRequest:
|
|
|
|
default:
|
|
|
|
isReplicationRequest = false
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-31 08:20:09 +00:00
|
|
|
return isReplicationRequest
|
|
|
|
}
|
|
|
|
|
2021-12-21 17:54:13 +00:00
|
|
|
// NewMiddlewareServerOption creates a new grpc.ServerOption that configures a
|
|
|
|
// common format for log messages and other gRPC related handlers.
|
|
|
|
func NewMiddlewareServerOption(withMetrics bool) grpc.ServerOption {
|
|
|
|
middleWare := []grpc.UnaryServerInterceptor{contextIDInjector, logGRPC, panicHandler}
|
|
|
|
|
|
|
|
if withMetrics {
|
|
|
|
middleWare = append(middleWare, grpc_prometheus.UnaryServerInterceptor)
|
|
|
|
}
|
|
|
|
|
|
|
|
return grpc_middleware.WithUnaryServerChain(middleWare...)
|
|
|
|
}
|
|
|
|
|
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
|
2021-03-31 08:20:09 +00:00
|
|
|
|
|
|
|
case *replication.EnableVolumeReplicationRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
case *replication.DisableVolumeReplicationRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
|
|
|
|
case *replication.PromoteVolumeRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
case *replication.DemoteVolumeRequest:
|
|
|
|
reqID = r.VolumeId
|
|
|
|
|
|
|
|
case *replication.ResyncVolumeRequest:
|
|
|
|
reqID = r.VolumeId
|
2019-09-11 06:04:07 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-09-11 06:04:07 +00:00
|
|
|
return reqID
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:11:21 +00:00
|
|
|
var id uint64
|
|
|
|
|
2021-06-25 11:20:31 +00:00
|
|
|
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)
|
2021-08-24 15:03:25 +00:00
|
|
|
ctx = context.WithValue(ctx, log.CtxKey, id)
|
2021-05-06 09:49:27 +00:00
|
|
|
if reqID := getReqID(req); reqID != "" {
|
2021-08-24 15:03:25 +00:00
|
|
|
ctx = context.WithValue(ctx, log.ReqID, reqID)
|
2019-09-11 06:04:07 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-08-09 17:11:21 +00:00
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
|
2021-06-25 11:20:31 +00:00
|
|
|
func logGRPC(
|
|
|
|
ctx context.Context,
|
|
|
|
req interface{},
|
|
|
|
info *grpc.UnaryServerInfo,
|
|
|
|
handler grpc.UnaryHandler) (interface{}, error) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ExtendedLog(ctx, "GRPC call: %s", info.FullMethod)
|
2021-03-31 08:20:09 +00:00
|
|
|
if isReplicationRequest(req) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.TraceLog(ctx, "GRPC request: %s", rp.StripReplicationSecrets(req))
|
2021-03-31 08:20:09 +00:00
|
|
|
} else {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.TraceLog(ctx, "GRPC request: %s", protosanitizer.StripSecrets(req))
|
2021-03-31 08:20:09 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
resp, err := handler(ctx, req)
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
klog.Errorf(log.Log(ctx, "GRPC error: %v"), err)
|
2018-01-09 18:57:14 +00:00
|
|
|
} else {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.TraceLog(ctx, "GRPC response: %s", protosanitizer.StripSecrets(resp))
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
2019-07-30 13:26:48 +00:00
|
|
|
|
2021-06-25 11:20:31 +00:00
|
|
|
func panicHandler(
|
|
|
|
ctx context.Context,
|
|
|
|
req interface{},
|
|
|
|
info *grpc.UnaryServerInfo,
|
|
|
|
handler grpc.UnaryHandler) (resp interface{}, err error) {
|
2019-07-30 13:26:48 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
klog.Errorf("panic occurred: %v", r)
|
|
|
|
debug.PrintStack()
|
|
|
|
err = status.Errorf(codes.Internal, "panic %v", r)
|
|
|
|
}
|
|
|
|
}()
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-07-30 13:26:48 +00:00
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
2021-01-11 08:02:41 +00:00
|
|
|
|
|
|
|
// FilesystemNodeGetVolumeStats can be used for getting the metrics as
|
|
|
|
// requested by the NodeGetVolumeStats CSI procedure.
|
|
|
|
// It is shared for FileMode volumes, both the CephFS and RBD NodeServers call
|
|
|
|
// this.
|
|
|
|
func FilesystemNodeGetVolumeStats(ctx context.Context, targetPath string) (*csi.NodeGetVolumeStatsResponse, error) {
|
|
|
|
isMnt, err := util.IsMountPoint(targetPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "targetpath %s does not exist", targetPath)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-07 05:35:08 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2021-01-11 08:02:41 +00:00
|
|
|
}
|
|
|
|
if !isMnt {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "targetpath %s is not mounted", targetPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
cephMetricsProvider := volume.NewMetricsStatFS(targetPath)
|
|
|
|
volMetrics, volMetErr := cephMetricsProvider.GetMetrics()
|
|
|
|
if volMetErr != nil {
|
|
|
|
return nil, status.Error(codes.Internal, volMetErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
available, ok := (*(volMetrics.Available)).AsInt64()
|
|
|
|
if !ok {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to fetch available bytes")
|
2021-01-11 08:02:41 +00:00
|
|
|
}
|
|
|
|
capacity, ok := (*(volMetrics.Capacity)).AsInt64()
|
|
|
|
if !ok {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to fetch capacity bytes")
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-01-11 08:02:41 +00:00
|
|
|
return nil, status.Error(codes.Unknown, "failed to fetch capacity bytes")
|
|
|
|
}
|
|
|
|
used, ok := (*(volMetrics.Used)).AsInt64()
|
|
|
|
if !ok {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to fetch used bytes")
|
2021-01-11 08:02:41 +00:00
|
|
|
}
|
|
|
|
inodes, ok := (*(volMetrics.Inodes)).AsInt64()
|
|
|
|
if !ok {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to fetch available inodes")
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-01-11 08:02:41 +00:00
|
|
|
return nil, status.Error(codes.Unknown, "failed to fetch available inodes")
|
|
|
|
}
|
|
|
|
inodesFree, ok := (*(volMetrics.InodesFree)).AsInt64()
|
|
|
|
if !ok {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to fetch free inodes")
|
2021-01-11 08:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inodesUsed, ok := (*(volMetrics.InodesUsed)).AsInt64()
|
|
|
|
if !ok {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to fetch used inodes")
|
2021-01-11 08:02:41 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-01-11 08:02:41 +00:00
|
|
|
return &csi.NodeGetVolumeStatsResponse{
|
|
|
|
Usage: []*csi.VolumeUsage{
|
|
|
|
{
|
2021-10-19 12:07:40 +00:00
|
|
|
Available: requirePositive(available),
|
|
|
|
Total: requirePositive(capacity),
|
|
|
|
Used: requirePositive(used),
|
2021-01-11 08:02:41 +00:00
|
|
|
Unit: csi.VolumeUsage_BYTES,
|
|
|
|
},
|
|
|
|
{
|
2021-10-19 12:07:40 +00:00
|
|
|
Available: requirePositive(inodesFree),
|
|
|
|
Total: requirePositive(inodes),
|
|
|
|
Used: requirePositive(inodesUsed),
|
2021-01-11 08:02:41 +00:00
|
|
|
Unit: csi.VolumeUsage_INODES,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2021-10-19 12:07:40 +00:00
|
|
|
|
|
|
|
// requirePositive returns the value for `x` when it is greater or equal to 0,
|
|
|
|
// or returns 0 in the acse `x` is negative.
|
|
|
|
//
|
|
|
|
// This is used for VolumeUsage entries in the NodeGetVolumeStatsResponse. The
|
|
|
|
// CSI spec does not allow negative values in the VolumeUsage objects.
|
|
|
|
func requirePositive(x int64) int64 {
|
|
|
|
if x >= 0 {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
2021-12-09 16:23:02 +00:00
|
|
|
|
|
|
|
// IsBlockMultiNode checks the volume capabilities for BlockMode and MultiNode.
|
|
|
|
func IsBlockMultiNode(caps []*csi.VolumeCapability) (bool, bool) {
|
|
|
|
isMultiNode := false
|
|
|
|
isBlock := false
|
|
|
|
for _, capability := range caps {
|
|
|
|
if capability.GetAccessMode().GetMode() == csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER {
|
|
|
|
isMultiNode = true
|
|
|
|
}
|
|
|
|
if capability.GetBlock() != nil {
|
|
|
|
isBlock = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isBlock, isMultiNode
|
|
|
|
}
|