Repository: threefoldtech/zos
Pull request #572 (2020-03-13 12:49:43)

Decomission workload


Patch:
diff --git a/pkg/gedis/commands_provision.go b/pkg/gedis/commands_provision.go
index a793a95c..bad14866 100644
--- a/pkg/gedis/commands_provision.go
+++ b/pkg/gedis/commands_provision.go
@@ -110,7 +110,7 @@ func (g *Gedis) Poll(nodeID pkg.Identifier, from uint64) ([]*provision.Reservati
 	}))
 
 	if err != nil {
-		return nil, provision.ErrTemporary
+		return nil, provision.NewErrTemporary(err)
 	}
 
 	var out struct {
@@ -218,6 +218,7 @@ func reservationFromSchema(w ptypes.TfgridReservationWorkload1) (*provision.Rese
 		Signature: []byte(w.Signature),
 		Data:      w.Workload,
 		Tag:       provision.Tag{"source": "BCDB"},
+		ToDelete:  w.ToDelete,
 	}
 
 	var (
diff --git a/pkg/provision/engine.go b/pkg/provision/engine.go
index 06f56f10..b55d4db8 100644
--- a/pkg/provision/engine.go
+++ b/pkg/provision/engine.go
@@ -74,14 +74,17 @@ func (e *defaultEngine) Run(ctx context.Context) error {
 				return nil
 			}
 
+			expired := reservation.Expired()
 			slog := log.With().
 				Str("id", string(reservation.ID)).
 				Str("type", string(reservation.Type)).
 				Str("duration", fmt.Sprintf("%v", reservation.Duration)).
 				Str("tag", reservation.Tag.String()).
+				Bool("to delete", reservation.ToDelete).
+				Bool("expired", expired).
 				Logger()
 
-			if reservation.Expired() || reservation.ToDelete {
+			if expired || reservation.ToDelete {
 				slog.Info().Msg("start decommissioning reservation")
 				if err := e.decommission(ctx, reservation); err != nil {
 					log.Error().Err(err).Msgf("failed to decommission reservation %s", reservation.ID)
diff --git a/pkg/provision/provision.go b/pkg/provision/provision.go
index 26041963..87d37dd2 100644
--- a/pkg/provision/provision.go
+++ b/pkg/provision/provision.go
@@ -10,7 +10,6 @@ package provision
 import (
 	"context"
 
-	"github.com/pkg/errors"
 	"github.com/threefoldtech/zos/pkg"
 )
 
@@ -58,4 +57,27 @@ var (
 
 // ErrTemporary is return when a reservation source failed to contact the BCDB
 // user usually want to retry after getting this error
-var ErrTemporary = errors.New("network error contacting BCDB")
+type ErrTemporary struct {
+	err error
+}
+
+// NewErrTemporary wrap an error and mark it as temporary
+func NewErrTemporary(err error) error {
+	return ErrTemporary{err: err}
+}
+
+// Error implements the errors.Error interface
+func (e ErrTemporary) Error() string {
+	return e.err.Error()
+}
+
+// Is implements errors.Is interface
+func (e ErrTemporary) Is(target error) bool {
+	_, ok := target.(ErrTemporary)
+	return ok
+}
+
+// Unwrap implements errors.Unwrap interface
+func (e ErrTemporary) Unwrap() error {
+	return e.err
+}
diff --git a/pkg/provision/remote_store.go b/pkg/provision/remote_store.go
index 8183f3ad..1d62e243 100644
--- a/pkg/provision/remote_store.go
+++ b/pkg/provision/remote_store.go
@@ -71,17 +71,17 @@ func (s *HTTPStore) Poll(nodeID pkg.Identifier, from uint64) ([]*Reservation, er
 
 	resp, err := http.Get(u.String())
 	if err != nil {
-		return nil, ErrTemporary
+		return nil, NewErrTemporary(err)
 	}
 
 	defer resp.Body.Close()
 
 	if resp.StatusCode != http.StatusOK {
-		return nil, ErrTemporary
+		return nil, NewErrTemporary(err)
 	}
 
 	if resp.Header.Get("content-type") != "application/json" {
-		return nil, ErrTemporary
+		return nil, NewErrTemporary(err)
 	}
 
 	reservations := []*Reservation{}
diff --git a/pkg/provision/source.go b/pkg/provision/source.go
index daab7ff8..e5132afc 100644
--- a/pkg/provision/source.go
+++ b/pkg/provision/source.go
@@ -68,7 +68,7 @@ func (s *pollSource) Reservations(ctx context.Context) <-chan *Reservation {
 				log.Error().Err(err).Msg("failed to get reservation")
 				// if this is not a temporary error, then skip the reservation entirely
 				// and try to get the next one
-				if !errors.Is(err, ErrTemporary) {
+				if !errors.Is(err, ErrTemporary{}) {
 					next++
 				}
 				continue


Test patch:
diff --git a/pkg/provision/provision_test.go b/pkg/provision/provision_test.go
index 5ea40534..e2085fc9 100644
--- a/pkg/provision/provision_test.go
+++ b/pkg/provision/provision_test.go
@@ -3,8 +3,11 @@ package provision
 import (
 	"context"
 	"encoding/json"
+	"fmt"
 	"testing"
 
+	"github.com/pkg/errors"
+	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/mock"
 	"github.com/stretchr/testify/require"
 	"github.com/threefoldtech/zbus"
@@ -64,3 +67,13 @@ func (t *TestOwnerCache) OwnerOf(id string) (string, error) {
 	result := t.Called(id)
 	return result.String(0), result.Error(1)
 }
+
+func TestErrTemporary(t *testing.T) {
+	err := fmt.Errorf("base error")
+	wrap1 := NewErrTemporary(err)
+	wrap2 := fmt.Errorf("upper err: %w", wrap1)
+
+	assert.True(t, errors.Is(wrap1, ErrTemporary{}))
+	assert.True(t, errors.Is(wrap2, ErrTemporary{}))
+	assert.Equal(t, err.Error(), wrap1.Error())
+}