github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/domain/infra/tunnel/helpers.go (about)

     1  package tunnel
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/containers/podman/v2/libpod/define"
     7  	"github.com/containers/podman/v2/pkg/bindings"
     8  	"github.com/containers/podman/v2/pkg/bindings/containers"
     9  	"github.com/containers/podman/v2/pkg/bindings/pods"
    10  	"github.com/containers/podman/v2/pkg/domain/entities"
    11  	"github.com/containers/podman/v2/pkg/errorhandling"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // FIXME: the `ignore` parameter is very likely wrong here as it should rather
    16  //        be used on *errors* from operations such as remove.
    17  func getContainersByContext(contextWithConnection context.Context, all, ignore bool, namesOrIDs []string) ([]entities.ListContainer, error) {
    18  	if all && len(namesOrIDs) > 0 {
    19  		return nil, errors.New("cannot lookup containers and all")
    20  	}
    21  
    22  	allContainers, err := containers.List(contextWithConnection, nil, bindings.PTrue, nil, nil, nil, bindings.PTrue)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	if all {
    27  		return allContainers, err
    28  	}
    29  
    30  	// Note: it would be nicer if the lists endpoint would support that as
    31  	// we could use the libpod backend for looking up containers rather
    32  	// than risking diverging the local and remote lookups.
    33  	//
    34  	// A `--filter nameOrId=abc` that can be specified multiple times would
    35  	// be awesome to have.
    36  	filtered := []entities.ListContainer{}
    37  	for _, nameOrID := range namesOrIDs {
    38  		// First determine if the container exists by doing an inspect.
    39  		// Inspect takes supports names and IDs and let's us determine
    40  		// a containers full ID.
    41  		inspectData, err := containers.Inspect(contextWithConnection, nameOrID, bindings.PFalse)
    42  		if err != nil {
    43  			if ignore && errorhandling.Contains(err, define.ErrNoSuchCtr) {
    44  				continue
    45  			}
    46  			return nil, err
    47  		}
    48  
    49  		// Now we can do a full match of the ID to find the right
    50  		// container. Note that we *really* need a full ID match to
    51  		// prevent any ambiguities between IDs and names (see #7837).
    52  		found := false
    53  		for _, ctr := range allContainers {
    54  			if ctr.ID == inspectData.ID {
    55  				filtered = append(filtered, ctr)
    56  				found = true
    57  				break
    58  			}
    59  
    60  		}
    61  
    62  		if !found && !ignore {
    63  			return nil, errors.Wrapf(define.ErrNoSuchCtr, "unable to find container %q", nameOrID)
    64  		}
    65  	}
    66  	return filtered, nil
    67  }
    68  
    69  func getPodsByContext(contextWithConnection context.Context, all bool, namesOrIDs []string) ([]*entities.ListPodsReport, error) {
    70  	if all && len(namesOrIDs) > 0 {
    71  		return nil, errors.New("cannot lookup specific pods and all")
    72  	}
    73  
    74  	allPods, err := pods.List(contextWithConnection, nil)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	if all {
    79  		return allPods, nil
    80  	}
    81  
    82  	filtered := []*entities.ListPodsReport{}
    83  	// Note: it would be nicer if the lists endpoint would support that as
    84  	// we could use the libpod backend for looking up pods rather than
    85  	// risking diverging the local and remote lookups.
    86  	//
    87  	// A `--filter nameOrId=abc` that can be specified multiple times would
    88  	// be awesome to have.
    89  	for _, nameOrID := range namesOrIDs {
    90  		// First determine if the pod exists by doing an inspect.
    91  		// Inspect takes supports names and IDs and let's us determine
    92  		// a containers full ID.
    93  		inspectData, err := pods.Inspect(contextWithConnection, nameOrID)
    94  		if err != nil {
    95  			if errorhandling.Contains(err, define.ErrNoSuchPod) {
    96  				return nil, errors.Wrapf(define.ErrNoSuchPod, "unable to find pod %q", nameOrID)
    97  			}
    98  			return nil, err
    99  		}
   100  
   101  		// Now we can do a full match of the ID to find the right pod.
   102  		// Note that we *really* need a full ID match to prevent any
   103  		// ambiguities between IDs and names (see #7837).
   104  		found := false
   105  		for _, pod := range allPods {
   106  			if pod.Id == inspectData.ID {
   107  				filtered = append(filtered, pod)
   108  				found = true
   109  				break
   110  			}
   111  
   112  		}
   113  
   114  		if !found {
   115  			return nil, errors.Wrapf(define.ErrNoSuchPod, "unable to find pod %q", nameOrID)
   116  		}
   117  	}
   118  	return filtered, nil
   119  }