Skip to content

Commit

Permalink
unify some config handling paths
Browse files Browse the repository at this point in the history
still not great, but slightly better

Signed-off-by: Alex Suraci <suraci.alex@gmail.com>
  • Loading branch information
vito committed Jul 10, 2023
1 parent 0757c6d commit 8bb27ca
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
37 changes: 30 additions & 7 deletions session/networks/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
context "context"
strings "strings"

"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/session"
)

Expand All @@ -15,21 +16,44 @@ func (config *Config) ExtraHosts() string {
return out
}

func MergeConfig(ctx context.Context, c session.Caller, base *Config, id string) (*Config, error) {
func FromDNSConfig(dns *executor.DNSConfig) *Config {
if dns == nil {
return &Config{}
}

return &Config{
Dns: &DNSConfig{
Nameservers: dns.Nameservers,
Options: dns.Options,
SearchDomains: dns.SearchDomains,
},
}
}

func LoadConfig(ctx context.Context, c session.Caller, id string) (*Config, error) {
nw, err := NewNetworksClient(c.Conn()).GetNetwork(ctx, &GetNetworkRequest{ID: id})
if err != nil {
return nil, err
}

if nw.Config == nil {
return nw.Config, nil
}

func MergeConfig(ctx context.Context, c session.Caller, base *Config, id string) (*Config, error) {
custom, err := LoadConfig(ctx, c, id)
if err != nil {
return nil, err
}

if custom == nil {
return base, nil
}

cp := *base

cp.IpHosts = append(nw.Config.IpHosts, cp.IpHosts...)
cp.IpHosts = append(custom.IpHosts, cp.IpHosts...)

dns := nw.Config.Dns
dns := custom.Dns
if dns != nil {
var dnsCp DNSConfig
if cp.Dns != nil {
Expand All @@ -38,15 +62,14 @@ func MergeConfig(ctx context.Context, c session.Caller, base *Config, id string)

cp.Dns = &dnsCp

// override individual fields
// TODO append? should match behavior with git source

if len(dns.Nameservers) > 0 {
cp.Dns.Nameservers = dns.Nameservers
}

if len(dns.Options) > 0 {
cp.Dns.Options = dns.Options
}

if len(dns.SearchDomains) > 0 {
cp.Dns.SearchDomains = dns.SearchDomains
}
Expand Down
4 changes: 2 additions & 2 deletions solver/llbsolver/ops/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,10 @@ func (e *ExecOp) loadNetworkConfig(ctx context.Context, g session.Group) (*netwo
if id == "" {
return nil, nil
}
cfg := &networks.Config{}
var cfg *networks.Config
err := e.sm.Any(ctx, g, func(ctx context.Context, _ string, caller session.Caller) error {
var err error
cfg, err = networks.MergeConfig(ctx, caller, cfg, id)
cfg, err = networks.LoadConfig(ctx, caller, id)
return err
})
if err != nil {
Expand Down
12 changes: 2 additions & 10 deletions source/git/gitsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,8 @@ func (gs *gitSourceHandler) mountKnownHosts(ctx context.Context) (string, func()
}

func (gs *gitSourceHandler) networkConfig(ctx context.Context, g session.Group) (*networks.Config, error) {
netConfig := &networks.Config{}

if gs.dns != nil {
// base network config inherits from system-wide config
netConfig.Dns = &networks.DNSConfig{
Nameservers: gs.dns.Nameservers,
Options: gs.dns.Options,
SearchDomains: gs.dns.SearchDomains,
}
}
// base network config inherits from system-wide config
netConfig := networks.FromDNSConfig(gs.dns)

if gs.src.NetworkConfigID != "" {
err := gs.sm.Any(ctx, g, func(ctx context.Context, _ string, caller session.Caller) error {
Expand Down
12 changes: 2 additions & 10 deletions source/http/httpsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,8 @@ func (hs *httpSource) Resolve(ctx context.Context, id source.Identifier, sm *ses
}

func (hs *httpSourceHandler) client(ctx context.Context, g session.Group) (*http.Client, error) {
netConfig := &networks.Config{}

if hs.dns != nil {
// base network config inherits from system-wide config
netConfig.Dns = &networks.DNSConfig{
Nameservers: hs.dns.Nameservers,
Options: hs.dns.Options,
SearchDomains: hs.dns.SearchDomains,
}
}
// base network config inherits from system-wide config
netConfig := networks.FromDNSConfig(hs.dns)

if hs.src.NetworkConfigID != "" {
err := hs.sm.Any(ctx, g, func(ctx context.Context, _ string, caller session.Caller) error {
Expand Down

0 comments on commit 8bb27ca

Please sign in to comment.