2018-02-05 16:05:59 -05:00
|
|
|
package links // import "github.com/docker/docker/daemon/links"
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2015-04-04 00:06:48 -04:00
|
|
|
|
2015-12-18 12:58:48 -05:00
|
|
|
"github.com/docker/go-connections/nat"
|
2013-10-04 22:25:15 -04:00
|
|
|
)
|
|
|
|
|
2018-09-07 02:09:35 -04:00
|
|
|
// Link struct holds information about parent/child linked container
|
2013-10-04 22:25:15 -04:00
|
|
|
type Link struct {
|
2015-07-20 18:19:05 -04:00
|
|
|
// Parent container IP address
|
|
|
|
ParentIP string
|
|
|
|
// Child container IP address
|
|
|
|
ChildIP string
|
|
|
|
// Link name
|
|
|
|
Name string
|
|
|
|
// Child environments variables
|
2013-10-04 22:25:15 -04:00
|
|
|
ChildEnvironment []string
|
2015-07-20 18:19:05 -04:00
|
|
|
// Child exposed ports
|
|
|
|
Ports []nat.Port
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2015-07-20 18:19:05 -04:00
|
|
|
// NewLink initializes a new Link struct with the provided options.
|
|
|
|
func NewLink(parentIP, childIP, name string, env []string, exposedPorts map[nat.Port]struct{}) *Link {
|
2014-02-14 21:07:33 -05:00
|
|
|
var (
|
|
|
|
i int
|
|
|
|
ports = make([]nat.Port, len(exposedPorts))
|
|
|
|
)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2014-02-14 21:07:33 -05:00
|
|
|
for p := range exposedPorts {
|
2013-10-04 22:25:15 -04:00
|
|
|
ports[i] = p
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:19:05 -04:00
|
|
|
return &Link{
|
2013-10-04 22:25:15 -04:00
|
|
|
Name: name,
|
2014-02-14 21:07:33 -05:00
|
|
|
ChildIP: childIP,
|
|
|
|
ParentIP: parentIP,
|
|
|
|
ChildEnvironment: env,
|
2013-10-04 22:25:15 -04:00
|
|
|
Ports: ports,
|
|
|
|
}
|
2014-09-16 21:08:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-07 02:09:35 -04:00
|
|
|
// ToEnv creates a string's slice containing child container information in
|
2015-07-20 18:19:05 -04:00
|
|
|
// the form of environment variables which will be later exported on container
|
|
|
|
// startup.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (l *Link) ToEnv() []string {
|
|
|
|
env := []string{}
|
2015-07-20 18:19:05 -04:00
|
|
|
|
|
|
|
_, n := path.Split(l.Name)
|
|
|
|
alias := strings.Replace(strings.ToUpper(n), "-", "_", -1)
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
if p := l.getDefaultPort(); p != nil {
|
2013-10-28 19:58:59 -04:00
|
|
|
env = append(env, fmt.Sprintf("%s_PORT=%s://%s:%s", alias, p.Proto(), l.ChildIP, p.Port()))
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2014-09-16 21:08:30 -04:00
|
|
|
//sort the ports so that we can bulk the continuous ports together
|
|
|
|
nat.Sort(l.Ports, func(ip, jp nat.Port) bool {
|
|
|
|
// If the two ports have the same number, tcp takes priority
|
|
|
|
// Sort in desc order
|
|
|
|
return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && strings.ToLower(ip.Proto()) == "tcp")
|
|
|
|
})
|
|
|
|
|
|
|
|
for i := 0; i < len(l.Ports); {
|
|
|
|
p := l.Ports[i]
|
|
|
|
j := nextContiguous(l.Ports, p.Int(), i)
|
|
|
|
if j > i+1 {
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_START=%s://%s:%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Proto(), l.ChildIP, p.Port()))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_ADDR=%s", alias, p.Port(), strings.ToUpper(p.Proto()), l.ChildIP))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_PROTO=%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Proto()))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_PORT_START=%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Port()))
|
|
|
|
|
|
|
|
q := l.Ports[j]
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_END=%s://%s:%s", alias, p.Port(), strings.ToUpper(q.Proto()), q.Proto(), l.ChildIP, q.Port()))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_PORT_END=%s", alias, p.Port(), strings.ToUpper(q.Proto()), q.Port()))
|
|
|
|
|
|
|
|
i = j + 1
|
|
|
|
continue
|
2015-01-06 14:46:03 -05:00
|
|
|
} else {
|
|
|
|
i++
|
2014-09-16 21:08:30 -04:00
|
|
|
}
|
2015-01-06 14:46:03 -05:00
|
|
|
}
|
|
|
|
for _, p := range l.Ports {
|
2013-10-04 22:25:15 -04:00
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s=%s://%s:%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Proto(), l.ChildIP, p.Port()))
|
2013-10-29 02:02:28 -04:00
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_ADDR=%s", alias, p.Port(), strings.ToUpper(p.Proto()), l.ChildIP))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_PORT=%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Port()))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_PROTO=%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Proto()))
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the linked container's name into the environment
|
|
|
|
env = append(env, fmt.Sprintf("%s_NAME=%s", alias, l.Name))
|
|
|
|
|
|
|
|
if l.ChildEnvironment != nil {
|
|
|
|
for _, v := range l.ChildEnvironment {
|
2015-04-25 07:42:43 -04:00
|
|
|
parts := strings.SplitN(v, "=", 2)
|
|
|
|
if len(parts) < 2 {
|
2013-10-04 22:25:15 -04:00
|
|
|
continue
|
|
|
|
}
|
2014-02-22 22:01:45 -05:00
|
|
|
// Ignore a few variables that are added during docker build (and not really relevant to linked containers)
|
2013-10-04 22:25:15 -04:00
|
|
|
if parts[0] == "HOME" || parts[0] == "PATH" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
env = append(env, fmt.Sprintf("%s_ENV_%s=%s", alias, parts[0], parts[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:19:05 -04:00
|
|
|
func nextContiguous(ports []nat.Port, value int, index int) int {
|
|
|
|
if index+1 == len(ports) {
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
for i := index + 1; i < len(ports); i++ {
|
|
|
|
if ports[i].Int() > value+1 {
|
|
|
|
return i - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
value++
|
|
|
|
}
|
|
|
|
return len(ports) - 1
|
|
|
|
}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
// Default port rules
|
2014-02-11 19:48:44 -05:00
|
|
|
func (l *Link) getDefaultPort() *nat.Port {
|
|
|
|
var p nat.Port
|
2013-10-04 22:25:15 -04:00
|
|
|
i := len(l.Ports)
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
return nil
|
|
|
|
} else if i > 1 {
|
2014-02-11 19:48:44 -05:00
|
|
|
nat.Sort(l.Ports, func(ip, jp nat.Port) bool {
|
2013-10-04 22:25:15 -04:00
|
|
|
// If the two ports have the same number, tcp takes priority
|
|
|
|
// Sort in desc order
|
|
|
|
return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && strings.ToLower(ip.Proto()) == "tcp")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
p = l.Ports[0]
|
|
|
|
return &p
|
|
|
|
}
|