Skip to content

Commit 9765f17

Browse files
committed
store exec details to offer better dry-run status on ExecStart
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 25be264 commit 9765f17

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

pkg/api/dryrunclient.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
"net"
2424
"net/http"
2525
"strings"
26+
"sync"
2627

28+
"github.com/distribution/distribution/v3/uuid"
2729
moby "github.com/docker/docker/api/types"
2830
containerType "github.com/docker/docker/api/types/container"
2931
"github.com/docker/docker/api/types/events"
@@ -35,6 +37,7 @@ import (
3537
"github.com/docker/docker/api/types/volume"
3638
"github.com/docker/docker/client"
3739
specs "github.com/opencontainers/image-spec/specs-go/v1"
40+
"github.com/pkg/errors"
3841
)
3942

4043
const (
@@ -48,12 +51,19 @@ type DryRunKey struct{}
4851
// DryRunClient implements APIClient by delegating to implementation functions. This allows lazy init and per-method overrides
4952
type DryRunClient struct {
5053
apiClient client.APIClient
54+
execs sync.Map
55+
}
56+
57+
type execDetails struct {
58+
container string
59+
command []string
5160
}
5261

5362
// NewDryRunClient produces a DryRunClient
5463
func NewDryRunClient(apiClient client.APIClient) *DryRunClient {
5564
return &DryRunClient{
5665
apiClient: apiClient,
66+
execs: sync.Map{},
5767
}
5868
}
5969

@@ -156,13 +166,23 @@ func (d *DryRunClient) VolumeRemove(ctx context.Context, volumeID string, force
156166
}
157167

158168
func (d *DryRunClient) ContainerExecCreate(ctx context.Context, container string, config moby.ExecConfig) (moby.IDResponse, error) {
159-
fmt.Printf("%sCreating Exec configuration for container %s with command '%s'\n", DRYRUN_PREFIX, container, strings.Join(config.Cmd, " "))
160-
config.Cmd = []string{"true"}
161-
return d.apiClient.ContainerExecCreate(ctx, container, config)
169+
id := uuid.Generate().String()
170+
d.execs.Store(id, execDetails{
171+
container: container,
172+
command: config.Cmd,
173+
})
174+
return moby.IDResponse{
175+
ID: id,
176+
}, nil
162177
}
163178

164179
func (d *DryRunClient) ContainerExecStart(ctx context.Context, execID string, config moby.ExecStartCheck) error {
165-
fmt.Printf("%sExecuting command in detach mode\n", DRYRUN_PREFIX)
180+
v, ok := d.execs.LoadAndDelete(execID)
181+
if !ok {
182+
return fmt.Errorf("invalid exec ID %q", execID)
183+
}
184+
details := v.(execDetails)
185+
fmt.Printf("%sExecuting command %q in %s (detached mode)\n", DRYRUN_PREFIX, details.command, details.container)
166186
return nil
167187
}
168188

@@ -197,7 +217,7 @@ func (d *DryRunClient) ContainerDiff(ctx context.Context, container string) ([]c
197217
}
198218

199219
func (d *DryRunClient) ContainerExecAttach(ctx context.Context, execID string, config moby.ExecStartCheck) (moby.HijackedResponse, error) {
200-
return d.apiClient.ContainerExecAttach(ctx, execID, config)
220+
return moby.HijackedResponse{}, errors.New("interactive exec is not supported in dry-run mode")
201221
}
202222

203223
func (d *DryRunClient) ContainerExecInspect(ctx context.Context, execID string) (moby.ContainerExecInspect, error) {

0 commit comments

Comments
 (0)