From 415d92fedeec80165d85d6b9e71f6efa42b51987 Mon Sep 17 00:00:00 2001 From: cuigh Date: Thu, 9 Nov 2017 18:31:20 +0800 Subject: [PATCH] Add ability to set DNS for service --- biz/docker/service.go | 6 ++++++ misc/util.go | 1 + model/docker.go | 29 +++++++++++++++++++++++++++++ views/_modules/service.jet | 21 +++++++++++++++++++++ views/service/detail.jet | 23 +++++++++++++++++++++++ 5 files changed, 80 insertions(+) diff --git a/biz/docker/service.go b/biz/docker/service.go index 308d230..52149a4 100644 --- a/biz/docker/service.go +++ b/biz/docker/service.go @@ -301,6 +301,9 @@ func ServiceUpdate(info *model.ServiceInfo) error { } } + // DNS + spec.TaskTemplate.ContainerSpec.DNSConfig = info.GetDNSConfig() + options := types.ServiceUpdateOptions{ RegistryAuthFrom: types.RegistryAuthFromSpec, QueryRegistry: false, @@ -504,6 +507,9 @@ func ServiceCreate(info *model.ServiceInfo) error { } } + // DNS + service.TaskTemplate.ContainerSpec.DNSConfig = info.GetDNSConfig() + opts := types.ServiceCreateOptions{EncodedRegistryAuth: info.RegistryAuth} var resp types.ServiceCreateResponse resp, err = cli.ServiceCreate(ctx, service, opts) diff --git a/misc/util.go b/misc/util.go index ef7bb87..06d28e4 100644 --- a/misc/util.go +++ b/misc/util.go @@ -15,6 +15,7 @@ var Funcs = map[string]interface{}{ //"time": func(t time.Time) string { // return t.Local().Format("2006-01-02 15:04:05") //}, + "join": strings.Join, "eq": func(v1, v2 interface{}) bool { return fmt.Sprint(v1) == fmt.Sprint(v2) }, diff --git a/model/docker.go b/model/docker.go index b20239d..dd2a859 100644 --- a/model/docker.go +++ b/model/docker.go @@ -213,6 +213,11 @@ type ServiceInfo struct { Limit ResourceInfo `json:"limit"` Reserve ResourceInfo `json:"reserve"` } `json:"resource"` + DNS struct { + Nameservers string `json:"nameservers,omitempty"` + Search string `json:"search,omitempty"` + Options string `json:"options,omitempty"` + } `json:"dns"` } func NewServiceInfo(service swarm.Service) *ServiceInfo { @@ -318,13 +323,37 @@ func NewServiceInfo(service swarm.Service) *ServiceInfo { config := NewConfigInfo(c) si.Configs = append(si.Configs, config) } + if dns := spec.TaskTemplate.ContainerSpec.DNSConfig; dns != nil { + si.DNS.Nameservers = strings.Join(dns.Nameservers, ",") + si.DNS.Search = strings.Join(dns.Search, ",") + si.DNS.Options = strings.Join(dns.Options, ",") + } return si } +// TODO: finish this method func (si *ServiceInfo) ToServiceSpec() swarm.ServiceSpec { return swarm.ServiceSpec{} } +func (si *ServiceInfo) GetDNSConfig() *swarm.DNSConfig { + if si.DNS.Nameservers == "" && si.DNS.Search == "" && si.DNS.Options == "" { + return nil + } + + c := &swarm.DNSConfig{} + if si.DNS.Nameservers != "" { + c.Nameservers = strings.Split(si.DNS.Nameservers, ",") + } + if si.DNS.Search != "" { + c.Search = strings.Split(si.DNS.Search, ",") + } + if si.DNS.Options != "" { + c.Options = strings.Split(si.DNS.Options, ",") + } + return c +} + type EndpointPort struct { Name string `json:"name"` Protocol swarm.PortConfigProtocol `json:"protocol"` diff --git a/views/_modules/service.jet b/views/_modules/service.jet index 5775eb3..f51ab0f 100644 --- a/views/_modules/service.jet +++ b/views/_modules/service.jet @@ -98,6 +98,11 @@ Log driver +
  • + + DNS + +
  • @@ -526,6 +531,22 @@ {{ yield options_table(name="log_driver.option", items=.Service.LogDriver.Options) }}
    +
    +
    + +
    + +
    +
    +
    + + +
    +
    + + +
    +
    {{ end }} diff --git a/views/service/detail.jet b/views/service/detail.jet index 5df79c4..1204f33 100644 --- a/views/service/detail.jet +++ b/views/service/detail.jet @@ -178,6 +178,11 @@ Log driver +
  • + + DNS + +
  • @@ -409,6 +414,24 @@ {{end}}
    +
    + {{ if .Service.Spec.TaskTemplate.ContainerSpec.DNSConfig }} +
    + {{ if .Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Nameservers }} +
    Name servers
    +
    {{ join(.Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Nameservers, ",") }}
    + {{ end }} + {{ if .Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Search }} +
    Search
    +
    {{ join(.Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Search, ",") }}
    + {{ end }} + {{ if .Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Options }} +
    Options
    +
    {{ join(.Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Options, ",") }}
    + {{ end }} +
    + {{ end }} +
    {{if .Tasks}}