1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-10-04 09:23:21 +08:00

Add DNS support for PWD instances (#94)

* Add DNS support for PWD instances

* Store IP address of PWD in all session networks and restore it with the
same IP address

* Remove unnecesary print

* Change url format to pwd<ip>-port for better DNS filtering

* Make PWD listen on 80 and 443 for DNS resolve to work
This commit is contained in:
Jonathan Leibiusky
2017-02-17 11:10:01 -03:00
committed by GitHub
parent 9c4df837a8
commit f816be6f69
9 changed files with 102 additions and 31 deletions

View File

@@ -135,17 +135,33 @@ func CreateNetwork(name string) error {
return nil
}
func ConnectNetwork(containerId, networkId string) error {
err := c.NetworkConnect(context.Background(), networkId, containerId, &network.EndpointSettings{})
func ConnectNetwork(containerId, networkId, ip string) (string, error) {
settings := &network.EndpointSettings{}
if ip != "" {
settings.IPAddress = ip
}
err := c.NetworkConnect(context.Background(), networkId, containerId, settings)
if err != nil && !strings.Contains(err.Error(), "already exists") {
log.Printf("Connection container to network err [%s]\n", err)
return err
return "", err
}
return nil
// Obtain the IP of the PWD container in this network
container, err := c.ContainerInspect(context.Background(), containerId)
if err != nil {
return "", err
}
n, found := container.NetworkSettings.Networks[networkId]
if !found {
return "", fmt.Errorf("Container [%s] connected to the network [%s] but couldn't obtain it's IP address", containerId, networkId)
}
return n.IPAddress, nil
}
func DisconnectNetwork(containerId, networkId string) error {
err := c.NetworkDisconnect(context.Background(), networkId, containerId, true)
@@ -217,7 +233,15 @@ func CreateInstance(session *Session, dindImage string) (*Instance, error) {
break
}
}
conf := &container.Config{Hostname: nodeName, Image: dindImage, Tty: true, OpenStdin: true, AttachStdin: true, AttachStdout: true, AttachStderr: true}
conf := &container.Config{Hostname: nodeName,
Image: dindImage,
Tty: true,
OpenStdin: true,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Env: []string{fmt.Sprintf("PWD_IP_ADDRESS=%s", session.PwdIpAddress)},
}
networkConf := &network.NetworkingConfig{
map[string]*network.EndpointSettings{
session.Id: &network.EndpointSettings{Aliases: []string{nodeName}},

View File

@@ -42,14 +42,15 @@ func init() {
var wsServer *socketio.Server
type Session struct {
rw sync.Mutex
Id string `json:"id"`
Instances map[string]*Instance `json:"instances"`
clients []*Client `json:"-"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
scheduled bool `json:"-"`
ticker *time.Ticker `json:"-"`
rw sync.Mutex
Id string `json:"id"`
Instances map[string]*Instance `json:"instances"`
clients []*Client `json:"-"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
scheduled bool `json:"-"`
ticker *time.Ticker `json:"-"`
PwdIpAddress string `json:"pwd_ip_address"`
}
func (s *Session) Lock() {
@@ -239,10 +240,12 @@ func NewSession(duration time.Duration) (*Session, error) {
log.Printf("Network [%s] created for session [%s]\n", s.Id, s.Id)
// Connect PWD daemon to the new network
if err := ConnectNetwork("pwd", s.Id); err != nil {
ip, err := ConnectNetwork("pwd", s.Id, "")
if err != nil {
log.Println("ERROR NETWORKING")
return nil, err
}
s.PwdIpAddress = ip
log.Printf("Connected pwd to network [%s]\n", s.Id)
// Schedule peridic tasks execution
@@ -315,7 +318,10 @@ func LoadSessionsFromDisk() error {
}
// Connect PWD daemon to the new network
if err := ConnectNetwork("pwd", s.Id); err != nil {
if s.PwdIpAddress == "" {
log.Fatal("Cannot load stored sessions as they don't have the pwd ip address stored with them")
}
if _, err := ConnectNetwork("pwd", s.Id, s.PwdIpAddress); err != nil {
if strings.Contains(err.Error(), "Could not attach to network") {
log.Printf("Network for session [%s] doesn't exist. Removing all instances and session.", s.Id)
CloseSession(s)