Skip to content

Commit 8ceaf49

Browse files
kooogendeloof
authored andcommitted
Ignore missing containers when compose down -p
Signed-off-by: koooge <[email protected]>
1 parent bfee07e commit 8ceaf49

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

pkg/compose/dependencies.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func downDirectionTraversal(visitorFn func(context.Context, string) error) *grap
7777

7878
// InDependencyOrder applies the function to the services of the project taking in account the dependency order
7979
func InDependencyOrder(ctx context.Context, project *types.Project, fn func(context.Context, string) error, options ...func(*graphTraversal)) error {
80-
graph, err := NewGraph(project, ServiceStopped)
80+
graph, err := NewGraph(project, ServiceStopped, false)
8181
if err != nil {
8282
return err
8383
}
@@ -89,8 +89,8 @@ func InDependencyOrder(ctx context.Context, project *types.Project, fn func(cont
8989
}
9090

9191
// InReverseDependencyOrder applies the function to the services of the project in reverse order of dependencies
92-
func InReverseDependencyOrder(ctx context.Context, project *types.Project, fn func(context.Context, string) error, options ...func(*graphTraversal)) error {
93-
graph, err := NewGraph(project, ServiceStarted)
92+
func InReverseDependencyOrder(ctx context.Context, project *types.Project, ignoreMissing bool, fn func(context.Context, string) error, options ...func(*graphTraversal)) error {
93+
graph, err := NewGraph(project, ServiceStarted, ignoreMissing)
9494
if err != nil {
9595
return err
9696
}
@@ -257,7 +257,7 @@ func (v *Vertex) GetChildren() []*Vertex {
257257
}
258258

259259
// NewGraph returns the dependency graph of the services
260-
func NewGraph(project *types.Project, initialStatus ServiceStatus) (*Graph, error) {
260+
func NewGraph(project *types.Project, initialStatus ServiceStatus, ignoreMissing bool) (*Graph, error) {
261261
graph := &Graph{
262262
lock: sync.RWMutex{},
263263
Vertices: map[string]*Vertex{},
@@ -271,7 +271,7 @@ func NewGraph(project *types.Project, initialStatus ServiceStatus) (*Graph, erro
271271
for _, name := range s.GetDependencies() {
272272
err := graph.AddEdge(s.Name, name)
273273
if err != nil {
274-
if !s.DependsOn[name].Required {
274+
if ignoreMissing || !s.DependsOn[name].Required {
275275
delete(s.DependsOn, name)
276276
project.Services[index] = s
277277
continue

pkg/compose/dependencies_test.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestInDependencyReverseDownCommandOrder(t *testing.T) {
115115
t.Cleanup(cancel)
116116

117117
var order []string
118-
err := InReverseDependencyOrder(ctx, createTestProject(), func(ctx context.Context, service string) error {
118+
err := InReverseDependencyOrder(ctx, createTestProject(), false, func(ctx context.Context, service string) error {
119119
order = append(order, service)
120120
return nil
121121
})
@@ -270,7 +270,57 @@ func TestBuildGraph(t *testing.T) {
270270
Services: tC.services,
271271
}
272272

273-
graph, err := NewGraph(&project, ServiceStopped)
273+
graph, err := NewGraph(&project, ServiceStopped, false)
274+
assert.NilError(t, err, fmt.Sprintf("failed to build graph for: %s", tC.desc))
275+
276+
for k, vertex := range graph.Vertices {
277+
expected, ok := tC.expectedVertices[k]
278+
assert.Equal(t, true, ok)
279+
assert.Equal(t, true, isVertexEqual(*expected, *vertex))
280+
}
281+
})
282+
}
283+
}
284+
285+
func TestBuildGraphIgnoreMissing(t *testing.T) {
286+
testCases := []struct {
287+
desc string
288+
services types.Services
289+
expectedVertices map[string]*Vertex
290+
}{
291+
{
292+
desc: "service depends on init container which is already removed",
293+
services: types.Services{
294+
"test": {
295+
Name: "test",
296+
DependsOn: types.DependsOnConfig{
297+
"test-removed-init-container": types.ServiceDependency{
298+
Condition: "service_completed_successfully",
299+
Restart: false,
300+
Extensions: types.Extensions(nil),
301+
Required: true,
302+
},
303+
},
304+
},
305+
},
306+
expectedVertices: map[string]*Vertex{
307+
"test": {
308+
Key: "test",
309+
Service: "test",
310+
Status: ServiceStopped,
311+
Children: map[string]*Vertex{},
312+
Parents: map[string]*Vertex{},
313+
},
314+
},
315+
},
316+
}
317+
for _, tC := range testCases {
318+
t.Run(tC.desc, func(t *testing.T) {
319+
project := types.Project{
320+
Services: tC.services,
321+
}
322+
323+
graph, err := NewGraph(&project, ServiceStopped, true)
274324
assert.NilError(t, err, fmt.Sprintf("failed to build graph for: %s", tC.desc))
275325

276326
for k, vertex := range graph.Vertices {

pkg/compose/down.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
7474
resourceToRemove = true
7575
}
7676

77-
err = InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
77+
err = InReverseDependencyOrder(ctx, project, true, func(c context.Context, service string) error {
7878
serviceContainers := containers.filter(isService(service))
7979
err := s.removeContainers(ctx, serviceContainers, options.Timeout, options.Volumes)
8080
return err

pkg/compose/stop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (s *composeService) stop(ctx context.Context, projectName string, options a
5050
}
5151

5252
w := progress.ContextWriter(ctx)
53-
return InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
53+
return InReverseDependencyOrder(ctx, project, false, func(c context.Context, service string) error {
5454
if !utils.StringContains(options.Services, service) {
5555
return nil
5656
}

0 commit comments

Comments
 (0)