Set Required false to depends_on containers for compose -p stop/down

Signed-off-by: koooge <koooooge@gmail.com>
This commit is contained in:
koooge 2024-04-10 00:02:37 +02:00 committed by Nicolas De loof
parent 54a525bbe6
commit 5059a1d7bf
4 changed files with 26 additions and 13 deletions

View File

@ -77,7 +77,7 @@ func downDirectionTraversal(visitorFn func(context.Context, string) error) *grap
// 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, options ...func(*graphTraversal)) error {
graph, err := NewGraph(project, ServiceStopped, false)
graph, err := NewGraph(project, ServiceStopped)
if err != nil {
return err
}
@ -89,8 +89,8 @@ func InDependencyOrder(ctx context.Context, project *types.Project, fn func(cont
}
// InReverseDependencyOrder applies the function to the services of the project in reverse order of dependencies
func InReverseDependencyOrder(ctx context.Context, project *types.Project, ignoreMissing bool, fn func(context.Context, string) error, options ...func(*graphTraversal)) error {
graph, err := NewGraph(project, ServiceStarted, ignoreMissing)
func InReverseDependencyOrder(ctx context.Context, project *types.Project, fn func(context.Context, string) error, options ...func(*graphTraversal)) error {
graph, err := NewGraph(project, ServiceStarted)
if err != nil {
return err
}
@ -257,7 +257,7 @@ func (v *Vertex) GetChildren() []*Vertex {
}
// NewGraph returns the dependency graph of the services
func NewGraph(project *types.Project, initialStatus ServiceStatus, ignoreMissing bool) (*Graph, error) {
func NewGraph(project *types.Project, initialStatus ServiceStatus) (*Graph, error) {
graph := &Graph{
lock: sync.RWMutex{},
Vertices: map[string]*Vertex{},
@ -271,7 +271,7 @@ func NewGraph(project *types.Project, initialStatus ServiceStatus, ignoreMissing
for _, name := range s.GetDependencies() {
err := graph.AddEdge(s.Name, name)
if err != nil {
if ignoreMissing || !s.DependsOn[name].Required {
if !s.DependsOn[name].Required {
delete(s.DependsOn, name)
project.Services[index] = s
continue

View File

@ -115,7 +115,7 @@ func TestInDependencyReverseDownCommandOrder(t *testing.T) {
t.Cleanup(cancel)
var order []string
err := InReverseDependencyOrder(ctx, createTestProject(), false, func(ctx context.Context, service string) error {
err := InReverseDependencyOrder(ctx, createTestProject(), func(ctx context.Context, service string) error {
order = append(order, service)
return nil
})
@ -270,7 +270,7 @@ func TestBuildGraph(t *testing.T) {
Services: tC.services,
}
graph, err := NewGraph(&project, ServiceStopped, false)
graph, err := NewGraph(&project, ServiceStopped)
assert.NilError(t, err, fmt.Sprintf("failed to build graph for: %s", tC.desc))
for k, vertex := range graph.Vertices {
@ -282,7 +282,7 @@ func TestBuildGraph(t *testing.T) {
}
}
func TestBuildGraphIgnoreMissing(t *testing.T) {
func TestBuildGraphDependsOn(t *testing.T) {
testCases := []struct {
desc string
services types.Services
@ -298,7 +298,7 @@ func TestBuildGraphIgnoreMissing(t *testing.T) {
Condition: "service_completed_successfully",
Restart: false,
Extensions: types.Extensions(nil),
Required: true,
Required: false,
},
},
},
@ -320,7 +320,7 @@ func TestBuildGraphIgnoreMissing(t *testing.T) {
Services: tC.services,
}
graph, err := NewGraph(&project, ServiceStopped, true)
graph, err := NewGraph(&project, ServiceStopped)
assert.NilError(t, err, fmt.Sprintf("failed to build graph for: %s", tC.desc))
for k, vertex := range graph.Vertices {

View File

@ -74,7 +74,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
resourceToRemove = true
}
err = InReverseDependencyOrder(ctx, project, true, func(c context.Context, service string) error {
err = InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
serviceContainers := containers.filter(isService(service))
err := s.removeContainers(ctx, serviceContainers, options.Timeout, options.Volumes)
return err
@ -344,10 +344,22 @@ func (s *composeService) stopAndRemoveContainer(ctx context.Context, container m
func (s *composeService) getProjectWithResources(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
containers = containers.filter(isNotOneOff)
project, err := s.projectFromName(containers, projectName)
p, err := s.projectFromName(containers, projectName)
if err != nil && !api.IsNotFoundError(err) {
return nil, err
}
project, err := p.WithServicesTransform(func(name string, service types.ServiceConfig) (types.ServiceConfig, error) {
for k := range service.DependsOn {
if dependency, ok := service.DependsOn[k]; ok {
dependency.Required = false
service.DependsOn[k] = dependency
}
}
return service, nil
})
if err != nil {
return nil, err
}
volumes, err := s.actualVolumes(ctx, projectName)
if err != nil {
@ -360,5 +372,6 @@ func (s *composeService) getProjectWithResources(ctx context.Context, containers
return nil, err
}
project.Networks = networks
return project, nil
}

View File

@ -50,7 +50,7 @@ func (s *composeService) stop(ctx context.Context, projectName string, options a
}
w := progress.ContextWriter(ctx)
return InReverseDependencyOrder(ctx, project, true, func(c context.Context, service string) error {
return InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
if !utils.StringContains(options.Services, service) {
return nil
}