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-07-30 06:20:28 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2020-04-17 09:23:49 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2019-07-30 06:20:28 +00:00
|
|
|
|
2019-08-24 09:14:15 +00:00
|
|
|
"context"
|
|
|
|
|
2018-11-26 18:23:56 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2018-01-09 18:57:14 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2021-03-24 13:13:56 +00:00
|
|
|
"k8s.io/klog/v2"
|
2019-07-30 06:20:28 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
2018-01-09 18:57:14 +00:00
|
|
|
)
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// DefaultNodeServer stores driver object.
|
2018-01-09 18:57:14 +00:00
|
|
|
type DefaultNodeServer struct {
|
|
|
|
Driver *CSIDriver
|
2019-07-30 06:20:28 +00:00
|
|
|
Type string
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// NodeStageVolume returns unimplemented response.
|
2019-02-18 11:30:28 +00:00
|
|
|
func (ns *DefaultNodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
|
2018-01-09 18:57:14 +00:00
|
|
|
return nil, status.Error(codes.Unimplemented, "")
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// NodeUnstageVolume returns unimplemented response.
|
2019-02-18 11:30:28 +00:00
|
|
|
func (ns *DefaultNodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
|
2018-01-09 18:57:14 +00:00
|
|
|
return nil, status.Error(codes.Unimplemented, "")
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// NodeExpandVolume returns unimplemented response.
|
2019-04-03 07:57:13 +00:00
|
|
|
func (ns *DefaultNodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
|
|
|
|
return nil, status.Error(codes.Unimplemented, "")
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// NodeGetInfo returns node ID.
|
2018-07-18 14:47:22 +00:00
|
|
|
func (ns *DefaultNodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
|
2020-07-09 14:48:24 +00:00
|
|
|
util.TraceLog(ctx, "Using default NodeGetInfo")
|
2018-07-18 14:47:22 +00:00
|
|
|
|
2020-01-24 16:26:56 +00:00
|
|
|
csiTopology := &csi.Topology{
|
|
|
|
Segments: ns.Driver.topology,
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
return &csi.NodeGetInfoResponse{
|
2020-01-24 16:26:56 +00:00
|
|
|
NodeId: ns.Driver.nodeID,
|
|
|
|
AccessibleTopology: csiTopology,
|
2018-07-18 14:47:22 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 11:54:29 +00:00
|
|
|
// NodeGetCapabilities returns RPC unknown capability.
|
2018-01-09 18:57:14 +00:00
|
|
|
func (ns *DefaultNodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
|
2020-07-09 14:48:24 +00:00
|
|
|
util.TraceLog(ctx, "Using default NodeGetCapabilities")
|
2018-01-09 18:57:14 +00:00
|
|
|
|
2018-02-15 13:50:31 +00:00
|
|
|
return &csi.NodeGetCapabilitiesResponse{
|
|
|
|
Capabilities: []*csi.NodeServiceCapability{
|
|
|
|
{
|
|
|
|
Type: &csi.NodeServiceCapability_Rpc{
|
|
|
|
Rpc: &csi.NodeServiceCapability_RPC{
|
|
|
|
Type: csi.NodeServiceCapability_RPC_UNKNOWN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
2018-11-26 18:23:56 +00:00
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// NodeGetVolumeStats returns volume stats.
|
2019-07-30 06:20:28 +00:00
|
|
|
func (ns *DefaultNodeServer) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
|
|
|
|
var err error
|
|
|
|
targetPath := req.GetVolumePath()
|
|
|
|
if targetPath == "" {
|
|
|
|
err = fmt.Errorf("targetpath %v is empty", targetPath)
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
volID := req.GetVolumeId()
|
|
|
|
|
|
|
|
TODO: Map the volumeID to the targetpath.
|
|
|
|
|
|
|
|
CephFS:
|
|
|
|
we need secret to connect to the ceph cluster to get the volumeID from volume
|
|
|
|
Name, however `secret` field/option is not available in NodeGetVolumeStats spec,
|
|
|
|
Below issue covers this request and once its available, we can do the validation
|
|
|
|
as per the spec.
|
|
|
|
|
|
|
|
https://github.com/container-storage-interface/spec/issues/371
|
|
|
|
|
|
|
|
RBD:
|
|
|
|
Below issue covers this request for RBD and once its available, we can do the validation
|
|
|
|
as per the spec.
|
|
|
|
|
|
|
|
https://github.com/ceph/ceph-csi/issues/511
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
isMnt, err := util.IsMountPoint(targetPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2020-11-24 11:54:29 +00:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "targetpath %s does not exist", targetPath)
|
2019-07-30 06:20:28 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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 {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "failed to fetch available bytes"))
|
2019-07-30 06:20:28 +00:00
|
|
|
}
|
|
|
|
capacity, ok := (*(volMetrics.Capacity)).AsInt64()
|
|
|
|
if !ok {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "failed to fetch capacity bytes"))
|
2019-07-30 06:20:28 +00:00
|
|
|
return nil, status.Error(codes.Unknown, "failed to fetch capacity bytes")
|
|
|
|
}
|
|
|
|
used, ok := (*(volMetrics.Used)).AsInt64()
|
|
|
|
if !ok {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "failed to fetch used bytes"))
|
2019-07-30 06:20:28 +00:00
|
|
|
}
|
|
|
|
inodes, ok := (*(volMetrics.Inodes)).AsInt64()
|
|
|
|
if !ok {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "failed to fetch available inodes"))
|
2019-07-30 06:20:28 +00:00
|
|
|
return nil, status.Error(codes.Unknown, "failed to fetch available inodes")
|
|
|
|
}
|
|
|
|
inodesFree, ok := (*(volMetrics.InodesFree)).AsInt64()
|
|
|
|
if !ok {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "failed to fetch free inodes"))
|
2019-07-30 06:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inodesUsed, ok := (*(volMetrics.InodesUsed)).AsInt64()
|
|
|
|
if !ok {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(util.Log(ctx, "failed to fetch used inodes"))
|
2019-07-30 06:20:28 +00:00
|
|
|
}
|
|
|
|
return &csi.NodeGetVolumeStatsResponse{
|
|
|
|
Usage: []*csi.VolumeUsage{
|
|
|
|
{
|
|
|
|
Available: available,
|
|
|
|
Total: capacity,
|
|
|
|
Used: used,
|
2020-06-11 07:54:28 +00:00
|
|
|
Unit: csi.VolumeUsage_BYTES,
|
2019-07-30 06:20:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Available: inodesFree,
|
|
|
|
Total: inodes,
|
|
|
|
Used: inodesUsed,
|
2020-06-11 07:54:28 +00:00
|
|
|
Unit: csi.VolumeUsage_INODES,
|
2019-07-30 06:20:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-11-26 18:23:56 +00:00
|
|
|
}
|
2020-01-23 08:24:46 +00:00
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// ConstructMountOptions returns only unique mount options in slice.
|
2020-01-23 08:24:46 +00:00
|
|
|
func ConstructMountOptions(mountOptions []string, volCap *csi.VolumeCapability) []string {
|
|
|
|
if m := volCap.GetMount(); m != nil {
|
|
|
|
hasOption := func(options []string, opt string) bool {
|
|
|
|
for _, o := range options {
|
|
|
|
if o == opt {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, f := range m.MountFlags {
|
|
|
|
if !hasOption(mountOptions, f) {
|
|
|
|
mountOptions = append(mountOptions, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mountOptions
|
|
|
|
}
|
2020-04-16 14:47:43 +00:00
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// MountOptionContains checks the opt is present in mountOptions.
|
2020-04-16 14:47:43 +00:00
|
|
|
func MountOptionContains(mountOptions []string, opt string) bool {
|
|
|
|
for _, mnt := range mountOptions {
|
|
|
|
if mnt == opt {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|