@@ -41,6 +41,15 @@ type containerPack struct {
4141
4242// ContainerStats returns stats of the container.
4343func (c * Client ) ContainerStats (ctx context.Context , id string ) (* containerdtypes.Metric , error ) {
44+ metric , err := c .containerStats (ctx , id )
45+ if err != nil {
46+ return metric , convertCtrdErr (err )
47+ }
48+ return metric , nil
49+ }
50+
51+ // containerStats returns stats of the container.
52+ func (c * Client ) containerStats (ctx context.Context , id string ) (* containerdtypes.Metric , error ) {
4453 if ! c .lock .Trylock (id ) {
4554 return nil , errtypes .ErrLockfailed
4655 }
@@ -56,6 +65,14 @@ func (c *Client) ContainerStats(ctx context.Context, id string) (*containerdtype
5665
5766// ExecContainer executes a process in container.
5867func (c * Client ) ExecContainer (ctx context.Context , process * Process ) error {
68+ if err := c .execContainer (ctx , process ); err != nil {
69+ return convertCtrdErr (err )
70+ }
71+ return nil
72+ }
73+
74+ // execContainer executes a process in container.
75+ func (c * Client ) execContainer (ctx context.Context , process * Process ) error {
5976 pack , err := c .watch .get (process .ContainerID )
6077 if err != nil {
6178 return err
@@ -123,6 +140,15 @@ func (c *Client) ExecContainer(ctx context.Context, process *Process) error {
123140
124141// ContainerPID returns the container's init process id.
125142func (c * Client ) ContainerPID (ctx context.Context , id string ) (int , error ) {
143+ pid , err := c .containerPID (ctx , id )
144+ if err != nil {
145+ return pid , convertCtrdErr (err )
146+ }
147+ return pid , nil
148+ }
149+
150+ // containerPID returns the container's init process id.
151+ func (c * Client ) containerPID (ctx context.Context , id string ) (int , error ) {
126152 pack , err := c .watch .get (id )
127153 if err != nil {
128154 return - 1 , err
@@ -132,6 +158,15 @@ func (c *Client) ContainerPID(ctx context.Context, id string) (int, error) {
132158
133159// ContainerPIDs returns the all processes's ids inside the container.
134160func (c * Client ) ContainerPIDs (ctx context.Context , id string ) ([]int , error ) {
161+ pids , err := c .containerPIDs (ctx , id )
162+ if err != nil {
163+ return pids , convertCtrdErr (err )
164+ }
165+ return pids , nil
166+ }
167+
168+ // containerPIDs returns the all processes's ids inside the container.
169+ func (c * Client ) containerPIDs (ctx context.Context , id string ) ([]int , error ) {
135170 if ! c .lock .Trylock (id ) {
136171 return nil , errtypes .ErrLockfailed
137172 }
@@ -178,6 +213,14 @@ func (c *Client) ProbeContainer(ctx context.Context, id string, timeout time.Dur
178213
179214// RecoverContainer reload the container from metadata and watch it, if program be restarted.
180215func (c * Client ) RecoverContainer (ctx context.Context , id string , io * containerio.IO ) error {
216+ if err := c .recoverContainer (ctx , id , io ); err != nil {
217+ return convertCtrdErr (err )
218+ }
219+ return nil
220+ }
221+
222+ // recoverContainer reload the container from metadata and watch it, if program be restarted.
223+ func (c * Client ) recoverContainer (ctx context.Context , id string , io * containerio.IO ) error {
181224 wrapperCli , err := c .Get (ctx )
182225 if err != nil {
183226 return fmt .Errorf ("failed to get a containerd grpc client: %v" , err )
@@ -226,6 +269,15 @@ func (c *Client) RecoverContainer(ctx context.Context, id string, io *containeri
226269
227270// DestroyContainer kill container and delete it.
228271func (c * Client ) DestroyContainer (ctx context.Context , id string , timeout int64 ) (* Message , error ) {
272+ msg , err := c .destroyContainer (ctx , id , timeout )
273+ if err != nil {
274+ return msg , convertCtrdErr (err )
275+ }
276+ return msg , nil
277+ }
278+
279+ // DestroyContainer kill container and delete it.
280+ func (c * Client ) destroyContainer (ctx context.Context , id string , timeout int64 ) (* Message , error ) {
229281 // TODO(ziren): if we just want to stop a container,
230282 // we may need lease to lock the snapshot of container,
231283 // in case, it be deleted by gc.
@@ -294,8 +346,16 @@ clean:
294346 return msg , c .watch .remove (ctx , id )
295347}
296348
297- // PauseContainer pause container.
349+ // PauseContainer pauses container.
298350func (c * Client ) PauseContainer (ctx context.Context , id string ) error {
351+ if err := c .pauseContainer (ctx , id ); err != nil {
352+ return convertCtrdErr (err )
353+ }
354+ return nil
355+ }
356+
357+ // pauseContainer pause container.
358+ func (c * Client ) pauseContainer (ctx context.Context , id string ) error {
299359 if ! c .lock .Trylock (id ) {
300360 return errtypes .ErrLockfailed
301361 }
@@ -317,8 +377,16 @@ func (c *Client) PauseContainer(ctx context.Context, id string) error {
317377 return nil
318378}
319379
320- // UnpauseContainer unpauses a container.
380+ // UnpauseContainer unpauses container.
321381func (c * Client ) UnpauseContainer (ctx context.Context , id string ) error {
382+ if err := c .unpauseContainer (ctx , id ); err != nil {
383+ return convertCtrdErr (err )
384+ }
385+ return nil
386+ }
387+
388+ // unpauseContainer unpauses a container.
389+ func (c * Client ) unpauseContainer (ctx context.Context , id string ) error {
322390 if ! c .lock .Trylock (id ) {
323391 return errtypes .ErrLockfailed
324392 }
@@ -352,7 +420,10 @@ func (c *Client) CreateContainer(ctx context.Context, container *Container) erro
352420 }
353421 defer c .lock .Unlock (id )
354422
355- return c .createContainer (ctx , ref , id , container )
423+ if err := c .createContainer (ctx , ref , id , container ); err != nil {
424+ return convertCtrdErr (err )
425+ }
426+ return nil
356427}
357428
358429func (c * Client ) createContainer (ctx context.Context , ref , id string , container * Container ) (err0 error ) {
@@ -472,28 +543,16 @@ func (c *Client) createTask(ctx context.Context, id string, container containerd
472543 return pack , nil
473544}
474545
475- func (c * Client ) listContainerStore (ctx context.Context ) ([]string , error ) {
476- wrapperCli , err := c .Get (ctx )
477- if err != nil {
478- return nil , fmt .Errorf ("failed to get a containerd grpc client: %v" , err )
479- }
480-
481- containers , err := wrapperCli .client .ContainerService ().List (ctx )
482- if err != nil {
483- return nil , err
484- }
485-
486- var cs []string
487-
488- for _ , c := range containers {
489- cs = append (cs , c .ID )
546+ // UpdateResources updates the configurations of a container.
547+ func (c * Client ) UpdateResources (ctx context.Context , id string , resources types.Resources ) error {
548+ if err := c .updateResources (ctx , id , resources ); err != nil {
549+ return convertCtrdErr (err )
490550 }
491-
492- return cs , nil
551+ return nil
493552}
494553
495- // UpdateResources updates the configurations of a container.
496- func (c * Client ) UpdateResources (ctx context.Context , id string , resources types.Resources ) error {
554+ // updateResources updates the configurations of a container.
555+ func (c * Client ) updateResources (ctx context.Context , id string , resources types.Resources ) error {
497556 if ! c .lock .Trylock (id ) {
498557 return errtypes .ErrLockfailed
499558 }
@@ -515,6 +574,15 @@ func (c *Client) UpdateResources(ctx context.Context, id string, resources types
515574// ResizeContainer changes the size of the TTY of the init process running
516575// in the container to the given height and width.
517576func (c * Client ) ResizeContainer (ctx context.Context , id string , opts types.ResizeOptions ) error {
577+ if err := c .resizeContainer (ctx , id , opts ); err != nil {
578+ return convertCtrdErr (err )
579+ }
580+ return nil
581+ }
582+
583+ // resizeContainer changes the size of the TTY of the init process running
584+ // in the container to the given height and width.
585+ func (c * Client ) resizeContainer (ctx context.Context , id string , opts types.ResizeOptions ) error {
518586 if ! c .lock .Trylock (id ) {
519587 return errtypes .ErrLockfailed
520588 }
@@ -530,6 +598,15 @@ func (c *Client) ResizeContainer(ctx context.Context, id string, opts types.Resi
530598
531599// WaitContainer waits until container's status is stopped.
532600func (c * Client ) WaitContainer (ctx context.Context , id string ) (types.ContainerWaitOKBody , error ) {
601+ waitBody , err := c .waitContainer (ctx , id )
602+ if err != nil {
603+ return waitBody , convertCtrdErr (err )
604+ }
605+ return waitBody , nil
606+ }
607+
608+ // waitContainer waits until container's status is stopped.
609+ func (c * Client ) waitContainer (ctx context.Context , id string ) (types.ContainerWaitOKBody , error ) {
533610 wrapperCli , err := c .Get (ctx )
534611 if err != nil {
535612 return types.ContainerWaitOKBody {}, fmt .Errorf ("failed to get a containerd grpc client: %v" , err )
0 commit comments