Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Fix to return empty slice
Browse files Browse the repository at this point in the history
  • Loading branch information
taku-k committed Oct 12, 2017
1 parent e4b39a4 commit 5cebb62
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 7 additions & 1 deletion pkg/ipam/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func getIPAddr(r *storage.Redis, ip net.IP) (*model.IPAddr, error) {
}

func getIPAddrs(r *storage.Redis, ips []net.IP) ([]*model.IPAddr, error) {
addrs := make([]*model.IPAddr, len(ips))

if len(ips) == 0 {
return addrs, nil
}

dkeys := make([]string, len(ips))
for i, ip := range ips {
dkeys[i] = makeIPDetailsKey(ip)
Expand All @@ -55,7 +61,7 @@ func getIPAddrs(r *storage.Redis, ips []net.IP) ([]*model.IPAddr, error) {
if err != nil {
return nil, err
}
addrs := make([]*model.IPAddr, len(ips))

for i, d := range data {
if s, ok := d.(string); ok {
addrs[i] = &model.IPAddr{}
Expand Down
8 changes: 5 additions & 3 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,11 @@ func (m *IPManager) CreatePool(ctx context.Context, n *model.Network, pool *mode
}
members = append(members, z)
}
_, err = m.redis.Client.ZAdd(usedkey, members...).Result()
if err != nil {
return err
if len(members) != 0 {
_, err = m.redis.Client.ZAdd(usedkey, members...).Result()
if err != nil {
return err
}
}

return setPool(m.redis, n, pool)
Expand Down
8 changes: 6 additions & 2 deletions pkg/ipam/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ func Test_getPools(t *testing.T) {
Status: model.Network_AVAILABLE,
}

_ = m.CreateNetwork(ctx, n)
_ = m.CreatePool(ctx, n, pool)
if err := m.CreateNetwork(ctx, n); err != nil {
t.Fatalf("CreateNetwork returns error %v; want success", err)
}
if err := m.CreatePool(ctx, n, pool); err != nil {
t.Fatalf("CreatePool returns error %v; want success", err)
}

pools, err := getPoolsInNetwork(r, n)
if err != nil {
Expand Down

0 comments on commit 5cebb62

Please sign in to comment.