Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sessionID HTTP Header to the Docker client setup #570

Merged
merged 5 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@ func NewDockerClient() (cli *client.Client, host string, tcConfig TestContainers
host = "unix:///var/run/docker.sock"
}

opts = append(opts, client.WithHTTPHeaders(
map[string]string{
"x-tc-sid": sessionID().String(),
}),
)

cli, err = client.NewClientWithOpts(opts...)

if err != nil {
Expand Down Expand Up @@ -934,7 +940,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
req.Labels = make(map[string]string)
}

sessionID := uuid.New()
sessionID := sessionID()

var termSignal chan bool
if !req.SkipReaper {
Expand Down Expand Up @@ -1155,7 +1161,7 @@ func (p *DockerProvider) ReuseOrCreateContainer(ctx context.Context, req Contain
return p.CreateContainer(ctx, req)
}

sessionID := uuid.New()
sessionID := sessionID()
var termSignal chan bool
if !req.SkipReaper {
r, err := NewReaper(ctx, sessionID.String(), p, req.ReaperImage)
Expand Down Expand Up @@ -1310,10 +1316,9 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
IPAM: req.IPAM,
}

sessionID := uuid.New()

var termSignal chan bool
if !req.SkipReaper {
sessionID := sessionID()
r, err := NewReaper(context.WithValue(ctx, dockerHostContextKey, p.host), sessionID.String(), p, req.ReaperImage)
if err != nil {
return nil, fmt.Errorf("%w: creating network reaper failed", err)
Expand Down
6 changes: 5 additions & 1 deletion reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func NewReaper(ctx context.Context, sessionID string, provider ReaperProvider, r
ExposedPorts: []string{string(listeningPort)},
NetworkMode: Bridge,
Labels: map[string]string{
TestcontainerLabel: "true",
TestcontainerLabelIsReaper: "true",
},
SkipReaper: true,
Expand All @@ -72,6 +71,11 @@ func NewReaper(ctx context.Context, sessionID string, provider ReaperProvider, r
WaitingFor: wait.ForListeningPort(listeningPort),
}

// include reaper-specific labels to the reaper container
for k, v := range reaper.Labels() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

req.Labels[k] = v
}

tcConfig := provider.Config()
req.Privileged = tcConfig.RyukPrivileged

Expand Down
18 changes: 18 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package testcontainers

import (
"sync"

"github.com/google/uuid"
)

var tcSessionID uuid.UUID
var tcSessionIDOnce sync.Once

func sessionID() uuid.UUID {
tcSessionIDOnce.Do(func() {
tcSessionID = uuid.New()
})

return tcSessionID
}