Пример #1
0
// StartCtr starts the specified Container with options specific to localkube. If etcdDataDir is empty, no directory will be mounted for etcd.
func (d *Controller) StartCtr(ctrId, etcdDataDir string) error {
	binds := []string{
		"/sys:/sys:rw",
		"/var/lib/docker:/var/lib/docker",
		"/mnt/sda1/var/lib/docker:/mnt/sda1/var/lib/docker",
		"/var/lib/kubelet:/var/lib/kubelet",
		"/var/run:/var/run:rw",
		"/:/rootfs:ro",
	}

	// if provided mount etcd data dir
	if len(etcdDataDir) != 0 {
		dataBind := fmt.Sprintf("%s:%s", etcdDataDir, ContainerDataDir)
		binds = append(binds, dataBind)
	}

	hostCfg := &docker.HostConfig{
		Binds:         binds,
		RestartPolicy: docker.AlwaysRestart(),
		NetworkMode:   "host",
		PidMode:       "host",
		Privileged:    true,
	}

	d.log.Println("Starting localkube container...")
	err := d.StartContainer(ctrId, hostCfg)
	if err != nil {
		return fmt.Errorf("could not start container `%s`: %v", ctrId, err)
	}
	return nil
}
Пример #2
0
func (s *S) TestServiceSpecForNodeContainer(c *check.C) {
	c1 := nodecontainer.NodeContainerConfig{
		Name: "swarmbs",
		Config: docker.Config{
			Image: "bsimg",
			Env: []string{
				"A=1",
				"B=2",
			},
			Labels: map[string]string{"label1": "val1"},
		},
		HostConfig: docker.HostConfig{
			RestartPolicy: docker.AlwaysRestart(),
			Privileged:    true,
			Binds:         []string{"/xyz:/abc:ro"},
		},
	}
	err := nodecontainer.AddNewContainer("", &c1)
	c.Assert(err, check.IsNil)
	serviceSpec, err := serviceSpecForNodeContainer("swarmbs", "")
	c.Assert(err, check.IsNil)
	expectedLabels := map[string]string{
		"label1":                   "val1",
		"tsuru.nodecontainer.name": "swarmbs",
		"tsuru.nodecontainer":      "true",
		"tsuru.node.pool":          "",
		"tsuru.node.provisioner":   "swarm",
	}
	expected := &swarm.ServiceSpec{
		Annotations: swarm.Annotations{
			Name:   "node-container-swarmbs-all",
			Labels: expectedLabels,
		},
		Mode: swarm.ServiceMode{Global: &swarm.GlobalService{}},
		TaskTemplate: swarm.TaskSpec{
			ContainerSpec: swarm.ContainerSpec{
				Image:  "bsimg",
				Env:    []string{"A=1", "B=2"},
				Labels: expectedLabels,
				Mounts: []mount.Mount{
					{
						Type:     mount.TypeBind,
						Source:   "/xyz",
						Target:   "/abc",
						ReadOnly: true,
					},
				},
			},
			Placement: &swarm.Placement{Constraints: []string(nil)},
		},
	}
	c.Assert(serviceSpec, check.DeepEquals, expected)
	err = nodecontainer.AddNewContainer("p1", &c1)
	c.Assert(err, check.IsNil)
	err = nodecontainer.AddNewContainer("p2", &c1)
	c.Assert(err, check.IsNil)
	serviceSpec, err = serviceSpecForNodeContainer("swarmbs", "p1")
	c.Assert(err, check.IsNil)
	c.Assert(serviceSpec.TaskTemplate.Placement.Constraints, check.DeepEquals, []string{"node.labels.pool == p1"})
	serviceSpec, err = serviceSpecForNodeContainer("swarmbs", "")
	c.Assert(err, check.IsNil)
	constraints := sort.StringSlice(serviceSpec.TaskTemplate.Placement.Constraints)
	constraints.Sort()
	c.Assert([]string(constraints), check.DeepEquals, []string{"node.labels.pool != p1", "node.labels.pool != p2"})
}