Skip to content

Commit

Permalink
Fix issue with missing container name and namespace (#2269)
Browse files Browse the repository at this point in the history
* Fix issue with missing container name

* Fix issue with missing container name

* populate pod options in pod controller

* use correct namespace

* use correct namespace

* Update pkg/kube/pod_controller.go

Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com>

---------

Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com>
  • Loading branch information
chaitalisg and pavannd1 committed Aug 16, 2023
1 parent 9875d91 commit 0fa348f
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions pkg/kube/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func NewPodControllerForExistingPod(cli kubernetes.Interface, pod *corev1.Pod) P
podName: pod.Name,
}

options := &PodOptions{
Name: pod.Name,
Namespace: pod.Namespace,
ContainerName: pod.Spec.Containers[0].Name,
}
r.podOptions = options

r.pcp = r

return r
Expand Down Expand Up @@ -204,12 +211,26 @@ func (p *podController) StopPod(ctx context.Context, stopTimeout time.Duration,
return nil
}

// getContainerName returns container name, which should be passed to
// operations that require it.
// If the container name was specified in podOptions, it will be used.
// Otherwise, the first container name from specs will be taken as best effort
// (when pods are created with sidecars, sidecar containers are placed after
// main container).
func (p *podController) getContainerName() string {
if p.podOptions.ContainerName != "" {
return p.podOptions.ContainerName
}

return p.pod.Spec.Containers[0].Name
}

func (p *podController) StreamPodLogs(ctx context.Context) (io.ReadCloser, error) {
if p.podName == "" {
return nil, ErrPodControllerPodNotStarted
}

return StreamPodLogs(ctx, p.cli, p.pod.Namespace, p.pod.Name, p.pod.Spec.Containers[0].Name)
return StreamPodLogs(ctx, p.cli, p.pod.Namespace, p.pod.Name, p.getContainerName())
}

func (p *podController) GetCommandExecutor() (PodCommandExecutor, error) {
Expand All @@ -221,16 +242,11 @@ func (p *podController) GetCommandExecutor() (PodCommandExecutor, error) {
return nil, ErrPodControllerPodNotReady
}

containerName := p.podOptions.ContainerName
if containerName == "" {
containerName = p.pod.Spec.Containers[0].Name
}

pce := &podCommandExecutor{
cli: p.cli,
namespace: p.podOptions.Namespace,
namespace: p.pod.Namespace,
podName: p.podName,
containerName: containerName,
containerName: p.getContainerName(),
}

pce.pcep = pce
Expand All @@ -251,7 +267,7 @@ func (p *podController) GetFileWriter() (PodFileWriter, error) {
cli: p.cli,
namespace: p.podOptions.Namespace,
podName: p.podName,
containerName: p.podOptions.ContainerName,
containerName: p.getContainerName(),
}

pfw.fileWriterProcessor = pfw
Expand Down

0 comments on commit 0fa348f

Please sign in to comment.