func ConvertPodSpecToMesosContainer()

in pkg/common/api/api_converter.go [839:957]


func ConvertPodSpecToMesosContainer(spec *pod.PodSpec) *mesosv1.ContainerInfo {
	if len(spec.GetContainers()) == 0 {
		return nil
	}

	mainContainer := spec.GetContainers()[0]
	if mainContainer.GetContainer() != nil {
		return mainContainer.GetContainer()
	}

	containerInfo := &mesosv1.ContainerInfo{}
	mesosPodSpec := spec.GetMesosSpec()

	// populate ContainerType
	if mesosPodSpec.GetType() == apachemesos.PodSpec_CONTAINER_TYPE_MESOS {
		containerType := mesosv1.ContainerInfo_MESOS
		containerInfo.Type = &containerType
	} else if mesosPodSpec.GetType() == apachemesos.PodSpec_CONTAINER_TYPE_DOCKER {
		containerType := mesosv1.ContainerInfo_DOCKER
		containerInfo.Type = &containerType
	} else {
		return nil
	}

	// Populate volumes
	var volumes []*mesosv1.Volume
	for _, volumeMount := range mainContainer.GetVolumeMounts() {
		volumeSpec := FindVolumeInPodSpec(spec, volumeMount.GetName())
		if volumeSpec.GetType() != volume.VolumeSpec_VOLUME_TYPE_HOST_PATH ||
			volumeSpec.GetHostPath() == nil {
			// unsupported volume
			continue
		}

		mode := mesosv1.Volume_RW
		if volumeMount.GetReadOnly() {
			mode = mesosv1.Volume_RO
		}
		mountPath := volumeMount.GetMountPath()
		containerPath := volumeSpec.GetHostPath().GetPath()

		mesosVolume := &mesosv1.Volume{
			Mode:          &mode,
			ContainerPath: &mountPath,
			HostPath:      &containerPath,
		}

		volumes = append(volumes, mesosVolume)
	}

	if len(volumes) > 0 {
		containerInfo.Volumes = volumes
	}

	// Populate container type specific info
	cached := true
	image := mainContainer.GetImage()
	if len(image) > 0 {
		if containerInfo.GetType() == mesosv1.ContainerInfo_DOCKER {
			var parameters []*mesosv1.Parameter

			for _, parameter := range mesosPodSpec.GetDockerParameters() {
				key := parameter.GetKey()
				value := parameter.GetValue()
				mesosParameter := &mesosv1.Parameter{
					Key:   &key,
					Value: &value,
				}
				parameters = append(parameters, mesosParameter)
			}

			// Fill in the network
			hostNetwork := mesosv1.ContainerInfo_DockerInfo_HOST
			if mesosPodSpec.GetNetworkSpec() != nil {
				switch mesosPodSpec.GetNetworkSpec().GetType() {
				case apachemesos.PodSpec_NetworkSpec_NETWORK_TYPE_INVALID:
					// This should never be set and will be mapped to NONE for now.
					hostNetwork = mesosv1.ContainerInfo_DockerInfo_NONE
				case apachemesos.PodSpec_NetworkSpec_NETWORK_TYPE_HOST:
					hostNetwork = mesosv1.ContainerInfo_DockerInfo_HOST
				case apachemesos.PodSpec_NetworkSpec_NETWORK_TYPE_BRIDGE:
					hostNetwork = mesosv1.ContainerInfo_DockerInfo_BRIDGE
				case apachemesos.PodSpec_NetworkSpec_NETWORK_TYPE_NONE:
					hostNetwork = mesosv1.ContainerInfo_DockerInfo_NONE
				case apachemesos.PodSpec_NetworkSpec_NETWORK_TYPE_USER:
					hostNetwork = mesosv1.ContainerInfo_DockerInfo_USER
				}

				networkName := mesosPodSpec.GetNetworkSpec().GetName()
				if len(networkName) > 0 {
					containerInfo.NetworkInfos = []*mesosv1.NetworkInfo{
						{
							Name: &networkName,
						},
					}
				}
			}

			containerInfo.Docker = &mesosv1.ContainerInfo_DockerInfo{
				Image:      &image,
				Network:    &hostNetwork,
				Parameters: parameters,
			}
		} else {
			imageType := mesosv1.Image_DOCKER
			containerInfo.Mesos = &mesosv1.ContainerInfo_MesosInfo{
				Image: &mesosv1.Image{
					Type: &imageType,
					Docker: &mesosv1.Image_Docker{
						Name: &image,
					},
					Cached: &cached,
				},
			}
		}
	}

	return containerInfo
}