Skip to content

fix: print logs if any error appears#2725

Merged
simar7 merged 6 commits intoaquasecurity:mainfrom
afdesk:fix/print-logs
Nov 24, 2025
Merged

fix: print logs if any error appears#2725
simar7 merged 6 commits intoaquasecurity:mainfrom
afdesk:fix/print-logs

Conversation

@afdesk
Copy link
Copy Markdown
Contributor

@afdesk afdesk commented Aug 27, 2025

Description

After changes introduced in #2591 , all console output was redirected to a file. As a result, error messages were silently ignored and not visible in the pod logs.

This PR addresses the issue by checking the exit code after execution. If an error occurs, the log file content is printed to the console, making debugging easier and preventing error messages from being overlooked.

Also there was updated a log message for unsupported OS/arch.

Before:

{"level":"error","ts":"2025-08-28T10:35:43Z","logger":"reconciler.scan job","msg":"Scan job container","job":"trivy-system/scan-vulnerabilityreport-5c6d5f497b","container":"trivy-operator","status.reason":"Error","status.message":"","stacktrace":"github.com/aquasecurity/trivy-operator/pkg/vulnerabilityreport/controller.(*ScanJobController).completedContainers\n\t/Users/amf/aqua/trivy-operator/pkg/vulnerabilityreport/controller/scanjob.go:441..."}

After:

{"level":"info","ts":"2025-08-28T10:11:23Z","logger":"reconciler.scan job","msg":"Scan job container","job":"trivy-system/scan-vulnerabilityreport-68bb4cf948","container":"trivy-operator","status.reason":"Error","status.message":"By default, only Linux amd64 images are supported for scanning."}

Related issues

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@github-actions github-actions bot added the bug label Aug 27, 2025
@afdesk afdesk marked this pull request as ready for review September 17, 2025 11:58
@afdesk afdesk requested a review from simar7 as a code owner September 17, 2025 11:58
args = append(args,
fmt.Sprintf(`&& bzip2 -c /tmp/scan/%s | base64`, resultFileName),
)
return []string{"/bin/sh"}, append([]string{"-c"}, strings.Join(args, " "))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TBH I'm not a huge fan of running a shell to capture commands as it can be used in a malicious way to inject something.

WDYT if we remove the reliance on arbitrary shell commands and capture the output (and the error codes) of Go library commands (such as compress logs, encode to base64, capture the output etc.?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@simar7 sorry, it seems I misunderstood something

These commands are executed inside a separate k8s Job based on the Trivy image.
I didn’t fully understand your point about using Go code directly.

To be honest, I’m also not a big fan of reading the report and/or the logs as container logs, but this was done by design and I don’t have a better solution at the moment. tried using volumes, but they also come with their own overhead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we can take this opportunity to get rid of getCommandAndArgs completely and replace it with actual library calls. Since we build these args and eventually call trivy, we should really use trivy as a library rather than call trivy binary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, we don’t call Trivy directly — instead, we create a Kubernetes Job based on the Trivy image. So this is a much more complex setup than it might appear at first glance.

Changing this approach would significantly impact the application’s architecture and needs to be carefully considered. For example, at the moment, Kubernetes handles Job scheduling automatically, but with such a change, image scanning workloads could start overloading a specific node, among other potential issues.

Therefore, I’d suggest discussing this idea separately before making any architectural decisions.

As for this PR, it simply restores log output in case of errors, which will greatly improve troubleshooting (this behavior was present before #2591).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fair enough. Can we open an issue to change this in a different PR? I think we should find a way to not rely on arbitrary shell commands within the operator.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@simar7
oh, missed it. sure

@mayanksharma6790
Copy link
Copy Markdown

i also got the same issue
job.batch/scan-vulnerabilityreport failed but no errors in logs and even operator logs says nothing

when we can expect this to get merge in main branch ?

@afdesk afdesk changed the title fix: print logs if any errors appear fix: print logs if any error appears Nov 5, 2025
@afdesk afdesk added this to the v0.30.0 milestone Nov 17, 2025
@simar7 simar7 merged commit 7647456 into aquasecurity:main Nov 24, 2025
13 of 14 checks passed
@cthtrifork cthtrifork mentioned this pull request Jan 20, 2026
5 tasks
@afdesk afdesk deleted the fix/print-logs branch February 16, 2026 06:22
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.

3 participants