Initial support for native docker client

This commit updates the implementation to use a native docker client as
opposed to calling an external executable.
This commit is contained in:
Tom Wiesing 2023-03-04 09:18:36 +01:00
parent 2ee8dfaaec
commit 1855090f26
No known key found for this signature in database
9 changed files with 379 additions and 58 deletions

View file

@ -0,0 +1,126 @@
package docker
import (
"context"
"path/filepath"
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/loader"
ctypes "github.com/compose-spec/compose-go/types"
"golang.org/x/exp/slices"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
)
// ComposeProject represents a compose project
type ComposeProject = *ctypes.Project
// LoadComposeProject loads a docker compose project from the given path.
// The returned name indicates the name, as would be found by the 'docker compose' executable.
// If the project could not be found, an appropriate error is returned.
//
// NOTE: This intentionally omits using any kind of api for docker compose.
// This saves a *a lot* of dependencies./
func (docker *Docker) LoadComposeProject(path string) (project ComposeProject, err error) {
ppath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
opts, err := cli.NewProjectOptions(
/* configs = */ nil,
cli.WithWorkingDirectory(ppath),
cli.WithDefaultConfigPath,
cli.WithName(loader.NormalizeProjectName(filepath.Base(ppath))),
cli.WithDotEnv,
)
if err != nil {
return nil, err
}
proj, err := cli.ProjectFromOptions(opts)
if err != nil {
return nil, err
}
return proj, nil
}
// Containers loads the compose project at path, connects to the docker daemon, and then lists all containers belonging to the given services.
// If services is empty, all containers belonging to any service are returned.
func (docker *Docker) Containers(ctx context.Context, path string, services ...string) (containers []types.Container, err error) {
proj, err := docker.LoadComposeProject(path)
if err != nil {
return nil, err
}
client, err := docker.APIClient()
if err != nil {
return nil, err
}
defer client.Close()
return docker.containers(ctx, proj, client, false, services...)
}
const (
projectLabel = "com.docker.compose.project"
workingDirLabel = "com.docker.compose.project.working_dir"
serviceLabel = "com.docker.compose.service"
)
// containers uses the given project and client to find containers belonging to the provided services.
// If all is false, only running containers are returned.
// If all is true, all containers are returned.
//
// services optionally filters the returned containers by the services they belong to.
// If services is empty, all containers are returned, else containers belonging to any of the services included.
func (*Docker) containers(ctx context.Context, project ComposeProject, client DockerClient, all bool, services ...string) ([]types.Container, error) {
// build filters
f := filters.NewArgs(
filters.Arg("label", projectLabel+"="+project.Name),
filters.Arg("label", workingDirLabel+"="+project.WorkingDir),
)
// if there is only one label requested, filter it in the query!
if len(services) == 1 {
f.Add("label", serviceLabel+"="+services[0])
}
// find the containers
containers, err := client.ContainerList(ctx, types.ContainerListOptions{
All: all,
Filters: f,
})
if err != nil {
return nil, err
}
// for all services or exactly one service (case above)
// we can immediatly return!
if len(services) <= 1 {
return containers, err
}
// make a map of services that were requested
req := make(map[string]struct{}, len(services))
for _, service := range services {
req[service] = struct{}{}
}
// filter the containers by what was requested
result := containers[:0]
for _, container := range containers {
service, ok := container.Labels[serviceLabel]
if !ok {
continue
}
if _, ok := req[service]; ok {
result = append(result, container)
}
}
// and clip the results
return slices.Clip(result), nil
}

View file

@ -0,0 +1,71 @@
package docker
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)
type Docker struct {
component.Base
}
// DockerClient is a client to the docker api
type DockerClient = *client.Client
func (docker *Docker) APIClient() (DockerClient, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
return cli, nil
}
// Ping pings the docker daemon to check if it is properly working
func (docker *Docker) Ping(ctx context.Context) (types.Ping, error) {
client, err := docker.APIClient()
if err != nil {
return types.Ping{}, err
}
defer client.Close()
ping, err := client.Ping(ctx)
if err != nil {
return types.Ping{}, err
}
return ping, err
}
// CreateNetwork creates a docker network with the given name unless it already exists.
// The new network will be of default type.
// exists indicates if the network already exists.
func (docker *Docker) CreateNetwork(ctx context.Context, name string) (id string, exists bool, err error) {
// create a new docker client
client, err := docker.APIClient()
if err != nil {
return "", false, err
}
defer client.Close()
// check if the network exists, by listing the network name
list, err := client.NetworkList(ctx, types.NetworkListOptions{Filters: filters.NewArgs(filters.KeyValuePair{Key: "name", Value: name})})
if err != nil {
return "", false, err
}
// network already exists => nothing to do
if len(list) == 1 {
return list[0].ID, true, nil
}
// do the actual create!
create, err := client.NetworkCreate(ctx, name, types.NetworkCreate{CheckDuplicate: true})
if err != nil {
return "", false, err
}
return create.ID, false, nil
}