Skip to content

Commit

Permalink
Add hostname specifying for building
Browse files Browse the repository at this point in the history
Fix: #1301

Signed-off-by: l00397676 <lujingxiao@huawei.com>
  • Loading branch information
jingxiaolu committed Jan 22, 2020
1 parent 8a26782 commit f594804
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 169 deletions.
10 changes: 6 additions & 4 deletions client/llb/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Meta struct {
Env EnvList
Cwd string
User string
Hostname string
ProxyEnv *ProxyEnv
ExtraHosts []HostIP
Network pb.NetMode
Expand Down Expand Up @@ -155,10 +156,11 @@ func (e *ExecOp) Marshal(c *Constraints) (digest.Digest, []byte, *pb.OpMetadata,
}

meta := &pb.Meta{
Args: e.meta.Args,
Env: e.meta.Env.ToArray(),
Cwd: e.meta.Cwd,
User: e.meta.User,
Args: e.meta.Args,
Env: e.meta.Env.ToArray(),
Cwd: e.meta.Cwd,
User: e.meta.User,
Hostname: e.meta.Hostname,
}
if len(e.meta.ExtraHosts) > 0 {
hosts := make([]*pb.HostIP, len(e.meta.ExtraHosts))
Expand Down
15 changes: 15 additions & 0 deletions client/llb/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
keyDir = contextKeyT("llb.exec.dir")
keyEnv = contextKeyT("llb.exec.env")
keyUser = contextKeyT("llb.exec.user")
keyHostname = contextKeyT("llb.exec.hostname")
keyExtraHost = contextKeyT("llb.exec.extrahost")
keyPlatform = contextKeyT("llb.platform")
keyNetwork = contextKeyT("llb.network")
Expand Down Expand Up @@ -99,6 +100,20 @@ func getUser(s State) string {
return ""
}

func hostname(str string) StateOption {
return func(s State) State {
return s.WithValue(keyHostname, str)
}
}

func getHostname(s State) string {
v := s.Value(keyHostname)
if v != nil {
return v.(string)
}
return ""
}

func args(args ...string) StateOption {
return func(s State) State {
return s.WithValue(keyArgs, args)
Expand Down
9 changes: 9 additions & 0 deletions client/llb/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func (s State) Run(ro ...RunOption) ExecState {
Cwd: getDir(ei.State),
Env: getEnv(ei.State),
User: getUser(ei.State),
Hostname: getHostname(ei.State),
ProxyEnv: ei.ProxyEnv,
ExtraHosts: getExtraHosts(ei.State),
Network: getNetwork(ei.State),
Expand Down Expand Up @@ -278,6 +279,14 @@ func (s State) User(v string) State {
return user(v)(s)
}

func (s State) Hostname(v string) State {
return hostname(v)(s)
}

func (s State) GetHostname() string {
return getHostname(s)
}

func (s State) Platform(p specs.Platform) State {
return platform(p)(s)
}
Expand Down
2 changes: 1 addition & 1 deletion executor/containerdexecutor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (w containerdExecutor) Exec(ctx context.Context, meta executor.Meta, root c
return err
}

hostsFile, clean, err := oci.GetHostsFile(ctx, w.root, meta.ExtraHosts, nil)
hostsFile, clean, err := oci.GetHostsFile(ctx, w.root, meta.ExtraHosts, nil, meta.Hostname)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Meta struct {
Env []string
User string
Cwd string
Hostname string
Tty bool
ReadonlyRootFS bool
ExtraHosts []HostIP
Expand Down
15 changes: 10 additions & 5 deletions executor/oci/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/executor"
Expand All @@ -18,21 +19,21 @@ const hostsContent = `
::1 localhost ip6-localhost ip6-loopback
`

func GetHostsFile(ctx context.Context, stateDir string, extraHosts []executor.HostIP, idmap *idtools.IdentityMapping) (string, func(), error) {
func GetHostsFile(ctx context.Context, stateDir string, extraHosts []executor.HostIP, idmap *idtools.IdentityMapping, hostname string) (string, func(), error) {
if len(extraHosts) == 0 {
_, err := g.Do(ctx, stateDir, func(ctx context.Context) (interface{}, error) {
_, _, err := makeHostsFile(stateDir, nil, idmap)
_, _, err := makeHostsFile(stateDir, nil, idmap, hostname)
return nil, err
})
if err != nil {
return "", nil, err
}
return filepath.Join(stateDir, "hosts"), func() {}, nil
}
return makeHostsFile(stateDir, extraHosts, idmap)
return makeHostsFile(stateDir, extraHosts, idmap, hostname)
}

func makeHostsFile(stateDir string, extraHosts []executor.HostIP, idmap *idtools.IdentityMapping) (string, func(), error) {
func makeHostsFile(stateDir string, extraHosts []executor.HostIP, idmap *idtools.IdentityMapping, hostname string) (string, func(), error) {
p := filepath.Join(stateDir, "hosts")
if len(extraHosts) != 0 {
p += "." + identity.NewID()
Expand All @@ -47,7 +48,11 @@ func makeHostsFile(stateDir string, extraHosts []executor.HostIP, idmap *idtools

b := &bytes.Buffer{}

if _, err := b.Write([]byte(hostsContent)); err != nil {
var hosts = hostsContent
if hostname != "" {
hosts = strings.Replace(hosts, "buildkitsandbox", hostname, 1)
}
if _, err := b.Write([]byte(hosts)); err != nil {
return "", nil, err
}

Expand Down
4 changes: 4 additions & 0 deletions executor/oci/spec_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
s.Process.Cwd = meta.Cwd
s.Process.Rlimits = nil // reset open files limit
s.Process.NoNewPrivileges = false // reset nonewprivileges

s.Hostname = "buildkitsandbox"
if meta.Hostname != "" {
s.Hostname = meta.Hostname
}

s.Mounts, err = GetMounts(ctx,
withProcessMode(processMode),
Expand Down
2 changes: 1 addition & 1 deletion executor/runcexecutor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (w *runcExecutor) Exec(ctx context.Context, meta executor.Meta, root cache.
return err
}

hostsFile, clean, err := oci.GetHostsFile(ctx, w.root, meta.ExtraHosts, w.idmap)
hostsFile, clean, err := oci.GetHostsFile(ctx, w.root, meta.ExtraHosts, w.idmap, meta.Hostname)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State,
k, v := parseKeyValue(env)
d.state = d.state.AddEnv(k, v)
}
if val, ok := opt.BuildArgs["HOSTNAME"]; ok {
d.state = d.state.Hostname(val)
}
if d.image.Config.WorkingDir != "" {
if err = dispatchWorkdir(d, &instructions.WorkdirCommand{Path: d.image.Config.WorkingDir}, false, nil); err != nil {
return nil, nil, err
Expand Down
1 change: 1 addition & 0 deletions solver/llbsolver/ops/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ func (e *execOp) Exec(ctx context.Context, inputs []solver.Result) ([]solver.Res
Env: e.op.Meta.Env,
Cwd: e.op.Meta.Cwd,
User: e.op.Meta.User,
Hostname: e.op.Meta.Hostname,
ReadonlyRootFS: readonlyRootFS,
ExtraHosts: extraHosts,
NetMode: e.op.Network,
Expand Down
Loading

0 comments on commit f594804

Please sign in to comment.