mirror of
https://github.com/docker/compose.git
synced 2025-07-15 09:44:28 +02:00
Refactor on services
This refactors the interfaces and implementations of services Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
parent
03e418cbbb
commit
ba455916c6
@ -21,11 +21,6 @@ import (
|
|||||||
"github.com/docker/api/context/store"
|
"github.com/docker/api/context/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
type aciApiService struct {
|
|
||||||
containerGroupsClient containerinstance.ContainerGroupsClient
|
|
||||||
ctx store.AciContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
backend.Register("aci", "aci", func(ctx context.Context) (interface{}, error) {
|
backend.Register("aci", "aci", func(ctx context.Context) (interface{}, error) {
|
||||||
return New(ctx)
|
return New(ctx)
|
||||||
@ -36,13 +31,8 @@ func getter() interface{} {
|
|||||||
return &store.AciContext{}
|
return &store.AciContext{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type AciService interface {
|
// New creates a backend that can manage containers
|
||||||
containers.ContainerService
|
func New(ctx context.Context) (backend.Service, error) {
|
||||||
compose.Service
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a backend that can manage containers on ACI
|
|
||||||
func New(ctx context.Context) (AciService, error) {
|
|
||||||
currentContext := apicontext.CurrentContext(ctx)
|
currentContext := apicontext.CurrentContext(ctx)
|
||||||
contextStore, err := store.New()
|
contextStore, err := store.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -58,13 +48,47 @@ func New(ctx context.Context) (AciService, error) {
|
|||||||
containerGroupsClient := containerinstance.NewContainerGroupsClient(aciContext.SubscriptionID)
|
containerGroupsClient := containerinstance.NewContainerGroupsClient(aciContext.SubscriptionID)
|
||||||
containerGroupsClient.Authorizer = auth
|
containerGroupsClient.Authorizer = auth
|
||||||
|
|
||||||
return &aciApiService{
|
return getAciApiService(containerGroupsClient, aciContext), nil
|
||||||
containerGroupsClient: containerGroupsClient,
|
|
||||||
ctx: aciContext,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciApiService) List(ctx context.Context) ([]containers.Container, error) {
|
func getAciApiService(cgc containerinstance.ContainerGroupsClient, aciCtx store.AciContext) *aciApiService {
|
||||||
|
return &aciApiService{
|
||||||
|
container: aciContainerService{
|
||||||
|
containerGroupsClient: cgc,
|
||||||
|
ctx: aciCtx,
|
||||||
|
},
|
||||||
|
compose: aciComposeService{
|
||||||
|
containerGroupsClient: cgc,
|
||||||
|
ctx: aciCtx,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type aciApiService struct {
|
||||||
|
container aciContainerService
|
||||||
|
compose aciComposeService
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *aciApiService) ContainerService() containers.Service {
|
||||||
|
return &aciContainerService{
|
||||||
|
containerGroupsClient: a.container.containerGroupsClient,
|
||||||
|
ctx: a.container.ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *aciApiService) ComposeService() compose.Service {
|
||||||
|
return &aciComposeService{
|
||||||
|
containerGroupsClient: a.compose.containerGroupsClient,
|
||||||
|
ctx: a.compose.ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type aciContainerService struct {
|
||||||
|
containerGroupsClient containerinstance.ContainerGroupsClient
|
||||||
|
ctx store.AciContext
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cs *aciContainerService) List(ctx context.Context) ([]containers.Container, error) {
|
||||||
var containerGroups []containerinstance.ContainerGroup
|
var containerGroups []containerinstance.ContainerGroup
|
||||||
result, err := cs.containerGroupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
|
result, err := cs.containerGroupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -101,7 +125,7 @@ func (cs *aciApiService) List(ctx context.Context) ([]containers.Container, erro
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciApiService) Run(ctx context.Context, r containers.ContainerConfig) error {
|
func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error {
|
||||||
var ports []types.ServicePortConfig
|
var ports []types.ServicePortConfig
|
||||||
for _, p := range r.Ports {
|
for _, p := range r.Ports {
|
||||||
ports = append(ports, types.ServicePortConfig{
|
ports = append(ports, types.ServicePortConfig{
|
||||||
@ -132,7 +156,7 @@ func (cs *aciApiService) Run(ctx context.Context, r containers.ContainerConfig)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciApiService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
|
func (cs *aciContainerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
|
||||||
containerExecResponse, err := execACIContainer(ctx, cs.ctx, command, name, name)
|
containerExecResponse, err := execACIContainer(ctx, cs.ctx, command, name, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -147,7 +171,7 @@ func (cs *aciApiService) Exec(ctx context.Context, name string, command string,
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciApiService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
|
func (cs *aciContainerService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
|
||||||
logs, err := getACIContainerLogs(ctx, cs.ctx, containerName, containerName)
|
logs, err := getACIContainerLogs(ctx, cs.ctx, containerName, containerName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -169,7 +193,12 @@ func (cs *aciApiService) Logs(ctx context.Context, containerName string, req con
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciApiService) Up(ctx context.Context, opts compose.ProjectOptions) error {
|
type aciComposeService struct {
|
||||||
|
containerGroupsClient containerinstance.ContainerGroupsClient
|
||||||
|
ctx store.AciContext
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cs *aciComposeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
|
||||||
project, err := compose.ProjectFromOptions(&opts)
|
project, err := compose.ProjectFromOptions(&opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -183,7 +212,7 @@ func (cs *aciApiService) Up(ctx context.Context, opts compose.ProjectOptions) er
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciApiService) Down(ctx context.Context, opts compose.ProjectOptions) error {
|
func (cs *aciComposeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
|
||||||
project, err := compose.ProjectFromOptions(&opts)
|
project, err := compose.ProjectFromOptions(&opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -4,6 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/docker/api/compose"
|
||||||
|
"github.com/docker/api/containers"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -24,17 +29,23 @@ var backends = struct {
|
|||||||
r []*registeredBackend
|
r []*registeredBackend
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
|
// Aggregation of service interfaces
|
||||||
|
type Service interface {
|
||||||
|
ContainerService() containers.Service
|
||||||
|
ComposeService() compose.Service
|
||||||
|
}
|
||||||
|
|
||||||
// Register adds a typed backend to the registry
|
// Register adds a typed backend to the registry
|
||||||
func Register(name string, backendType string, init initFunc) {
|
func Register(name string, backendType string, init initFunc) {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
panic(errNoName)
|
logrus.Fatal(errNoName)
|
||||||
}
|
}
|
||||||
if backendType == "" {
|
if backendType == "" {
|
||||||
panic(errNoType)
|
logrus.Fatal(errNoType)
|
||||||
}
|
}
|
||||||
for _, b := range backends.r {
|
for _, b := range backends.r {
|
||||||
if b.backendType == backendType {
|
if b.backendType == backendType {
|
||||||
panic(errTypeRegistered)
|
logrus.Fatal(errTypeRegistered)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,11 +28,11 @@ func upCommand() *cobra.Command {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return c.AciService().Up(cmd.Context(), *opts)
|
return c.ComposeService().Up(cmd.Context(), *opts)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
upCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
|
upCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
|
||||||
upCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
|
upCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
|
||||||
upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
|
upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
|
||||||
upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
|
upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
|
||||||
|
|
||||||
@ -48,11 +48,11 @@ func downCommand() *cobra.Command {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return c.AciService().Down(cmd.Context(), *opts)
|
return c.ComposeService().Down(cmd.Context(), *opts)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
downCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
|
downCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
|
||||||
downCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
|
downCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
|
||||||
|
|
||||||
return downCmd
|
return downCmd
|
||||||
}
|
}
|
@ -62,5 +62,5 @@ func runExec(ctx context.Context, opts execOpts, name string, command string) er
|
|||||||
stdout = con
|
stdout = con
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.AciService().Exec(ctx, name, command, os.Stdin, stdout)
|
return c.ContainerService().Exec(ctx, name, command, os.Stdin, stdout)
|
||||||
}
|
}
|
||||||
|
@ -46,5 +46,5 @@ func runLogs(ctx context.Context, containerName string, opts logsOpts) error {
|
|||||||
Writer: os.Stdout,
|
Writer: os.Stdout,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.AciService().Logs(ctx, containerName, req)
|
return c.ContainerService().Logs(ctx, containerName, req)
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ var PsCommand = cobra.Command{
|
|||||||
return errors.Wrap(err, "cannot connect to backend")
|
return errors.Wrap(err, "cannot connect to backend")
|
||||||
}
|
}
|
||||||
|
|
||||||
containers, err := c.AciService().List(ctx)
|
containers, err := c.ContainerService().List(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "fetch containers")
|
return errors.Wrap(err, "fetch containers")
|
||||||
}
|
}
|
||||||
|
@ -65,5 +65,5 @@ func runRun(ctx context.Context, image string, opts runOpts) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.AciService().Run(ctx, project)
|
return c.ContainerService().Run(ctx, project)
|
||||||
}
|
}
|
||||||
|
@ -31,11 +31,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/docker/api/azure"
|
|
||||||
"github.com/docker/api/backend"
|
"github.com/docker/api/backend"
|
||||||
backendv1 "github.com/docker/api/backend/v1"
|
backendv1 "github.com/docker/api/backend/v1"
|
||||||
cliv1 "github.com/docker/api/cli/v1"
|
cliv1 "github.com/docker/api/cli/v1"
|
||||||
|
"github.com/docker/api/compose"
|
||||||
composev1 "github.com/docker/api/compose/v1"
|
composev1 "github.com/docker/api/compose/v1"
|
||||||
|
"github.com/docker/api/containers"
|
||||||
containersv1 "github.com/docker/api/containers/v1"
|
containersv1 "github.com/docker/api/containers/v1"
|
||||||
apicontext "github.com/docker/api/context"
|
apicontext "github.com/docker/api/context"
|
||||||
"github.com/docker/api/context/store"
|
"github.com/docker/api/context/store"
|
||||||
@ -57,13 +58,13 @@ func New(ctx context.Context) (*Client, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
aciService, ok := b.(azure.AciService)
|
service, ok := b.(backend.Service)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("backend not found")
|
return nil, errors.New("backend not found")
|
||||||
}
|
}
|
||||||
return &Client{
|
return &Client{
|
||||||
backendType: contextType,
|
backendType: contextType,
|
||||||
cc: aciService,
|
bs: service,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -76,10 +77,15 @@ type Client struct {
|
|||||||
composev1.ComposeClient
|
composev1.ComposeClient
|
||||||
|
|
||||||
backendType string
|
backendType string
|
||||||
cc azure.AciService
|
bs backend.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
// AciService returns the backend service for the current context
|
// ContainerService returns the backend service for the current context
|
||||||
func (c *Client) AciService() azure.AciService {
|
func (c *Client) ContainerService() containers.Service {
|
||||||
return c.cc
|
return c.bs.ContainerService()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ComposeService returns the backend service for the current context
|
||||||
|
func (c *Client) ComposeService() compose.Service {
|
||||||
|
return c.bs.ComposeService()
|
||||||
}
|
}
|
||||||
|
@ -44,8 +44,8 @@ type LogsRequest struct {
|
|||||||
Writer io.Writer
|
Writer io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContainerService interacts with the underlying container backend
|
// Service interacts with the underlying container backend
|
||||||
type ContainerService interface {
|
type Service interface {
|
||||||
// List returns all the containers
|
// List returns all the containers
|
||||||
List(ctx context.Context) ([]Container, error)
|
List(ctx context.Context) ([]Container, error)
|
||||||
// Run creates and starts a container
|
// Run creates and starts a container
|
||||||
|
@ -31,7 +31,7 @@ type proxyContainerAPI struct{}
|
|||||||
func (p *proxyContainerAPI) List(ctx context.Context, _ *v1.ListRequest) (*v1.ListResponse, error) {
|
func (p *proxyContainerAPI) List(ctx context.Context, _ *v1.ListRequest) (*v1.ListResponse, error) {
|
||||||
client := Client(ctx)
|
client := Client(ctx)
|
||||||
|
|
||||||
c, err := client.AciService().List(ctx)
|
c, err := client.ContainerService().List(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &v1.ListResponse{}, nil
|
return &v1.ListResponse{}, nil
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ func (p *proxyContainerAPI) List(ctx context.Context, _ *v1.ListRequest) (*v1.Li
|
|||||||
func (p *proxyContainerAPI) Create(ctx context.Context, request *v1.CreateRequest) (*v1.CreateResponse, error) {
|
func (p *proxyContainerAPI) Create(ctx context.Context, request *v1.CreateRequest) (*v1.CreateResponse, error) {
|
||||||
client := Client(ctx)
|
client := Client(ctx)
|
||||||
|
|
||||||
err := client.AciService().Run(ctx, containers.ContainerConfig{
|
err := client.ContainerService().Run(ctx, containers.ContainerConfig{
|
||||||
ID: request.Id,
|
ID: request.Id,
|
||||||
Image: request.Image,
|
Image: request.Image,
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user