Skip to content

Commit

Permalink
Merge pull request kubernetes-csi#26 from sbezverk/building_static_im…
Browse files Browse the repository at this point in the history
…age_external-attacher

refactor attacher to use new api objects
  • Loading branch information
jsafrane authored Feb 28, 2018
2 parents 3654f11 + 7d64d12 commit 6f85450
Show file tree
Hide file tree
Showing 27 changed files with 1,875 additions and 1,852 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ FROM alpine
LABEL maintainers="Kubernetes Authors"
LABEL description="CSI External Attacher"

COPY csi-attacher csi-attacher
COPY ./bin/csi-attacher csi-attacher
ENTRYPOINT ["/csi-attacher"]
8 changes: 0 additions & 8 deletions Dockerfile.builder

This file was deleted.

10 changes: 5 additions & 5 deletions Gopkg.lock

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

6 changes: 3 additions & 3 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
branch = "master"
name = "github.com/golang/glog"

[[constraint]]
name = "github.com/golang/mock"
version = "1.0.0"
#[[constraint]]
# name = "github.com/golang/mock"
# version = "1.0.0"

[[constraint]]
branch = "master"
Expand Down
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ endif
all: csi-attacher

csi-attacher:
go install github.com/kubernetes-csi/external-attacher/cmd/csi-attacher
mkdir -p bin
cp ${GOPATH}/bin/csi-attacher bin/csi-attacher
CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o ./bin/csi-attacher ./cmd/csi-attacher

clean:
-rm -rf bin deploy/docker/csi-attacher
-rm -rf bin

container: csi-attacher
cp bin/csi-attacher deploy/docker
docker build -t $(IMAGE_NAME):$(IMAGE_VERSION) .

push: container
Expand Down
7 changes: 3 additions & 4 deletions cmd/csi-attacher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,15 @@ func waitForDriverReady(csiConn connection.CSIConnection, timeout time.Duration)
now := time.Now()
finish := now.Add(timeout)
var err error

for {
ctx, cancel := context.WithTimeout(context.Background(), csiTimeout)
defer cancel()
err = csiConn.ControllerProbe(ctx)
err = csiConn.Probe(ctx)
if err == nil {
glog.V(2).Infof("ControllerProbe succeeded")
glog.V(2).Infof("Probe succeeded")
return nil
}
glog.V(2).Infof("ControllerProbe failed with %s", err)
glog.V(2).Infof("Probe failed with %s", err)

now := time.Now()
if now.After(finish) {
Expand Down
58 changes: 23 additions & 35 deletions pkg/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"strings"
"time"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/golang/glog"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -56,7 +56,7 @@ type CSIConnection interface {
Detach(ctx context.Context, volumeID string, nodeID string) (detached bool, err error)

// Probe checks that the CSI driver is ready to process requests
ControllerProbe(ctx context.Context) error
Probe(ctx context.Context) error

// Close the connection
Close() error
Expand All @@ -68,13 +68,6 @@ type csiConnection struct {

var (
_ CSIConnection = &csiConnection{}

// Version of CSI this client implements
csiVersion = csi.Version{
Major: 0,
Minor: 2,
Patch: 0,
}
)

func New(address string, timeout time.Duration) (CSIConnection, error) {
Expand Down Expand Up @@ -122,9 +115,7 @@ func connect(address string, timeout time.Duration) (*grpc.ClientConn, error) {
func (c *csiConnection) GetDriverName(ctx context.Context) (string, error) {
client := csi.NewIdentityClient(c.conn)

req := csi.GetPluginInfoRequest{
Version: &csiVersion,
}
req := csi.GetPluginInfoRequest{}

rsp, err := client.GetPluginInfo(ctx, &req)
if err != nil {
Expand All @@ -137,11 +128,21 @@ func (c *csiConnection) GetDriverName(ctx context.Context) (string, error) {
return name, nil
}

func (c *csiConnection) Probe(ctx context.Context) error {
client := csi.NewIdentityClient(c.conn)

req := csi.ProbeRequest{}

_, err := client.Probe(ctx, &req)
if err != nil {
return err
}
return nil
}

func (c *csiConnection) SupportsControllerPublish(ctx context.Context) (bool, error) {
client := csi.NewControllerClient(c.conn)
req := csi.ControllerGetCapabilitiesRequest{
Version: &csiVersion,
}
req := csi.ControllerGetCapabilitiesRequest{}

rsp, err := client.ControllerGetCapabilities(ctx, &req)
if err != nil {
Expand All @@ -167,13 +168,12 @@ func (c *csiConnection) Attach(ctx context.Context, volumeID string, readOnly bo
client := csi.NewControllerClient(c.conn)

req := csi.ControllerPublishVolumeRequest{
Version: &csiVersion,
VolumeId: volumeID,
NodeId: nodeID,
VolumeCapability: caps,
Readonly: readOnly,
VolumeAttributes: attributes,
ControllerPublishCredentials: nil,
VolumeId: volumeID,
NodeId: nodeID,
VolumeCapability: caps,
Readonly: readOnly,
VolumeAttributes: attributes,
ControllerPublishSecrets: nil,
}

rsp, err := client.ControllerPublishVolume(ctx, &req)
Expand All @@ -187,10 +187,9 @@ func (c *csiConnection) Detach(ctx context.Context, volumeID string, nodeID stri
client := csi.NewControllerClient(c.conn)

req := csi.ControllerUnpublishVolumeRequest{
Version: &csiVersion,
VolumeId: volumeID,
NodeId: nodeID,
ControllerUnpublishCredentials: nil,
ControllerUnpublishSecrets: nil,
}

_, err = client.ControllerUnpublishVolume(ctx, &req)
Expand All @@ -200,17 +199,6 @@ func (c *csiConnection) Detach(ctx context.Context, volumeID string, nodeID stri
return true, nil
}

func (c *csiConnection) ControllerProbe(ctx context.Context) error {
client := csi.NewControllerClient(c.conn)

req := csi.ControllerProbeRequest{
Version: &csiVersion,
}

_, err := client.ControllerProbe(ctx, &req)
return err
}

func (c *csiConnection) Close() error {
return c.conn.Close()
}
Expand Down
82 changes: 7 additions & 75 deletions pkg/connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"reflect"
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/golang/mock/gomock"
"github.com/kubernetes-csi/csi-test/driver"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -97,13 +97,7 @@ func TestGetPluginInfo(t *testing.T) {

for _, test := range tests {

in := &csi.GetPluginInfoRequest{
Version: &csi.Version{
Major: 0,
Minor: 2,
Patch: 0,
},
}
in := &csi.GetPluginInfoRequest{}

out := test.output
var injectedErr error = nil
Expand Down Expand Up @@ -138,14 +132,14 @@ func TestSupportsControllerPublish(t *testing.T) {
name: "success",
output: &csi.ControllerGetCapabilitiesResponse{
Capabilities: []*csi.ControllerServiceCapability{
&csi.ControllerServiceCapability{
{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
},
},
},
&csi.ControllerServiceCapability{
{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
Expand All @@ -166,7 +160,7 @@ func TestSupportsControllerPublish(t *testing.T) {
name: "no publish",
output: &csi.ControllerGetCapabilitiesResponse{
Capabilities: []*csi.ControllerServiceCapability{
&csi.ControllerServiceCapability{
{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
Expand All @@ -181,7 +175,7 @@ func TestSupportsControllerPublish(t *testing.T) {
name: "empty capability",
output: &csi.ControllerGetCapabilitiesResponse{
Capabilities: []*csi.ControllerServiceCapability{
&csi.ControllerServiceCapability{
{
Type: nil,
},
},
Expand All @@ -207,13 +201,7 @@ func TestSupportsControllerPublish(t *testing.T) {

for _, test := range tests {

in := &csi.ControllerGetCapabilitiesRequest{
Version: &csi.Version{
Major: 0,
Minor: 2,
Patch: 0,
},
}
in := &csi.ControllerGetCapabilitiesRequest{}

out := test.output
var injectedErr error = nil
Expand Down Expand Up @@ -251,21 +239,18 @@ func TestAttach(t *testing.T) {
"third": "baz",
}
defaultRequest := &csi.ControllerPublishVolumeRequest{
Version: &csiVersion,
VolumeId: defaultVolumeID,
NodeId: defaultNodeID,
VolumeCapability: defaultCaps,
Readonly: false,
}
readOnlyRequest := &csi.ControllerPublishVolumeRequest{
Version: &csiVersion,
VolumeId: defaultVolumeID,
NodeId: defaultNodeID,
VolumeCapability: defaultCaps,
Readonly: true,
}
attributesRequest := &csi.ControllerPublishVolumeRequest{
Version: &csiVersion,
VolumeId: defaultVolumeID,
NodeId: defaultNodeID,
VolumeCapability: defaultCaps,
Expand Down Expand Up @@ -406,7 +391,6 @@ func TestDetachAttach(t *testing.T) {
defaultNodeID := "MyNodeID"

defaultRequest := &csi.ControllerUnpublishVolumeRequest{
Version: &csiVersion,
VolumeId: defaultVolumeID,
NodeId: defaultNodeID,
}
Expand Down Expand Up @@ -485,55 +469,3 @@ func TestDetachAttach(t *testing.T) {
}
}
}

func TestControllerProbe(t *testing.T) {
tests := []struct {
name string
injectError bool
expectError bool
}{
{
name: "success",
expectError: false,
},
{
name: "gRPC error",
injectError: true,
expectError: true,
},
}

mockController, driver, _, controllerServer, csiConn, err := createMockServer(t)
if err != nil {
t.Fatal(err)
}
defer mockController.Finish()
defer driver.Stop()
defer csiConn.Close()

for _, test := range tests {
in := &csi.ControllerProbeRequest{
Version: &csi.Version{
Major: 0,
Minor: 2,
Patch: 0,
},
}
out := &csi.ControllerProbeResponse{}
var injectedErr error = nil
if test.injectError {
injectedErr = fmt.Errorf("mock error")
}

// Setup expectation
controllerServer.EXPECT().ControllerProbe(gomock.Any(), in).Return(out, injectedErr).Times(1)

err := csiConn.ControllerProbe(context.Background())
if test.expectError && err == nil {
t.Errorf("test %q: Expected error, got none", test.name)
}
if !test.expectError && err != nil {
t.Errorf("test %q: got error: %v", test.name, err)
}
}
}
2 changes: 1 addition & 1 deletion pkg/connection/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"regexp"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/container-storage-interface/spec/lib/go/csi/v0"
"k8s.io/api/core/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/connection/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/container-storage-interface/spec/lib/go/csi/v0"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down
Loading

0 comments on commit 6f85450

Please sign in to comment.