func (daemon *Daemon) CmdContainerInfo(job *engine.Job) error { if len(job.Args) == 0 { return fmt.Errorf("Can not get Pod info without Pod ID") } daemon.PodList.RLock() glog.V(2).Infof("lock read of PodList") defer daemon.PodList.RUnlock() defer glog.V(2).Infof("unlock read of PodList") var ( pod *Pod c *hypervisor.Container i int = 0 imageid string name string = job.Args[0] ) if name == "" { return fmt.Errorf("Null container name") } glog.Infof(name) wslash := name if name[0] != '/' { wslash = "/" + name } pod = daemon.PodList.Find(func(p *Pod) bool { for i, c = range p.status.Containers { if c.Name == wslash || c.Id == name { return true } } return false }) if pod == nil { return fmt.Errorf("Can not find container by name(%s)", name) } ports := []types.ContainerPort{} envs := []types.EnvironmentVar{} vols := []types.VolumeMount{} jsonResponse, err := daemon.DockerCli.GetContainerInfo(c.Id) if err == nil { for _, e := range jsonResponse.Config.Env { envs = append(envs, types.EnvironmentVar{ Env: e[:strings.Index(e, "=")], Value: e[strings.Index(e, "=")+1:]}) } imageid = jsonResponse.Image } for _, port := range pod.spec.Containers[i].Ports { ports = append(ports, types.ContainerPort{ HostPort: port.HostPort, ContainerPort: port.ContainerPort, Protocol: port.Protocol}) } for _, e := range pod.spec.Containers[i].Envs { envs = append(envs, types.EnvironmentVar{ Env: e.Env, Value: e.Value}) } for _, v := range pod.spec.Containers[i].Volumes { vols = append(vols, types.VolumeMount{ Name: v.Volume, MountPath: v.Path, ReadOnly: v.ReadOnly}) } s := types.ContainerStatus{} s.Name = c.Name s.ContainerID = c.Id s.Waiting = types.WaitingStatus{Reason: ""} s.Running = types.RunningStatus{StartedAt: ""} s.Terminated = types.TermStatus{} if c.Status == runvtypes.S_POD_CREATED { s.Waiting.Reason = "Pending" s.Phase = "pending" } else if c.Status == runvtypes.S_POD_RUNNING { s.Running.StartedAt = pod.status.StartedAt s.Phase = "running" } else { // S_POD_FAILED or S_POD_SUCCEEDED if c.Status == runvtypes.S_POD_FAILED { s.Terminated.ExitCode = c.ExitCode s.Terminated.Reason = "Failed" s.Phase = "failed" } else { s.Terminated.ExitCode = c.ExitCode s.Terminated.Reason = "Succeeded" s.Phase = "succeeded" } s.Terminated.StartedAt = pod.status.StartedAt s.Terminated.FinishedAt = pod.status.FinishedAt } container := types.ContainerInfo{ Name: c.Name, ContainerID: c.Id, PodID: pod.id, Image: c.Image, ImageID: imageid, Commands: pod.spec.Containers[i].Command, Args: []string{}, Workdir: pod.spec.Containers[i].Workdir, Ports: ports, Environment: envs, Volume: vols, ImagePullPolicy: "", Status: s, } v := &engine.Env{} v.SetJson("data", container) if _, err := v.WriteTo(job.Stdout); err != nil { return err } return nil }
func (daemon *Daemon) CmdPodInfo(job *engine.Job) error { if len(job.Args) == 0 { return fmt.Errorf("Can not get Pod info without Pod ID") } daemon.PodList.RLock() glog.V(2).Infof("lock read of PodList") defer daemon.PodList.RUnlock() defer glog.V(2).Infof("unlock read of PodList") var ( pod *Pod podId string ok bool imageid string ) if strings.Contains(job.Args[0], "pod-") { podId = job.Args[0] pod, ok = daemon.PodList.Get(podId) if !ok { return fmt.Errorf("Can not get Pod info with pod ID(%s)", podId) } } else { pod = daemon.PodList.GetByName(job.Args[0]) if pod == nil { return fmt.Errorf("Can not get Pod info with pod name(%s)", job.Args[0]) } } // Construct the PodInfo JSON structure cStatus := []types.ContainerStatus{} containers := []types.Container{} for i, c := range pod.status.Containers { ports := []types.ContainerPort{} envs := []types.EnvironmentVar{} vols := []types.VolumeMount{} jsonResponse, err := daemon.DockerCli.GetContainerInfo(c.Id) if err == nil { for _, e := range jsonResponse.Config.Env { envs = append(envs, types.EnvironmentVar{ Env: e[:strings.Index(e, "=")], Value: e[strings.Index(e, "=")+1:]}) } imageid = jsonResponse.Image } for _, port := range pod.spec.Containers[i].Ports { ports = append(ports, types.ContainerPort{ HostPort: port.HostPort, ContainerPort: port.ContainerPort, Protocol: port.Protocol}) } for _, e := range pod.spec.Containers[i].Envs { envs = append(envs, types.EnvironmentVar{ Env: e.Env, Value: e.Value}) } for _, v := range pod.spec.Containers[i].Volumes { vols = append(vols, types.VolumeMount{ Name: v.Volume, MountPath: v.Path, ReadOnly: v.ReadOnly}) } container := types.Container{ Name: c.Name, ContainerID: c.Id, Image: c.Image, ImageID: imageid, Commands: pod.spec.Containers[i].Command, Args: []string{}, Workdir: pod.spec.Containers[i].Workdir, Ports: ports, Environment: envs, Volume: vols, ImagePullPolicy: "", } containers = append(containers, container) // Set ContainerStatus s := types.ContainerStatus{} s.Name = c.Name s.ContainerID = c.Id s.Waiting = types.WaitingStatus{Reason: ""} s.Running = types.RunningStatus{StartedAt: ""} s.Terminated = types.TermStatus{} if c.Status == runvtypes.S_POD_CREATED { s.Waiting.Reason = "Pending" s.Phase = "pending" } else if c.Status == runvtypes.S_POD_RUNNING { s.Running.StartedAt = pod.status.StartedAt s.Phase = "running" } else { // S_POD_FAILED or S_POD_SUCCEEDED if c.Status == runvtypes.S_POD_FAILED { s.Terminated.ExitCode = c.ExitCode s.Terminated.Reason = "Failed" s.Phase = "failed" } else { s.Terminated.ExitCode = c.ExitCode s.Terminated.Reason = "Succeeded" s.Phase = "succeeded" } s.Terminated.StartedAt = pod.status.StartedAt s.Terminated.FinishedAt = pod.status.FinishedAt } cStatus = append(cStatus, s) } podVoumes := []types.PodVolume{} for _, v := range pod.spec.Volumes { podVoumes = append(podVoumes, types.PodVolume{ Name: v.Name, HostPath: v.Source, Driver: v.Driver}) } spec := types.PodSpec{ Volumes: podVoumes, Containers: containers, Labels: pod.spec.Labels, Vcpu: pod.spec.Resource.Vcpu, Memory: pod.spec.Resource.Memory, } podIPs := []string{} if pod.vm != nil { podIPs = pod.status.GetPodIP(pod.vm) } status := types.PodStatus{ Status: cStatus, HostIP: utils.GetHostIP(), PodIP: podIPs, StartTime: pod.status.StartedAt, } switch pod.status.Status { case runvtypes.S_POD_CREATED: status.Phase = "Pending" break case runvtypes.S_POD_RUNNING: status.Phase = "Running" break case runvtypes.S_POD_SUCCEEDED: status.Phase = "Succeeded" break case runvtypes.S_POD_FAILED: status.Phase = "Failed" break } data := types.PodInfo{ Kind: "Pod", ApiVersion: utils.APIVERSION, Vm: pod.status.Vm, Spec: spec, Status: status, } v := &engine.Env{} v.SetJson("data", data) if _, err := v.WriteTo(job.Stdout); err != nil { return err } return nil }
func (daemon *Daemon) GetContainerInfo(name string) (types.ContainerInfo, error) { var ( pod *Pod c *hypervisor.Container i int = 0 imageid string ok bool cmd []string args []string ) if name == "" { return types.ContainerInfo{}, fmt.Errorf("Null container name") } glog.Infof(name) daemon.PodList.RLock() glog.V(2).Infof("lock read of PodList") pod, i, ok = daemon.PodList.GetByContainerIdOrName(name) if !ok { daemon.PodList.RUnlock() glog.V(2).Infof("unlock read of PodList") return types.ContainerInfo{}, fmt.Errorf("Can not find container by name(%s)", name) } c = pod.status.Containers[i] daemon.PodList.RUnlock() glog.V(2).Infof("unlock read of PodList") ports := []types.ContainerPort{} envs := []types.EnvironmentVar{} vols := []types.VolumeMount{} rsp, err := daemon.Daemon.ContainerInspect(c.Id, false, version.Version("1.21")) if err == nil { var jsonResponse *dockertypes.ContainerJSON jsonResponse, _ = rsp.(*dockertypes.ContainerJSON) for _, e := range jsonResponse.Config.Env { envs = append(envs, types.EnvironmentVar{ Env: e[:strings.Index(e, "=")], Value: e[strings.Index(e, "=")+1:]}) } imageid = jsonResponse.Image cmd = []string{jsonResponse.Path} args = jsonResponse.Args } if len(cmd) == 0 { glog.Warning("length of commands in inspect result should not be zero") cmd = pod.spec.Containers[i].Command } for _, port := range pod.spec.Containers[i].Ports { ports = append(ports, types.ContainerPort{ HostPort: port.HostPort, ContainerPort: port.ContainerPort, Protocol: port.Protocol}) } for _, e := range pod.spec.Containers[i].Envs { envs = append(envs, types.EnvironmentVar{ Env: e.Env, Value: e.Value}) } for _, v := range pod.spec.Containers[i].Volumes { vols = append(vols, types.VolumeMount{ Name: v.Volume, MountPath: v.Path, ReadOnly: v.ReadOnly}) } s := types.ContainerStatus{} s.Name = c.Name s.ContainerID = c.Id s.Waiting = types.WaitingStatus{Reason: ""} s.Running = types.RunningStatus{StartedAt: ""} s.Terminated = types.TermStatus{} if c.Status == runvtypes.S_POD_CREATED { s.Waiting.Reason = "Pending" s.Phase = "pending" } else if c.Status == runvtypes.S_POD_RUNNING { s.Running.StartedAt = pod.status.StartedAt s.Phase = "running" } else { // S_POD_FAILED or S_POD_SUCCEEDED if c.Status == runvtypes.S_POD_FAILED { s.Terminated.ExitCode = c.ExitCode s.Terminated.Reason = "Failed" s.Phase = "failed" } else { s.Terminated.ExitCode = c.ExitCode s.Terminated.Reason = "Succeeded" s.Phase = "succeeded" } s.Terminated.StartedAt = pod.status.StartedAt s.Terminated.FinishedAt = pod.status.FinishedAt } return types.ContainerInfo{ Container: types.Container{ Name: c.Name, ContainerID: c.Id, Image: pod.spec.Containers[i].Image, ImageID: imageid, Commands: cmd, Args: args, Workdir: pod.spec.Containers[i].Workdir, Ports: ports, Environment: envs, Volume: vols, Tty: pod.spec.Containers[i].Tty, ImagePullPolicy: "", }, PodID: pod.id, Status: s, }, nil }