Exemple #1
0
func GetServices(vm *hypervisor.Vm, container string) ([]pod.UserService, error) {
	var services []pod.UserService
	config := path.Join(ServiceVolume, ServiceConfig)

	data, err := vm.ReadFile(container, config)
	if err != nil {
		return nil, err
	}

	token := bytes.Split(data, []byte("\n"))

	for _, tok := range token {
		first := bytes.Split(tok, []byte(" "))
		reader := bytes.NewReader(tok)
		if len(first) > 0 {
			var t1, t2, t3, t4 string
			if string(first[0][:]) == "frontend" {
				s := pod.UserService{
					Protocol: "TCP",
				}

				_, err := fmt.Fscanf(reader, "%s %s %s", &t1, &t2, &t3)
				if err != nil {
					return nil, err
				}

				hostport := strings.Split(t3, ":")
				s.ServiceIP = hostport[0]
				port, err := strconv.ParseInt(hostport[1], 10, 32)
				if err != nil {
					return nil, err
				}
				s.ServicePort = int(port)

				services = append(services, s)
			} else if string(first[0][:]) == "\tserver" {
				var idx int
				var h pod.UserServiceBackend
				_, err := fmt.Fscanf(reader, "%s %s %s %s", &t1, &t2, &t3, &t4)
				if err != nil {
					return nil, err
				}

				hostport := strings.Split(t3, ":")
				h.HostIP = hostport[0]
				port, err := strconv.ParseInt(hostport[1], 10, 32)
				if err != nil {
					return nil, err
				}
				h.HostPort = int(port)

				idxs := strings.Split(t2, "-")
				idxLong, err := strconv.ParseInt(idxs[1], 10, 32)
				if err != nil {
					return nil, err
				}
				idx = int(idxLong)

				services[idx].Hosts = append(services[idx].Hosts, h)
			}
		}
	}
	return services, nil
}
Exemple #2
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
}