Add ability to set DNS for service

This commit is contained in:
cuigh 2017-11-09 18:31:20 +08:00
parent 0ff76725bb
commit 415d92fede
5 changed files with 80 additions and 0 deletions

View File

@ -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)

View File

@ -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)
},

View File

@ -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"`

View File

@ -98,6 +98,11 @@
<span>Log driver</span>
</a>
</li>
<li>
<a>
<span>DNS</span>
</a>
</li>
</ul>
</div>
<div id="tab-content" class="tabs-content has-no-top-border">
@ -526,6 +531,22 @@
{{ yield options_table(name="log_driver.option", items=.Service.LogDriver.Options) }}
</div>
</div>
<div style="display: none">
<div class="field">
<label class="label">Name servers</label>
<div class="control">
<input name="dns.nameservers" class="input" value="{{ .Service.DNS.Nameservers }}" placeholder="e.g. 10.10.10.200,10.10.10.201">
</div>
</div>
<div class="field">
<label class="label">Search</label>
<input name="dns.search" class="input" value="{{ .Service.DNS.Search }}" placeholder="e.g. xxx.com,yyy.net">
</div>
<div class="field">
<label class="label">Options</label>
<input name="dns.options" class="input" value="{{ .Service.DNS.Options }}" placeholder="">
</div>
</div>
</div>
{{ end }}

View File

@ -178,6 +178,11 @@
<span>Log driver</span>
</a>
</li>
<li>
<a>
<span>DNS</span>
</a>
</li>
</ul>
</div>
<div id="tab-content" class="tabs-content has-no-top-border">
@ -409,6 +414,24 @@
</dl>
{{end}}
</div>
<div style="display: none">
{{ if .Service.Spec.TaskTemplate.ContainerSpec.DNSConfig }}
<dl class="is-horizontal is-marginless">
{{ if .Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Nameservers }}
<dt class="has-text-left">Name servers</dt>
<dd>{{ join(.Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Nameservers, ",") }}</dd>
{{ end }}
{{ if .Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Search }}
<dt class="has-text-left">Search</dt>
<dd>{{ join(.Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Search, ",") }}</dd>
{{ end }}
{{ if .Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Options }}
<dt class="has-text-left">Options</dt>
<dd>{{ join(.Service.Spec.TaskTemplate.ContainerSpec.DNSConfig.Options, ",") }}</dd>
{{ end }}
</dl>
{{ end }}
</div>
</div>
{{if .Tasks}}