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 (
|
2018-03-06 22:33:18 +00:00
|
|
|
"fmt"
|
2018-11-26 18:23:56 +00:00
|
|
|
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
|
|
|
|
2019-02-18 11:30:28 +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"
|
2018-01-09 18:57:14 +00:00
|
|
|
)
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// CSIDriver stores driver information.
|
2018-01-09 18:57:14 +00:00
|
|
|
type CSIDriver struct {
|
|
|
|
name string
|
|
|
|
nodeID string
|
2018-03-06 22:33:18 +00:00
|
|
|
version string
|
2020-01-24 16:26:56 +00:00
|
|
|
// topology constraints that this nodeserver will advertise
|
2020-10-21 07:32:19 +00:00
|
|
|
topology map[string]string
|
|
|
|
capabilities []*csi.ControllerServiceCapability
|
|
|
|
vc []*csi.VolumeCapability_AccessMode
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
// NewCSIDriver Creates a NewCSIDriver object. Assumes vendor
|
|
|
|
// version is equal to driver version & does not support optional
|
|
|
|
// driver plugin info manifest field. Refer to CSI spec for more details.
|
2019-06-10 06:48:41 +00:00
|
|
|
func NewCSIDriver(name, v, nodeID string) *CSIDriver {
|
2018-01-09 18:57:14 +00:00
|
|
|
if name == "" {
|
2019-02-18 11:30:28 +00:00
|
|
|
klog.Errorf("Driver name missing")
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodeID == "" {
|
2019-02-18 11:30:28 +00:00
|
|
|
klog.Errorf("NodeID missing")
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-03-06 22:33:18 +00:00
|
|
|
// TODO version format and validation
|
2019-06-10 06:48:41 +00:00
|
|
|
if v == "" {
|
2019-02-18 11:30:28 +00:00
|
|
|
klog.Errorf("Version argument missing")
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := CSIDriver{
|
|
|
|
name: name,
|
|
|
|
version: v,
|
|
|
|
nodeID: nodeID,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &driver
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
// ValidateControllerServiceRequest validates the controller
|
2020-07-19 12:21:03 +00:00
|
|
|
// plugin capabilities.
|
2018-03-06 22:33:18 +00:00
|
|
|
func (d *CSIDriver) ValidateControllerServiceRequest(c csi.ControllerServiceCapability_RPC_Type) error {
|
2018-01-09 18:57:14 +00:00
|
|
|
if c == csi.ControllerServiceCapability_RPC_UNKNOWN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-21 07:32:19 +00:00
|
|
|
for _, capability := range d.capabilities {
|
2020-10-19 07:31:11 +00:00
|
|
|
if c == capability.GetRpc().GetType() {
|
2018-01-09 18:57:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2023-06-02 08:59:52 +00:00
|
|
|
return status.Error(codes.InvalidArgument, fmt.Sprintf("%s", c))
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
// AddControllerServiceCapabilities stores the controller capabilities
|
2020-07-19 12:21:03 +00:00
|
|
|
// in driver object.
|
2018-01-09 18:57:14 +00:00
|
|
|
func (d *CSIDriver) AddControllerServiceCapabilities(cl []csi.ControllerServiceCapability_RPC_Type) {
|
2021-08-09 05:45:40 +00:00
|
|
|
csc := make([]*csi.ControllerServiceCapability, 0, len(cl))
|
2018-01-09 18:57:14 +00:00
|
|
|
|
|
|
|
for _, c := range cl {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DefaultLog("Enabling controller service capability: %v", c.String())
|
2018-01-09 18:57:14 +00:00
|
|
|
csc = append(csc, NewControllerServiceCapability(c))
|
|
|
|
}
|
|
|
|
|
2020-10-21 07:32:19 +00:00
|
|
|
d.capabilities = csc
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// AddVolumeCapabilityAccessModes stores volume access modes.
|
2021-06-25 11:20:31 +00:00
|
|
|
func (d *CSIDriver) AddVolumeCapabilityAccessModes(
|
2022-06-01 10:17:19 +00:00
|
|
|
vc []csi.VolumeCapability_AccessMode_Mode,
|
|
|
|
) []*csi.VolumeCapability_AccessMode {
|
2021-08-09 05:45:40 +00:00
|
|
|
vca := make([]*csi.VolumeCapability_AccessMode, 0, len(vc))
|
2018-01-09 18:57:14 +00:00
|
|
|
for _, c := range vc {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DefaultLog("Enabling volume access mode: %v", c.String())
|
2018-01-09 18:57:14 +00:00
|
|
|
vca = append(vca, NewVolumeCapabilityAccessMode(c))
|
|
|
|
}
|
|
|
|
d.vc = vca
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
return vca
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// GetVolumeCapabilityAccessModes returns access modes.
|
2018-01-09 18:57:14 +00:00
|
|
|
func (d *CSIDriver) GetVolumeCapabilityAccessModes() []*csi.VolumeCapability_AccessMode {
|
|
|
|
return d.vc
|
|
|
|
}
|