github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/domain/infra/tunnel/pods.go (about)

     1  package tunnel
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/hanks177/podman/v4/libpod/define"
     7  	"github.com/hanks177/podman/v4/pkg/bindings/pods"
     8  	"github.com/hanks177/podman/v4/pkg/domain/entities"
     9  	"github.com/hanks177/podman/v4/pkg/util"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func (ic *ContainerEngine) PodExists(ctx context.Context, nameOrID string) (*entities.BoolReport, error) {
    14  	exists, err := pods.Exists(ic.ClientCtx, nameOrID, nil)
    15  	return &entities.BoolReport{Value: exists}, err
    16  }
    17  
    18  func (ic *ContainerEngine) PodKill(ctx context.Context, namesOrIds []string, opts entities.PodKillOptions) ([]*entities.PodKillReport, error) {
    19  	_, err := util.ParseSignal(opts.Signal)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	foundPods, err := getPodsByContext(ic.ClientCtx, opts.All, namesOrIds)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	reports := make([]*entities.PodKillReport, 0, len(foundPods))
    29  	options := new(pods.KillOptions).WithSignal(opts.Signal)
    30  	for _, p := range foundPods {
    31  		response, err := pods.Kill(ic.ClientCtx, p.Id, options)
    32  		if err != nil {
    33  			report := entities.PodKillReport{
    34  				Errs: []error{err},
    35  				Id:   p.Id,
    36  			}
    37  			reports = append(reports, &report)
    38  			continue
    39  		}
    40  		reports = append(reports, response)
    41  	}
    42  	return reports, nil
    43  }
    44  
    45  func (ic *ContainerEngine) PodLogs(ctx context.Context, nameOrIDs string, options entities.PodLogsOptions) error {
    46  	// PodLogsOptions are similar but contains few extra fields like ctrName
    47  	// So cast other values as is so we can re-use the code
    48  	containerLogsOpts := entities.PodLogsOptionsToContainerLogsOptions(options)
    49  
    50  	// interface only accepts slice, keep everything consistent
    51  	name := []string{options.ContainerName}
    52  	return ic.ContainerLogs(ctx, name, containerLogsOpts)
    53  }
    54  
    55  func (ic *ContainerEngine) PodPause(ctx context.Context, namesOrIds []string, options entities.PodPauseOptions) ([]*entities.PodPauseReport, error) {
    56  	foundPods, err := getPodsByContext(ic.ClientCtx, options.All, namesOrIds)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	reports := make([]*entities.PodPauseReport, 0, len(foundPods))
    61  	for _, p := range foundPods {
    62  		response, err := pods.Pause(ic.ClientCtx, p.Id, nil)
    63  		if err != nil {
    64  			report := entities.PodPauseReport{
    65  				Errs: []error{err},
    66  				Id:   p.Id,
    67  			}
    68  			reports = append(reports, &report)
    69  			continue
    70  		}
    71  		reports = append(reports, response)
    72  	}
    73  	return reports, nil
    74  }
    75  
    76  func (ic *ContainerEngine) PodUnpause(ctx context.Context, namesOrIds []string, options entities.PodunpauseOptions) ([]*entities.PodUnpauseReport, error) {
    77  	foundPods, err := getPodsByContext(ic.ClientCtx, options.All, namesOrIds)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	reports := make([]*entities.PodUnpauseReport, 0, len(foundPods))
    82  	for _, p := range foundPods {
    83  		response, err := pods.Unpause(ic.ClientCtx, p.Id, nil)
    84  		if err != nil {
    85  			report := entities.PodUnpauseReport{
    86  				Errs: []error{err},
    87  				Id:   p.Id,
    88  			}
    89  			reports = append(reports, &report)
    90  			continue
    91  		}
    92  		reports = append(reports, response)
    93  	}
    94  	return reports, nil
    95  }
    96  
    97  func (ic *ContainerEngine) PodStop(ctx context.Context, namesOrIds []string, opts entities.PodStopOptions) ([]*entities.PodStopReport, error) {
    98  	timeout := -1
    99  	foundPods, err := getPodsByContext(ic.ClientCtx, opts.All, namesOrIds)
   100  	if err != nil && !(opts.Ignore && errors.Cause(err) == define.ErrNoSuchPod) {
   101  		return nil, err
   102  	}
   103  	if opts.Timeout != -1 {
   104  		timeout = opts.Timeout
   105  	}
   106  	reports := make([]*entities.PodStopReport, 0, len(foundPods))
   107  	options := new(pods.StopOptions).WithTimeout(timeout)
   108  	for _, p := range foundPods {
   109  		response, err := pods.Stop(ic.ClientCtx, p.Id, options)
   110  		if err != nil {
   111  			report := entities.PodStopReport{
   112  				Errs: []error{err},
   113  				Id:   p.Id,
   114  			}
   115  			reports = append(reports, &report)
   116  			continue
   117  		}
   118  		reports = append(reports, response)
   119  	}
   120  	return reports, nil
   121  }
   122  
   123  func (ic *ContainerEngine) PodRestart(ctx context.Context, namesOrIds []string, options entities.PodRestartOptions) ([]*entities.PodRestartReport, error) {
   124  	foundPods, err := getPodsByContext(ic.ClientCtx, options.All, namesOrIds)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  	reports := make([]*entities.PodRestartReport, 0, len(foundPods))
   129  	for _, p := range foundPods {
   130  		response, err := pods.Restart(ic.ClientCtx, p.Id, nil)
   131  		if err != nil {
   132  			report := entities.PodRestartReport{
   133  				Errs: []error{err},
   134  				Id:   p.Id,
   135  			}
   136  			reports = append(reports, &report)
   137  			continue
   138  		}
   139  		reports = append(reports, response)
   140  	}
   141  	return reports, nil
   142  }
   143  
   144  func (ic *ContainerEngine) PodStart(ctx context.Context, namesOrIds []string, options entities.PodStartOptions) ([]*entities.PodStartReport, error) {
   145  	foundPods, err := getPodsByContext(ic.ClientCtx, options.All, namesOrIds)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	reports := make([]*entities.PodStartReport, 0, len(foundPods))
   150  	for _, p := range foundPods {
   151  		response, err := pods.Start(ic.ClientCtx, p.Id, nil)
   152  		if err != nil {
   153  			report := entities.PodStartReport{
   154  				Errs: []error{err},
   155  				Id:   p.Id,
   156  			}
   157  			reports = append(reports, &report)
   158  			continue
   159  		}
   160  		reports = append(reports, response)
   161  	}
   162  	return reports, nil
   163  }
   164  
   165  func (ic *ContainerEngine) PodRm(ctx context.Context, namesOrIds []string, opts entities.PodRmOptions) ([]*entities.PodRmReport, error) {
   166  	foundPods, err := getPodsByContext(ic.ClientCtx, opts.All, namesOrIds)
   167  	if err != nil && !(opts.Ignore && errors.Cause(err) == define.ErrNoSuchPod) {
   168  		return nil, err
   169  	}
   170  	reports := make([]*entities.PodRmReport, 0, len(foundPods))
   171  	options := new(pods.RemoveOptions).WithForce(opts.Force)
   172  	if opts.Timeout != nil {
   173  		options = options.WithTimeout(*opts.Timeout)
   174  	}
   175  	for _, p := range foundPods {
   176  		response, err := pods.Remove(ic.ClientCtx, p.Id, options)
   177  		if err != nil {
   178  			report := entities.PodRmReport{
   179  				Err: err,
   180  				Id:  p.Id,
   181  			}
   182  			reports = append(reports, &report)
   183  			continue
   184  		}
   185  		reports = append(reports, response)
   186  	}
   187  	return reports, nil
   188  }
   189  
   190  func (ic *ContainerEngine) PodPrune(ctx context.Context, opts entities.PodPruneOptions) ([]*entities.PodPruneReport, error) {
   191  	return pods.Prune(ic.ClientCtx, nil)
   192  }
   193  
   194  func (ic *ContainerEngine) PodCreate(ctx context.Context, specg entities.PodSpec) (*entities.PodCreateReport, error) {
   195  	return pods.CreatePodFromSpec(ic.ClientCtx, &specg)
   196  }
   197  
   198  func (ic *ContainerEngine) PodTop(ctx context.Context, opts entities.PodTopOptions) (*entities.StringSliceReport, error) {
   199  	switch {
   200  	case opts.Latest:
   201  		return nil, errors.New("latest is not supported")
   202  	case opts.NameOrID == "":
   203  		return nil, errors.New("NameOrID must be specified")
   204  	}
   205  	options := new(pods.TopOptions).WithDescriptors(opts.Descriptors)
   206  	topOutput, err := pods.Top(ic.ClientCtx, opts.NameOrID, options)
   207  	if err != nil {
   208  		return nil, err
   209  	}
   210  	return &entities.StringSliceReport{Value: topOutput}, nil
   211  }
   212  
   213  func (ic *ContainerEngine) PodPs(ctx context.Context, opts entities.PodPSOptions) ([]*entities.ListPodsReport, error) {
   214  	options := new(pods.ListOptions).WithFilters(opts.Filters)
   215  	return pods.List(ic.ClientCtx, options)
   216  }
   217  
   218  func (ic *ContainerEngine) PodInspect(ctx context.Context, options entities.PodInspectOptions) (*entities.PodInspectReport, error) {
   219  	switch {
   220  	case options.Latest:
   221  		return nil, errors.New("latest is not supported")
   222  	case options.NameOrID == "":
   223  		return nil, errors.New("NameOrID must be specified")
   224  	}
   225  	return pods.Inspect(ic.ClientCtx, options.NameOrID, nil)
   226  }
   227  
   228  func (ic *ContainerEngine) PodStats(ctx context.Context, namesOrIds []string, opts entities.PodStatsOptions) ([]*entities.PodStatsReport, error) {
   229  	options := new(pods.StatsOptions).WithAll(opts.All)
   230  	return pods.Stats(ic.ClientCtx, namesOrIds, options)
   231  }