github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/worker/gclient/client.go (about)

     1  package gclient
     2  
     3  import (
     4  	"code.cloudfoundry.org/garden"
     5  	"github.com/pf-qiu/concourse/v6/atc/worker/gclient/connection"
     6  )
     7  
     8  //go:generate counterfeiter . Client
     9  type Client interface {
    10  	// Pings the garden server. Checks connectivity to the server. The server may, optionally, respond with specific
    11  	// errors indicating health issues.
    12  	//
    13  	// Errors:
    14  	// * garden.UnrecoverableError indicates that the garden server has entered an error state from which it cannot recover
    15  	Ping() error
    16  
    17  	// Capacity returns the physical capacity of the server's machine.
    18  	//
    19  	// Errors:
    20  	// * None.
    21  	Capacity() (garden.Capacity, error)
    22  
    23  	// Create creates a new container.
    24  	//
    25  	// Errors:
    26  	// * When the handle, if specified, is already taken.
    27  	// * When one of the bind_mount paths does not exist.
    28  	// * When resource allocations fail (subnet, user ID, etc).
    29  	Create(garden.ContainerSpec) (Container, error)
    30  
    31  	// Destroy destroys a container.
    32  	//
    33  	// When a container is destroyed, its resource allocations are released,
    34  	// its filesystem is removed, and all references to its handle are removed.
    35  	//
    36  	// All resources that have been acquired during the lifetime of the container are released.
    37  	// Examples of these resources are its subnet, its UID, and ports that were redirected to the container.
    38  	//
    39  	// TODO: list the resources that can be acquired during the lifetime of a container.
    40  	//
    41  	// Errors:
    42  	// * TODO.
    43  	Destroy(handle string) error
    44  
    45  	// Containers lists all containers filtered by Properties (which are ANDed together).
    46  	//
    47  	// Errors:
    48  	// * None.
    49  	Containers(garden.Properties) ([]Container, error)
    50  
    51  	// BulkInfo returns info or error for a list of containers.
    52  	BulkInfo(handles []string) (map[string]garden.ContainerInfoEntry, error)
    53  
    54  	// BulkMetrics returns metrics or error for a list of containers.
    55  	BulkMetrics(handles []string) (map[string]garden.ContainerMetricsEntry, error)
    56  
    57  	// Lookup returns the container with the specified handle.
    58  	//
    59  	// Errors:
    60  	// * Container not found.
    61  	Lookup(handle string) (Container, error)
    62  }
    63  
    64  type client struct {
    65  	connection connection.Connection
    66  }
    67  
    68  func NewClient(connection connection.Connection) Client {
    69  	return &client{
    70  		connection: connection,
    71  	}
    72  }
    73  
    74  func (client *client) Ping() error {
    75  	return client.connection.Ping()
    76  }
    77  
    78  func (client *client) Capacity() (garden.Capacity, error) {
    79  	return client.connection.Capacity()
    80  }
    81  
    82  func (client *client) Create(spec garden.ContainerSpec) (Container, error) {
    83  	handle, err := client.connection.Create(spec)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	return newContainer(handle, client.connection), nil
    89  }
    90  
    91  func (client *client) Containers(properties garden.Properties) ([]Container, error) {
    92  	handles, err := client.connection.List(properties)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	containers := []Container{}
    98  	for _, handle := range handles {
    99  		containers = append(containers, newContainer(handle, client.connection))
   100  	}
   101  
   102  	return containers, nil
   103  }
   104  
   105  func (client *client) Destroy(handle string) error {
   106  	err := client.connection.Destroy(handle)
   107  
   108  	return err
   109  }
   110  
   111  func (client *client) BulkInfo(handles []string) (map[string]garden.ContainerInfoEntry, error) {
   112  	return client.connection.BulkInfo(handles)
   113  }
   114  
   115  func (client *client) BulkMetrics(handles []string) (map[string]garden.ContainerMetricsEntry, error) {
   116  	return client.connection.BulkMetrics(handles)
   117  }
   118  
   119  func (client *client) Lookup(handle string) (Container, error) {
   120  	handles, err := client.connection.List(nil)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	for _, h := range handles {
   126  		if h == handle {
   127  			return newContainer(handle, client.connection), nil
   128  		}
   129  	}
   130  
   131  	return nil, garden.ContainerNotFoundError{Handle: handle}
   132  }