本文整理匯總了Golang中github.com/docker/swarm/cluster.BuildContainerConfig函數的典型用法代碼示例。如果您正苦於以下問題:Golang BuildContainerConfig函數的具體用法?Golang BuildContainerConfig怎麽用?Golang BuildContainerConfig使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了BuildContainerConfig函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestFilterSoftConstraint
func TestFilterSoftConstraint(t *testing.T) {
var (
f = ConstraintFilter{}
nodes = testFixtures()
result []*node.Node
err error
)
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:node==~node-1-name"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[1])
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{`constraint:name!=~/(?i)abc*/`}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 4)
// Check not with globber pattern
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:region!=~us*"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 2)
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:region!=~can*"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 4)
// Check matching
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:region==~us~"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.Error(t, err)
assert.Len(t, result, 0)
}
示例2: TestConstraintRegExp
func TestConstraintRegExp(t *testing.T) {
var (
f = ConstraintFilter{}
nodes = testFixtures()
result []*node.Node
err error
)
// Check with regular expression /node\d/ matches node{0..2}
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{`constraint:name==/node\d/`}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 3)
// Check with regular expression /node\d/ matches node{0..2}
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{`constraint:name==/node[12]/`}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 2)
// Check with regular expression ! and regexp /node[12]/ matches node[0] and node[3]
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{`constraint:name!=/node[12]/`}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 2)
// Validate node pinning by ! and regexp.
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:node!=/node-[01]-id/"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 2)
}
示例3: TestFilterRegExpCaseInsensitive
func TestFilterRegExpCaseInsensitive(t *testing.T) {
var (
f = ConstraintFilter{}
nodes = testFixtures()
result []*node.Node
err error
)
// Prepare node with a strange name
nodes[3].Labels = map[string]string{
"name": "aBcDeF",
"group": "2",
"region": "eu",
}
// Case-sensitive, so not match
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{`constraint:name==/abcdef/`}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.Error(t, err)
assert.Len(t, result, 0)
// Match with case-insensitive
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{`constraint:name==/(?i)abcdef/`}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[3])
assert.Equal(t, result[0].Labels["name"], "aBcDeF")
// Test ! filter combined with case insensitive
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{`constraint:name!=/(?i)abc*/`}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 3)
}
示例4: TestConstraintNotExpr
func TestConstraintNotExpr(t *testing.T) {
var (
f = ConstraintFilter{}
nodes = testFixtures()
result []*node.Node
err error
)
// Check not (!) expression
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:name!=node0"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 3)
// Check not does_not_exist. All should be found
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:name!=does_not_exist"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 4)
// Check name must not start with n
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:name!=n*"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
// Check not with globber pattern
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:region!=us*"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 2)
}
示例5: TestAffinityFilterLabels
func TestAffinityFilterLabels(t *testing.T) {
var (
f = AffinityFilter{}
nodes = []*node.Node{
{
ID: "node-0-id",
Name: "node-0-name",
Addr: "node-0",
Containers: []*cluster.Container{
{Container: dockerclient.Container{
Id: "container-n0-id",
Names: []string{"/container-n0-name"},
}},
},
Images: []*cluster.Image{{Image: types.Image{
ID: "image-0-id",
RepoTags: []string{"image-0:tag0"},
}}},
},
{
ID: "node-1-id",
Name: "node-1-name",
Addr: "node-1",
Containers: []*cluster.Container{
{Container: dockerclient.Container{
Id: "container-n1-id",
Names: []string{"/container-n1-name"},
}},
},
Images: []*cluster.Image{{Image: types.Image{
ID: "image-1-id",
RepoTags: []string{"image-1:tag1"},
}}},
},
}
result []*node.Node
err error
)
result, err = f.Filter(cluster.BuildContainerConfig(dockerclient.ContainerConfig{Env: []string{"affinity:image==image-1"}}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[1])
result, err = f.Filter(cluster.BuildContainerConfig(dockerclient.ContainerConfig{Env: []string{"affinity:image!=image-1"}}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[0])
result, err = f.Filter(cluster.BuildContainerConfig(dockerclient.ContainerConfig{Labels: map[string]string{"com.docker.swarm.affinities": "[\"image==image-1\"]"}}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[1])
result, err = f.Filter(cluster.BuildContainerConfig(dockerclient.ContainerConfig{Labels: map[string]string{"com.docker.swarm.affinities": "[\"image!=image-1\"]"}}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[0])
}
示例6: TestContainerLookup
func TestContainerLookup(t *testing.T) {
c := &Cluster{
engines: make(map[string]*cluster.Engine),
}
container1 := &cluster.Container{
Container: types.Container{
ID: "container1-id",
Names: []string{"/container1-name1", "/container1-name2"},
},
Config: cluster.BuildContainerConfig(containertypes.Config{
Labels: map[string]string{
"com.docker.swarm.id": "swarm1-id",
},
}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}),
}
container2 := &cluster.Container{
Container: types.Container{
ID: "container2-id",
Names: []string{"/con"},
},
Config: cluster.BuildContainerConfig(containertypes.Config{
Labels: map[string]string{
"com.docker.swarm.id": "swarm2-id",
},
}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}),
}
n := createEngine(t, "test-engine", container1, container2)
c.engines[n.ID] = n
assert.Equal(t, len(c.Containers()), 2)
// Invalid lookup
assert.Nil(t, c.Container("invalid-id"))
assert.Nil(t, c.Container(""))
// Container ID lookup.
assert.NotNil(t, c.Container("container1-id"))
// Container ID prefix lookup.
assert.NotNil(t, c.Container("container1-"))
assert.Nil(t, c.Container("container"))
// Container name lookup.
assert.NotNil(t, c.Container("container1-name1"))
assert.NotNil(t, c.Container("container1-name2"))
// Container engine/name matching.
assert.NotNil(t, c.Container("test-engine/container1-name1"))
assert.NotNil(t, c.Container("test-engine/container1-name2"))
// Swarm ID lookup.
assert.NotNil(t, c.Container("swarm1-id"))
// Swarm ID prefix lookup.
assert.NotNil(t, c.Container("swarm1-"))
assert.Nil(t, c.Container("swarm"))
// Match name before ID prefix
cc := c.Container("con")
assert.NotNil(t, cc)
assert.Equal(t, cc.ID, "container2-id")
}
示例7: TestConstrainteFilter
func TestConstrainteFilter(t *testing.T) {
var (
f = ConstraintFilter{}
nodes = testFixtures()
result []*node.Node
err error
)
// Without constraints we should get the unfiltered list of nodes back.
result, err = f.Filter(&cluster.ContainerConfig{}, nodes, true)
assert.NoError(t, err)
assert.Equal(t, result, nodes)
// Set a constraint that cannot be fulfilled and expect an error back.
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:does_not_exist==true"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.Error(t, err)
// Set a contraint that can only be filled by a single node.
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:name==node1"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[1])
// This constraint can only be fulfilled by a subset of nodes.
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:group==1"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 2)
assert.NotContains(t, result, nodes[2])
// Validate node pinning by id.
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:node==node-2-id"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[2])
// Validate node pinning by name.
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:node==node-1-name"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[1])
// Make sure constraints are evaluated as logical ANDs.
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:name==node0", "constraint:group==1"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[0])
// Check matching
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:region==us"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.Error(t, err)
assert.Len(t, result, 0)
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:region==us*"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 2)
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:region==*us*"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.NoError(t, err)
assert.Len(t, result, 2)
}
示例8: TestContainerLookup
func TestContainerLookup(t *testing.T) {
c := &Cluster{
slaves: make(map[string]*slave),
}
container1 := &cluster.Container{
Container: dockerclient.Container{
Id: "container1-id",
Names: []string{"/container1-name1", "/container1-name2"},
},
Config: cluster.BuildContainerConfig(dockerclient.ContainerConfig{
Labels: map[string]string{
"com.docker.swarm.id": "swarm1-id",
},
}),
}
container2 := &cluster.Container{
Container: dockerclient.Container{
Id: "container2-id",
Names: []string{"/con"},
},
Config: cluster.BuildContainerConfig(dockerclient.ContainerConfig{
Labels: map[string]string{
"com.docker.swarm.id": "swarm2-id",
},
}),
}
s := createSlave(t, "test-engine", container1, container2)
c.slaves[s.id] = s
// Invalid lookup
assert.Nil(t, c.Container("invalid-id"))
assert.Nil(t, c.Container(""))
// Container ID lookup.
assert.NotNil(t, c.Container("container1-id"))
// Container ID prefix lookup.
assert.NotNil(t, c.Container("container1-"))
assert.Nil(t, c.Container("container"))
// Container name lookup.
assert.NotNil(t, c.Container("container1-name1"))
assert.NotNil(t, c.Container("container1-name2"))
// Container engine/name matching.
assert.NotNil(t, c.Container("test-engine/container1-name1"))
assert.NotNil(t, c.Container("test-engine/container1-name2"))
// Swarm ID lookup.
assert.NotNil(t, c.Container("swarm1-id"))
// Swarm ID prefix lookup.
assert.NotNil(t, c.Container("swarm1-"))
assert.Nil(t, c.Container("swarm"))
// Match name before ID prefix
cc := c.Container("con")
assert.NotNil(t, cc)
assert.Equal(t, cc.Id, "container2-id")
}
示例9: TestAdd
func TestAdd(t *testing.T) {
q := NewTasks(&testLauncher{count: 1})
task1, _ := NewTask(cluster.BuildContainerConfig(containerConfig, hostConfig, networkingConfig), "name1", 5*time.Second)
task2, _ := NewTask(cluster.BuildContainerConfig(containerConfig, hostConfig, networkingConfig), "name2", 5*time.Second)
q.Add(task1)
assert.Equal(t, len(q.Tasks), 0)
q.Add(task2)
assert.Equal(t, len(q.Tasks), 1)
}
示例10: TestBuild
func TestBuild(t *testing.T) {
task, err := NewTask(cluster.BuildContainerConfig(containerConfig, hostConfig, networkingConfig), name, 5*time.Second)
assert.NoError(t, err)
task.Build("slave-id", nil)
assert.Equal(t, task.Container.GetType(), mesosproto.ContainerInfo_DOCKER)
assert.Equal(t, task.Container.Docker.GetImage(), "test-image")
assert.Equal(t, task.Container.Docker.GetNetwork(), mesosproto.ContainerInfo_DockerInfo_BRIDGE)
assert.Equal(t, len(task.Resources), 2)
assert.Equal(t, task.Resources[0], mesosutil.NewScalarResource("cpus", 42.0))
assert.Equal(t, task.Resources[1], mesosutil.NewScalarResource("mem", 2))
assert.Equal(t, task.Command.GetValue(), "ls")
assert.Equal(t, task.Command.GetArguments(), []string{"foo", "bar"})
parameters := []string{task.Container.Docker.GetParameters()[0].GetValue(), task.Container.Docker.GetParameters()[1].GetValue()}
sort.Strings(parameters)
assert.Equal(t, len(parameters), 2)
assert.Equal(t, parameters[0], "com.docker.swarm.mesos.name="+name)
assert.Equal(t, parameters[1], "com.docker.swarm.mesos.task="+*task.TaskId.Value)
assert.Equal(t, task.SlaveId.GetValue(), "slave-id")
}
示例11: TestUnsupportedOperators
func TestUnsupportedOperators(t *testing.T) {
var (
f = ConstraintFilter{}
nodes = testFixtures()
result []*node.Node
err error
)
result, err = f.Filter(cluster.BuildContainerConfig(dockerclient.ContainerConfig{Env: []string{"constraint:name=node0"}}), nodes)
assert.Error(t, err)
assert.Len(t, result, 0)
result, err = f.Filter(cluster.BuildContainerConfig(dockerclient.ContainerConfig{Env: []string{"constraint:name=!node0"}}), nodes)
assert.Error(t, err)
assert.Len(t, result, 0)
}
示例12: TestUnsupportedOperators
func TestUnsupportedOperators(t *testing.T) {
var (
f = ConstraintFilter{}
nodes = testFixtures()
result []*node.Node
err error
)
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:name=node0"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.Error(t, err)
assert.Len(t, result, 0)
result, err = f.Filter(cluster.BuildContainerConfig(containertypes.Config{Env: []string{"constraint:name=!node0"}}, containertypes.HostConfig{}, networktypes.NetworkingConfig{}), nodes, true)
assert.Error(t, err)
assert.Len(t, result, 0)
}
示例13: CreateNetwork
// CreateNetwork creates a network in the cluster
func (c *Cluster) CreateNetwork(request *dockerclient.NetworkCreate) (response *dockerclient.NetworkCreateResponse, err error) {
var (
parts = strings.SplitN(request.Name, "/", 2)
config = &cluster.ContainerConfig{}
)
if len(parts) == 2 {
// a node was specified, create the container only on this node
request.Name = parts[1]
config = cluster.BuildContainerConfig(dockerclient.ContainerConfig{Env: []string{"constraint:node==" + parts[0]}})
}
nodes, err := c.scheduler.SelectNodesForContainer(c.listNodes(), config)
if err != nil {
return nil, err
}
if nodes != nil {
resp, err := c.engines[nodes[0].ID].CreateNetwork(request)
if err == nil {
if network := c.engines[nodes[0].ID].Networks().Get(resp.ID); network != nil && network.Scope == "global" {
for id, engine := range c.engines {
if id != nodes[0].ID {
engine.AddNetwork(network)
}
}
}
}
return resp, err
}
return nil, nil
}
示例14: BuildImage
// BuildImage build an image
func (c *Cluster) BuildImage(buildImage *dockerclient.BuildImage, out io.Writer) error {
c.scheduler.Lock()
// get an engine
config := cluster.BuildContainerConfig(dockerclient.ContainerConfig{
CpuShares: buildImage.CpuShares,
Memory: buildImage.Memory,
Env: convertMapToKVStrings(buildImage.BuildArgs),
})
buildImage.BuildArgs = convertKVStringsToMap(config.Env)
nodes, err := c.scheduler.SelectNodesForContainer(c.listNodes(), config)
c.scheduler.Unlock()
if err != nil {
return err
}
n := nodes[0]
reader, err := c.engines[n.ID].BuildImage(buildImage)
if err != nil {
return err
}
if _, err := io.Copy(out, reader); err != nil {
return err
}
c.engines[n.ID].RefreshImages()
return nil
}
示例15: CreateNetwork
// CreateNetwork creates a network in the cluster
func (c *Cluster) CreateNetwork(request *dockerclient.NetworkCreate) (*dockerclient.NetworkCreateResponse, error) {
var (
parts = strings.SplitN(request.Name, "/", 2)
config = &cluster.ContainerConfig{}
)
if len(parts) == 2 {
// a node was specified, create the container only on this node
request.Name = parts[1]
config = cluster.BuildContainerConfig(dockerclient.ContainerConfig{Env: []string{"constraint:node==" + parts[0]}})
}
c.scheduler.Lock()
nodes, err := c.scheduler.SelectNodesForContainer(c.listNodes(), config)
c.scheduler.Unlock()
if err != nil {
return nil, err
}
if nodes == nil {
return nil, errors.New("cannot find node to create network")
}
n := nodes[0]
s, ok := c.agents[n.ID]
if !ok {
return nil, fmt.Errorf("Unable to create network on agent %q", n.ID)
}
resp, err := s.engine.CreateNetwork(request)
c.refreshNetworks()
return resp, err
}