Documentation ¶
Overview ¶
Package client is a Go client for the Malice Engine API.
The "malice" command uses this package to communicate with the daemon. It can also be used by your own Go applications to do anything the command-line interface does - running containers, pulling images, managing swarms, etc.
For more information about the Engine API, see the documentation: https://docs.malice.io/engine/reference/api/
Usage ¶
You use the library by creating a client object and calling methods on it. The client can be created either from environment variables with NewEnvClient, or configured manually with NewClient.
For example, to list running containers (the equivalent of "malice plugin ls"):
package main import ( "context" "fmt" "github.com/maliceio/engine/api/types" "github.com/maliceio/engine/client" ) func main() { cli, err := client.NewEnvClient() if err != nil { panic(err) } plugins, err := cli.PluginList(context.Background(), types.PluginListListOptions{}) if err != nil { panic(err) } for _, plugin := range plugins { fmt.Printf("%s %s\n", plugin.Name, plugin.Image) } }
Index ¶
- Constants
- Variables
- func CheckRedirect(req *http.Request, via []*http.Request) error
- func ErrorConnectionFailed(host string) error
- func FromEnv(c *Client) error
- func IsErrConnectionFailed(err error) bool
- func IsErrNotFound(err error) bool
- func IsErrNotImplemented(err error) bool
- func IsErrPluginPermissionDenied(err error) bool
- func IsErrUnauthorized(err error) bool
- func ParseHostURL(host string) (*url.URL, error)
- func WithDialer(dialer *net.Dialer) func(*Client) error
- func WithHTTPClient(client *http.Client) func(*Client) error
- func WithHTTPHeaders(headers map[string]string) func(*Client) error
- func WithHost(host string) func(*Client) error
- func WithTLSClientConfig(cacertPath, certPath, keyPath string) func(*Client) error
- func WithVersion(version string) func(*Client) error
- type APIClient
- type Client
- func (cli *Client) ClientVersion() string
- func (cli *Client) Close() error
- func (cli *Client) CustomHTTPHeaders() map[string]string
- func (cli *Client) DaemonHost() string
- func (cli *Client) DialSession(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error)
- func (cli *Client) DiskUsage(ctx context.Context) (types.DiskUsage, error)
- func (cli *Client) HTTPClient() *http.Client
- func (cli *Client) NegotiateAPIVersion(ctx context.Context)
- func (cli *Client) NegotiateAPIVersionPing(p types.Ping)
- func (cli *Client) NewVersionError(APIrequired, feature string) error
- func (cli *Client) Ping(ctx context.Context) (types.Ping, error)
- func (cli *Client) PluginDisable(ctx context.Context, name string, options plugin.DisableOptions) error
- func (cli *Client) PluginEnable(ctx context.Context, name string, options plugin.EnableOptions) error
- func (cli *Client) PluginInstall(ctx context.Context, name string, options plugin.InstallOptions) (rc io.ReadCloser, err error)
- func (cli *Client) PluginList(ctx context.Context, filter filters.Args) (plugin.ListResponse, error)
- func (cli *Client) PluginRemove(ctx context.Context, name string, options plugin.RemoveOptions) error
- func (cli *Client) PluginSet(ctx context.Context, name string, args []string) error
- func (cli *Client) PluginUpgrade(ctx context.Context, name string, options plugin.InstallOptions) (rc io.ReadCloser, err error)
- func (cli *Client) ServerVersion(ctx context.Context) (types.Version, error)
- func (cli *Client) SetCustomHTTPHeaders(headers map[string]string)
- type CommonAPIClient
- type PluginAPIClient
- type SystemAPIClient
Constants ¶
const DefaultDockerHost = "unix:///var/run/malice.sock"
DefaultDockerHost defines os specific default if DOCKER_HOST is unset
Variables ¶
var ErrRedirect = errors.New("unexpected redirect in response")
ErrRedirect is the error returned by checkRedirect when the request is non-GET.
Functions ¶
func CheckRedirect ¶
CheckRedirect specifies the policy for dealing with redirect responses: If the request is non-GET return `ErrRedirect`. Otherwise use the last response.
Go 1.8 changes behavior for HTTP redirects (specifically 301, 307, and 308) in the client . The Docker client (and by extension maliceio API client) can be made to to send a request like POST /containers//start where what would normally be in the name section of the URL is empty. This triggers an HTTP 301 from the daemon. In go 1.8 this 301 will be converted to a GET request, and ends up getting a 404 from the daemon. This behavior change manifests in the client in that before the 301 was not followed and the client did not generate an error, but now results in a message like Error response from daemon: page not found.
func ErrorConnectionFailed ¶
ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
func FromEnv ¶
FromEnv configures the client with values from environment variables.
Supported environment variables: DOCKER_HOST to set the url to the maliceio server. DOCKER_API_VERSION to set the version of the API to reach, leave empty for latest. DOCKER_CERT_PATH to load the TLS certificates from. DOCKER_TLS_VERIFY to enable or disable TLS verification, off by default.
func IsErrConnectionFailed ¶
IsErrConnectionFailed returns true if the error is caused by connection failed.
func IsErrNotFound ¶
IsErrNotFound returns true if the error is a NotFound error, which is returned by the API when some object is not found.
func IsErrNotImplemented ¶
IsErrNotImplemented returns true if the error is a NotImplemented error. This is returned by the API when a requested feature has not been implemented.
func IsErrPluginPermissionDenied ¶
IsErrPluginPermissionDenied returns true if the error is caused when a user denies a plugin's permissions
func IsErrUnauthorized ¶
IsErrUnauthorized returns true if the error is caused when a remote registry authentication fails
func ParseHostURL ¶
ParseHostURL parses a url string, validates the string is a host url, and returns the parsed URL
func WithDialer ¶
WithDialer applies the dialer.DialContext to the client transport. This can be used to set the Timeout and KeepAlive settings of the client.
func WithHTTPClient ¶
WithHTTPClient overrides the client http client with the specified one
func WithHTTPHeaders ¶
WithHTTPHeaders overrides the client default http headers
func WithTLSClientConfig ¶
WithTLSClientConfig applies a tls config to the client transport.
func WithVersion ¶
WithVersion overrides the client version with the specified one
Types ¶
type APIClient ¶
type APIClient interface { CommonAPIClient }
APIClient is an interface that clients that talk with a docker server must implement.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the API client that performs all operations against a maliceio server.
func NewClient ¶
func NewClient(host string, version string, client *http.Client, httpHeaders map[string]string) (*Client, error)
NewClient initializes a new API client for the given host and API version. It uses the given http client as transport. It also initializes the custom http headers to add to each request.
It won't send any version information if the version number is empty. It is highly recommended that you set a version or your client may break if the server is upgraded. Deprecated: use NewClientWithOpts
func NewClientWithOpts ¶
NewClientWithOpts initializes a new API client with default values. It takes functors to modify values when creating it, like `NewClientWithOpts(WithVersion(…))` It also initializes the custom http headers to add to each request.
It won't send any version information if the version number is empty. It is highly recommended that you set a version or your client may break if the server is upgraded.
func NewEnvClient
deprecated
func (*Client) ClientVersion ¶
ClientVersion returns the API version used by this client.
func (*Client) CustomHTTPHeaders ¶
CustomHTTPHeaders returns the custom http headers stored by the client.
func (*Client) DaemonHost ¶
DaemonHost returns the host address used by the client
func (*Client) DialSession ¶
func (cli *Client) DialSession(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error)
DialSession returns a connection that can be used communication with daemon
func (*Client) HTTPClient ¶
HTTPClient returns a copy of the HTTP client bound to the server
func (*Client) NegotiateAPIVersion ¶
NegotiateAPIVersion queries the API and updates the version to match the API version. Any errors are silently ignored.
func (*Client) NegotiateAPIVersionPing ¶
NegotiateAPIVersionPing updates the client version to match the Ping.APIVersion if the ping version is less than the default version.
func (*Client) NewVersionError ¶
NewVersionError returns an error if the APIVersion required if less than the current supported version
func (*Client) Ping ¶
Ping pings the server and returns the value of the "Docker-Experimental", "OS-Type" & "API-Version" headers
func (*Client) PluginDisable ¶
func (cli *Client) PluginDisable(ctx context.Context, name string, options plugin.DisableOptions) error
PluginDisable disables a plugin
func (*Client) PluginEnable ¶
func (cli *Client) PluginEnable(ctx context.Context, name string, options plugin.EnableOptions) error
PluginEnable enables a plugin
func (*Client) PluginInstall ¶
func (cli *Client) PluginInstall(ctx context.Context, name string, options plugin.InstallOptions) (rc io.ReadCloser, err error)
PluginInstall installs a plugin
func (*Client) PluginList ¶
func (cli *Client) PluginList(ctx context.Context, filter filters.Args) (plugin.ListResponse, error)
PluginList returns the installed plugins
func (*Client) PluginRemove ¶
func (cli *Client) PluginRemove(ctx context.Context, name string, options plugin.RemoveOptions) error
PluginRemove removes a plugin
func (*Client) PluginUpgrade ¶
func (cli *Client) PluginUpgrade(ctx context.Context, name string, options plugin.InstallOptions) (rc io.ReadCloser, err error)
PluginUpgrade upgrades a plugin
func (*Client) ServerVersion ¶
ServerVersion returns information of the docker client and server host.
func (*Client) SetCustomHTTPHeaders ¶
SetCustomHTTPHeaders that will be set on every HTTP request made by the client. Deprecated: use WithHTTPHeaders when creating the client.
type CommonAPIClient ¶
type CommonAPIClient interface { // ConfigAPIClient // ContainerAPIClient // DistributionAPIClient // ImageAPIClient // NodeAPIClient // NetworkAPIClient PluginAPIClient // ServiceAPIClient // SwarmAPIClient // SecretAPIClient SystemAPIClient // VolumeAPIClient ClientVersion() string DaemonHost() string HTTPClient() *http.Client ServerVersion(ctx context.Context) (types.Version, error) NegotiateAPIVersion(ctx context.Context) NegotiateAPIVersionPing(types.Ping) DialSession(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) Close() error }
CommonAPIClient is the common methods between stable and experimental versions of APIClient.
type PluginAPIClient ¶
type PluginAPIClient interface { PluginList(ctx context.Context, filter filters.Args) (plugin.ListResponse, error) PluginRemove(ctx context.Context, name string, options plugin.RemoveOptions) error PluginEnable(ctx context.Context, name string, options plugin.EnableOptions) error PluginDisable(ctx context.Context, name string, options plugin.DisableOptions) error PluginInstall(ctx context.Context, name string, options plugin.InstallOptions) (io.ReadCloser, error) PluginUpgrade(ctx context.Context, name string, options plugin.InstallOptions) (io.ReadCloser, error) }
PluginAPIClient defines API client methods for the plugins
type SystemAPIClient ¶
type SystemAPIClient interface { // Events(ctx context.Context, options types.EventsOptions) (<-chan events.Message, <-chan error) // Info(ctx context.Context) (types.Info, error) // RegistryLogin(ctx context.Context, auth types.AuthConfig) (registry.AuthenticateOKBody, error) // DiskUsage(ctx context.Context) (types.DiskUsage, error) Ping(ctx context.Context) (types.Ping, error) }
SystemAPIClient defines API client methods for the system