Modify PlayKubeReport to preserve pod->container mapping

Signed-off-by: Aditya Kamath <theunrealgeek@gmail.com>
This commit is contained in:
theunrealgeek 2020-05-23 20:47:30 -07:00
parent ce7a9f0314
commit 478f296fb3
3 changed files with 36 additions and 25 deletions

View File

@ -92,8 +92,10 @@ func kube(cmd *cobra.Command, args []string) error {
return err return err
} }
for _, l := range report.Logs { for _, pod := range report.Pods {
fmt.Fprintf(os.Stderr, l) for _, l := range pod.Logs {
fmt.Fprintf(os.Stderr, l)
}
} }
switch len(report.Pods) { switch len(report.Pods) {
@ -105,19 +107,19 @@ func kube(cmd *cobra.Command, args []string) error {
fmt.Printf("Pods:\n") fmt.Printf("Pods:\n")
} }
for _, pod := range report.Pods { for _, pod := range report.Pods {
fmt.Println(pod) fmt.Println(pod.ID)
}
switch len(report.Containers) { switch len(pod.Containers) {
case 0: case 0:
return nil return nil
case 1: case 1:
fmt.Printf("Container:\n") fmt.Printf("Container:\n")
default: default:
fmt.Printf("Containers:\n") fmt.Printf("Containers:\n")
} }
for _, ctr := range report.Containers { for _, ctr := range pod.Containers {
fmt.Println(ctr) fmt.Println(ctr)
}
} }
return nil return nil

View File

@ -26,12 +26,17 @@ type PlayKubeOptions struct {
SeccompProfileRoot string SeccompProfileRoot string
} }
// PlayKubeReport contains the results of running play kube. // PlayKubePods represents a single pod and associated containers created by play kube
type PlayKubeReport struct { type PlayKubePod struct {
// Pods - the IDs of the created pods. ID string
Pods []string
// Containers - the IDs of the containers running in the created pod. // Containers - the IDs of the containers running in the created pod.
Containers []string Containers []string
// Logs - non-fatal erros and log messages while processing. // Logs - non-fatal erros and log messages while processing.
Logs []string Logs []string
} }
// PlayKubeReport contains the results of running play kube.
type PlayKubeReport struct {
// Pods - pods created by play kube.
Pods []PlayKubePod
}

View File

@ -103,8 +103,6 @@ func (ic *ContainerEngine) playKubeDeployment(ctx context.Context, deploymentYAM
return nil, errors.Wrapf(err, "Error encountered while bringing up pod %s", podName) return nil, errors.Wrapf(err, "Error encountered while bringing up pod %s", podName)
} }
report.Pods = append(report.Pods, podReport.Pods...) report.Pods = append(report.Pods, podReport.Pods...)
report.Containers = append(report.Containers, podReport.Containers...)
report.Logs = append(report.Logs, podReport.Logs...)
} }
return &report, nil return &report, nil
} }
@ -116,6 +114,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
podOptions []libpod.PodCreateOption podOptions []libpod.PodCreateOption
registryCreds *types.DockerAuthConfig registryCreds *types.DockerAuthConfig
writer io.Writer writer io.Writer
playKubePod entities.PlayKubePod
report entities.PlayKubeReport report entities.PlayKubeReport
) )
@ -125,7 +124,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
} }
for _, n := range podYAML.Spec.Containers { for _, n := range podYAML.Spec.Containers {
if n.Name == podName { if n.Name == podName {
report.Logs = append(report.Logs, playKubePod.Logs = append(playKubePod.Logs,
fmt.Sprintf("a container exists with the same name (%q) as the pod in your YAML file; changing pod name to %s_pod\n", podName, podName)) fmt.Sprintf("a container exists with the same name (%q) as the pod in your YAML file; changing pod name to %s_pod\n", podName, podName))
podName = fmt.Sprintf("%s_pod", podName) podName = fmt.Sprintf("%s_pod", podName)
} }
@ -314,11 +313,13 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
} }
} }
report.Pods = append(report.Pods, pod.ID()) playKubePod.ID = pod.ID()
for _, ctr := range containers { for _, ctr := range containers {
report.Containers = append(report.Containers, ctr.ID()) playKubePod.Containers = append(playKubePod.Containers, ctr.ID())
} }
report.Pods = append(report.Pods, playKubePod)
return &report, nil return &report, nil
} }
@ -425,9 +426,12 @@ func kubeContainerToCreateConfig(ctx context.Context, containerYAML v1.Container
containerConfig.ImageID = newImage.ID() containerConfig.ImageID = newImage.ID()
containerConfig.Name = containerYAML.Name containerConfig.Name = containerYAML.Name
if podName != "" { // podName should be non-empty for Deployment objects to be able to create
containerConfig.Name = fmt.Sprintf("%s-%s", podName, containerYAML.Name) // multiple pods having containers with unique names
if podName == "" {
return nil, errors.Errorf("kubeContainerToCreateConfig got empty podName")
} }
containerConfig.Name = fmt.Sprintf("%s-%s", podName, containerYAML.Name)
containerConfig.Tty = containerYAML.TTY containerConfig.Tty = containerYAML.TTY