Skip to content

Commit d0d0a11

Browse files
committed
add endpoint to stop and resume a workload
1 parent e8aa47f commit d0d0a11

File tree

7 files changed

+75
-11
lines changed

7 files changed

+75
-11
lines changed

cmds/modules/provisiond/events.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (r *ContractEventHandler) sync(ctx context.Context) error {
107107
action = r.engine.Pause
108108
}
109109

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

179-
if err := action(ctx, event.TwinId, event.Contract); err != nil {
179+
if err := action(event.TwinId, event.Contract); err != nil {
180180
log.Error().Err(err).
181181
Uint32("twin", event.TwinId).
182182
Uint64("contract", event.Contract).

pkg/provision.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type Provision interface {
2020
Changes(twin uint32, contractID uint64) ([]gridtypes.Workload, error)
2121
ListPublicIPs() ([]string, error)
2222
ListPrivateIPs(twin uint32, network gridtypes.Name) ([]string, error)
23+
Pause(twin uint32, id uint64) error
24+
Resume(twin uint32, id uint64) error
2325
}
2426

2527
type Statistics interface {
@@ -30,6 +32,7 @@ type Statistics interface {
3032
GetCounters() (Counters, error)
3133
ListGPUs() ([]GPUInfo, error)
3234
OpenConnections() ([]byte, error)
35+
// Pause(id gridtypes.WorkloadID) error
3336
}
3437

3538
type Counters struct {

pkg/provision/engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func (e *NativeEngine) Provision(ctx context.Context, deployment gridtypes.Deplo
361361
}
362362

363363
// Pause deployment
364-
func (e *NativeEngine) Pause(ctx context.Context, twin uint32, id uint64) error {
364+
func (e *NativeEngine) Pause(twin uint32, id uint64) error {
365365
deployment, err := e.storage.Get(twin, id)
366366
if err != nil {
367367
return err
@@ -381,7 +381,7 @@ func (e *NativeEngine) Pause(ctx context.Context, twin uint32, id uint64) error
381381
}
382382

383383
// Resume deployment
384-
func (e *NativeEngine) Resume(ctx context.Context, twin uint32, id uint64) error {
384+
func (e *NativeEngine) Resume(twin uint32, id uint64) error {
385385
deployment, err := e.storage.Get(twin, id)
386386
if err != nil {
387387
return err

pkg/provision/interface.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type Engine interface {
1919
// and will be processes later
2020
Provision(ctx context.Context, wl gridtypes.Deployment) error
2121
Deprovision(ctx context.Context, twin uint32, id uint64, reason string) error
22-
Pause(ctx context.Context, twin uint32, id uint64) error
23-
Resume(ctx context.Context, twin uint32, id uint64) error
22+
Pause(twin uint32, id uint64) error
23+
Resume(twin uint32, id uint64) error
2424
Update(ctx context.Context, update gridtypes.Deployment) error
2525
Storage() Storage
2626
Twins() Twins
@@ -61,7 +61,7 @@ var (
6161
// ErrDeploymentConflict returned if deployment cannot be stored because
6262
// it conflicts with another deployment
6363
ErrDeploymentConflict = fmt.Errorf("conflict")
64-
//ErrDeploymentNotExists returned if object not exists
64+
// ErrDeploymentNotExists returned if object not exists
6565
ErrDeploymentNotExists = fmt.Errorf("deployment does not exist")
6666
// ErrWorkloadNotExist returned by storage if workload does not exist
6767
ErrWorkloadNotExist = fmt.Errorf("workload does not exist")
@@ -78,10 +78,12 @@ var (
7878
)
7979

8080
// Field interface
81-
type Field interface{}
82-
type VersionField struct {
83-
Version uint32
84-
}
81+
type (
82+
Field interface{}
83+
VersionField struct {
84+
Version uint32
85+
}
86+
)
8587

8688
type DescriptionField struct {
8789
Description string

pkg/stubs/provision_stub.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,33 @@ func (s *ProvisionStub) ListPublicIPs(ctx context.Context) (ret0 []string, ret1
159159
}
160160
return
161161
}
162+
163+
func (s *ProvisionStub) Pause(ctx context.Context, arg0 uint32, arg1 uint64) (ret0 error) {
164+
args := []interface{}{arg0, arg1}
165+
result, err := s.client.RequestContext(ctx, s.module, s.object, "Pause", args...)
166+
if err != nil {
167+
panic(err)
168+
}
169+
result.PanicOnError()
170+
ret0 = result.CallError()
171+
loader := zbus.Loader{}
172+
if err := result.Unmarshal(&loader); err != nil {
173+
panic(err)
174+
}
175+
return
176+
}
177+
178+
func (s *ProvisionStub) Resume(ctx context.Context, arg0 uint32, arg1 uint64) (ret0 error) {
179+
args := []interface{}{arg0, arg1}
180+
result, err := s.client.RequestContext(ctx, s.module, s.object, "Resume", args...)
181+
if err != nil {
182+
panic(err)
183+
}
184+
result.PanicOnError()
185+
ret0 = result.CallError()
186+
loader := zbus.Loader{}
187+
if err := result.Unmarshal(&loader); err != nil {
188+
panic(err)
189+
}
190+
return
191+
}

pkg/zos_api/admin.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,32 @@ func (g *ZosAPI) adminShowOpenConnectionsHandler(ctx context.Context, payload []
6464
return g.statisticsStub.OpenConnections(ctx)
6565
}
6666

67+
func (g *ZosAPI) adminStopWorkloadHandler(ctx context.Context, payload []byte) (interface{}, error) {
68+
type Payload struct {
69+
Twin uint32 `json:"twin"`
70+
ID uint64 `json:"id"`
71+
}
72+
var input Payload
73+
if err := json.Unmarshal(payload, &input); err != nil {
74+
return nil, fmt.Errorf("failed to decode input, expecting twin id and workload id: %w", err)
75+
}
76+
77+
return nil, g.provisionStub.Pause(ctx, input.Twin, input.ID)
78+
}
79+
80+
func (g *ZosAPI) adminResumeWorkloadHandler(ctx context.Context, payload []byte) (interface{}, error) {
81+
type Payload struct {
82+
Twin uint32 `json:"twin"`
83+
ID uint64 `json:"id"`
84+
}
85+
var input Payload
86+
if err := json.Unmarshal(payload, &input); err != nil {
87+
return nil, fmt.Errorf("failed to decode input, expecting twin id and workload id: %w", err)
88+
}
89+
90+
return nil, g.provisionStub.Resume(ctx, input.Twin, input.ID)
91+
}
92+
6793
func (g *ZosAPI) adminInterfacesHandler(ctx context.Context, payload []byte) (interface{}, error) {
6894
// list all interfaces on node
6995
type Interface struct {

pkg/zos_api/routes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) {
5353
admin.WithHandler("show_resolve", g.adminShowResolveHandler)
5454
admin.WithHandler("get_open_connections", g.adminShowOpenConnectionsHandler)
5555

56+
admin.WithHandler("stop_workload", g.adminStopWorkloadHandler)
57+
admin.WithHandler("resume_workload", g.adminResumeWorkloadHandler)
58+
5659
admin.WithHandler("interfaces", g.adminInterfacesHandler)
5760
admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler)
5861
admin.WithHandler("get_public_nic", g.adminGetPublicNICHandler)

0 commit comments

Comments
 (0)