Exemple #1
0
func (daemon *Daemon) CreatePod(podId string, podSpec *apitypes.UserPod) (*pod.XPod, error) {
	//FIXME: why restrict to 1024
	if daemon.PodList.CountRunning() >= 1024 {
		return nil, fmt.Errorf("There have already been %d running Pods", 1024)
	}
	if podId == "" {
		podId = fmt.Sprintf("pod-%s", utils.RandStr(10, "alpha"))
	}

	if podSpec.Id == "" {
		podSpec.Id = podId
	}

	if _, ok := daemon.PodList.Get(podSpec.Id); ok {
		return nil, fmt.Errorf("pod %s already exist", podSpec.Id)
	}

	if err := podSpec.Validate(); err != nil {
		return nil, err
	}

	factory := pod.NewPodFactory(daemon.Factory, daemon.PodList, daemon.db, daemon.Storage, daemon.Daemon, daemon.DefaultLog)

	p, err := pod.CreateXPod(factory, podSpec)
	if err != nil {
		glog.Errorf("%s: failed to add pod: %v", podSpec.Id, err)
		return nil, err
	}

	return p, nil
}
Exemple #2
0
func (daemon *Daemon) Restore() error {
	if daemon.GetPodNum() == 0 {
		return nil
	}

	ch := pod.LoadAllPods(daemon.db)
	if ch == nil {
		estr := "Cannot list pods in leveldb"
		glog.Error(estr)
		return errors.New(estr)
	}

	for {
		layout, ok := <-ch
		if !ok {
			break
		}
		if layout == nil {
			estr := "error during load pods from leveldb"
			glog.Error(estr)
			return errors.New(estr)
		}

		glog.V(1).Infof("reloading pod %s: %#v", layout.Id, layout)
		fc := pod.NewPodFactory(daemon.Factory, daemon.PodList, daemon.db, daemon.Storage, daemon.Daemon, daemon.DefaultLog)

		p, err := pod.LoadXPod(fc, layout)
		if err != nil {
			glog.Warningf("Got a unexpected error when creating(load) pod %s, %v", layout.Id, err)
			continue
		}

		if glog.V(3) {
			p.Log(pod.TRACE, "containers in pod %s: %v", p.Id(), p.ContainerIds())
		}
	}

	return nil
}