github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/domain/infra/tunnel/pods.go (about)

     1  package tunnel
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/containers/libpod/pkg/bindings/pods"
     7  	"github.com/containers/libpod/pkg/domain/entities"
     8  	"github.com/containers/libpod/pkg/specgen"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  func (ic *ContainerEngine) PodExists(ctx context.Context, nameOrId string) (*entities.BoolReport, error) {
    13  	exists, err := pods.Exists(ic.ClientCxt, nameOrId)
    14  	return &entities.BoolReport{Value: exists}, err
    15  }
    16  
    17  func (ic *ContainerEngine) PodKill(ctx context.Context, namesOrIds []string, options entities.PodKillOptions) ([]*entities.PodKillReport, error) {
    18  	var (
    19  		reports []*entities.PodKillReport
    20  	)
    21  	foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	for _, p := range foundPods {
    26  		response, err := pods.Kill(ic.ClientCxt, p.Id, &options.Signal)
    27  		if err != nil {
    28  			report := entities.PodKillReport{
    29  				Errs: []error{err},
    30  				Id:   p.Id,
    31  			}
    32  			reports = append(reports, &report)
    33  			continue
    34  		}
    35  		reports = append(reports, response)
    36  	}
    37  	return reports, nil
    38  }
    39  
    40  func (ic *ContainerEngine) PodPause(ctx context.Context, namesOrIds []string, options entities.PodPauseOptions) ([]*entities.PodPauseReport, error) {
    41  	var (
    42  		reports []*entities.PodPauseReport
    43  	)
    44  	foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	for _, p := range foundPods {
    49  		response, err := pods.Pause(ic.ClientCxt, p.Id)
    50  		if err != nil {
    51  			report := entities.PodPauseReport{
    52  				Errs: []error{err},
    53  				Id:   p.Id,
    54  			}
    55  			reports = append(reports, &report)
    56  			continue
    57  		}
    58  		reports = append(reports, response)
    59  	}
    60  	return reports, nil
    61  }
    62  
    63  func (ic *ContainerEngine) PodUnpause(ctx context.Context, namesOrIds []string, options entities.PodunpauseOptions) ([]*entities.PodUnpauseReport, error) {
    64  	var (
    65  		reports []*entities.PodUnpauseReport
    66  	)
    67  	foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	for _, p := range foundPods {
    72  		response, err := pods.Unpause(ic.ClientCxt, p.Id)
    73  		if err != nil {
    74  			report := entities.PodUnpauseReport{
    75  				Errs: []error{err},
    76  				Id:   p.Id,
    77  			}
    78  			reports = append(reports, &report)
    79  			continue
    80  		}
    81  		reports = append(reports, response)
    82  	}
    83  	return reports, nil
    84  }
    85  
    86  func (ic *ContainerEngine) PodStop(ctx context.Context, namesOrIds []string, options entities.PodStopOptions) ([]*entities.PodStopReport, error) {
    87  	var (
    88  		reports []*entities.PodStopReport
    89  		timeout int = -1
    90  	)
    91  	foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	if options.Timeout != -1 {
    96  		timeout = options.Timeout
    97  	}
    98  	for _, p := range foundPods {
    99  		response, err := pods.Stop(ic.ClientCxt, p.Id, &timeout)
   100  		if err != nil {
   101  			report := entities.PodStopReport{
   102  				Errs: []error{err},
   103  				Id:   p.Id,
   104  			}
   105  			reports = append(reports, &report)
   106  			continue
   107  		}
   108  		reports = append(reports, response)
   109  	}
   110  	return reports, nil
   111  }
   112  
   113  func (ic *ContainerEngine) PodRestart(ctx context.Context, namesOrIds []string, options entities.PodRestartOptions) ([]*entities.PodRestartReport, error) {
   114  	var reports []*entities.PodRestartReport
   115  	foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	for _, p := range foundPods {
   120  		response, err := pods.Restart(ic.ClientCxt, p.Id)
   121  		if err != nil {
   122  			report := entities.PodRestartReport{
   123  				Errs: []error{err},
   124  				Id:   p.Id,
   125  			}
   126  			reports = append(reports, &report)
   127  			continue
   128  		}
   129  		reports = append(reports, response)
   130  	}
   131  	return reports, nil
   132  }
   133  
   134  func (ic *ContainerEngine) PodStart(ctx context.Context, namesOrIds []string, options entities.PodStartOptions) ([]*entities.PodStartReport, error) {
   135  	var reports []*entities.PodStartReport
   136  	foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  	for _, p := range foundPods {
   141  		response, err := pods.Start(ic.ClientCxt, p.Id)
   142  		if err != nil {
   143  			report := entities.PodStartReport{
   144  				Errs: []error{err},
   145  				Id:   p.Id,
   146  			}
   147  			reports = append(reports, &report)
   148  			continue
   149  		}
   150  		reports = append(reports, response)
   151  	}
   152  	return reports, nil
   153  }
   154  
   155  func (ic *ContainerEngine) PodRm(ctx context.Context, namesOrIds []string, options entities.PodRmOptions) ([]*entities.PodRmReport, error) {
   156  	var reports []*entities.PodRmReport
   157  	foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds)
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  	for _, p := range foundPods {
   162  		response, err := pods.Remove(ic.ClientCxt, p.Id, &options.Force)
   163  		if err != nil {
   164  			report := entities.PodRmReport{
   165  				Err: err,
   166  				Id:  p.Id,
   167  			}
   168  			reports = append(reports, &report)
   169  			continue
   170  		}
   171  		reports = append(reports, response)
   172  	}
   173  	return reports, nil
   174  }
   175  
   176  func (ic *ContainerEngine) PodCreate(ctx context.Context, opts entities.PodCreateOptions) (*entities.PodCreateReport, error) {
   177  	podSpec := specgen.NewPodSpecGenerator()
   178  	opts.ToPodSpecGen(podSpec)
   179  	return pods.CreatePodFromSpec(ic.ClientCxt, podSpec)
   180  }
   181  
   182  func (ic *ContainerEngine) PodTop(ctx context.Context, options entities.PodTopOptions) (*entities.StringSliceReport, error) {
   183  	switch {
   184  	case options.Latest:
   185  		return nil, errors.New("latest is not supported")
   186  	case options.NameOrID == "":
   187  		return nil, errors.New("NameOrID must be specified")
   188  	}
   189  
   190  	topOutput, err := pods.Top(ic.ClientCxt, options.NameOrID, options.Descriptors)
   191  	if err != nil {
   192  		return nil, err
   193  	}
   194  	return &entities.StringSliceReport{Value: topOutput}, nil
   195  }
   196  
   197  func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOptions) ([]*entities.ListPodsReport, error) {
   198  	return pods.List(ic.ClientCxt, options.Filters)
   199  }
   200  
   201  func (ic *ContainerEngine) PodInspect(ctx context.Context, options entities.PodInspectOptions) (*entities.PodInspectReport, error) {
   202  	switch {
   203  	case options.Latest:
   204  		return nil, errors.New("latest is not supported")
   205  	case options.NameOrID == "":
   206  		return nil, errors.New("NameOrID must be specified")
   207  	}
   208  	return pods.Inspect(ic.ClientCxt, options.NameOrID)
   209  }