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

Conversation

davidgammon-mettle
Copy link

Fixes: #9626

Description
See issue #9626

Copy link

google-cla bot commented Dec 23, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link

@code-review-assist code-review-assist bot left a comment

Choose a reason for hiding this comment

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

Hi team,

I'm currently reviewing this pull request and will have a full review ready in a few minutes. In the meantime, here's a quick summary to help you and other reviewers get up to speed:

This pull request addresses issue #9626 by adding the container name to the log configuration in Skaffold's Kubernetes Job logger. The goal is to improve log streaming by specifying the container from which logs should be retrieved.

Here's a breakdown of the changes:

  • pkg/skaffold/k8sjob/logger/log.go: This file received the core changes.
    • Three sections were modified to incorporate the containerName variable.
    • Lines 168-169: A containerName variable is declared to store the container name.
    • Lines 183-184: The containerName variable is populated with the name of the first container in the pod's specification.
    • Lines 199-200: The containerName is used in the PodLogOptions to specify which container's logs to retrieve.

The changes ensure that logs are streamed from the correct container, resolving potential ambiguity and improving the accuracy of log output.


And now, a little haiku to lighten the mood:

Containers aligned,
Logs stream clear, a joyful sight,
Skaffold's heart sings on.

Thanks,
Gemini

Copy link

@code-review-assist code-review-assist bot left a comment

Choose a reason for hiding this comment

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

This pull request addresses issue #9626 by adding the container name to the log configuration, enabling Skaffold to stream logs from a specific container in a Kubernetes Job. This is a valuable improvement. The implementation generally looks correct and efficient. However, the current approach assumes the job only has one container. It's crucial to handle cases where a job might have multiple containers.

Here are a few suggestions to enhance the implementation:

@@ -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.

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sidecar/Init containers cause verify to stop working
1 participant