Allow custom hostnames when creating instances
This commit is contained in:
@@ -60,6 +60,7 @@ type Instance struct {
|
|||||||
type InstanceConfig struct {
|
type InstanceConfig struct {
|
||||||
ImageName string
|
ImageName string
|
||||||
Alias string
|
Alias string
|
||||||
|
Hostname string
|
||||||
ServerCert []byte
|
ServerCert []byte
|
||||||
ServerKey []byte
|
ServerKey []byte
|
||||||
CACert []byte
|
CACert []byte
|
||||||
@@ -178,35 +179,43 @@ func (p *pwd) InstanceDelete(session *Session, instance *Instance) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *pwd) checkHostnameExists(session *Session, hostname string) bool {
|
||||||
|
containerName := fmt.Sprintf("%s_%s", session.Id[:8], hostname)
|
||||||
|
exists := false
|
||||||
|
for _, instance := range session.Instances {
|
||||||
|
if instance.Name == containerName {
|
||||||
|
exists = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return exists
|
||||||
|
}
|
||||||
|
|
||||||
func (p *pwd) InstanceNew(session *Session, conf InstanceConfig) (*Instance, error) {
|
func (p *pwd) InstanceNew(session *Session, conf InstanceConfig) (*Instance, error) {
|
||||||
if conf.ImageName == "" {
|
if conf.ImageName == "" {
|
||||||
conf.ImageName = config.GetDindImageName()
|
conf.ImageName = config.GetDindImageName()
|
||||||
}
|
}
|
||||||
log.Printf("NewInstance - using image: [%s]\n", conf.ImageName)
|
log.Printf("NewInstance - using image: [%s]\n", conf.ImageName)
|
||||||
|
|
||||||
var nodeName string
|
if conf.Hostname == "" {
|
||||||
var containerName string
|
var nodeName string
|
||||||
for i := 1; ; i++ {
|
for i := 1; ; i++ {
|
||||||
nodeName = fmt.Sprintf("node%d", i)
|
nodeName = fmt.Sprintf("node%d", i)
|
||||||
containerName = fmt.Sprintf("%s_%s", session.Id[:8], nodeName)
|
exists := p.checkHostnameExists(session, nodeName)
|
||||||
exists := false
|
if !exists {
|
||||||
for _, instance := range session.Instances {
|
|
||||||
if instance.Name == containerName {
|
|
||||||
exists = true
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !exists {
|
conf.Hostname = nodeName
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
containerName := fmt.Sprintf("%s_%s", session.Id[:8], conf.Hostname)
|
||||||
|
|
||||||
opts := docker.CreateContainerOpts{
|
opts := docker.CreateContainerOpts{
|
||||||
Image: conf.ImageName,
|
Image: conf.ImageName,
|
||||||
SessionId: session.Id,
|
SessionId: session.Id,
|
||||||
PwdIpAddress: session.PwdIpAddress,
|
PwdIpAddress: session.PwdIpAddress,
|
||||||
ContainerName: containerName,
|
ContainerName: containerName,
|
||||||
Hostname: nodeName,
|
Hostname: conf.Hostname,
|
||||||
ServerCert: conf.ServerCert,
|
ServerCert: conf.ServerCert,
|
||||||
ServerKey: conf.ServerKey,
|
ServerKey: conf.ServerKey,
|
||||||
CACert: conf.CACert,
|
CACert: conf.CACert,
|
||||||
@@ -229,7 +238,7 @@ func (p *pwd) InstanceNew(session *Session, conf InstanceConfig) (*Instance, err
|
|||||||
instance.Image = opts.Image
|
instance.Image = opts.Image
|
||||||
instance.IP = ip
|
instance.IP = ip
|
||||||
instance.Name = containerName
|
instance.Name = containerName
|
||||||
instance.Hostname = nodeName
|
instance.Hostname = conf.Hostname
|
||||||
instance.Alias = conf.Alias
|
instance.Alias = conf.Alias
|
||||||
instance.Cert = conf.Cert
|
instance.Cert = conf.Cert
|
||||||
instance.Key = conf.Key
|
instance.Key = conf.Key
|
||||||
|
|||||||
@@ -134,6 +134,54 @@ func TestInstanceNew_WithNotAllowedImage(t *testing.T) {
|
|||||||
assert.Equal(t, expectedContainerOpts, containerOpts)
|
assert.Equal(t, expectedContainerOpts, containerOpts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInstanceNew_WithCustomHostname(t *testing.T) {
|
||||||
|
containerOpts := docker.CreateContainerOpts{}
|
||||||
|
dock := &mockDocker{}
|
||||||
|
dock.createContainer = func(opts docker.CreateContainerOpts) (string, error) {
|
||||||
|
containerOpts = opts
|
||||||
|
return "10.0.0.1", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks := &mockTasks{}
|
||||||
|
broadcast := &mockBroadcast{}
|
||||||
|
storage := &mockStorage{}
|
||||||
|
|
||||||
|
p := NewPWD(dock, tasks, broadcast, storage)
|
||||||
|
|
||||||
|
session, err := p.SessionNew(time.Hour, "", "")
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
instance, err := p.InstanceNew(session, InstanceConfig{ImageName: "redis", Hostname: "redis-master"})
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
expectedInstance := Instance{
|
||||||
|
Name: fmt.Sprintf("%s_redis-master", session.Id[:8]),
|
||||||
|
Hostname: "redis-master",
|
||||||
|
IP: "10.0.0.1",
|
||||||
|
Alias: "",
|
||||||
|
Image: "redis",
|
||||||
|
IsDockerHost: false,
|
||||||
|
session: session,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, expectedInstance, *instance)
|
||||||
|
|
||||||
|
expectedContainerOpts := docker.CreateContainerOpts{
|
||||||
|
Image: expectedInstance.Image,
|
||||||
|
SessionId: session.Id,
|
||||||
|
PwdIpAddress: session.PwdIpAddress,
|
||||||
|
ContainerName: expectedInstance.Name,
|
||||||
|
Hostname: expectedInstance.Hostname,
|
||||||
|
ServerCert: nil,
|
||||||
|
ServerKey: nil,
|
||||||
|
CACert: nil,
|
||||||
|
Privileged: false,
|
||||||
|
}
|
||||||
|
assert.Equal(t, expectedContainerOpts, containerOpts)
|
||||||
|
}
|
||||||
|
|
||||||
func TestInstanceAllowedImages(t *testing.T) {
|
func TestInstanceAllowedImages(t *testing.T) {
|
||||||
dock := &mockDocker{}
|
dock := &mockDocker{}
|
||||||
tasks := &mockTasks{}
|
tasks := &mockTasks{}
|
||||||
|
|||||||
Reference in New Issue
Block a user