Exemple #1
0
func processImageVolumes(config *dockertypes.ContainerJSON, id string, userPod *pod.UserPod, container *pod.UserContainer) {
	if config.Config.Volumes == nil {
		return
	}

	existed := make(map[string]bool)
	for _, v := range container.Volumes {
		existed[v.Path] = true
	}

	for tgt := range config.Config.Volumes {
		if _, ok := existed[tgt]; ok {
			continue
		}

		n := id + strings.Replace(tgt, "/", "_", -1)
		v := pod.UserVolume{
			Name:   n,
			Source: "",
		}
		r := pod.UserVolumeReference{
			Volume:   n,
			Path:     tgt,
			ReadOnly: false,
		}
		userPod.Volumes = append(userPod.Volumes, v)
		container.Volumes = append(container.Volumes, r)
	}
}
Exemple #2
0
func ParseServiceDiscovery(id string, spec *pod.UserPod) error {
	var containers []pod.UserContainer
	var serviceType string = "service-discovery"
	var serviceDir string = path.Join(utils.HYPER_ROOT, "services", id)

	if len(spec.Services) == 0 || spec.Type == serviceType {
		return nil
	}

	spec.Type = serviceType
	serviceContainer := pod.UserContainer{
		Name:    ServiceDiscoveryContainerName(spec.Name),
		Image:   servicediscovery.ServiceImage,
		Command: []string{"haproxy", "-D", "-f", "/usr/local/etc/haproxy/haproxy.cfg", "-p", "/var/run/haproxy.pid"},
	}

	serviceVolRef := pod.UserVolumeReference{
		Volume:   "service-volume",
		Path:     servicediscovery.ServiceVolume,
		ReadOnly: false,
	}

	/* PrepareServices will check service volume */
	serviceVolume := pod.UserVolume{
		Name:   "service-volume",
		Source: serviceDir,
		Driver: "vfs",
	}

	spec.Volumes = append(spec.Volumes, serviceVolume)

	serviceContainer.Volumes = append(serviceContainer.Volumes, serviceVolRef)

	containers = append(containers, serviceContainer)

	for _, c := range spec.Containers {
		containers = append(containers, c)
	}

	spec.Containers = containers

	return nil
}
Exemple #3
0
func processImageVolumes(config *dockertypes.ContainerJSONRaw, id string, userPod *pod.UserPod, container *pod.UserContainer) {
	if config.Config.Volumes == nil {
		return
	}

	for tgt := range config.Config.Volumes {
		n := id + strings.Replace(tgt, "/", "_", -1)
		v := pod.UserVolume{
			Name:   n,
			Source: "",
		}
		r := pod.UserVolumeReference{
			Volume:   n,
			Path:     tgt,
			ReadOnly: false,
		}
		userPod.Volumes = append(userPod.Volumes, v)
		container.Volumes = append(container.Volumes, r)
	}
}
Exemple #4
0
func processImageVolumes(config *dockertypes.ContainerJSONRaw, id string, userPod *pod.UserPod, container *pod.UserContainer) {
	if config.Config.Volumes == nil {
		return
	}

	for tgt := range config.Config.Volumes {
		n := id + "-" + tgt
		v := pod.UserVolume{
			Name:   n,
			Source: "",
			Driver: "vfs", //will check if it should equal to the storage engine, and it should be reclaim after
			//after the container finished
		}
		r := pod.UserVolumeReference{
			Volume:   n,
			Path:     tgt,
			ReadOnly: false,
		}
		userPod.Volumes = append(userPod.Volumes, v)
		container.Volumes = append(container.Volumes, r)
	}
}
Exemple #5
0
func (daemon *Daemon) CreatePod(podId, podArgs string, config interface{}, autoremove bool) (err error) {
	glog.V(1).Infof("podArgs: %s", podArgs)
	var (
		userPod      *pod.UserPod
		containerIds []string
		cId          []byte
	)
	userPod, err = pod.ProcessPodBytes([]byte(podArgs))
	if err != nil {
		glog.V(1).Infof("Process POD file error: %s", err.Error())
		return err
	}

	if err = userPod.Validate(); err != nil {
		return err
	}

	mypod := hypervisor.NewPod(podId, userPod)
	mypod.Handler.Handle = hyperHandlePodEvent
	mypod.Handler.Data = daemon
	mypod.Autoremove = autoremove

	defer func() {
		if err != nil {
			if containerIds == nil {
				daemon.DeletePodFromDB(podId)
				if mypod != nil {
					for _, c := range mypod.Containers {
						glog.V(1).Infof("Ready to rm container: %s", c.Id)
						if _, _, err = daemon.DockerCli.SendCmdDelete(c.Id); err != nil {
							glog.Warningf("Error to rm container: %s", err.Error())
						}
					}
				}
				daemon.RemovePod(podId)
				daemon.DeletePodContainerFromDB(podId)
			}
		}
	}()
	// store the UserPod into the db
	if err = daemon.WritePodToDB(podId, []byte(podArgs)); err != nil {
		glog.V(1).Info("Found an error while saveing the POD file")
		return err
	}
	containerIds, err = daemon.GetPodContainersByName(podId)
	if err != nil {
		glog.V(1).Info(err.Error())
	}

	if containerIds != nil {
		for _, id := range containerIds {
			var (
				name  string
				image string
			)
			if jsonResponse, err := daemon.DockerCli.GetContainerInfo(id); err == nil {
				name = jsonResponse.Name
				image = jsonResponse.Config.Image
			}
			mypod.AddContainer(id, name, image, []string{}, types.S_POD_CREATED)
		}
	} else {
		// Process the 'Containers' section
		glog.V(1).Info("Process the Containers section in POD SPEC\n")
		for _, c := range userPod.Containers {
			imgName := c.Image
			cId, _, err = daemon.DockerCli.SendCmdCreate(c.Name, imgName, []string{}, nil)
			if err != nil {
				glog.Error(err.Error())
				return err
			}
			var (
				name  string
				image string
			)
			if jsonResponse, err := daemon.DockerCli.GetContainerInfo(string(cId)); err == nil {
				name = jsonResponse.Name
				image = jsonResponse.Config.Image
			}

			mypod.AddContainer(string(cId), name, image, []string{}, types.S_POD_CREATED)
		}
	}

	daemon.AddPod(mypod)

	if err = daemon.WritePodAndContainers(podId); err != nil {
		glog.V(1).Info("Found an error while saveing the Containers info")
		return err
	}

	return nil
}
Exemple #6
0
// TODO: remove convertToRunvPodSpec after pod.UserPod is deleted from runv
func convertToRunvPodSpec(podSpec *apitypes.UserPod) (*pod.UserPod, error) {
	var userPod pod.UserPod

	userPod.Name = podSpec.Id
	if podSpec.Id == "" {
		userPod.Name = utils.RandStr(10, "alphanum")
	}

	if podSpec.PortmappingWhiteLists != nil {
		for _, cidr := range podSpec.PortmappingWhiteLists.ExternalNetworks {
			_, _, err := net.ParseCIDR(cidr)
			if err != nil {
				return nil, fmt.Errorf("PortmappingWhiteLists.ExternalNetwork %s format error", cidr)
			}
		}
		filteredInternalNetworks := make([]string, 0)
		for _, cidr := range podSpec.PortmappingWhiteLists.InternalNetworks {
			_, _, err := net.ParseCIDR(cidr)
			if err != nil {
				return nil, fmt.Errorf("PortmappingWhiteLists.InternalNetworks %s format error", cidr)
			}

			// filter cidr out if the cidr is also in ExternalNetworks
			found := false
			for _, ext := range podSpec.PortmappingWhiteLists.ExternalNetworks {
				if cidr == ext {
					found = true
					break
				}
			}
			if !found {
				filteredInternalNetworks = append(filteredInternalNetworks, cidr)
			}
		}

		userPod.PortmappingWhiteLists = &pod.PortmappingWhiteList{
			InternalNetworks: filteredInternalNetworks,
			ExternalNetworks: podSpec.PortmappingWhiteLists.ExternalNetworks,
		}
	}

	userPod.Hostname = podSpec.Hostname
	userPod.Type = podSpec.Type
	userPod.RestartPolicy = podSpec.RestartPolicy
	userPod.Dns = podSpec.Dns
	userPod.Tty = podSpec.Tty
	userPod.Labels = podSpec.Labels

	if podSpec.Labels == nil {
		userPod.Labels = make(map[string]string)
	}

	if podSpec.Resource != nil {
		userPod.Resource = pod.UserResource{
			Vcpu:   int(podSpec.Resource.Vcpu),
			Memory: int(podSpec.Resource.Memory),
		}
	}
	if userPod.Resource.Vcpu == 0 {
		userPod.Resource.Vcpu = 1
	}
	if userPod.Resource.Memory == 0 {
		userPod.Resource.Memory = 128
	}

	if len(podSpec.Containers) > 0 {
		containers := make([]pod.UserContainer, 0, len(podSpec.Containers))
		for _, v := range podSpec.Containers {
			if v.Image == "" {
				return nil, fmt.Errorf("Please specific your image for your container, it can not be null!\n")
			}

			containers = append(containers, convertToRunvContainerSpec(v, userPod.Tty))
		}

		userPod.Containers = containers
	}

	if len(podSpec.Files) > 0 {
		files := make([]pod.UserFile, 0, len(podSpec.Files))
		for _, f := range podSpec.Files {
			files = append(files, pod.UserFile{
				Name:     f.Name,
				Encoding: f.Encoding,
				Uri:      f.Uri,
				Contents: f.Content,
			})
		}
		userPod.Files = files
	}

	if len(podSpec.Volumes) > 0 {
		vols := make([]pod.UserVolume, 0, len(podSpec.Volumes))
		for _, vol := range podSpec.Volumes {
			if vol.Name == "" {
				return nil, fmt.Errorf("Hyper ERROR: please specific your volume name, it can not be null!\n")
			}

			v := pod.UserVolume{
				Name:   vol.Name,
				Driver: vol.Driver,
				Source: vol.Source,
			}
			if vol.Option != nil {
				v.Option = pod.UserVolumeOption{
					Monitors: vol.Option.Monitors,
					Keyring:  vol.Option.Keyring,
					User:     vol.Option.User,
				}
			}
			vols = append(vols, v)
		}
		userPod.Volumes = vols
	}

	if len(podSpec.Services) > 0 {
		services := make([]pod.UserService, 0, len(podSpec.Services))
		for _, svc := range podSpec.Services {
			s := pod.UserService{
				ServiceIP:   svc.ServiceIP,
				ServicePort: int(svc.ServicePort),
				Protocol:    svc.Protocol,
			}
			if len(svc.Hosts) > 0 {
				hosts := make([]pod.UserServiceBackend, 0, len(svc.Hosts))
				for _, host := range svc.Hosts {
					hosts = append(hosts, pod.UserServiceBackend{
						HostIP:   host.HostIP,
						HostPort: int(host.HostPort),
					})
				}
				s.Hosts = hosts
			}
			services = append(services, s)
		}
		userPod.Services = services
	}

	if len(podSpec.Interfaces) > 0 {
		interfaces := make([]pod.UserInterface, 0, len(podSpec.Interfaces))
		for _, i := range podSpec.Interfaces {
			interfaces = append(interfaces, pod.UserInterface{
				Bridge: i.Bridge,
				Ip:     i.Ip,
				Ifname: i.Ifname,
				Mac:    i.Mac,
				Gw:     i.Gateway,
			})
		}
		userPod.Interfaces = interfaces
	}

	if podSpec.Log != nil {
		userPod.LogConfig = pod.PodLogConfig{
			Type:   podSpec.Log.Type,
			Config: podSpec.Log.Config,
		}
	}

	return &userPod, nil
}