Restrict compose project to selected services and dependencies on compose start

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
This commit is contained in:
Laura Brehm 2022-09-06 20:31:42 +02:00
parent 361194472e
commit 209293e449
No known key found for this signature in database
GPG Key ID: 526E3FC49260D47A
4 changed files with 27 additions and 10 deletions

View File

@ -51,5 +51,6 @@ func runStart(ctx context.Context, backend api.Service, opts startOptions, servi
return backend.Start(ctx, name, api.StartOptions{
AttachTo: services,
Project: project,
Services: services,
})
}

View File

@ -129,6 +129,8 @@ type StartOptions struct {
ExitCodeFrom string
// Wait won't return until containers reached the running|healthy state
Wait bool
// Services passed in the command line to be started
Services []string
}
// RestartOptions group options of the Restart API

View File

@ -63,21 +63,24 @@ var (
)
// InDependencyOrder applies the function to the services of the project taking in account the dependency order
func InDependencyOrder(ctx context.Context, project *types.Project, fn func(context.Context, string) error) error {
return visit(ctx, project, upDirectionTraversalConfig, fn, ServiceStopped)
func InDependencyOrder(ctx context.Context, project *types.Project, fn func(context.Context, string) error, options ...func(*graphTraversalConfig)) error {
graph, err := NewGraph(project.Services, ServiceStopped)
if err != nil {
return err
}
return visit(ctx, graph, upDirectionTraversalConfig, fn)
}
// InReverseDependencyOrder applies the function to the services of the project in reverse order of dependencies
func InReverseDependencyOrder(ctx context.Context, project *types.Project, fn func(context.Context, string) error) error {
return visit(ctx, project, downDirectionTraversalConfig, fn, ServiceStarted)
}
func visit(ctx context.Context, project *types.Project, traversalConfig graphTraversalConfig, fn func(context.Context, string) error, initialStatus ServiceStatus) error {
g := NewGraph(project.Services, initialStatus)
if b, err := g.HasCycles(); b {
graph, err := NewGraph(project.Services, ServiceStarted)
if err != nil {
return err
}
return visit(ctx, graph, downDirectionTraversalConfig, fn)
}
func visit(ctx context.Context, g *Graph, traversalConfig graphTraversalConfig, fn func(context.Context, string) error) error {
nodes := traversalConfig.extremityNodesFn(g)
eg, _ := errgroup.WithContext(ctx)
@ -155,7 +158,7 @@ func (v *Vertex) GetChildren() []*Vertex {
}
// NewGraph returns the dependency graph of the services
func NewGraph(services types.Services, initialStatus ServiceStatus) *Graph {
func NewGraph(services types.Services, initialStatus ServiceStatus) (*Graph, error) {
graph := &Graph{
lock: sync.RWMutex{},
Vertices: map[string]*Vertex{},
@ -171,7 +174,11 @@ func NewGraph(services types.Services, initialStatus ServiceStatus) *Graph {
}
}
return graph
if b, err := graph.HasCycles(); b {
return nil, err
}
return graph, nil
}
// NewVertex is the constructor function for the Vertex

View File

@ -50,6 +50,13 @@ func (s *composeService) start(ctx context.Context, projectName string, options
}
}
if len(options.Services) > 0 {
err := project.ForServices(options.Services)
if err != nil {
return err
}
}
eg, ctx := errgroup.WithContext(ctx)
if listener != nil {
attached, err := s.attach(ctx, project, listener, options.AttachTo)