diff --git a/pkg/provision/explorer/source.go b/pkg/provision/explorer/source.go index 56b1ca63c..b6a398399 100644 --- a/pkg/provision/explorer/source.go +++ b/pkg/provision/explorer/source.go @@ -1,12 +1,16 @@ package explorer import ( + "errors" "fmt" "sort" + "github.com/rs/zerolog/log" + "github.com/threefoldtech/tfexplorer/client" "github.com/threefoldtech/zos/pkg" "github.com/threefoldtech/zos/pkg/provision" + "github.com/threefoldtech/zos/pkg/provision/primitives" ) // Poller is an implementation of the provision.ReservationPoller @@ -41,6 +45,10 @@ func (r *Poller) Poll(nodeID pkg.Identifier, from uint64) ([]*provision.Reservat for _, wl := range list { r, err := r.inputConv(wl) if err != nil { + if errors.Is(err, primitives.ErrUnsupportedWorkload) { + log.Warn().Err(err).Msgf("received unsupported workload, skipping") + continue + } return nil, 0, err } diff --git a/pkg/provision/explorer/source_test.go b/pkg/provision/explorer/source_test.go new file mode 100644 index 000000000..de8392f48 --- /dev/null +++ b/pkg/provision/explorer/source_test.go @@ -0,0 +1,71 @@ +package explorer + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/threefoldtech/tfexplorer/client" + "github.com/threefoldtech/tfexplorer/models/generated/workloads" + wrklds "github.com/threefoldtech/tfexplorer/pkg/workloads" + "github.com/threefoldtech/tfexplorer/schema" + "github.com/threefoldtech/zos/pkg" + "github.com/threefoldtech/zos/pkg/provision/primitives" +) + +type clientMock struct { + workloads []workloads.ReservationWorkload +} + +func (c *clientMock) Create(reservation workloads.Reservation) (resp wrklds.ReservationCreateResponse, err error) { + return +} +func (c *clientMock) List(nextAction *workloads.NextActionEnum, customerTid int64, page *client.Pager) (reservation []workloads.Reservation, err error) { + return +} +func (c *clientMock) Get(id schema.ID) (reservation workloads.Reservation, err error) { + return +} +func (c *clientMock) SignProvision(id schema.ID, user schema.ID, signature string) error { + return nil +} +func (c *clientMock) SignDelete(id schema.ID, user schema.ID, signature string) error { + return nil +} + +func (c *clientMock) Workloads(nodeID string, from uint64) ([]workloads.ReservationWorkload, uint64, error) { + return c.workloads, 0, nil +} +func (c *clientMock) WorkloadGet(gwid string) (result workloads.ReservationWorkload, err error) { + return +} +func (c *clientMock) WorkloadPutResult(nodeID, gwid string, result workloads.Result) error { + return nil +} +func (c *clientMock) WorkloadPutDeleted(nodeID, gwid string) error { + return nil +} + +func TestSkipUnsupportedType(t *testing.T) { + type UnsupportedWorkload struct{} + + client := &clientMock{ + workloads: []workloads.ReservationWorkload{ + { + Content: workloads.Container{}, + }, + { + Content: UnsupportedWorkload{}, + }, + }, + } + + p := &Poller{ + wl: client, + inputConv: primitives.WorkloadToProvisionType, + } + + result, _, err := p.Poll(pkg.StrIdentifier(""), 0) + assert.NoError(t, err) + assert.Equal(t, 1, len(result)) +} diff --git a/pkg/provision/primitives/converter.go b/pkg/provision/primitives/converter.go index 800424925..216913ed5 100644 --- a/pkg/provision/primitives/converter.go +++ b/pkg/provision/primitives/converter.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/pkg/errors" "github.com/threefoldtech/tfexplorer/models/generated/workloads" "github.com/threefoldtech/tfexplorer/schema" "github.com/threefoldtech/zos/pkg" @@ -16,6 +17,10 @@ import ( "github.com/threefoldtech/zos/pkg/provision" ) +// ErrUnsupportedWorkload is return when a workload of a type not supported by +// provisiond is received from the explorer +var ErrUnsupportedWorkload = errors.New("workload type not supported") + // ContainerToProvisionType converts TfgridReservationContainer1 to Container func ContainerToProvisionType(c workloads.Container, reservationID string) (Container, string, error) { container := Container{ @@ -259,7 +264,7 @@ func WorkloadToProvisionType(w workloads.ReservationWorkload) (*provision.Reserv return nil, err } default: - return nil, fmt.Errorf("unknown workload type (%s) (%T)", w.Type.String(), tmp) + return nil, fmt.Errorf("%w (%s) (%T)", ErrUnsupportedWorkload, w.Type.String(), tmp) } reservation.Data, err = json.Marshal(data)