From 8d35d42ade73588222fbd637f156c0b89bfcf571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=81=92=E5=BF=A0=40=E8=BF=90=E7=BB=B4=E5=B0=8F?= =?UTF-8?q?=E7=9B=86=E5=8F=8B?= <10927831@qq.com> Date: Thu, 18 Jan 2018 22:37:44 +0800 Subject: [PATCH 1/3] refactor using the fast return approach, make the code less indentation, in order to improve the readability of the code. --- volume/modules/ceph/map.go | 65 +++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/volume/modules/ceph/map.go b/volume/modules/ceph/map.go index 717c82f56..37d9c8ed5 100644 --- a/volume/modules/ceph/map.go +++ b/volume/modules/ceph/map.go @@ -122,43 +122,44 @@ func doUnmap(ctx driver.Context, v *types.Volume, rbdmap RBDMap) error { name := v.Name for _, rbd := range rbdmap.MapDevice { - if rbd.Image == name && rbd.Pool == poolName { - ctx.Log.Debugf("Ceph unmapping volume %s/%s at device %q", poolName, name, strings.TrimSpace(rbd.Device)) + if rbd.Image != name || rbd.Pool != poolName { + continue + } + ctx.Log.Debugf("Ceph unmapping volume %s/%s at device %q", poolName, name, strings.TrimSpace(rbd.Device)) - // Check device is exist or not. - if _, err := os.Stat(rbd.Device); err != nil { - ctx.Log.Errorf("Ceph trying to unmap device %q for %s/%s that does not exist, continuing", - poolName, name, rbd.Device) - continue - } + // Check device is exist or not. + if _, err := os.Stat(rbd.Device); err != nil { + ctx.Log.Errorf("Ceph trying to unmap device %q for %s/%s that does not exist, continuing", + poolName, name, rbd.Device) + continue + } - // Unmap device. - exit, _, stderr, err := exec.Run(defaultTimeout, bin, "unmap", rbd.Device) - if err != nil { - ctx.Log.Errorf("Ceph could not unmap volume %q (device %q): %d (%v) (%s)", - name, rbd.Device, exit, err, stderr) - return err - } else if exit != 0 { - err = fmt.Errorf("Ceph could not unmap volume %q (device %q): %d (%s)", - name, rbd.Device, exit, stderr) - ctx.Log.Error(err) - return err - } + // Unmap device. + exit, _, stderr, err := exec.Run(defaultTimeout, bin, "unmap", rbd.Device) + if err != nil { + ctx.Log.Errorf("Ceph could not unmap volume %q (device %q): %d (%v) (%s)", + name, rbd.Device, exit, err, stderr) + return err + } + if exit != 0 { + err = fmt.Errorf("Ceph could not unmap volume %q (device %q): %d (%s)", + name, rbd.Device, exit, stderr) + ctx.Log.Error(err) + return err + } - // Check have unmapped or not. - rbdmap2, err := showMapped(ctx) - if err != nil { - return err - } - for _, rbd2 := range rbdmap2.MapDevice { - if rbd.Image == rbd2.Image && rbd.Pool == rbd2.Pool { - return fmt.Errorf("could not unmap volume %q, device: %q is exist", - name, rbd.Image) - } + // Check have unmapped or not. + rbdmap2, err := showMapped(ctx) + if err != nil { + return err + } + for _, rbd2 := range rbdmap2.MapDevice { + if rbd.Image == rbd2.Image && rbd.Pool == rbd2.Pool { + return fmt.Errorf("could not unmap volume %q, device: %q is exist", + name, rbd.Image) } - - break } + break } return nil From 2a4635c98cc1641cf6882d5ed0341a67aceb3233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=81=92=E5=BF=A0=40=E8=BF=90=E7=BB=B4=E5=B0=8F?= =?UTF-8?q?=E7=9B=86=E5=8F=8B?= <10927831@qq.com> Date: Thu, 18 Jan 2018 23:44:20 +0800 Subject: [PATCH 2/3] gofmt gofmt --- volume/modules/ceph/map.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/volume/modules/ceph/map.go b/volume/modules/ceph/map.go index 37d9c8ed5..536a1389c 100644 --- a/volume/modules/ceph/map.go +++ b/volume/modules/ceph/map.go @@ -122,7 +122,7 @@ func doUnmap(ctx driver.Context, v *types.Volume, rbdmap RBDMap) error { name := v.Name for _, rbd := range rbdmap.MapDevice { - if rbd.Image != name || rbd.Pool != poolName { + if rbd.Image != name || rbd.Pool != poolName { continue } ctx.Log.Debugf("Ceph unmapping volume %s/%s at device %q", poolName, name, strings.TrimSpace(rbd.Device)) From 92105afde9b14fe60ee8e6a2a0f462118c653019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=81=92=E5=BF=A0=40=E8=BF=90=E7=BB=B4=E5=B0=8F?= =?UTF-8?q?=E7=9B=86=E5=8F=8B?= <10927831@qq.com> Date: Fri, 19 Jan 2018 00:07:56 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Signed-off-by:=20=E8=B0=A2=E6=81=92?= =?UTF-8?q?=E5=BF=A0@=E8=BF=90=E7=BB=B4=E5=B0=8F=E7=9B=86=E5=8F=8B=20<1092?= =?UTF-8?q?7831@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- volume/modules/ceph/map.go | 1 - 1 file changed, 1 deletion(-) diff --git a/volume/modules/ceph/map.go b/volume/modules/ceph/map.go index 536a1389c..1fc7d4450 100644 --- a/volume/modules/ceph/map.go +++ b/volume/modules/ceph/map.go @@ -161,6 +161,5 @@ func doUnmap(ctx driver.Context, v *types.Volume, rbdmap RBDMap) error { } break } - return nil }