From f40b12d0f73b7ecc670a3266344086dcda71742d Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Wed, 19 Oct 2016 17:07:44 -0700 Subject: [PATCH] Add custom DNS settings to service definition This fix tries to fix the issue raised in 24391 about allowing custom DNS settings to service definition. This fix adds `DNSConfig` (`Nameservers`, `Options`, `Search`) to service definition, as well as `--dns`, `--dns-opt`, and `dns-search` to `service create`. An integration test has been added to cover the changes in this fix. This fix fixes 24391. A PR in swarmkit will be created separately. Signed-off-by: Yong Tang --- command/service/create.go | 3 +++ command/service/opts.go | 36 +++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/command/service/create.go b/command/service/create.go index e2c4c4d116..6aca4635ae 100644 --- a/command/service/create.go +++ b/command/service/create.go @@ -41,6 +41,9 @@ func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command { flags.StringSliceVar(&opts.networks, flagNetwork, []string{}, "Network attachments") flags.VarP(&opts.endpoint.ports, flagPublish, "p", "Publish a port as a node port") flags.StringSliceVar(&opts.groups, flagGroup, []string{}, "Set one or more supplementary user groups for the container") + flags.Var(&opts.dns, flagDNS, "Set custom DNS servers") + flags.Var(&opts.dnsOptions, flagDNSOptions, "Set DNS options") + flags.Var(&opts.dnsSearch, flagDNSSearch, "Set custom DNS search domains") flags.SetInterspersed(false) return cmd diff --git a/command/service/opts.go b/command/service/opts.go index 989fd18b8f..5f1ca86344 100644 --- a/command/service/opts.go +++ b/command/service/opts.go @@ -296,6 +296,9 @@ type serviceOptions struct { groups []string tty bool mounts opts.MountOpt + dns opts.ListOpts + dnsSearch opts.ListOpts + dnsOptions opts.ListOpts resources resourceOptions stopGrace DurationOpt @@ -325,7 +328,10 @@ func newServiceOptions() *serviceOptions { endpoint: endpointOptions{ ports: opts.NewListOpts(ValidatePort), }, - logDriver: newLogDriverOptions(), + logDriver: newLogDriverOptions(), + dns: opts.NewListOpts(opts.ValidateIPAddress), + dnsOptions: opts.NewListOpts(nil), + dnsSearch: opts.NewListOpts(opts.ValidateDNSSearch), } } @@ -358,16 +364,21 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) { }, TaskTemplate: swarm.TaskSpec{ ContainerSpec: swarm.ContainerSpec{ - Image: opts.image, - Args: opts.args, - Env: currentEnv, - Hostname: opts.hostname, - Labels: runconfigopts.ConvertKVStringsToMap(opts.containerLabels.GetAll()), - Dir: opts.workdir, - User: opts.user, - Groups: opts.groups, - TTY: opts.tty, - Mounts: opts.mounts.Value(), + Image: opts.image, + Args: opts.args, + Env: currentEnv, + Hostname: opts.hostname, + Labels: runconfigopts.ConvertKVStringsToMap(opts.containerLabels.GetAll()), + Dir: opts.workdir, + User: opts.user, + Groups: opts.groups, + TTY: opts.tty, + Mounts: opts.mounts.Value(), + DNSConfig: &swarm.DNSConfig{ + Nameservers: opts.dns.GetAll(), + Search: opts.dnsSearch.GetAll(), + Options: opts.dnsOptions.GetAll(), + }, StopGracePeriod: opts.stopGrace.Value(), }, Networks: convertNetworks(opts.networks), @@ -463,6 +474,9 @@ const ( flagContainerLabel = "container-label" flagContainerLabelRemove = "container-label-rm" flagContainerLabelAdd = "container-label-add" + flagDNS = "dns" + flagDNSOptions = "dns-opt" + flagDNSSearch = "dns-search" flagEndpointMode = "endpoint-mode" flagHostname = "hostname" flagEnv = "env"