Skip to content

Commit

Permalink
Removed csi-comman package dependency
Browse files Browse the repository at this point in the history
csi-common package is part of kuberntes-csi/drivers which is deprecated and
unmaintained. The main parts used from csi-common are default
{node,identity,controller}server implementations and NonBlockingGRPCServer.

Default server implementations can be integrated to hostpath driver code, this
makes the driver code simple and clean. Lifted over the NonBlockingGRPCServer
code and simplified as per the driver needs.

Signed-off-by: Amarnath Valluri <[email protected]>
  • Loading branch information
avalluri committed Feb 12, 2019
1 parent a5de831 commit 6ee5eeb
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 700 deletions.
10 changes: 1 addition & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions cmd/hostpathplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"fmt"
"os"

"github.com/kubernetes-csi/csi-driver-host-path/pkg/hostpath"
Expand All @@ -41,6 +42,10 @@ func main() {
}

func handle() {
driver := hostpath.GetHostPathDriver()
driver.Run(*driverName, *nodeID, *endpoint)
driver, err := hostpath.NewHostPathDriver(*driverName, *nodeID, *endpoint)
if err != nil {
fmt.Printf("Failed to initialize driver: %s", err.Error())
os.Exit(1)
}
driver.Run()
}
78 changes: 70 additions & 8 deletions pkg/hostpath/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"google.golang.org/grpc/status"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/drivers/pkg/csi-common"
utilexec "k8s.io/utils/exec"
)

Expand All @@ -44,11 +43,22 @@ const (
)

type controllerServer struct {
*csicommon.DefaultControllerServer
caps []*csi.ControllerServiceCapability
}

func NewControllerServer() *controllerServer {
return &controllerServer{
caps: getControllerServiceCapabilities(
[]csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
}),
}
}

func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
if err := cs.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
glog.V(3).Infof("invalid create volume req: %v", req)
return nil, err
}
Expand Down Expand Up @@ -134,7 +144,7 @@ func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}

if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
if err := cs.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
glog.V(3).Infof("invalid delete volume req: %v", req)
return nil, err
}
Expand All @@ -146,14 +156,36 @@ func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
return &csi.DeleteVolumeResponse{}, nil
}

func (cs *controllerServer) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
return &csi.ControllerGetCapabilitiesResponse{
Capabilities: cs.caps,
}, nil
}

func (cs *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
return cs.DefaultControllerServer.ValidateVolumeCapabilities(ctx, req)
return nil, status.Error(codes.Unimplemented, "")
}

func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}

func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}

func (cs *controllerServer) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}

func (cs *controllerServer) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}

// CreateSnapshot uses tar command to create snapshot for hostpath volume. The tar command can quickly create
// archives of entire directories. The host image must have "tar" binaries in /bin, /usr/sbin, or /usr/bin.
func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT); err != nil {
if err := cs.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT); err != nil {
glog.V(3).Infof("invalid create snapshot req: %v", req)
return nil, err
}
Expand Down Expand Up @@ -232,7 +264,7 @@ func (cs *controllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
return nil, status.Error(codes.InvalidArgument, "Snapshot ID missing in request")
}

if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT); err != nil {
if err := cs.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT); err != nil {
glog.V(3).Infof("invalid delete snapshot req: %v", req)
return nil, err
}
Expand All @@ -245,7 +277,7 @@ func (cs *controllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
}

func (cs *controllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS); err != nil {
if err := cs.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS); err != nil {
glog.V(3).Infof("invalid list snapshot req: %v", req)
return nil, err
}
Expand Down Expand Up @@ -365,3 +397,33 @@ func convertSnapshot(snap hostPathSnapshot) *csi.ListSnapshotsResponse {

return rsp
}

func (cs *controllerServer) validateControllerServiceRequest(c csi.ControllerServiceCapability_RPC_Type) error {
if c == csi.ControllerServiceCapability_RPC_UNKNOWN {
return nil
}

for _, cap := range cs.caps {
if c == cap.GetRpc().GetType() {
return nil
}
}
return status.Error(codes.InvalidArgument, fmt.Sprintf("%s", c))
}

func getControllerServiceCapabilities(cl []csi.ControllerServiceCapability_RPC_Type) []*csi.ControllerServiceCapability {
var csc []*csi.ControllerServiceCapability

for _, cap := range cl {
glog.Infof("Enabling controller service capability: %v", cap.String())
csc = append(csc, &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: cap,
},
},
})
}

return csc
}
68 changes: 26 additions & 42 deletions pkg/hostpath/hostpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ package hostpath
import (
"fmt"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/glog"

timestamp "github.com/golang/protobuf/ptypes/timestamp"
"github.com/kubernetes-csi/drivers/pkg/csi-common"
)

const (
Expand All @@ -36,14 +34,14 @@ const (
)

type hostPath struct {
driver *csicommon.CSIDriver
name string
nodeID string
version string
endpoint string

ids *identityServer
ns *nodeServer
cs *controllerServer

cap []*csi.VolumeCapability_AccessMode
cscap []*csi.ControllerServiceCapability
}

type hostPathVolume struct {
Expand All @@ -67,61 +65,47 @@ var hostPathVolumes map[string]hostPathVolume
var hostPathVolumeSnapshots map[string]hostPathSnapshot

var (
hostPathDriver *hostPath
vendorVersion = "dev"
vendorVersion = "dev"
)

func init() {
hostPathVolumes = map[string]hostPathVolume{}
hostPathVolumeSnapshots = map[string]hostPathSnapshot{}
}

func GetHostPathDriver() *hostPath {
return &hostPath{}
}

func NewIdentityServer(d *csicommon.CSIDriver) *identityServer {
return &identityServer{
DefaultIdentityServer: csicommon.NewDefaultIdentityServer(d),
func NewHostPathDriver(driverName, nodeID, endpoint string) (*hostPath, error) {
if driverName == "" {
return nil, fmt.Errorf("No driver name provided")
}
}

func NewControllerServer(d *csicommon.CSIDriver) *controllerServer {
return &controllerServer{
DefaultControllerServer: csicommon.NewDefaultControllerServer(d),
if nodeID == "" {
return nil, fmt.Errorf("No node id provided")
}
}

func NewNodeServer(d *csicommon.CSIDriver) *nodeServer {
return &nodeServer{
DefaultNodeServer: csicommon.NewDefaultNodeServer(d),
if endpoint == "" {
return nil, fmt.Errorf("No driver endpoint provided")
}
}

func (hp *hostPath) Run(driverName, nodeID, endpoint string) {
glog.Infof("Driver: %v ", driverName)
glog.Infof("Version: %s", vendorVersion)

// Initialize default library driver
hp.driver = csicommon.NewCSIDriver(driverName, vendorVersion, nodeID)
if hp.driver == nil {
glog.Fatalln("Failed to initialize CSI Driver.")
}
hp.driver.AddControllerServiceCapabilities(
[]csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
})
hp.driver.AddVolumeCapabilityAccessModes([]csi.VolumeCapability_AccessMode_Mode{csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER})
return &hostPath{
name: driverName,
version: vendorVersion,
nodeID: nodeID,
endpoint: endpoint,
}, nil
}

func (hp *hostPath) Run() {

// Create GRPC servers
hp.ids = NewIdentityServer(hp.driver)
hp.ns = NewNodeServer(hp.driver)
hp.cs = NewControllerServer(hp.driver)
hp.ids = NewIdentityServer(hp.name, hp.version)
hp.ns = NewNodeServer(hp.nodeID)
hp.cs = NewControllerServer()

s := csicommon.NewNonBlockingGRPCServer()
s.Start(endpoint, hp.ids, hp.cs, hp.ns)
s := NewNonBlockingGRPCServer()
s.Start(hp.endpoint, hp.ids, hp.cs, hp.ns)
s.Wait()
}

Expand Down
52 changes: 50 additions & 2 deletions pkg/hostpath/identityserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,57 @@ limitations under the License.
package hostpath

import (
"github.com/kubernetes-csi/drivers/pkg/csi-common"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/glog"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type identityServer struct {
*csicommon.DefaultIdentityServer
name string
version string
}

func NewIdentityServer(name, version string) *identityServer {
return &identityServer{
name: name,
version: version,
}
}

func (ids *identityServer) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoRequest) (*csi.GetPluginInfoResponse, error) {
glog.V(5).Infof("Using default GetPluginInfo")

if ids.name == "" {
return nil, status.Error(codes.Unavailable, "Driver name not configured")
}

if ids.version == "" {
return nil, status.Error(codes.Unavailable, "Driver is missing version")
}

return &csi.GetPluginInfoResponse{
Name: ids.name,
VendorVersion: ids.version,
}, nil
}

func (ids *identityServer) Probe(ctx context.Context, req *csi.ProbeRequest) (*csi.ProbeResponse, error) {
return &csi.ProbeResponse{}, nil
}

func (ids *identityServer) GetPluginCapabilities(ctx context.Context, req *csi.GetPluginCapabilitiesRequest) (*csi.GetPluginCapabilitiesResponse, error) {
glog.V(5).Infof("Using default capabilities")
return &csi.GetPluginCapabilitiesResponse{
Capabilities: []*csi.PluginCapability{
{
Type: &csi.PluginCapability_Service_{
Service: &csi.PluginCapability_Service{
Type: csi.PluginCapability_Service_CONTROLLER_SERVICE,
},
},
},
},
}, nil
}
Loading

0 comments on commit 6ee5eeb

Please sign in to comment.