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

fix: add logging un-parsed resource #285

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ on:
- 'NOTICE'

env:
GO_VERSION: "1.20.4"
GO_VERSION: "1.21.6"

# Disable permissions granted to the GITHUB_TOKEN for all the available scopes.
permissions: {}
Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:
uses: golangci/golangci-lint-action@v3.7.0
with:
args: --verbose --deadline=5m
version: v1.53.2
version: v1.55.2
skip-pkg-cache: true
skip-build-cache: true
- name: Verify YAML code
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
tags:
- "v*"
env:
GO_VERSION: "1.20.4"
GO_VERSION: "1.21.6"
jobs:
unit-tests:
name: Run unit tests
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
k8s.io/apimachinery v0.29.0
k8s.io/cli-runtime v0.29.0
k8s.io/client-go v0.29.0
k8s.io/klog/v2 v2.120.0
k8s.io/kubectl v0.29.0
k8s.io/utils v0.0.0-20231127182322-b307cd553661
sigs.k8s.io/yaml v1.4.0
Expand All @@ -24,6 +23,7 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
golang.org/x/sync v0.3.0 // indirect
k8s.io/klog/v2 v2.120.0 // indirect
)

require (
Expand Down
4 changes: 4 additions & 0 deletions pkg/artifacts/artifacts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package artifacts

import (
"fmt"
"log/slog"

"github.com/aquasecurity/trivy-kubernetes/pkg/k8s"
"github.com/aquasecurity/trivy-kubernetes/pkg/k8s/docker"
"github.com/aquasecurity/trivy-kubernetes/utils"
Expand Down Expand Up @@ -34,6 +37,7 @@ func FromResource(resource unstructured.Unstructured, serverAuths map[string]doc
for _, im := range cTypeImages {
as, err := k8s.MapContainerNamesToDockerAuths(im, serverAuths)
if err != nil {
slog.Warn(fmt.Sprintf("unable to parse image reference, skipping: %s", im))
continue
}
if as != nil {
Expand Down
13 changes: 3 additions & 10 deletions pkg/jobs/runnable_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jobs
import (
"context"
"fmt"
"log/slog"
"time"

batchv1 "k8s.io/api/batch/v1"
Expand All @@ -12,7 +13,6 @@ import (
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
)

Expand Down Expand Up @@ -66,10 +66,8 @@ func (r *runnableJob) Run(ctx context.Context) error {
}
switch condition := newJob.Status.Conditions[0]; condition.Type {
case batchv1.JobComplete:
klog.V(3).Infof("Stopping runnable job on task completion with status: %s", batchv1.JobComplete)
complete <- nil
case batchv1.JobFailed:
klog.V(3).Infof("Stopping runnable job on task failure with status: %s", batchv1.JobFailed)
complete <- fmt.Errorf("job failed: %s: %s", condition.Reason, condition.Message)
}
},
Expand All @@ -85,10 +83,6 @@ func (r *runnableJob) Run(ctx context.Context) error {
return
}

if event.Type == corev1.EventTypeNormal {
klog.V(3).Infof("Event: %s (%s)", event.Message, event.Reason)
}

if event.Type == corev1.EventTypeWarning {
complete <- fmt.Errorf("warning event received: %s (%s)", event.Message, event.Reason)
return
Expand All @@ -113,14 +107,13 @@ func (r *runnableJob) Run(ctx context.Context) error {
func (r *runnableJob) logTerminatedContainersErrors(ctx context.Context) {
statuses, err := r.logsReader.GetTerminatedContainersStatusesByJob(ctx, r.job)
if err != nil {
klog.Errorf("Error while getting terminated containers statuses for job %q", r.job.Namespace+"/"+r.job.Name)
slog.Error(fmt.Sprintf("Error while getting terminated containers statuses for job %q", r.job.Namespace+"/"+r.job.Name))
}

for container, status := range statuses {
for _, status := range statuses {
if status.ExitCode == 0 {
continue
}
klog.Errorf("Container %s terminated with %s: %s", container, status.Reason, status.Message)
}
}

Expand Down
5 changes: 0 additions & 5 deletions pkg/jobs/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"errors"
"time"

"k8s.io/klog/v2"
)

// ErrTimeout is returned when Runner's Run method fails due to a timeout event.
Expand Down Expand Up @@ -84,15 +82,12 @@ func (r *runner) runWithTimeout(ctx context.Context) error {
ctx, cancel = context.WithTimeout(ctx, r.timeoutDuration)
defer cancel()
}
klog.V(3).Infof("Running task with timeout: %v", r.timeoutDuration)
select {
// Signaled when processing is done.
case err := <-r.complete:
klog.V(3).Infof("Stopping runner on task completion with error: %v", err)
return err
// Signaled when we run out of time.
case <-ctx.Done():
klog.V(3).Info("Stopping runner on timeout")
return ErrTimeout
}
}
3 changes: 3 additions & 0 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package k8s
import (
"context"
"fmt"
"log/slog"
"strings"

"github.com/aquasecurity/trivy-kubernetes/pkg/bom"
Expand Down Expand Up @@ -473,6 +474,7 @@ func PodInfo(pod corev1.Pod, labelSelector string) (*bom.Component, error) {
for _, s := range pod.Status.ContainerStatuses {
imageName, err := utils.ParseReference(s.Image)
if err != nil {
slog.Warn(fmt.Sprintf("unable to parse image reference, skipping: %s", s.Image))
continue
}
imageID := getImageID(s.ImageID, s.Image)
Expand All @@ -481,6 +483,7 @@ func PodInfo(pod corev1.Pod, labelSelector string) (*bom.Component, error) {
}
imageRef, err := utils.ParseReference(imageID)
if err != nil {
slog.Warn(fmt.Sprintf("unable to parse image reference, skipping: %s", s.Image))
continue
}
co, err := GetContainer(imageRef, imageName)
Expand Down
Loading