Skip to content

Commit

Permalink
feat(cli): Provide more details when scan job fails (#328)
Browse files Browse the repository at this point in the history
Resolves: #70
  • Loading branch information
danielpacak committed Jan 13, 2021
1 parent 39d80a4 commit 2a490f9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
25 changes: 8 additions & 17 deletions pkg/kube/pod/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import (
"fmt"
"io"

"k8s.io/klog"

"github.com/aquasecurity/starboard/pkg/kube"

apps "k8s.io/api/apps/v1"
batch "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
)

type Manager struct {
Expand Down Expand Up @@ -123,6 +121,9 @@ func (pw *Manager) GetTerminatedContainersStatusesByJob(ctx context.Context, job

func GetTerminatedContainersStatusesByPod(pod *core.Pod) map[string]*core.ContainerStateTerminated {
states := make(map[string]*core.ContainerStateTerminated)
if pod == nil {
return states
}
for _, status := range pod.Status.InitContainerStatuses {
if status.State.Terminated == nil {
continue
Expand Down Expand Up @@ -159,7 +160,10 @@ func (pw *Manager) GetPodByJob(ctx context.Context, job *batch.Job) (*core.Pod,
if err != nil {
return nil, err
}
return &podList.Items[0], nil
if podList != nil && len(podList.Items) > 0 {
return &podList.Items[0], nil
}
return nil, nil
}

func (pw *Manager) GetPodLogs(ctx context.Context, pod *core.Pod, container string) (io.ReadCloser, error) {
Expand All @@ -186,16 +190,3 @@ func (pw *Manager) logTerminatedContainersErrors(statuses map[string]*core.Conta
klog.Errorf("Container %s terminated with %s: %s", container, status.Reason, status.Message)
}
}

// GetImages gets a slice of images for the specified PodSpec.
func GetImages(spec core.PodSpec) (images []string) {
for _, c := range spec.InitContainers {
images = append(images, c.Image)
}

for _, c := range spec.Containers {
images = append(images, c.Image)
}

return
}
29 changes: 26 additions & 3 deletions pkg/kube/runnable_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func (r *runnableJob) Run(ctx context.Context) error {
defaultResyncDuration,
informers.WithNamespace(r.job.Namespace),
)
jobInformer := informerFactory.Batch().V1().Jobs()
jobsInformer := informerFactory.Batch().V1().Jobs()
eventsInformer := informerFactory.Core().V1().Events()

var err error

Expand Down Expand Up @@ -90,8 +91,8 @@ func (r *runnableJob) Run(ctx context.Context) error {

complete := make(chan error)

jobInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj, newObj interface{}) {
jobsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(_, newObj interface{}) {
newJob, ok := newObj.(*batchv1.Job)
if !ok {
return
Expand All @@ -113,6 +114,28 @@ func (r *runnableJob) Run(ctx context.Context) error {
},
})

eventsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
event := obj.(*corev1.Event)

// TODO We might want to look into events associate with the pod controlled by the current scan job.
// For example, when a pod cannot be scheduled due to insufficient resource requests,
// the event will be attached to the pod, not the job.
if event.InvolvedObject.UID != r.job.UID {
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
}
},
})

informerFactory.Start(wait.NeverStop)
informerFactory.WaitForCacheSync(wait.NeverStop)

Expand Down
5 changes: 0 additions & 5 deletions pkg/vulnerabilityreport/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ func (s *Scanner) Scan(ctx context.Context, workload kube.Object) ([]v1alpha1.Vu

klog.V(3).Infof("Scan job completed: %s/%s", job.Namespace, job.Name)

job, err = s.clientset.BatchV1().Jobs(job.Namespace).Get(ctx, job.Name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("getting scan job: %w", err)
}

return s.getVulnerabilityReportsByScanJob(ctx, job, owner)
}

Expand Down

0 comments on commit 2a490f9

Please sign in to comment.