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

Dood to dind #201

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Request struct {
Contents []*Content
PortBindings nat.PortMap
ExtraHosts []string
Volumes map[string]struct{}
}

type ResourcesRequest struct {
Expand Down Expand Up @@ -137,6 +138,7 @@ func (d *Client) Start(ctx context.Context, req *Request) (*Response, error) {
Labels: d.withDefaultLabels(req.Labels),
Healthcheck: req.HealthCheck,
ExposedPorts: exposedPorts,
Volumes: req.Volumes,
},
&container.HostConfig{
ExtraHosts: req.ExtraHosts,
Expand Down
49 changes: 26 additions & 23 deletions internal/harness/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"time"

client "github.com/chainguard-dev/terraform-provider-imagetest/internal/docker"
"github.com/chainguard-dev/terraform-provider-imagetest/internal/harness"
"github.com/docker/docker/api/types/mount"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/moby/docker-image-spec/specs-go/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

var _ harness.Harness = &docker{}
var _ harness.Harness = &dind{}

const DefaultDockerSocketPath = "/var/run/docker.sock"

type docker struct {
type dind struct {
Name string
ImageRef name.Reference
Networks []client.NetworkAttachment
Expand All @@ -32,8 +34,8 @@ type docker struct {
}

func New(opts ...Option) (harness.Harness, error) {
h := &docker{
ImageRef: name.MustParseReference("cgr.dev/chainguard/docker-cli:latest-dev"),
h := &dind{
ImageRef: name.MustParseReference("docker:dind"), // NOTE: This will basically always be overridden by the bundled image
Resources: client.ResourcesRequest{
MemoryRequest: resource.MustParse("1Gi"),
MemoryLimit: resource.MustParse("2Gi"),
Expand All @@ -54,13 +56,15 @@ func New(opts ...Option) (harness.Harness, error) {
}

// Create implements harness.Harness.
func (h *docker) Create(ctx context.Context) error {
func (h *dind) Create(ctx context.Context) error {
cli, err := client.New()
if err != nil {
return err
}

nw, err := cli.CreateNetwork(ctx, &client.NetworkRequest{})
nw, err := cli.CreateNetwork(ctx, &client.NetworkRequest{
Name: h.Name,
})
if err != nil {
return fmt.Errorf("creating network: %w", err)
}
Expand All @@ -76,15 +80,9 @@ func (h *docker) Create(ctx context.Context) error {
return fmt.Errorf("creating docker config json: %w", err)
}

mounts := append(h.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: "/var/run/docker.sock",
Target: "/var/run/docker.sock",
})

if len(h.Volumes) > 0 {
for _, vol := range h.Volumes {
mounts = append(mounts, mount.Mount{
h.Mounts = append(h.Mounts, mount.Mount{
Type: mount.TypeVolume,
Source: vol.Name, // mount.Mount refers to "Source" as the name for a named volume
Target: vol.Target,
Expand All @@ -95,19 +93,29 @@ func (h *docker) Create(ctx context.Context) error {
resp, err := cli.Start(ctx, &client.Request{
Name: h.Name,
Ref: h.ImageRef,
Entrypoint: harness.DefaultEntrypoint(),
Cmd: harness.DefaultCmd(),
Entrypoint: []string{"/usr/bin/dockerd-entrypoint.sh"},
Privileged: true,
Cmd: []string{},
Networks: h.Networks,
Resources: h.Resources,
User: "0:0",
Mounts: mounts,
Mounts: h.Mounts,
Env: h.Envs,
Contents: []*client.Content{
client.NewContentFromString(string(dockerconfigjson), "/root/.docker/config.json"),
},
ExtraHosts: []string{
"host.docker.internal:host-gateway",
},
HealthCheck: &v1.HealthcheckConfig{
Test: []string{"CMD", "/bin/sh", "-c", "docker info"},
Interval: 1 * time.Second,
Retries: 30,
Timeout: 1 * time.Minute,
},
Volumes: map[string]struct{}{
"/var/lib/docker": {},
},
})
if err != nil {
return fmt.Errorf("starting container: %w", err)
Expand All @@ -127,16 +135,11 @@ func (h *docker) Create(ctx context.Context) error {
}

// Run implements harness.Harness.
func (h *docker) Run(ctx context.Context, cmd harness.Command) error {
func (h *dind) Run(ctx context.Context, cmd harness.Command) error {
return h.runner(ctx, cmd)
}

func (h *docker) DebugLogCommand() string {
// TODO implement something here
return ""
}

func (h *docker) Destroy(ctx context.Context) error {
func (h *dind) Destroy(ctx context.Context) error {
return h.stack.Teardown(ctx)
}

Expand Down
20 changes: 10 additions & 10 deletions internal/harness/docker/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/google/go-containerregistry/pkg/name"
)

type Option func(*docker) error
type Option func(*dind) error

type VolumeConfig struct {
Name string
Expand All @@ -34,21 +34,21 @@ type RegistryTlsConfig struct {
}

func WithName(name string) Option {
return func(opt *docker) error {
return func(opt *dind) error {
opt.Name = name
return nil
}
}

func WithImageRef(ref name.Reference) Option {
return func(opt *docker) error {
return func(opt *dind) error {
opt.ImageRef = ref
return nil
}
}

func WithMounts(mounts ...mount.Mount) Option {
return func(opt *docker) error {
return func(opt *dind) error {
if mounts != nil {
opt.Mounts = append(opt.Mounts, mounts...)
}
Expand All @@ -57,14 +57,14 @@ func WithMounts(mounts ...mount.Mount) Option {
}

func WithNetworks(networks ...client.NetworkAttachment) Option {
return func(opt *docker) error {
return func(opt *dind) error {
opt.Networks = append(opt.Networks, networks...)
return nil
}
}

func WithAuthFromStatic(registry, username, password, auth string) Option {
return func(opt *docker) error {
return func(opt *dind) error {
if opt.Registries == nil {
opt.Registries = make(map[string]*RegistryConfig)
}
Expand All @@ -83,7 +83,7 @@ func WithAuthFromStatic(registry, username, password, auth string) Option {
}

func WithAuthFromKeychain(registry string) Option {
return func(opt *docker) error {
return func(opt *dind) error {
if opt.Registries == nil {
opt.Registries = make(map[string]*RegistryConfig)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func WithAuthFromKeychain(registry string) Option {
}

func WithEnvs(env ...string) Option {
return func(opt *docker) error {
return func(opt *dind) error {
if opt.Envs == nil {
opt.Envs = make([]string, 0)
}
Expand All @@ -127,14 +127,14 @@ func WithEnvs(env ...string) Option {
}

func WithResources(req client.ResourcesRequest) Option {
return func(opt *docker) error {
return func(opt *dind) error {
opt.Resources = req
return nil
}
}

func WithVolumes(volumes ...VolumeConfig) Option {
return func(opt *docker) error {
return func(opt *dind) error {
if volumes == nil {
return nil
}
Expand Down
Loading