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"
|
2024-09-17 13:52:30 +00:00
|
|
|
"time"
|
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"
|
2024-02-13 07:00:21 +00:00
|
|
|
"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"
|
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"
|
2022-07-18 16:13:36 +00:00
|
|
|
mount "k8s.io/mount-utils"
|
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.
|
2023-11-17 06:29:00 +00:00
|
|
|
func NewDefaultNodeServer(
|
|
|
|
d *CSIDriver, t, cliReadAffinityMapOptions string,
|
|
|
|
topology, nodeLabels map[string]string,
|
|
|
|
) *DefaultNodeServer {
|
2020-01-24 16:26:56 +00:00
|
|
|
d.topology = topology
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
return &DefaultNodeServer{
|
2023-11-17 06:29:00 +00:00
|
|
|
Driver: d,
|
|
|
|
Type: t,
|
|
|
|
Mounter: mount.NewWithoutSystemd(""),
|
|
|
|
NodeLabels: nodeLabels,
|
|
|
|
CLIReadAffinityOptions: cliReadAffinityMapOptions,
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 08:29:56 +00:00
|
|
|
// NewGroupControllerServiceCapability returns group controller capabilities.
|
|
|
|
func NewGroupControllerServiceCapability(ctrlCap csi.GroupControllerServiceCapability_RPC_Type,
|
|
|
|
) *csi.GroupControllerServiceCapability {
|
|
|
|
return &csi.GroupControllerServiceCapability{
|
|
|
|
Type: &csi.GroupControllerServiceCapability_Rpc{
|
|
|
|
Rpc: &csi.GroupControllerServiceCapability_RPC{
|
|
|
|
Type: ctrlCap,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-17 13:52:30 +00:00
|
|
|
// MiddlewareServerOptionConfig contains configuration parameters
|
|
|
|
// that are passed to the respective middleware interceptors that
|
|
|
|
// are instantiated when starting gRPC servers.
|
|
|
|
type MiddlewareServerOptionConfig struct {
|
|
|
|
LogSlowOpInterval time.Duration
|
|
|
|
}
|
|
|
|
|
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.
|
2024-09-17 13:52:30 +00:00
|
|
|
func NewMiddlewareServerOption(config MiddlewareServerOptionConfig) grpc.ServerOption {
|
|
|
|
middleWare := []grpc.UnaryServerInterceptor{
|
|
|
|
contextIDInjector,
|
|
|
|
logGRPC,
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.LogSlowOpInterval > 0 {
|
|
|
|
middleWare = append(middleWare, func(
|
|
|
|
ctx context.Context,
|
|
|
|
req interface{},
|
|
|
|
info *grpc.UnaryServerInfo,
|
|
|
|
handler grpc.UnaryHandler,
|
|
|
|
) (interface{}, error) {
|
|
|
|
return logSlowGRPC(
|
|
|
|
config.LogSlowOpInterval, ctx, req, info, handler,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
middleWare = append(middleWare, panicHandler)
|
2021-12-21 17:54:13 +00:00
|
|
|
|
2023-05-09 07:54:10 +00:00
|
|
|
return grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(middleWare...))
|
2021-12-21 17:54:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 11:00:34 +00:00
|
|
|
// GetIDFromReplication returns the volumeID for Replication.
|
|
|
|
func GetIDFromReplication(req interface{}) string {
|
|
|
|
getID := func(r interface {
|
|
|
|
GetVolumeId() string
|
|
|
|
GetReplicationSource() *replication.ReplicationSource
|
|
|
|
},
|
|
|
|
) string {
|
|
|
|
reqID := ""
|
|
|
|
src := r.GetReplicationSource()
|
|
|
|
if src != nil && src.GetVolume() != nil {
|
|
|
|
reqID = src.GetVolume().GetVolumeId()
|
|
|
|
}
|
2024-07-30 14:54:39 +00:00
|
|
|
if reqID == "" {
|
|
|
|
if src != nil && src.GetVolumegroup() != nil {
|
|
|
|
reqID = src.GetVolumegroup().GetVolumeGroupId()
|
|
|
|
}
|
|
|
|
}
|
2024-06-24 11:00:34 +00:00
|
|
|
if reqID == "" {
|
|
|
|
reqID = r.GetVolumeId() //nolint:nolintlint,staticcheck // req.VolumeId is deprecated
|
|
|
|
}
|
|
|
|
|
|
|
|
return reqID
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r := req.(type) {
|
|
|
|
case *replication.EnableVolumeReplicationRequest:
|
|
|
|
return getID(r)
|
|
|
|
case *replication.DisableVolumeReplicationRequest:
|
|
|
|
return getID(r)
|
|
|
|
case *replication.PromoteVolumeRequest:
|
|
|
|
return getID(r)
|
|
|
|
case *replication.DemoteVolumeRequest:
|
|
|
|
return getID(r)
|
|
|
|
case *replication.ResyncVolumeRequest:
|
|
|
|
return getID(r)
|
|
|
|
case *replication.GetVolumeReplicationInfoRequest:
|
|
|
|
return getID(r)
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetName()
|
2019-09-11 06:04:07 +00:00
|
|
|
|
|
|
|
case *csi.DeleteVolumeRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetVolumeId()
|
2019-09-11 06:04:07 +00:00
|
|
|
|
|
|
|
case *csi.CreateSnapshotRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetName()
|
2019-09-11 06:04:07 +00:00
|
|
|
case *csi.DeleteSnapshotRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetSnapshotId()
|
2019-09-11 06:04:07 +00:00
|
|
|
|
|
|
|
case *csi.ControllerExpandVolumeRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetVolumeId()
|
2019-09-11 06:04:07 +00:00
|
|
|
|
|
|
|
case *csi.NodeStageVolumeRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetVolumeId()
|
2019-09-11 06:04:07 +00:00
|
|
|
case *csi.NodeUnstageVolumeRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetVolumeId()
|
2019-09-11 06:04:07 +00:00
|
|
|
|
|
|
|
case *csi.NodePublishVolumeRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetVolumeId()
|
2019-09-11 06:04:07 +00:00
|
|
|
case *csi.NodeUnpublishVolumeRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetVolumeId()
|
2019-09-11 06:04:07 +00:00
|
|
|
|
|
|
|
case *csi.NodeExpandVolumeRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetVolumeId()
|
2024-02-05 08:44:58 +00:00
|
|
|
|
|
|
|
case *csi.CreateVolumeGroupSnapshotRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetName()
|
2024-02-05 08:44:58 +00:00
|
|
|
case *csi.DeleteVolumeGroupSnapshotRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetGroupSnapshotId()
|
2024-02-05 08:44:58 +00:00
|
|
|
case *csi.GetVolumeGroupSnapshotRequest:
|
2024-04-04 08:55:49 +00:00
|
|
|
reqID = r.GetGroupSnapshotId()
|
2024-02-13 07:00:21 +00:00
|
|
|
|
|
|
|
// Replication
|
|
|
|
case *replication.EnableVolumeReplicationRequest:
|
2024-06-24 11:00:34 +00:00
|
|
|
reqID = GetIDFromReplication(r)
|
2024-02-13 07:00:21 +00:00
|
|
|
case *replication.DisableVolumeReplicationRequest:
|
2024-06-24 11:00:34 +00:00
|
|
|
reqID = GetIDFromReplication(r)
|
2024-02-13 07:00:21 +00:00
|
|
|
case *replication.PromoteVolumeRequest:
|
2024-06-24 11:00:34 +00:00
|
|
|
reqID = GetIDFromReplication(r)
|
2024-02-13 07:00:21 +00:00
|
|
|
case *replication.DemoteVolumeRequest:
|
2024-06-24 11:00:34 +00:00
|
|
|
reqID = GetIDFromReplication(r)
|
2024-02-13 07:00:21 +00:00
|
|
|
case *replication.ResyncVolumeRequest:
|
2024-06-24 11:00:34 +00:00
|
|
|
reqID = GetIDFromReplication(r)
|
2024-02-13 07:00:21 +00:00
|
|
|
case *replication.GetVolumeReplicationInfoRequest:
|
2024-06-24 11:00:34 +00:00
|
|
|
reqID = GetIDFromReplication(r)
|
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,
|
2022-06-01 10:17:19 +00:00
|
|
|
handler grpc.UnaryHandler,
|
|
|
|
) (interface{}, 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,
|
2022-06-01 10:17:19 +00:00
|
|
|
handler grpc.UnaryHandler,
|
|
|
|
) (interface{}, error) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ExtendedLog(ctx, "GRPC call: %s", info.FullMethod)
|
2023-11-03 08:49:08 +00:00
|
|
|
log.TraceLog(ctx, "GRPC request: %s", protosanitizer.StripSecrets(req))
|
2022-08-29 06:59:22 +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
|
|
|
|
2024-09-17 13:52:30 +00:00
|
|
|
func logSlowGRPC(
|
|
|
|
logInterval time.Duration,
|
|
|
|
ctx context.Context,
|
|
|
|
req interface{},
|
|
|
|
info *grpc.UnaryServerInfo,
|
|
|
|
handler grpc.UnaryHandler,
|
|
|
|
) (interface{}, error) {
|
|
|
|
handlerFinished := make(chan struct{})
|
|
|
|
callStartTime := time.Now()
|
|
|
|
|
|
|
|
// Ticks at a logInterval rate and logs a slow-call message until handler finishes.
|
|
|
|
// This is called once the handler outlives its context, see below.
|
|
|
|
doLogSlowGRPC := func() {
|
|
|
|
ticker := time.NewTicker(logInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case t := <-ticker.C:
|
|
|
|
timePassed := t.Sub(callStartTime).Truncate(time.Second)
|
|
|
|
log.ExtendedLog(ctx,
|
|
|
|
"Slow GRPC call %s (%s)", info.FullMethod, timePassed)
|
|
|
|
log.TraceLog(ctx,
|
|
|
|
"Slow GRPC request: %s", protosanitizer.StripSecrets(req))
|
|
|
|
case <-handlerFinished:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// The call (most likely) outlived its context. Start logging slow messages.
|
|
|
|
doLogSlowGRPC()
|
|
|
|
case <-handlerFinished:
|
|
|
|
// The call finished, exit.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
resp, err := handler(ctx, req)
|
|
|
|
close(handlerFinished)
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:17:19 +00:00
|
|
|
//nolint:nonamedreturns // named return used to send recovered panic error.
|
2021-06-25 11:20:31 +00:00
|
|
|
func panicHandler(
|
|
|
|
ctx context.Context,
|
|
|
|
req interface{},
|
|
|
|
info *grpc.UnaryServerInfo,
|
2022-06-01 10:17:19 +00:00
|
|
|
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.
|
2022-07-18 16:13:36 +00:00
|
|
|
func FilesystemNodeGetVolumeStats(
|
|
|
|
ctx context.Context,
|
|
|
|
mounter mount.Interface,
|
|
|
|
targetPath string,
|
2022-10-03 15:46:52 +00:00
|
|
|
includeInodes bool,
|
2022-07-18 16:13:36 +00:00
|
|
|
) (*csi.NodeGetVolumeStatsResponse, error) {
|
|
|
|
isMnt, err := util.IsMountPoint(mounter, targetPath)
|
2021-01-11 08:02:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-03 15:46:52 +00:00
|
|
|
res := &csi.NodeGetVolumeStatsResponse{
|
2021-01-11 08:02:41 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
2022-10-03 15:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if includeInodes {
|
|
|
|
inodes, ok := (*(volMetrics.Inodes)).AsInt64()
|
|
|
|
if !ok {
|
|
|
|
log.ErrorLog(ctx, "failed to fetch available inodes")
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Unknown, "failed to fetch available inodes")
|
|
|
|
}
|
|
|
|
inodesFree, ok := (*(volMetrics.InodesFree)).AsInt64()
|
|
|
|
if !ok {
|
|
|
|
log.ErrorLog(ctx, "failed to fetch free inodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
inodesUsed, ok := (*(volMetrics.InodesUsed)).AsInt64()
|
|
|
|
if !ok {
|
|
|
|
log.ErrorLog(ctx, "failed to fetch used inodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Usage = append(res.Usage, &csi.VolumeUsage{
|
|
|
|
Available: requirePositive(inodesFree),
|
|
|
|
Total: requirePositive(inodes),
|
|
|
|
Used: requirePositive(inodesUsed),
|
|
|
|
Unit: csi.VolumeUsage_INODES,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-17 11:55:38 +00:00
|
|
|
// include marker for a healthy volume by default
|
|
|
|
res.VolumeCondition = &csi.VolumeCondition{
|
|
|
|
Abnormal: false,
|
|
|
|
Message: "volume is in a healthy condition",
|
|
|
|
}
|
|
|
|
|
2022-10-03 15:46:52 +00:00
|
|
|
return res, nil
|
2021-01-11 08:02:41 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
2021-10-26 07:49:10 +00:00
|
|
|
|
|
|
|
// IsFileRWO checks if it is of type RWO and file mode, if it is return value
|
|
|
|
// will be set to true.
|
|
|
|
func IsFileRWO(caps []*csi.VolumeCapability) bool {
|
|
|
|
// the return value has been set to true, if the volume is of file mode and if the capabilities are of RWO
|
|
|
|
// kind, ie SINGLE NODE but flexible to have one or more writers. This is also used as a validation in caller
|
|
|
|
// to preserve the backward compatibility we had with file mode RWO volumes.
|
|
|
|
|
|
|
|
// to preserve backward compatibility we allow RWO filemode, ideally SINGLE_NODE_WRITER check is good enough,
|
|
|
|
// however more granular level check could help us in future, so keeping it here as an additional measure.
|
|
|
|
for _, cap := range caps {
|
2024-04-04 08:55:49 +00:00
|
|
|
if cap.GetAccessMode() != nil {
|
2021-10-26 07:49:10 +00:00
|
|
|
if cap.GetMount() != nil {
|
2024-04-04 08:55:49 +00:00
|
|
|
switch cap.GetAccessMode().GetMode() { //nolint:exhaustive // only check what we want
|
2021-10-26 07:49:10 +00:00
|
|
|
case csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
|
|
|
|
csi.VolumeCapability_AccessMode_SINGLE_NODE_MULTI_WRITER,
|
|
|
|
csi.VolumeCapability_AccessMode_SINGLE_NODE_SINGLE_WRITER:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsReaderOnly check and set return value true only when the access mode is `READER ONLY` regardless of file
|
|
|
|
// or block mode.
|
|
|
|
func IsReaderOnly(caps []*csi.VolumeCapability) bool {
|
|
|
|
for _, cap := range caps {
|
2024-04-04 08:55:49 +00:00
|
|
|
if cap.GetAccessMode() != nil {
|
|
|
|
switch cap.GetAccessMode().GetMode() { //nolint:exhaustive // only check what we want
|
2021-10-26 07:49:10 +00:00
|
|
|
case csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY,
|
|
|
|
csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsBlockMultiWriter validates the volume capability slice against the access modes and access type.
|
|
|
|
// if the capability is of multi write the first return value will be set to true and if the request
|
|
|
|
// is of type block, the second return value will be set to true.
|
|
|
|
func IsBlockMultiWriter(caps []*csi.VolumeCapability) (bool, bool) {
|
|
|
|
// multiWriter has been set and returned after validating multi writer caps regardless of
|
|
|
|
// single or multi node access mode. The caps check is agnostic to whether it is a filesystem or block
|
|
|
|
// mode volume.
|
|
|
|
var multiWriter bool
|
|
|
|
|
|
|
|
// block has been set and returned if the passed in capability is of block volume mode.
|
|
|
|
var block bool
|
|
|
|
|
|
|
|
for _, cap := range caps {
|
2024-04-04 08:55:49 +00:00
|
|
|
if cap.GetAccessMode() != nil {
|
|
|
|
switch cap.GetAccessMode().GetMode() { //nolint:exhaustive // only check what we want
|
2021-10-26 07:49:10 +00:00
|
|
|
case csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
|
|
|
|
csi.VolumeCapability_AccessMode_SINGLE_NODE_MULTI_WRITER:
|
|
|
|
multiWriter = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cap.GetBlock() != nil {
|
|
|
|
block = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multiWriter, block
|
|
|
|
}
|