Skip to content

Commit

Permalink
Address warnings from golint/go vet, use gofmt -s
Browse files Browse the repository at this point in the history
  • Loading branch information
jellonek committed Feb 4, 2019
1 parent 3baf28b commit f8c0ded
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 80 deletions.
2 changes: 1 addition & 1 deletion cmd/flexvolume_driver/flexvolume_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ import (

func main() {
rand.Seed(time.Now().UnixNano())
driver := flexvolume.NewFlexVolumeDriver(utils.NewUUID, utils.NewMounter())
driver := flexvolume.NewDriver(utils.NewUUID, utils.NewMounter())
os.Stdout.WriteString(driver.Run(os.Args[1:]))
}
10 changes: 5 additions & 5 deletions pkg/config/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
func configMappingProps() *apiext.JSONSchemaProps {
return &apiext.JSONSchemaProps{
Properties: map[string]apiext.JSONSchemaProps{
"spec": apiext.JSONSchemaProps{
"spec": {
Properties: map[string]apiext.JSONSchemaProps{
"nodeName": apiext.JSONSchemaProps{
"nodeName": {
Type: "string",
},
"nodeSelector": apiext.JSONSchemaProps{
"nodeSelector": {
Type: "object",
// FIXME: https://github.com/kubernetes/kubernetes/issues/59485
// AdditionalProperties: &apiext.JSONSchemaPropsOrBool{
Expand All @@ -42,10 +42,10 @@ func configMappingProps() *apiext.JSONSchemaProps {
// },
// },
},
"priority": apiext.JSONSchemaProps{
"priority": {
Type: "integer",
},
"config": apiext.JSONSchemaProps{
"config": {
Properties: configFieldSet(&virtlet_v1.VirtletConfig{}).schemaProps(),
},
},
Expand Down
14 changes: 7 additions & 7 deletions pkg/diag/diag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,38 @@ func TestDiagServer(t *testing.T) {
Name: "diagnostics",
IsDir: true,
Children: map[string]Result{
"foo": Result{
"foo": {
Name: "foo",
Ext: "txt",
Data: "this is foo",
},
"bar": Result{
"bar": {
Name: "bar",
Ext: "log",
Data: "this is bar",
},
"simple_text": Result{
"simple_text": {
Name: "simple_text",
Ext: "txt",
Data: "baz",
},
"logdir": Result{
"logdir": {
Name: "logdir",
IsDir: true,
Children: map[string]Result{
"log1": Result{
"log1": {
Name: "log1",
Ext: "txt",
Data: "log1 contents",
},
"log2": Result{
"log2": {
Name: "log2",
Ext: "txt",
Data: "log2 contents",
},
},
},
"fail": Result{
"fail": {
Name: "fail",
Error: "oops",
},
Expand Down
62 changes: 32 additions & 30 deletions pkg/flexvolume/flexvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,52 +46,55 @@ func init() {
}
}

type UuidGen func() string
// UUIDGen type function returns newly generated UUIDv4 as a string
type UUIDGen func() string

type FlexVolumeDriver struct {
uuidGen UuidGen
// Driver provides a virtlet specific implementation of
// https://kubernetes.io/docs/concepts/storage/volumes/#flexVolume
type Driver struct {
uuidGen UUIDGen
mounter utils.Mounter
}

// NewFlexVolumeDriver creates a FlexVolumeDriver struct
func NewFlexVolumeDriver(uuidGen UuidGen, mounter utils.Mounter) *FlexVolumeDriver {
return &FlexVolumeDriver{uuidGen: uuidGen, mounter: mounter}
// NewDriver creates a Driver struct
func NewDriver(uuidGen UUIDGen, mounter utils.Mounter) *Driver {
return &Driver{uuidGen: uuidGen, mounter: mounter}
}

func (d *FlexVolumeDriver) populateVolumeDir(targetDir string, opts map[string]interface{}) error {
func (d *Driver) populateVolumeDir(targetDir string, opts map[string]interface{}) error {
return utils.WriteJSON(filepath.Join(targetDir, flexvolumeDataFile), opts, 0700)
}

// The following functions are not currently needed, but still
// keeping them to make it easier to actually implement them

// Invocation: <driver executable> init
func (d *FlexVolumeDriver) init() (map[string]interface{}, error) {
func (d *Driver) init() (map[string]interface{}, error) {
return nil, nil
}

// Invocation: <driver executable> attach <json options> <node name>
func (d *FlexVolumeDriver) attach(jsonOptions, nodeName string) (map[string]interface{}, error) {
func (d *Driver) attach(jsonOptions, nodeName string) (map[string]interface{}, error) {
return nil, nil
}

// Invocation: <driver executable> detach <mount device> <node name>
func (d *FlexVolumeDriver) detach(mountDev, nodeName string) (map[string]interface{}, error) {
func (d *Driver) detach(mountDev, nodeName string) (map[string]interface{}, error) {
return nil, nil
}

// Invocation: <driver executable> waitforattach <mount device> <json options>
func (d *FlexVolumeDriver) waitForAttach(mountDev, jsonOptions string) (map[string]interface{}, error) {
func (d *Driver) waitForAttach(mountDev, jsonOptions string) (map[string]interface{}, error) {
return map[string]interface{}{"device": mountDev}, nil
}

// Invocation: <driver executable> isattached <json options> <node name>
func (d *FlexVolumeDriver) isAttached(jsonOptions, nodeName string) (map[string]interface{}, error) {
func (d *Driver) isAttached(jsonOptions, nodeName string) (map[string]interface{}, error) {
return map[string]interface{}{"attached": true}, nil
}

//Invocation: <driver executable> mount <target mount dir> <json options>
func (d *FlexVolumeDriver) mount(targetMountDir, jsonOptions string) (map[string]interface{}, error) {
func (d *Driver) mount(targetMountDir, jsonOptions string) (map[string]interface{}, error) {
var opts map[string]interface{}
if err := json.Unmarshal([]byte(jsonOptions), &opts); err != nil {
return nil, fmt.Errorf("failed to unmarshal json options: %v", err)
Expand Down Expand Up @@ -137,7 +140,7 @@ func (d *FlexVolumeDriver) mount(targetMountDir, jsonOptions string) (map[string
}

// Invocation: <driver executable> unmount <mount dir>
func (d *FlexVolumeDriver) unmount(targetMountDir string) (map[string]interface{}, error) {
func (d *Driver) unmount(targetMountDir string) (map[string]interface{}, error) {
if err := d.mounter.Unmount(targetMountDir, true); err != nil {
return nil, fmt.Errorf("unmount %q: %v", targetMountDir, err.Error())
}
Expand All @@ -149,7 +152,7 @@ func (d *FlexVolumeDriver) unmount(targetMountDir string) (map[string]interface{
return nil, nil
}

type driverOp func(*FlexVolumeDriver, []string) (map[string]interface{}, error)
type driverOp func(*Driver, []string) (map[string]interface{}, error)

type cmdInfo struct {
numArgs int
Expand All @@ -158,43 +161,43 @@ type cmdInfo struct {

var commands = map[string]cmdInfo{
"init": {
0, func(d *FlexVolumeDriver, args []string) (map[string]interface{}, error) {
0, func(d *Driver, args []string) (map[string]interface{}, error) {
return d.init()
},
},
"attach": {
2, func(d *FlexVolumeDriver, args []string) (map[string]interface{}, error) {
2, func(d *Driver, args []string) (map[string]interface{}, error) {
return d.attach(args[0], args[1])
},
},
"detach": {
2, func(d *FlexVolumeDriver, args []string) (map[string]interface{}, error) {
2, func(d *Driver, args []string) (map[string]interface{}, error) {
return d.detach(args[0], args[1])
},
},
"waitforattach": {
2, func(d *FlexVolumeDriver, args []string) (map[string]interface{}, error) {
2, func(d *Driver, args []string) (map[string]interface{}, error) {
return d.waitForAttach(args[0], args[1])
},
},
"isattached": {
2, func(d *FlexVolumeDriver, args []string) (map[string]interface{}, error) {
2, func(d *Driver, args []string) (map[string]interface{}, error) {
return d.isAttached(args[0], args[1])
},
},
"mount": {
2, func(d *FlexVolumeDriver, args []string) (map[string]interface{}, error) {
2, func(d *Driver, args []string) (map[string]interface{}, error) {
return d.mount(args[0], args[1])
},
},
"unmount": {
1, func(d *FlexVolumeDriver, args []string) (map[string]interface{}, error) {
1, func(d *Driver, args []string) (map[string]interface{}, error) {
return d.unmount(args[0])
},
},
}

func (d *FlexVolumeDriver) doRun(args []string) (map[string]interface{}, error) {
func (d *Driver) doRun(args []string) (map[string]interface{}, error) {
if len(args) == 0 {
return nil, errors.New("no arguments passed to flexvolume driver")
}
Expand All @@ -203,17 +206,16 @@ func (d *FlexVolumeDriver) doRun(args []string) (map[string]interface{}, error)
if cmdInfo, found := commands[op]; found {
if cmdInfo.numArgs == nArgs {
return cmdInfo.run(d, args[1:])
} else {
return nil, fmt.Errorf("unexpected number of args %d (expected %d) for operation %q", nArgs, cmdInfo.numArgs, op)
}
} else {
return map[string]interface{}{
"status": "Not supported",
}, nil
return nil, fmt.Errorf("unexpected number of args %d (expected %d) for operation %q", nArgs, cmdInfo.numArgs, op)
}
return map[string]interface{}{
"status": "Not supported",
}, nil
}

func (d *FlexVolumeDriver) Run(args []string) string {
// Run runs the driver
func (d *Driver) Run(args []string) string {
r := formatResult(d.doRun(args))

if flexVolumeDebug {
Expand Down
8 changes: 4 additions & 4 deletions pkg/flexvolume/flexvolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

const (
fakeUuid = "abb67e3c-71b3-4ddd-5505-8c4215d5c4eb"
fakeUUID = "abb67e3c-71b3-4ddd-5505-8c4215d5c4eb"
)

type fakeMounter struct {
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestFlexVolume(t *testing.T) {
"user": "libvirt",
}
cephJSONVolumeInfo := map[string]interface{}{
"uuid": fakeUuid,
"uuid": fakeUUID,
}
for k, v := range cephJSONOpts {
cephJSONVolumeInfo[k] = v
Expand Down Expand Up @@ -257,8 +257,8 @@ func TestFlexVolume(t *testing.T) {
var subdir string
args := step.args
mounter := newFakeMounter(t, tmpDir)
d := NewFlexVolumeDriver(func() string {
return fakeUuid
d := NewDriver(func() string {
return fakeUUID
}, mounter)
result := d.Run(args)
var m map[string]interface{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/libvirttools/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ func TestAddingFileLikeMount(t *testing.T) {

func TestMtuForMacAddress(t *testing.T) {
interfaces := []*network.InterfaceDescription{
&network.InterfaceDescription{
{
MTU: 1234,
HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0xa, 0xb},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/libvirttools/virtualization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ type volDevice struct {
}

func TestDomainDefinitions(t *testing.T) {
flexVolumeDriver := flexvolume.NewFlexVolumeDriver(func() string {
flexVolumeDriver := flexvolume.NewDriver(func() string {
// note that this is only good for just one flexvolume
return fakeUUID
}, utils.NullMounter)
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (v *VirtletImageService) ImageFsInfo(ctx context.Context, in *kubeapi.Image
}
return &kubeapi.ImageFsInfoResponse{
ImageFilesystems: []*kubeapi.FilesystemUsage{
&kubeapi.FilesystemUsage{
{
Timestamp: v.clock.Now().UnixNano(),
FsId: &kubeapi.FilesystemIdentifier{
Mountpoint: stats.Mountpoint,
Expand Down
14 changes: 6 additions & 8 deletions pkg/manager/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,14 @@ func (tst *virtletCRITester) invoke(name string, req interface{}, failOnError bo
}
}
return nil, err
} else {
resp := vals[0].Interface()
tst.rec.Rec("leave: "+name, resp)
return resp, nil
}
resp := vals[0].Interface()
tst.rec.Rec("leave: "+name, resp)
return resp, nil
}

func (tst *virtletCRITester) getSampleFlexvolMounts(podSandboxID string) []*kubeapi.Mount {
flexVolumeDriver := flexvolume.NewFlexVolumeDriver(func() string {
flexVolumeDriver := flexvolume.NewDriver(func() string {
return "abb67e3c-71b3-4ddd-5505-8c4215d5c4eb"
}, utils.NullMounter)
flexVolDir := filepath.Join(tst.kubeletRootDir, podSandboxID, "volumes/virtlet~flexvolume_driver", "vol1")
Expand Down Expand Up @@ -385,10 +384,9 @@ func (tst *virtletCRITester) createContainer(sandbox *kubeapi.PodSandboxConfig,
}
if r, ok := resp.(*kubeapi.CreateContainerResponse); ok {
return r.ContainerId
} else {
tst.t.Fatalf("bad value returned by CreateContainer: %#v", resp)
return "" // unreachable
}
tst.t.Fatalf("bad value returned by CreateContainer: %#v", resp)
return "" // unreachable
}

func (tst *virtletCRITester) containerStatus(containerID string) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/metadata/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ type VMConfig struct {
// RootVolumeDevice returns the volume device that should be used for
// a persistent root filesystem, that is, its DevicePath is "/"
func (c *VMConfig) RootVolumeDevice() *VMVolumeDevice {
for n, _ := range c.VolumeDevices {
for n := range c.VolumeDevices {
dev := &c.VolumeDevices[n]
if dev.IsRoot() {
return dev
Expand Down
Loading

0 comments on commit f8c0ded

Please sign in to comment.