Skip to content

Commit 24b2a3a

Browse files
committed
fix orphan detection
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 36078c5 commit 24b2a3a

3 files changed

Lines changed: 47 additions & 26 deletions

File tree

pkg/compose/hash.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ func ServiceHash(o types.ServiceConfig) (string, error) {
4242
return digest.SHA256.FromBytes(bytes).Encoded(), nil
4343
}
4444

45+
// ContainerSpecHash computes the configuration hash for a ContainerSpec (used by jobs and one-off containers).
46+
func ContainerSpecHash(o types.ContainerSpec) (string, error) {
47+
o.Build = nil
48+
o.PullPolicy = ""
49+
o.DependsOn = nil
50+
51+
bytes, err := json.Marshal(o)
52+
if err != nil {
53+
return "", err
54+
}
55+
return digest.SHA256.FromBytes(bytes).Encoded(), nil
56+
}
57+
4558
// NetworkHash computes the configuration hash for a network.
4659
func NetworkHash(o *types.NetworkConfig) (string, error) {
4760
bytes, err := json.Marshal(o)

pkg/compose/run.go

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -190,39 +190,17 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
190190
return prepareRunResult{}, err
191191
}
192192

193-
createOpts := createOptions{
194-
AutoRemove: opts.AutoRemove,
195-
AttachStdin: opts.Interactive,
196-
UseNetworkAliases: opts.UseNetworkAliases,
197-
Labels: mergeLabels(target.Labels, target.CustomLabels),
198-
}
199-
200-
eventName := "Container " + target.ContainerName
201-
s.events.On(creatingEvent(eventName))
202-
created, err := s.createMobyContainer(ctx, project, target.Name, &target.ContainerSpec, nil, target.ContainerName, -1, nil, createOpts)
203-
if err != nil {
204-
if ctx.Err() == nil {
205-
s.events.On(api.Resource{
206-
ID: eventName,
207-
Status: api.Error,
208-
Text: err.Error(),
209-
})
210-
}
211-
return prepareRunResult{}, err
212-
}
213-
s.events.On(createdEvent(eventName))
214-
215-
inspect, err := s.apiClient().ContainerInspect(ctx, created.ID, client.ContainerInspectOptions{})
193+
created, err := s.createOneOffContainer(ctx, project, target, opts)
216194
if err != nil {
217195
return prepareRunResult{}, err
218196
}
219197

220-
err = s.injectSecrets(ctx, project, target.Name, &target.ContainerSpec, inspect.Container.ID)
198+
err = s.injectSecrets(ctx, project, target.Name, &target.ContainerSpec, created.ID)
221199
if err != nil {
222200
return prepareRunResult{containerID: created.ID}, err
223201
}
224202

225-
err = s.injectConfigs(ctx, project, target.Name, &target.ContainerSpec, inspect.Container.ID)
203+
err = s.injectConfigs(ctx, project, target.Name, &target.ContainerSpec, created.ID)
226204
return prepareRunResult{
227205
containerID: created.ID,
228206
target: target,
@@ -247,6 +225,36 @@ func resolveRunTarget(project *types.Project, opts api.RunOptions) (runTarget, e
247225
return runTarget{Name: service.Name, ContainerSpec: service.ContainerSpec}, nil
248226
}
249227

228+
func (s *composeService) createOneOffContainer(ctx context.Context, project *types.Project, target runTarget, opts api.RunOptions) (container.Summary, error) {
229+
hash, err := ContainerSpecHash(target.ContainerSpec)
230+
if err != nil {
231+
return container.Summary{}, err
232+
}
233+
createOpts := createOptions{
234+
AutoRemove: opts.AutoRemove,
235+
AttachStdin: opts.Interactive,
236+
UseNetworkAliases: opts.UseNetworkAliases,
237+
Labels: mergeLabels(target.Labels, target.CustomLabels).
238+
Add(api.ConfigHashLabel, hash),
239+
}
240+
241+
eventName := "Container " + target.ContainerName
242+
s.events.On(creatingEvent(eventName))
243+
created, err := s.createMobyContainer(ctx, project, target.Name, &target.ContainerSpec, nil, target.ContainerName, -1, nil, createOpts)
244+
if err != nil {
245+
if ctx.Err() == nil {
246+
s.events.On(api.Resource{
247+
ID: eventName,
248+
Status: api.Error,
249+
Text: err.Error(),
250+
})
251+
}
252+
return container.Summary{}, err
253+
}
254+
s.events.On(createdEvent(eventName))
255+
return created, nil
256+
}
257+
250258
func prepareBuildOptions(opts api.RunOptions) *api.BuildOptions {
251259
if opts.Build == nil {
252260
return nil

pkg/e2e/compose_run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestLocalComposeRun(t *testing.T) {
7878
assert.Assert(t, strings.Contains(res.Stdout(), "run-test-back"), res.Stdout())
7979
})
8080

81-
t.Run("down", func(t *testing.T) {
81+
t.Run("down --remove-orphans", func(t *testing.T) {
8282
c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "down", "--remove-orphans")
8383
res := c.RunDockerCmd(t, "ps", "--all")
8484
assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())

0 commit comments

Comments
 (0)