diff --git a/cli/compose/convert/service.go b/cli/compose/convert/service.go index 66f7868767..11ce70ad6c 100644 --- a/cli/compose/convert/service.go +++ b/cli/compose/convert/service.go @@ -93,6 +93,11 @@ func convertService( return swarm.ServiceSpec{}, err } + dnsConfig, err := convertDNSConfig(service.DNS, service.DNSSearch) + if err != nil { + return swarm.ServiceSpec{}, err + } + var logDriver *swarm.Driver if service.Logging != nil { logDriver = &swarm.Driver{ @@ -113,6 +118,7 @@ func convertService( Args: service.Command, Hostname: service.Hostname, Hosts: sortStrings(convertExtraHosts(service.ExtraHosts)), + DNSConfig: dnsConfig, Healthcheck: healthcheck, Env: sortStrings(convertEnvironment(service.Environment)), Labels: AddStackLabel(namespace, service.Labels), @@ -446,3 +452,13 @@ func convertDeployMode(mode string, replicas *uint64) (swarm.ServiceMode, error) } return serviceMode, nil } + +func convertDNSConfig(DNS []string, DNSSearch []string) (*swarm.DNSConfig, error) { + if DNS != nil || DNSSearch != nil { + return &swarm.DNSConfig{ + Nameservers: DNS, + Search: DNSSearch, + }, nil + } + return nil, nil +} diff --git a/cli/compose/convert/service_test.go b/cli/compose/convert/service_test.go index 352e9a61b5..91af572994 100644 --- a/cli/compose/convert/service_test.go +++ b/cli/compose/convert/service_test.go @@ -277,3 +277,42 @@ func (s byTargetSort) Less(i, j int) bool { func (s byTargetSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + +func TestConvertDNSConfigEmpty(t *testing.T) { + dnsConfig, err := convertDNSConfig(nil, nil) + + assert.NilError(t, err) + assert.Equal(t, dnsConfig, (*swarm.DNSConfig)(nil)) +} + +var ( + nameservers = []string{"8.8.8.8", "9.9.9.9"} + search = []string{"dc1.example.com", "dc2.example.com"} +) + +func TestConvertDNSConfigAll(t *testing.T) { + dnsConfig, err := convertDNSConfig(nameservers, search) + assert.NilError(t, err) + assert.DeepEqual(t, dnsConfig, &swarm.DNSConfig{ + Nameservers: nameservers, + Search: search, + }) +} + +func TestConvertDNSConfigNameservers(t *testing.T) { + dnsConfig, err := convertDNSConfig(nameservers, nil) + assert.NilError(t, err) + assert.DeepEqual(t, dnsConfig, &swarm.DNSConfig{ + Nameservers: nameservers, + Search: nil, + }) +} + +func TestConvertDNSConfigSearch(t *testing.T) { + dnsConfig, err := convertDNSConfig(nil, search) + assert.NilError(t, err) + assert.DeepEqual(t, dnsConfig, &swarm.DNSConfig{ + Nameservers: nil, + Search: search, + }) +} diff --git a/cli/compose/types/types.go b/cli/compose/types/types.go index 9b6ac120b3..f973a2bc1f 100644 --- a/cli/compose/types/types.go +++ b/cli/compose/types/types.go @@ -11,8 +11,6 @@ var UnsupportedProperties = []string{ "cap_drop", "cgroup_parent", "devices", - "dns", - "dns_search", "domainname", "external_links", "ipc",