composeService to rely on apiClient

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-11-26 14:44:55 +01:00
parent a60b008819
commit 646aca82d9
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
4 changed files with 94 additions and 67 deletions

View File

@ -35,6 +35,7 @@ import (
type local struct { type local struct {
*containerService *containerService
*volumeService *volumeService
*composeService
} }
func init() { func init() {
@ -50,6 +51,7 @@ func service(ctx context.Context) (backend.Service, error) {
return &local{ return &local{
containerService: &containerService{apiClient}, containerService: &containerService{apiClient},
volumeService: &volumeService{apiClient}, volumeService: &volumeService{apiClient},
composeService: &composeService{apiClient},
}, nil }, nil
} }
@ -58,7 +60,7 @@ func (s *local) ContainerService() containers.Service {
} }
func (s *local) ComposeService() compose.Service { func (s *local) ComposeService() compose.Service {
return s return s.composeService
} }
func (s *local) SecretsService() secrets.Service { func (s *local) SecretsService() secrets.Service {

View File

@ -25,6 +25,8 @@ import (
"path" "path"
"strings" "strings"
"github.com/docker/docker/errdefs"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/docker/buildx/build" "github.com/docker/buildx/build"
"github.com/docker/buildx/driver" "github.com/docker/buildx/driver"
@ -32,7 +34,7 @@ import (
"github.com/docker/buildx/util/progress" "github.com/docker/buildx/util/progress"
) )
func (s *local) ensureImagesExists(ctx context.Context, project *types.Project) error { func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project) error {
opts := map[string]build.Options{} opts := map[string]build.Options{}
for _, service := range project.Services { for _, service := range project.Services {
if service.Image == "" && service.Build == nil { if service.Image == "" && service.Build == nil {
@ -71,9 +73,20 @@ func (s *local) ensureImagesExists(ctx context.Context, project *types.Project)
return s.build(ctx, project, opts) return s.build(ctx, project, opts)
} }
func (s *local) build(ctx context.Context, project *types.Project, opts map[string]build.Options) error { func (s *composeService) needPull(ctx context.Context, service types.ServiceConfig) (bool, error) {
_, _, err := s.apiClient.ImageInspectWithRaw(ctx, service.Image)
if err != nil {
if errdefs.IsNotFound(err) {
return true, nil
}
return false, err
}
return false, nil
}
func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options) error {
const drivername = "default" const drivername = "default"
d, err := driver.GetDriver(ctx, drivername, nil, s.containerService.apiClient, nil, nil, "", nil, project.WorkingDir) d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient, nil, nil, "", nil, project.WorkingDir)
if err != nil { if err != nil {
return err return err
} }
@ -89,7 +102,7 @@ func (s *local) build(ctx context.Context, project *types.Project, opts map[stri
return err return err
} }
func (s *local) buildImage(ctx context.Context, service types.ServiceConfig, contextPath string) build.Options { func (s *composeService) buildImage(ctx context.Context, service types.ServiceConfig, contextPath string) build.Options {
var tags []string var tags []string
if service.Image != "" { if service.Image != "" {
tags = append(tags, service.Image) tags = append(tags, service.Image)

View File

@ -27,7 +27,6 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
moby "github.com/docker/docker/api/types" moby "github.com/docker/docker/api/types"
@ -36,19 +35,25 @@ import (
"github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/strslice" "github.com/docker/docker/api/types/strslice"
mobyvolume "github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs" "github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sanathkr/go-yaml" "github.com/sanathkr/go-yaml"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/containers"
"github.com/docker/compose-cli/formatter" "github.com/docker/compose-cli/formatter"
"github.com/docker/compose-cli/progress" "github.com/docker/compose-cli/progress"
) )
func (s *local) Up(ctx context.Context, project *types.Project, detach bool) error { type composeService struct {
apiClient *client.Client
}
func (s *composeService) Up(ctx context.Context, project *types.Project, detach bool) error {
for k, network := range project.Networks { for k, network := range project.Networks {
if !network.External.External && network.Name != "" { if !network.External.External && network.Name != "" {
network.Name = fmt.Sprintf("%s_%s", project.Name, k) network.Name = fmt.Sprintf("%s_%s", project.Name, k)
@ -95,19 +100,8 @@ func getContainerName(c moby.Container) string {
return c.Names[0][1:] return c.Names[0][1:]
} }
func (s *local) needPull(ctx context.Context, service types.ServiceConfig) (bool, error) { func (s *composeService) Down(ctx context.Context, projectName string) error {
_, _, err := s.containerService.apiClient.ImageInspectWithRaw(ctx, service.Image) list, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
if err != nil {
if errdefs.IsNotFound(err) {
return true, nil
}
return false, err
}
return false, nil
}
func (s *local) Down(ctx context.Context, projectName string) error {
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs( Filters: filters.NewArgs(
projectFilter(projectName), projectFilter(projectName),
), ),
@ -126,7 +120,7 @@ func (s *local) Down(ctx context.Context, projectName string) error {
Text: "Stopping", Text: "Stopping",
Status: progress.Working, Status: progress.Working,
}) })
err := s.containerService.Stop(ctx, container.ID, nil) err := s.apiClient.ContainerStop(ctx, container.ID, nil)
if err != nil { if err != nil {
return err return err
} }
@ -135,7 +129,7 @@ func (s *local) Down(ctx context.Context, projectName string) error {
Text: "Removing", Text: "Removing",
Status: progress.Working, Status: progress.Working,
}) })
err = s.containerService.Delete(ctx, container.ID, containers.DeleteRequest{}) err = s.apiClient.ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{})
if err != nil { if err != nil {
return err return err
} }
@ -150,8 +144,8 @@ func (s *local) Down(ctx context.Context, projectName string) error {
return eg.Wait() return eg.Wait()
} }
func (s *local) Logs(ctx context.Context, projectName string, w io.Writer) error { func (s *composeService) Logs(ctx context.Context, projectName string, w io.Writer) error {
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{ list, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs( Filters: filters.NewArgs(
projectFilter(projectName), projectFilter(projectName),
), ),
@ -159,26 +153,41 @@ func (s *local) Logs(ctx context.Context, projectName string, w io.Writer) error
if err != nil { if err != nil {
return err return err
} }
var wg sync.WaitGroup
consumer := formatter.NewLogConsumer(w) consumer := formatter.NewLogConsumer(w)
eg, ctx := errgroup.WithContext(ctx)
for _, c := range list { for _, c := range list {
service := c.Labels[serviceLabel] service := c.Labels[serviceLabel]
containerID := c.ID container, err := s.apiClient.ContainerInspect(ctx, c.ID)
go func() { if err != nil {
_ = s.containerService.Logs(ctx, containerID, containers.LogsRequest{ return err
Follow: true, }
Writer: consumer.GetWriter(service, containerID),
eg.Go(func() error {
r, err := s.apiClient.ContainerLogs(ctx, container.ID, moby.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
}) })
wg.Done() defer r.Close() // nolint errcheck
}()
wg.Add(1) if err != nil {
return err
}
w := consumer.GetWriter(service, container.ID)
if container.Config.Tty {
_, err = io.Copy(w, r)
} else {
_, err = stdcopy.StdCopy(w, w, r)
}
return err
})
} }
wg.Wait() eg.Wait()
return nil return nil
} }
func (s *local) Ps(ctx context.Context, projectName string) ([]compose.ServiceStatus, error) { func (s *composeService) Ps(ctx context.Context, projectName string) ([]compose.ServiceStatus, error) {
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{ list, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs( Filters: filters.NewArgs(
projectFilter(projectName), projectFilter(projectName),
), ),
@ -233,8 +242,8 @@ func groupContainerByLabel(containers []moby.Container, labelName string) (map[s
return containersByLabel, keys, nil return containersByLabel, keys, nil
} }
func (s *local) List(ctx context.Context, projectName string) ([]compose.Stack, error) { func (s *composeService) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{ list, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs(hasProjectLabelFilter()), Filters: filters.NewArgs(hasProjectLabelFilter()),
}) })
if err != nil { if err != nil {
@ -291,7 +300,7 @@ func combinedStatus(statuses []string) string {
return result return result
} }
func (s *local) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { func (s *composeService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) {
switch format { switch format {
case "json": case "json":
return json.MarshalIndent(project, "", " ") return json.MarshalIndent(project, "", " ")
@ -535,8 +544,8 @@ func getNetworksForService(s types.ServiceConfig) map[string]*types.ServiceNetwo
return map[string]*types.ServiceNetworkConfig{"default": nil} return map[string]*types.ServiceNetworkConfig{"default": nil}
} }
func (s *local) ensureNetwork(ctx context.Context, n types.NetworkConfig) error { func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfig) error {
_, err := s.containerService.apiClient.NetworkInspect(ctx, n.Name, moby.NetworkInspectOptions{}) _, err := s.apiClient.NetworkInspect(ctx, n.Name, moby.NetworkInspectOptions{})
if err != nil { if err != nil {
if errdefs.IsNotFound(err) { if errdefs.IsNotFound(err) {
createOpts := moby.NetworkCreate{ createOpts := moby.NetworkCreate{
@ -568,7 +577,7 @@ func (s *local) ensureNetwork(ctx context.Context, n types.NetworkConfig) error
Status: progress.Working, Status: progress.Working,
StatusText: "Create", StatusText: "Create",
}) })
if _, err := s.containerService.apiClient.NetworkCreate(ctx, n.Name, createOpts); err != nil { if _, err := s.apiClient.NetworkCreate(ctx, n.Name, createOpts); err != nil {
return errors.Wrapf(err, "failed to create network %s", n.Name) return errors.Wrapf(err, "failed to create network %s", n.Name)
} }
w.Event(progress.Event{ w.Event(progress.Event{
@ -583,9 +592,9 @@ func (s *local) ensureNetwork(ctx context.Context, n types.NetworkConfig) error
return nil return nil
} }
func (s *local) ensureVolume(ctx context.Context, volume types.VolumeConfig) error { func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeConfig) error {
// TODO could identify volume by label vs name // TODO could identify volume by label vs name
_, err := s.volumeService.Inspect(ctx, volume.Name) _, err := s.apiClient.VolumeInspect(ctx, volume.Name)
if err != nil { if err != nil {
if errdefs.IsNotFound(err) { if errdefs.IsNotFound(err) {
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
@ -595,7 +604,10 @@ func (s *local) ensureVolume(ctx context.Context, volume types.VolumeConfig) err
StatusText: "Create", StatusText: "Create",
}) })
// TODO we miss support for driver_opts and labels // TODO we miss support for driver_opts and labels
_, err := s.volumeService.Create(ctx, volume.Name, nil) _, err := s.apiClient.VolumeCreate(ctx, mobyvolume.VolumeCreateBody{
Labels: nil,
Name: volume.Name,
})
w.Event(progress.Event{ w.Event(progress.Event{
ID: fmt.Sprintf("Volume %q", volume.Name), ID: fmt.Sprintf("Volume %q", volume.Name),
Status: progress.Done, Status: progress.Done,

View File

@ -30,7 +30,6 @@ import (
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"github.com/docker/compose-cli/api/containers"
"github.com/docker/compose-cli/progress" "github.com/docker/compose-cli/progress"
) )
@ -39,13 +38,13 @@ const (
forceRecreate = "force_recreate" forceRecreate = "force_recreate"
) )
func (s *local) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig) error { func (s *composeService) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
err := s.waitDependencies(ctx, project, service) err := s.waitDependencies(ctx, project, service)
if err != nil { if err != nil {
return err return err
} }
actual, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{ actual, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs( Filters: filters.NewArgs(
filters.Arg("label", fmt.Sprintf("%s=%s", projectLabel, project.Name)), filters.Arg("label", fmt.Sprintf("%s=%s", projectLabel, project.Name)),
filters.Arg("label", fmt.Sprintf("%s=%s", serviceLabel, service.Name)), filters.Arg("label", fmt.Sprintf("%s=%s", serviceLabel, service.Name)),
@ -77,11 +76,11 @@ func (s *local) ensureService(ctx context.Context, project *types.Project, servi
for i := scale; i < len(actual); i++ { for i := scale; i < len(actual); i++ {
container := actual[i] container := actual[i]
eg.Go(func() error { eg.Go(func() error {
err := s.containerService.Stop(ctx, container.ID, nil) err := s.apiClient.ContainerStop(ctx, container.ID, nil)
if err != nil { if err != nil {
return err return err
} }
return s.containerService.Delete(ctx, container.ID, containers.DeleteRequest{}) return s.apiClient.ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{})
}) })
} }
actual = actual[:scale] actual = actual[:scale]
@ -114,7 +113,7 @@ func (s *local) ensureService(ctx context.Context, project *types.Project, servi
return eg.Wait() return eg.Wait()
} }
func (s *local) waitDependencies(ctx context.Context, project *types.Project, service types.ServiceConfig) error { func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
eg, _ := errgroup.WithContext(ctx) eg, _ := errgroup.WithContext(ctx)
for dep, config := range service.DependsOn { for dep, config := range service.DependsOn {
switch config.Condition { switch config.Condition {
@ -163,7 +162,7 @@ func getScale(config types.ServiceConfig) int {
return 1 return 1
} }
func (s *local) createContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int) error { func (s *composeService) createContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int) error {
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
w.Event(progress.Event{ w.Event(progress.Event{
ID: fmt.Sprintf("Service %q", service.Name), ID: fmt.Sprintf("Service %q", service.Name),
@ -182,20 +181,20 @@ func (s *local) createContainer(ctx context.Context, project *types.Project, ser
return nil return nil
} }
func (s *local) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, container moby.Container) error { func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, container moby.Container) error {
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
w.Event(progress.Event{ w.Event(progress.Event{
ID: fmt.Sprintf("Service %q", service.Name), ID: fmt.Sprintf("Service %q", service.Name),
Status: progress.Working, Status: progress.Working,
StatusText: "Recreate", StatusText: "Recreate",
}) })
err := s.containerService.Stop(ctx, container.ID, nil) err := s.apiClient.ContainerStop(ctx, container.ID, nil)
if err != nil { if err != nil {
return err return err
} }
name := getContainerName(container) name := getContainerName(container)
tmpName := fmt.Sprintf("%s_%s", container.ID[:12], name) tmpName := fmt.Sprintf("%s_%s", container.ID[:12], name)
err = s.containerService.apiClient.ContainerRename(ctx, container.ID, tmpName) err = s.apiClient.ContainerRename(ctx, container.ID, tmpName)
if err != nil { if err != nil {
return err return err
} }
@ -207,7 +206,7 @@ func (s *local) recreateContainer(ctx context.Context, project *types.Project, s
if err != nil { if err != nil {
return err return err
} }
err = s.containerService.Delete(ctx, container.ID, containers.DeleteRequest{}) err = s.apiClient.ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{})
if err != nil { if err != nil {
return err return err
} }
@ -233,14 +232,14 @@ func setDependentLifecycle(project *types.Project, service string, strategy stri
} }
} }
func (s *local) restartContainer(ctx context.Context, service types.ServiceConfig, container moby.Container) error { func (s *composeService) restartContainer(ctx context.Context, service types.ServiceConfig, container moby.Container) error {
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
w.Event(progress.Event{ w.Event(progress.Event{
ID: fmt.Sprintf("Service %q", service.Name), ID: fmt.Sprintf("Service %q", service.Name),
Status: progress.Working, Status: progress.Working,
StatusText: "Restart", StatusText: "Restart",
}) })
err := s.containerService.Start(ctx, container.ID) err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
if err != nil { if err != nil {
return err return err
} }
@ -252,15 +251,16 @@ func (s *local) restartContainer(ctx context.Context, service types.ServiceConfi
return nil return nil
} }
func (s *local) runContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, container *moby.Container) error { func (s *composeService) runContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, container *moby.Container) error {
containerConfig, hostConfig, networkingConfig, err := getContainerCreateOptions(project, service, number, container) containerConfig, hostConfig, networkingConfig, err := getContainerCreateOptions(project, service, number, container)
if err != nil { if err != nil {
return err return err
} }
id, err := s.containerService.create(ctx, containerConfig, hostConfig, networkingConfig, name) created, err := s.apiClient.ContainerCreate(ctx, containerConfig, hostConfig, networkingConfig, name)
if err != nil { if err != nil {
return err return err
} }
id := created.ID
for net := range service.Networks { for net := range service.Networks {
name := fmt.Sprintf("%s_%s", project.Name, net) name := fmt.Sprintf("%s_%s", project.Name, net)
err = s.connectContainerToNetwork(ctx, id, service.Name, name) err = s.connectContainerToNetwork(ctx, id, service.Name, name)
@ -268,15 +268,15 @@ func (s *local) runContainer(ctx context.Context, project *types.Project, servic
return err return err
} }
} }
err = s.containerService.apiClient.ContainerStart(ctx, id, moby.ContainerStartOptions{}) err = s.apiClient.ContainerStart(ctx, id, moby.ContainerStartOptions{})
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
func (s *local) connectContainerToNetwork(ctx context.Context, id string, service string, n string) error { func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, service string, n string) error {
err := s.containerService.apiClient.NetworkConnect(ctx, n, id, &network.EndpointSettings{ err := s.apiClient.NetworkConnect(ctx, n, id, &network.EndpointSettings{
Aliases: []string{service}, Aliases: []string{service},
}) })
if err != nil { if err != nil {
@ -285,8 +285,8 @@ func (s *local) connectContainerToNetwork(ctx context.Context, id string, servic
return nil return nil
} }
func (s *local) isServiceHealthy(ctx context.Context, project *types.Project, service string) (bool, error) { func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string) (bool, error) {
containers, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{ containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs( Filters: filters.NewArgs(
filters.Arg("label", fmt.Sprintf("%s=%s", projectLabel, project.Name)), filters.Arg("label", fmt.Sprintf("%s=%s", projectLabel, project.Name)),
filters.Arg("label", fmt.Sprintf("%s=%s", serviceLabel, service)), filters.Arg("label", fmt.Sprintf("%s=%s", serviceLabel, service)),
@ -297,7 +297,7 @@ func (s *local) isServiceHealthy(ctx context.Context, project *types.Project, se
} }
for _, c := range containers { for _, c := range containers {
container, err := s.containerService.apiClient.ContainerInspect(ctx, c.ID) container, err := s.apiClient.ContainerInspect(ctx, c.ID)
if err != nil { if err != nil {
return false, err return false, err
} }