Skip to content

Commit

Permalink
add dns
Browse files Browse the repository at this point in the history
  • Loading branch information
tonicmuroq committed Sep 27, 2016
1 parent c359410 commit d186738
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
16 changes: 13 additions & 3 deletions cluster/calcium/create_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
}

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

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

func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs, opts *types.DeployOptions, optionMode string) (
func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs, opts *types.DeployOptions, optionMode, nodeIP string) (
*enginecontainer.Config,
*enginecontainer.HostConfig,
*enginenetwork.NetworkingConfig,
Expand Down Expand Up @@ -573,6 +573,15 @@ func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs,
networkMode = c.config.Docker.NetworkMode
}

// dns
// 如果有给dns就优先用给定的dns.
// 没有给出dns的时候, 如果设定是用宿主机IP作为dns, 就会把宿主机IP设置过去.
// 其他情况就是默认值.
dns := specs.DNS
if len(dns) == 0 && c.config.Docker.UseLocalDNS && nodeIP != "" {
dns = []string{nodeIP}
}

config := &enginecontainer.Config{
Env: env,
Cmd: cmd,
Expand Down Expand Up @@ -602,6 +611,7 @@ func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs,

hostConfig := &enginecontainer.HostConfig{
Binds: binds,
DNS: dns,
LogConfig: enginecontainer.LogConfig{Type: logConfig},
NetworkMode: enginecontainer.NetworkMode(networkMode),
RestartPolicy: enginecontainer.RestartPolicy{Name: entry.RestartPolicy, MaximumRetryCount: 3},
Expand Down
1 change: 1 addition & 0 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type DockerConfig struct {
Hub string `yaml:"hub"` // docker hub address
HubPrefix string `yaml:"hub_prefix"` // docker hub prefix, will be set to $Hub/$HubPrefix/$appname
BuildPod string `yaml:"build_pod"` // podname used to build
UseLocalDNS bool `yaml:"local_dns"` // use node IP as dns
}

type SchedConfig struct {
Expand Down
18 changes: 18 additions & 0 deletions types/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"net"
"net/url"
"sync"
"time"

Expand Down Expand Up @@ -54,3 +56,19 @@ func (n *Node) Info() (enginetypes.Info, error) {
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
return n.Engine.Info(ctx)
}

// get IP for node
// will not return error
func (n *Node) GetIP() string {
u, err := url.Parse(n.Endpoint)
if err != nil {
return ""
}

host, _, err := net.SplitHostPort(u.Host)
if err != nil {
return ""
}

return host
}
1 change: 1 addition & 0 deletions types/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Specs struct {
Meta map[string]string `yaml:"meta,omitempty,flow"`
Base string `yaml:"base"`
MountPaths []string `yaml:"mount_paths,omitempty,flow"`
DNS []string `yaml:"dns,omitempty,flow"`
}

// single entrypoint
Expand Down

0 comments on commit d186738

Please sign in to comment.