Skip to content

Commit

Permalink
Merge pull request rexray#1232 from commodityvectors/fixCeph13Parser
Browse files Browse the repository at this point in the history
Fix JSON parser for rbd showmapped in Ceph v13
  • Loading branch information
codenrhoden committed Jun 11, 2018
2 parents 6340189 + 02f129f commit a8f051b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
43 changes: 38 additions & 5 deletions libstorage/drivers/storage/rbd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ var (
ctxConfigKey = interface{}("rbd.config")
)

type preMimicRbdMappedEntry struct {
Device string `json:"device"`
Name string `json:"name"`
Pool string `json:"pool"`
Snap string `json:"snap"`
}

type rbdMappedEntry struct {
ID string `json:"id"`
Device string `json:"device"`
Name string `json:"name"`
Pool string `json:"pool"`
Expand Down Expand Up @@ -161,12 +169,10 @@ func GetMappedRBDs(ctx types.Context) (map[string]string, error) {
}

devMap := map[string]string{}
rbdMap := map[string]*rbdMappedEntry{}
rbdMap, parseErr := parseMappedRBDs(out)

err = json.Unmarshal(out, &rbdMap)
if err != nil {
return nil, goof.WithError(
"unable to parse rbd showmapped", err)
if parseErr != nil {
return nil, parseErr
}

for _, mapped := range rbdMap {
Expand All @@ -177,6 +183,33 @@ func GetMappedRBDs(ctx types.Context) (map[string]string, error) {
return devMap, nil
}

//parseMappedRBDs is a two-stage parse because Ceph v13 changed JSON format
func parseMappedRBDs(output []byte) ([]*rbdMappedEntry, error) {
rbdMap := []*rbdMappedEntry{}
rbdMapPreMimic := map[string]*preMimicRbdMappedEntry{}

err := json.Unmarshal(output, &rbdMapPreMimic)
if err != nil {

err2 := json.Unmarshal(output, &rbdMap)
if err2 != nil {
return nil, goof.WithError(
"unable to parse rbd showmapped", err2)
}

} else {
for index, entry := range rbdMapPreMimic {
rbdMap = append(rbdMap, &rbdMappedEntry{
ID: index,
Device: entry.Device,
Name: entry.Name,
Pool: entry.Pool,
Snap: entry.Snap})
}
}
return rbdMap, nil
}

//RBDCreate creates a new RBD volume on the cluster
func RBDCreate(
ctx types.Context,
Expand Down
21 changes: 21 additions & 0 deletions libstorage/drivers/storage/rbd/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"net"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -119,3 +120,23 @@ func TestArgsClient(t *testing.T) {
})
}
}

func TestRBDMapParser(t *testing.T) {
oldCephJSON := []byte("{\"0\":{\"pool\":\"luminpool\",\"name\":\"fooimg\",\"snap\":\"-\",\"device\":\"/dev/rbd0\"},\"1\":{\"pool\":\"luminpool\",\"name\":\"barimg\",\"snap\":\"-\",\"device\":\"/dev/rbd1\"}}")
v13CephJSON := []byte("[{\"id\":\"0\",\"pool\":\"luminpool\",\"name\":\"fooimg\",\"snap\":\"-\",\"device\":\"/dev/rbd0\"},{\"id\":\"1\",\"pool\":\"luminpool\",\"name\":\"barimg\",\"snap\":\"-\",\"device\":\"/dev/rbd1\"}]")

oldval, olderr := parseMappedRBDs(oldCephJSON)
v13val, v13err := parseMappedRBDs(v13CephJSON)

if olderr != nil {
t.Error("Parsing Ceph output from version < 13 failed")
}

if v13err != nil {
t.Error("Parsing Ceph output from version >= 13 failed")
}

if !reflect.DeepEqual(oldval, v13val) {
t.Errorf("Parser failed to yield identical output for \n%s and \n%s", oldval, v13val)
}
}

0 comments on commit a8f051b

Please sign in to comment.