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

     1  //go:build remote
     2  // +build remote
     3  
     4  package infra
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"sync"
    10  
    11  	"github.com/hanks177/podman/v4/pkg/bindings"
    12  	"github.com/hanks177/podman/v4/pkg/domain/entities"
    13  	"github.com/hanks177/podman/v4/pkg/domain/infra/tunnel"
    14  )
    15  
    16  var (
    17  	connectionMutex = &sync.Mutex{}
    18  	connection      *context.Context
    19  )
    20  
    21  func newConnection(uri string, identity string) (context.Context, error) {
    22  	connectionMutex.Lock()
    23  	defer connectionMutex.Unlock()
    24  
    25  	if connection == nil {
    26  		ctx, err := bindings.NewConnectionWithIdentity(context.Background(), uri, identity)
    27  		if err != nil {
    28  			return ctx, err
    29  		}
    30  		connection = &ctx
    31  	}
    32  	return *connection, nil
    33  }
    34  
    35  func NewContainerEngine(facts *entities.PodmanConfig) (entities.ContainerEngine, error) {
    36  	switch facts.EngineMode {
    37  	case entities.ABIMode:
    38  		return nil, fmt.Errorf("direct runtime not supported")
    39  	case entities.TunnelMode:
    40  		ctx, err := newConnection(facts.URI, facts.Identity)
    41  		return &tunnel.ContainerEngine{ClientCtx: ctx}, err
    42  	}
    43  	return nil, fmt.Errorf("runtime mode '%v' is not supported", facts.EngineMode)
    44  }
    45  
    46  // NewImageEngine factory provides a libpod runtime for image-related operations
    47  func NewImageEngine(facts *entities.PodmanConfig) (entities.ImageEngine, error) {
    48  	switch facts.EngineMode {
    49  	case entities.ABIMode:
    50  		return nil, fmt.Errorf("direct image runtime not supported")
    51  	case entities.TunnelMode:
    52  		ctx, err := newConnection(facts.URI, facts.Identity)
    53  		return &tunnel.ImageEngine{ClientCtx: ctx}, err
    54  	}
    55  	return nil, fmt.Errorf("runtime mode '%v' is not supported", facts.EngineMode)
    56  }