Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding container name to log config #9627

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/skaffold/k8sjob/logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func (l *Logger) streamLogsFromKubernetesJob(ctx context.Context, id, namespace
}
}
var podName string
var containerName string
w, err := clientset.CoreV1().Pods(namespace).Watch(ctx,
metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{"job-name": id, "skaffold.dev/run-id": l.labeller.GetRunID()}).String(),
Expand All @@ -180,6 +181,7 @@ func (l *Logger) streamLogsFromKubernetesJob(ctx context.Context, id, namespace
pod, ok := event.Object.(*corev1.Pod)
if ok {
podName = pod.Name
containerName = pod.Spec.Containers[0].Name

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This line assumes the pod has only one container, taking the name of the first container in the spec. Jobs can have multiple containers. Consider adding a mechanism to specify the target container. A few options include:

  • --container flag: Allow users to specify the container name via a command-line flag.
  • skaffold.yaml configuration: Add a field in the skaffold.yaml to configure the target container for logging.
  • Default behavior: If no container is explicitly specified, either log from all containers or emit a warning message indicating the ambiguity and perhaps defaulting to the first container.

Choosing a suitable approach will provide flexibility and prevent unexpected behavior when jobs have multiple containers.

done <- true
break
}
Expand All @@ -195,9 +197,7 @@ func (l *Logger) streamLogsFromKubernetesJob(ctx context.Context, id, namespace
return false, fmt.Errorf("timeout waiting for event from pod of kubernetes job: %s", id)
}

podLogOptions := &corev1.PodLogOptions{
Follow: true,
}
podLogOptions := &corev1.PodLogOptions{Follow: true, Container: containerName}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's good practice to check if containerName is empty before using it in podLogOptions. This will prevent potential issues if the pod has no containers or if the container name retrieval logic encounters an issue. If the name is empty, you could log a warning or return an error, depending on the desired behavior.

Suggested change
podLogOptions := &corev1.PodLogOptions{Follow: true, Container: containerName}
podLogOptions := &corev1.PodLogOptions{Follow: true}
if containerName != "" {
podLogOptions.Container = containerName
}


// Stream the logs
req := clientset.CoreV1().Pods(namespace).GetLogs(podName, podLogOptions)
Expand Down