From 69970ea1cc561c98e3ca58d24b4f54e6f8ac282c Mon Sep 17 00:00:00 2001 From: chenk Date: Thu, 18 Jan 2024 09:06:22 +0200 Subject: [PATCH 1/3] fix: add logging un-parsed resource Signed-off-by: chenk --- .github/workflows/build.yml | 2 +- .github/workflows/release.yaml | 2 +- go.mod | 2 +- pkg/artifacts/artifacts.go | 4 ++++ pkg/jobs/runnable_job.go | 12 ++---------- pkg/jobs/runner.go | 5 ----- pkg/k8s/k8s.go | 3 +++ 7 files changed, 12 insertions(+), 18 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8b4c56a..b59085d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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: {} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index f4534b4..eac0354 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -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 diff --git a/go.mod b/go.mod index 86ca141..82ad687 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ( diff --git a/pkg/artifacts/artifacts.go b/pkg/artifacts/artifacts.go index 2d9a785..5e6ae1a 100644 --- a/pkg/artifacts/artifacts.go +++ b/pkg/artifacts/artifacts.go @@ -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" @@ -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 { diff --git a/pkg/jobs/runnable_job.go b/pkg/jobs/runnable_job.go index 4a2e125..adb6364 100644 --- a/pkg/jobs/runnable_job.go +++ b/pkg/jobs/runnable_job.go @@ -12,7 +12,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" ) @@ -66,10 +65,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) } }, @@ -85,10 +82,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 @@ -113,14 +106,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) + fmt.Printf("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) } } diff --git a/pkg/jobs/runner.go b/pkg/jobs/runner.go index b391df5..d718814 100644 --- a/pkg/jobs/runner.go +++ b/pkg/jobs/runner.go @@ -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. @@ -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 } } diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go index 57eb06d..d66c7ec 100644 --- a/pkg/k8s/k8s.go +++ b/pkg/k8s/k8s.go @@ -3,6 +3,7 @@ package k8s import ( "context" "fmt" + "log/slog" "strings" "github.com/aquasecurity/trivy-kubernetes/pkg/bom" @@ -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) @@ -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) From de79a918a1227d18496c73b084b252f3b422b2c8 Mon Sep 17 00:00:00 2001 From: chenk Date: Thu, 18 Jan 2024 09:15:22 +0200 Subject: [PATCH 2/3] fix: add logging un-parsed resource Signed-off-by: chenk --- pkg/jobs/runnable_job.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/jobs/runnable_job.go b/pkg/jobs/runnable_job.go index adb6364..d46f9f0 100644 --- a/pkg/jobs/runnable_job.go +++ b/pkg/jobs/runnable_job.go @@ -3,6 +3,7 @@ package jobs import ( "context" "fmt" + "log/slog" "time" batchv1 "k8s.io/api/batch/v1" @@ -106,7 +107,7 @@ 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 { - fmt.Printf("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 _, status := range statuses { From 369a08e9161046e005a3146cc17fece4ae927f83 Mon Sep 17 00:00:00 2001 From: chenk Date: Thu, 18 Jan 2024 09:19:44 +0200 Subject: [PATCH 3/3] fix: add logging un-parsed resource Signed-off-by: chenk --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b59085d..ab296be 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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