Skip to content

Commit

Permalink
add endpoint to stop and resume a workload
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Jan 1, 2025
1 parent e8aa47f commit d0d0a11
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmds/modules/provisiond/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (r *ContractEventHandler) sync(ctx context.Context) error {
action = r.engine.Pause
}

if err := action(ctx, dl.TwinID, dl.ContractID); err != nil {
if err := action(dl.TwinID, dl.ContractID); err != nil {
log.Error().Err(err).Msg("failed to change contract state")
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func (r *ContractEventHandler) Run(ctx context.Context) error {
action = r.engine.Pause
}

if err := action(ctx, event.TwinId, event.Contract); err != nil {
if err := action(event.TwinId, event.Contract); err != nil {
log.Error().Err(err).
Uint32("twin", event.TwinId).
Uint64("contract", event.Contract).
Expand Down
3 changes: 3 additions & 0 deletions pkg/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Provision interface {
Changes(twin uint32, contractID uint64) ([]gridtypes.Workload, error)
ListPublicIPs() ([]string, error)
ListPrivateIPs(twin uint32, network gridtypes.Name) ([]string, error)
Pause(twin uint32, id uint64) error
Resume(twin uint32, id uint64) error
}

type Statistics interface {
Expand All @@ -30,6 +32,7 @@ type Statistics interface {
GetCounters() (Counters, error)
ListGPUs() ([]GPUInfo, error)
OpenConnections() ([]byte, error)
// Pause(id gridtypes.WorkloadID) error
}

type Counters struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/provision/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func (e *NativeEngine) Provision(ctx context.Context, deployment gridtypes.Deplo
}

// Pause deployment
func (e *NativeEngine) Pause(ctx context.Context, twin uint32, id uint64) error {
func (e *NativeEngine) Pause(twin uint32, id uint64) error {
deployment, err := e.storage.Get(twin, id)
if err != nil {
return err
Expand All @@ -381,7 +381,7 @@ func (e *NativeEngine) Pause(ctx context.Context, twin uint32, id uint64) error
}

// Resume deployment
func (e *NativeEngine) Resume(ctx context.Context, twin uint32, id uint64) error {
func (e *NativeEngine) Resume(twin uint32, id uint64) error {
deployment, err := e.storage.Get(twin, id)
if err != nil {
return err
Expand Down
16 changes: 9 additions & 7 deletions pkg/provision/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type Engine interface {
// and will be processes later
Provision(ctx context.Context, wl gridtypes.Deployment) error
Deprovision(ctx context.Context, twin uint32, id uint64, reason string) error
Pause(ctx context.Context, twin uint32, id uint64) error
Resume(ctx context.Context, twin uint32, id uint64) error
Pause(twin uint32, id uint64) error
Resume(twin uint32, id uint64) error
Update(ctx context.Context, update gridtypes.Deployment) error
Storage() Storage
Twins() Twins
Expand Down Expand Up @@ -61,7 +61,7 @@ var (
// ErrDeploymentConflict returned if deployment cannot be stored because
// it conflicts with another deployment
ErrDeploymentConflict = fmt.Errorf("conflict")
//ErrDeploymentNotExists returned if object not exists
// ErrDeploymentNotExists returned if object not exists
ErrDeploymentNotExists = fmt.Errorf("deployment does not exist")
// ErrWorkloadNotExist returned by storage if workload does not exist
ErrWorkloadNotExist = fmt.Errorf("workload does not exist")
Expand All @@ -78,10 +78,12 @@ var (
)

// Field interface
type Field interface{}
type VersionField struct {
Version uint32
}
type (
Field interface{}
VersionField struct {
Version uint32
}
)

type DescriptionField struct {
Description string
Expand Down
30 changes: 30 additions & 0 deletions pkg/stubs/provision_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,33 @@ func (s *ProvisionStub) ListPublicIPs(ctx context.Context) (ret0 []string, ret1
}
return
}

func (s *ProvisionStub) Pause(ctx context.Context, arg0 uint32, arg1 uint64) (ret0 error) {
args := []interface{}{arg0, arg1}
result, err := s.client.RequestContext(ctx, s.module, s.object, "Pause", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
ret0 = result.CallError()
loader := zbus.Loader{}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}

func (s *ProvisionStub) Resume(ctx context.Context, arg0 uint32, arg1 uint64) (ret0 error) {
args := []interface{}{arg0, arg1}
result, err := s.client.RequestContext(ctx, s.module, s.object, "Resume", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
ret0 = result.CallError()
loader := zbus.Loader{}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}
26 changes: 26 additions & 0 deletions pkg/zos_api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ func (g *ZosAPI) adminShowOpenConnectionsHandler(ctx context.Context, payload []
return g.statisticsStub.OpenConnections(ctx)
}

func (g *ZosAPI) adminStopWorkloadHandler(ctx context.Context, payload []byte) (interface{}, error) {
type Payload struct {
Twin uint32 `json:"twin"`
ID uint64 `json:"id"`
}
var input Payload
if err := json.Unmarshal(payload, &input); err != nil {
return nil, fmt.Errorf("failed to decode input, expecting twin id and workload id: %w", err)
}

return nil, g.provisionStub.Pause(ctx, input.Twin, input.ID)
}

func (g *ZosAPI) adminResumeWorkloadHandler(ctx context.Context, payload []byte) (interface{}, error) {
type Payload struct {
Twin uint32 `json:"twin"`
ID uint64 `json:"id"`
}
var input Payload
if err := json.Unmarshal(payload, &input); err != nil {
return nil, fmt.Errorf("failed to decode input, expecting twin id and workload id: %w", err)
}

return nil, g.provisionStub.Resume(ctx, input.Twin, input.ID)
}

func (g *ZosAPI) adminInterfacesHandler(ctx context.Context, payload []byte) (interface{}, error) {
// list all interfaces on node
type Interface struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/zos_api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) {
admin.WithHandler("show_resolve", g.adminShowResolveHandler)
admin.WithHandler("get_open_connections", g.adminShowOpenConnectionsHandler)

admin.WithHandler("stop_workload", g.adminStopWorkloadHandler)
admin.WithHandler("resume_workload", g.adminResumeWorkloadHandler)

admin.WithHandler("interfaces", g.adminInterfacesHandler)
admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler)
admin.WithHandler("get_public_nic", g.adminGetPublicNICHandler)
Expand Down

0 comments on commit d0d0a11

Please sign in to comment.