Skip to content

Commit

Permalink
provisiond: sort the reservation when the are received from BCDB (#391)
Browse files Browse the repository at this point in the history
* provisiond: sort the reservation when the are received from BCDB

workload needs to be deployed in this order:
- network
- zdb
- volume
- container

* add missing field in WireguardPeer1 type

* gofmt

* fix TestProvisionPoll

* extract map outside of function to avoid allocation
  • Loading branch information
zaibon authored Nov 21, 2019
1 parent 490782e commit 4b8b1ed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
17 changes: 17 additions & 0 deletions pkg/gedis/commands_provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gedis
import (
"encoding/json"
"fmt"
"sort"
"time"

"github.com/threefoldtech/zos/pkg/schema"
Expand All @@ -13,6 +14,16 @@ import (
"github.com/threefoldtech/zos/pkg"
)

// provisionOrder is used to sort the workload type
// in the right order for provisiond
var provisionOrder = map[provision.ReservationType]int{
provision.DebugReservation: 0,
provision.NetworkReservation: 1,
provision.ZDBReservation: 2,
provision.VolumeReservation: 3,
provision.ContainerReservation: 4,
}

// Reserve provision.Reserver
func (g *Gedis) Reserve(r *provision.Reservation, nodeID pkg.Identifier) (string, error) {
res := types.TfgridReservation1{
Expand Down Expand Up @@ -112,6 +123,12 @@ func (g *Gedis) Poll(nodeID pkg.Identifier, from uint64) ([]*provision.Reservati
reservations[i] = r
}

// sorts the primitive in the oder they need to be processed by provisiond
// network, zdb, volumes, container
sort.Slice(reservations, func(i int, j int) bool {
return provisionOrder[reservations[i].Type] < provisionOrder[reservations[j].Type]
})

return reservations, nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/gedis/commands_provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func TestProvisionPoll(t *testing.T) {

require.NoError(err)
require.Len(reservations, 2)
require.Equal(reservations[0].ID, "1-1")
require.Equal(reservations[0].Type, provision.VolumeReservation)
require.Equal(reservations[1].Type, provision.ZDBReservation)
require.Equal(reservations[0].ID, "2-1")
require.Equal(reservations[0].Type, provision.ZDBReservation)
require.Equal(reservations[1].Type, provision.VolumeReservation)
conn.AssertCalled(t, "Close")

args = Args{
Expand Down Expand Up @@ -115,8 +115,8 @@ func TestProvisionPoll(t *testing.T) {

require.NoError(err)
require.Len(reservations, 2)
require.Equal(reservations[0].Type, provision.VolumeReservation)
require.Equal(reservations[1].Type, provision.ZDBReservation)
require.Equal(reservations[0].Type, provision.ZDBReservation)
require.Equal(reservations[1].Type, provision.VolumeReservation)
conn.AssertCalled(t, "Close")

}
Expand Down
8 changes: 5 additions & 3 deletions pkg/gedis/types/provision/tfgrid_reservation_network_1.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ func (r TfgridNetworkNetResource1) ToProvisionType() (pkg.NetResource, error) {

// WireguardPeer1 jsx Schema
type WireguardPeer1 struct {
PublicKey string `json:"public_key"`
Endpoint string `json:"endpoint"`
AllowedIPs []string `json:"allowed_iprange"`
PublicKey string `json:"public_key"`
Endpoint string `json:"endpoint"`
AllowedIPs []string `json:"allowed_iprange"`
IPRange schema.IPRange `json:"iprange"`
}

//ToProvisionType converts WireguardPeer1 to pkg.Peer
Expand All @@ -79,6 +80,7 @@ func (p WireguardPeer1) ToProvisionType() (pkg.Peer, error) {
WGPublicKey: p.PublicKey,
Endpoint: p.Endpoint,
AllowedIPs: make([]types.IPNet, len(p.AllowedIPs)),
Subnet: types.NewIPNetFromSchema(p.IPRange),
}

var err error
Expand Down

0 comments on commit 4b8b1ed

Please sign in to comment.