Skip to content

Commit fd2e591

Browse files
authored
Merge pull request #9 from avalluri/remove-csicommon
Removed csi-comman package dependency
2 parents a5de831 + c8544f2 commit fd2e591

File tree

16 files changed

+699
-499
lines changed

16 files changed

+699
-499
lines changed

Gopkg.lock

+9-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/hostpathplugin/main.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"flag"
21+
"fmt"
2122
"os"
2223

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

4344
func handle() {
44-
driver := hostpath.GetHostPathDriver()
45-
driver.Run(*driverName, *nodeID, *endpoint)
45+
driver, err := hostpath.NewHostPathDriver(*driverName, *nodeID, *endpoint)
46+
if err != nil {
47+
fmt.Printf("Failed to initialize driver: %s", err.Error())
48+
os.Exit(1)
49+
}
50+
driver.Run()
4651
}

pkg/hostpath/controllerserver.go

+70-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"google.golang.org/grpc/status"
3333

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

@@ -44,11 +43,22 @@ const (
4443
)
4544

4645
type controllerServer struct {
47-
*csicommon.DefaultControllerServer
46+
caps []*csi.ControllerServiceCapability
47+
}
48+
49+
func NewControllerServer() *controllerServer {
50+
return &controllerServer{
51+
caps: getControllerServiceCapabilities(
52+
[]csi.ControllerServiceCapability_RPC_Type{
53+
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
54+
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
55+
csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
56+
}),
57+
}
4858
}
4959

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

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

159+
func (cs *controllerServer) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
160+
return &csi.ControllerGetCapabilitiesResponse{
161+
Capabilities: cs.caps,
162+
}, nil
163+
}
164+
149165
func (cs *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
150-
return cs.DefaultControllerServer.ValidateVolumeCapabilities(ctx, req)
166+
return nil, status.Error(codes.Unimplemented, "")
167+
}
168+
169+
func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
170+
return nil, status.Error(codes.Unimplemented, "")
171+
}
172+
173+
func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
174+
return nil, status.Error(codes.Unimplemented, "")
175+
}
176+
177+
func (cs *controllerServer) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
178+
return nil, status.Error(codes.Unimplemented, "")
179+
}
180+
181+
func (cs *controllerServer) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
182+
return nil, status.Error(codes.Unimplemented, "")
151183
}
152184

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

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

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

366398
return rsp
367399
}
400+
401+
func (cs *controllerServer) validateControllerServiceRequest(c csi.ControllerServiceCapability_RPC_Type) error {
402+
if c == csi.ControllerServiceCapability_RPC_UNKNOWN {
403+
return nil
404+
}
405+
406+
for _, cap := range cs.caps {
407+
if c == cap.GetRpc().GetType() {
408+
return nil
409+
}
410+
}
411+
return status.Error(codes.InvalidArgument, fmt.Sprintf("%s", c))
412+
}
413+
414+
func getControllerServiceCapabilities(cl []csi.ControllerServiceCapability_RPC_Type) []*csi.ControllerServiceCapability {
415+
var csc []*csi.ControllerServiceCapability
416+
417+
for _, cap := range cl {
418+
glog.Infof("Enabling controller service capability: %v", cap.String())
419+
csc = append(csc, &csi.ControllerServiceCapability{
420+
Type: &csi.ControllerServiceCapability_Rpc{
421+
Rpc: &csi.ControllerServiceCapability_RPC{
422+
Type: cap,
423+
},
424+
},
425+
})
426+
}
427+
428+
return csc
429+
}

pkg/hostpath/hostpath.go

+26-42
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ package hostpath
1919
import (
2020
"fmt"
2121

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

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

2927
const (
@@ -36,14 +34,14 @@ const (
3634
)
3735

3836
type hostPath struct {
39-
driver *csicommon.CSIDriver
37+
name string
38+
nodeID string
39+
version string
40+
endpoint string
4041

4142
ids *identityServer
4243
ns *nodeServer
4344
cs *controllerServer
44-
45-
cap []*csi.VolumeCapability_AccessMode
46-
cscap []*csi.ControllerServiceCapability
4745
}
4846

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

6967
var (
70-
hostPathDriver *hostPath
71-
vendorVersion = "dev"
68+
vendorVersion = "dev"
7269
)
7370

7471
func init() {
7572
hostPathVolumes = map[string]hostPathVolume{}
7673
hostPathVolumeSnapshots = map[string]hostPathSnapshot{}
7774
}
7875

79-
func GetHostPathDriver() *hostPath {
80-
return &hostPath{}
81-
}
82-
83-
func NewIdentityServer(d *csicommon.CSIDriver) *identityServer {
84-
return &identityServer{
85-
DefaultIdentityServer: csicommon.NewDefaultIdentityServer(d),
76+
func NewHostPathDriver(driverName, nodeID, endpoint string) (*hostPath, error) {
77+
if driverName == "" {
78+
return nil, fmt.Errorf("No driver name provided")
8679
}
87-
}
8880

89-
func NewControllerServer(d *csicommon.CSIDriver) *controllerServer {
90-
return &controllerServer{
91-
DefaultControllerServer: csicommon.NewDefaultControllerServer(d),
81+
if nodeID == "" {
82+
return nil, fmt.Errorf("No node id provided")
9283
}
93-
}
9484

95-
func NewNodeServer(d *csicommon.CSIDriver) *nodeServer {
96-
return &nodeServer{
97-
DefaultNodeServer: csicommon.NewDefaultNodeServer(d),
85+
if endpoint == "" {
86+
return nil, fmt.Errorf("No driver endpoint provided")
9887
}
99-
}
10088

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

105-
// Initialize default library driver
106-
hp.driver = csicommon.NewCSIDriver(driverName, vendorVersion, nodeID)
107-
if hp.driver == nil {
108-
glog.Fatalln("Failed to initialize CSI Driver.")
109-
}
110-
hp.driver.AddControllerServiceCapabilities(
111-
[]csi.ControllerServiceCapability_RPC_Type{
112-
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
113-
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
114-
csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
115-
})
116-
hp.driver.AddVolumeCapabilityAccessModes([]csi.VolumeCapability_AccessMode_Mode{csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER})
92+
return &hostPath{
93+
name: driverName,
94+
version: vendorVersion,
95+
nodeID: nodeID,
96+
endpoint: endpoint,
97+
}, nil
98+
}
99+
100+
func (hp *hostPath) Run() {
117101

118102
// Create GRPC servers
119-
hp.ids = NewIdentityServer(hp.driver)
120-
hp.ns = NewNodeServer(hp.driver)
121-
hp.cs = NewControllerServer(hp.driver)
103+
hp.ids = NewIdentityServer(hp.name, hp.version)
104+
hp.ns = NewNodeServer(hp.nodeID)
105+
hp.cs = NewControllerServer()
122106

123-
s := csicommon.NewNonBlockingGRPCServer()
124-
s.Start(endpoint, hp.ids, hp.cs, hp.ns)
107+
s := NewNonBlockingGRPCServer()
108+
s.Start(hp.endpoint, hp.ids, hp.cs, hp.ns)
125109
s.Wait()
126110
}
127111

pkg/hostpath/identityserver.go

+50-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,57 @@ limitations under the License.
1717
package hostpath
1818

1919
import (
20-
"github.com/kubernetes-csi/drivers/pkg/csi-common"
20+
"github.com/container-storage-interface/spec/lib/go/csi"
21+
"github.com/golang/glog"
22+
"golang.org/x/net/context"
23+
"google.golang.org/grpc/codes"
24+
"google.golang.org/grpc/status"
2125
)
2226

2327
type identityServer struct {
24-
*csicommon.DefaultIdentityServer
28+
name string
29+
version string
30+
}
31+
32+
func NewIdentityServer(name, version string) *identityServer {
33+
return &identityServer{
34+
name: name,
35+
version: version,
36+
}
37+
}
38+
39+
func (ids *identityServer) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoRequest) (*csi.GetPluginInfoResponse, error) {
40+
glog.V(5).Infof("Using default GetPluginInfo")
41+
42+
if ids.name == "" {
43+
return nil, status.Error(codes.Unavailable, "Driver name not configured")
44+
}
45+
46+
if ids.version == "" {
47+
return nil, status.Error(codes.Unavailable, "Driver is missing version")
48+
}
49+
50+
return &csi.GetPluginInfoResponse{
51+
Name: ids.name,
52+
VendorVersion: ids.version,
53+
}, nil
54+
}
55+
56+
func (ids *identityServer) Probe(ctx context.Context, req *csi.ProbeRequest) (*csi.ProbeResponse, error) {
57+
return &csi.ProbeResponse{}, nil
58+
}
59+
60+
func (ids *identityServer) GetPluginCapabilities(ctx context.Context, req *csi.GetPluginCapabilitiesRequest) (*csi.GetPluginCapabilitiesResponse, error) {
61+
glog.V(5).Infof("Using default capabilities")
62+
return &csi.GetPluginCapabilitiesResponse{
63+
Capabilities: []*csi.PluginCapability{
64+
{
65+
Type: &csi.PluginCapability_Service_{
66+
Service: &csi.PluginCapability_Service{
67+
Type: csi.PluginCapability_Service_CONTROLLER_SERVICE,
68+
},
69+
},
70+
},
71+
},
72+
}, nil
2573
}

0 commit comments

Comments
 (0)