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

docker: extra_hosts argument to add hosts to a container’s /etc/hosts… #6185

Merged
merged 3 commits into from
Aug 1, 2023
Merged
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
1 change: 1 addition & 0 deletions internal/build/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func Options(archive io.Reader, spec v1alpha1.DockerImageSpec) docker.BuildOptio
CacheFrom: spec.CacheFrom,
PullParent: spec.Pull,
Platform: spec.Platform,
ExtraHosts: spec.ExtraHosts,
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ func (c *Cli) ImageBuild(ctx context.Context, g *errgroup.Group, buildContext io
opts.CacheFrom = options.CacheFrom
opts.PullParent = options.PullParent
opts.Platform = options.Platform
opts.ExtraHosts = append([]string{}, options.ExtraHosts...)

if options.DirSource != nil {
opts.RemoteContext = clientSessionRemote
Expand Down
1 change: 1 addition & 0 deletions internal/docker/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ type BuildOptions struct {
ExtraTags []string
ForceLegacyBuilder bool
DirSource filesync.DirSource
ExtraHosts []string
}
6 changes: 5 additions & 1 deletion internal/tiltfile/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ type dockerImage struct {

dockerComposeService string
dockerComposeLocalVolumePaths []string

extraHosts []string
}

func (d *dockerImage) ID() model.TargetID {
Expand Down Expand Up @@ -114,7 +116,7 @@ func (s *tiltfileState) dockerBuild(thread *starlark.Thread, fn *starlark.Builti
entrypoint starlark.Value
var buildArgs value.StringStringMap
var network, platform value.Stringable
var ssh, secret, extraTags, cacheFrom value.StringOrStringList
var ssh, secret, extraTags, cacheFrom, extraHosts value.StringOrStringList
var matchInEnvVars, pullParent bool
var overrideArgsVal starlark.Sequence
if err := s.unpackArgs(fn.Name(), args, kwargs,
Expand All @@ -138,6 +140,7 @@ func (s *tiltfileState) dockerBuild(thread *starlark.Thread, fn *starlark.Builti
"cache_from?", &cacheFrom,
"pull?", &pullParent,
"platform?", &platform,
"extra_hosts?", &extraHosts,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -256,6 +259,7 @@ func (s *tiltfileState) dockerBuild(thread *starlark.Thread, fn *starlark.Builti
pullParent: pullParent,
platform: platform.Value,
tiltfilePath: starkit.CurrentExecPath(thread),
extraHosts: extraHosts.Values,
}
err = s.buildIndex.addImage(r)
if err != nil {
Expand Down
53 changes: 53 additions & 0 deletions internal/tiltfile/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tiltfile
import (
"fmt"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -204,3 +205,55 @@ custom_build('gcr.io/fe', 'export MY_REF="gcr.io/fe:dev" && docker build -t $MY_

f.loadErrString("Cannot specify both tag= and outputs_image_ref_to=")
}

func TestExtraHosts(t *testing.T) {
type tc struct {
name string
argValue []string
expected []string
}
tcs := []tc{
{
//docker_build('gcr.io/fe', '.')
name: "No Extra Hosts",
},
{
// docker_build('gcr.io/fe', '.', extra_hosts='testing.example.com:10.0.0.1')
name: "One Extra Host Only",
argValue: []string{"testing.example.com:10.0.0.1"},
expected: []string{"testing.example.com:10.0.0.1"},
},
{
// docker_build('gcr.io/fe', '.', extra_hosts=["testing.example.com:10.0.0.1","app.testing.example.com:10.0.0.3"])
name: "Two Extra Hosts",
argValue: []string{"testing.example.com:10.0.0.1", "app.testing.example.com:10.0.0.3"},
expected: []string{"testing.example.com:10.0.0.1", "app.testing.example.com:10.0.0.3"},
},
}

for _, tt := range tcs {
t.Run(
tt.name, func(t *testing.T) {
f := newFixture(t)

f.yaml("fe.yaml", deployment("fe", image("gcr.io/fe")))
f.file("Dockerfile", `FROM alpine`)

tf := "k8s_yaml('fe.yaml')\ndocker_build('gcr.io/fe', '.'"
if len(tt.argValue) == 1 {
tf += fmt.Sprintf(", extra_hosts='%s'", tt.argValue[0])
} else if len(tt.argValue) > 1 {
tf += `, extra_hosts=["` + strings.Join(tt.argValue, `","`) + `"]`
}
tf += ")"
fmt.Println(tf)

f.file("Tiltfile", tf)

f.load()
m := f.assertNextManifest("fe")
require.Equal(t, tt.expected, m.ImageTargetAt(0).DockerBuildInfo().ExtraHosts)
},
)
}
}
1 change: 1 addition & 0 deletions internal/tiltfile/tiltfile_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,7 @@ func (s *tiltfileState) imgTargetsForDepsHelper(mn model.ManifestName, imageMapD
Platform: image.platform,
ExtraTags: image.extraTags,
ContextIgnores: contextIgnores,
ExtraHosts: image.extraHosts,
}
iTarget = iTarget.WithBuildDetails(model.DockerBuild{DockerImageSpec: spec})
case CustomBuild:
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/core/v1alpha1/dockerimage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ type DockerImageSpec struct {
//
// +optional
ClusterNeeds ClusterImageNeeds `json:"clusterNeeds,omitempty" protobuf:"bytes,15,opt,name=clusterNeeds,casttype=ClusterImageNeeds"`

// Other hosts to be added into a container’s /etc/hosts file
//
// https://docs.docker.com/engine/reference/commandline/build/#add-host
//
// Equivalent to `--add-host` in the Docker CLI.
ExtraHosts []string `json:"extraHosts,omitempty" protobuf:"bytes,17,opt,name=extraHosts"`
}

var _ resource.Object = &DockerImage{}
Expand Down
Loading