Skip to content

Commit

Permalink
Support pool status (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsClark authored and wisererik committed Oct 23, 2019
1 parent 85238a2 commit 3a0f4a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pkg/dock/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ import (
uuid "github.com/satori/go.uuid"
)

const (
availableStatus = "available"
unavailableStatus = "unavailable"
)

type Context struct {
StopChan chan bool
ErrChan chan error
Expand Down Expand Up @@ -135,6 +140,7 @@ func (pdd *provisionDockDiscoverer) Discover() error {
// Clear existing pool info
pdd.pols = pdd.pols[:0]
var pols []*model.StoragePoolSpec
var polsInDb []*model.StoragePoolSpec
var err error
for _, dck := range pdd.dcks {
// Call function of StorageDrivers configured by storage drivers.
Expand All @@ -145,6 +151,7 @@ func (pdd *provisionDockDiscoverer) Discover() error {
for _, pol := range pols {
log.Infof("Backend %s discovered pool %s", dck.DriverName, pol.Name)
pol.DockId = dck.Id
pol.Status = availableStatus
}
} else {
d := drivers.Init(dck.DriverName)
Expand All @@ -162,6 +169,7 @@ func (pdd *provisionDockDiscoverer) Discover() error {
pol.DockId = dck.Id
pol.ReplicationType = replicationType
pol.ReplicationDriverName = replicationDriverName
pol.Status = availableStatus
}
}
if err != nil {
Expand All @@ -175,8 +183,24 @@ func (pdd *provisionDockDiscoverer) Discover() error {

pdd.pols = append(pdd.pols, pols...)
}
ctx := c.NewAdminContext()
polsInDb, err = pdd.c.ListPools(ctx)
if err != nil {
return fmt.Errorf("can not read pools in db")
}
dbPolsMap := make(map[string]*model.StoragePoolSpec)
for _, polInDb := range polsInDb {
dbPolsMap[polInDb.Id] = polInDb
}
for _, pol := range pdd.pols {
delete(dbPolsMap, pol.Id)
}
for _, unavailablePol := range dbPolsMap {
unavailablePol.Status = unavailableStatus
pdd.pols = append(pdd.pols, unavailablePol)
}
if len(pdd.pols) == 0 {
return fmt.Errorf("There is no pool can be found.")
return fmt.Errorf("there is no pool can be found")
}

return nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/dock/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ func TestDiscover(t *testing.T) {
fdd.dcks = append(fdd.dcks, &SampleDocks[i])
}
for i := range SamplePools {
fdd.pols = append(fdd.pols, &SamplePools[i])
expected = append(expected, &SamplePools[i])
}
mockClient := new(dbtest.Client)
mockClient.On("ListPools", c.NewAdminContext()).Return(fdd.pols, nil)
fdd.c = mockClient

if err := fdd.Discover(); err != nil {
t.Errorf("Failed to discoverer pools: %v\n", err)
}
Expand Down

0 comments on commit 3a0f4a2

Please sign in to comment.