Skip to content

Commit d186738

Browse files
committed
add dns
1 parent c359410 commit d186738

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

cluster/calcium/create_container.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
7878
}
7979

8080
for i := 0; i < connum; i++ {
81-
config, hostConfig, networkConfig, containerName, err := c.makeContainerOptions(nil, specs, opts, "cpuperiod")
81+
config, hostConfig, networkConfig, containerName, err := c.makeContainerOptions(nil, specs, opts, "cpuperiod", node.GetIP())
8282
if err != nil {
8383
log.Errorf("error when creating CreateContainerOptions, %v", err)
8484
ms[i].Error = err.Error()
@@ -334,7 +334,7 @@ func (c *calcium) doCreateContainerWithScheduler(nodename string, cpumap []types
334334

335335
for i, quota := range cpumap {
336336
// create options
337-
config, hostConfig, networkConfig, containerName, err := c.makeContainerOptions(quota, specs, opts, "scheduler")
337+
config, hostConfig, networkConfig, containerName, err := c.makeContainerOptions(quota, specs, opts, "scheduler", node.GetIP())
338338
if err != nil {
339339
log.Errorf("error when creating CreateContainerOptions, %v", err)
340340
ms[i].Error = err.Error()
@@ -435,7 +435,7 @@ func (c *calcium) releaseQuota(node *types.Node, quota types.CPUMap) {
435435
c.store.UpdateNodeCPU(node.Podname, node.Name, quota, "+")
436436
}
437437

438-
func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs, opts *types.DeployOptions, optionMode string) (
438+
func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs, opts *types.DeployOptions, optionMode, nodeIP string) (
439439
*enginecontainer.Config,
440440
*enginecontainer.HostConfig,
441441
*enginenetwork.NetworkingConfig,
@@ -573,6 +573,15 @@ func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs,
573573
networkMode = c.config.Docker.NetworkMode
574574
}
575575

576+
// dns
577+
// 如果有给dns就优先用给定的dns.
578+
// 没有给出dns的时候, 如果设定是用宿主机IP作为dns, 就会把宿主机IP设置过去.
579+
// 其他情况就是默认值.
580+
dns := specs.DNS
581+
if len(dns) == 0 && c.config.Docker.UseLocalDNS && nodeIP != "" {
582+
dns = []string{nodeIP}
583+
}
584+
576585
config := &enginecontainer.Config{
577586
Env: env,
578587
Cmd: cmd,
@@ -602,6 +611,7 @@ func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs,
602611

603612
hostConfig := &enginecontainer.HostConfig{
604613
Binds: binds,
614+
DNS: dns,
605615
LogConfig: enginecontainer.LogConfig{Type: logConfig},
606616
NetworkMode: enginecontainer.NetworkMode(networkMode),
607617
RestartPolicy: enginecontainer.RestartPolicy{Name: entry.RestartPolicy, MaximumRetryCount: 3},

types/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type DockerConfig struct {
2727
Hub string `yaml:"hub"` // docker hub address
2828
HubPrefix string `yaml:"hub_prefix"` // docker hub prefix, will be set to $Hub/$HubPrefix/$appname
2929
BuildPod string `yaml:"build_pod"` // podname used to build
30+
UseLocalDNS bool `yaml:"local_dns"` // use node IP as dns
3031
}
3132

3233
type SchedConfig struct {

types/node.go

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package types
22

33
import (
4+
"net"
5+
"net/url"
46
"sync"
57
"time"
68

@@ -54,3 +56,19 @@ func (n *Node) Info() (enginetypes.Info, error) {
5456
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
5557
return n.Engine.Info(ctx)
5658
}
59+
60+
// get IP for node
61+
// will not return error
62+
func (n *Node) GetIP() string {
63+
u, err := url.Parse(n.Endpoint)
64+
if err != nil {
65+
return ""
66+
}
67+
68+
host, _, err := net.SplitHostPort(u.Host)
69+
if err != nil {
70+
return ""
71+
}
72+
73+
return host
74+
}

types/specs.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Specs struct {
1717
Meta map[string]string `yaml:"meta,omitempty,flow"`
1818
Base string `yaml:"base"`
1919
MountPaths []string `yaml:"mount_paths,omitempty,flow"`
20+
DNS []string `yaml:"dns,omitempty,flow"`
2021
}
2122

2223
// single entrypoint

0 commit comments

Comments
 (0)