Skip to content

Commit 64d7d25

Browse files
J12934Ilyesbdlala
authored andcommitted
Rewrite job fetching logic to fetch the scan job based on labels and owner references
Signed-off-by: Jannik Hollenbach <jannik.hollenbach@iteratec.com>
1 parent b93ff2a commit 64d7d25

1 file changed

Lines changed: 41 additions & 33 deletions

File tree

scbctl/cmd/scans.go

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"github.com/spf13/cobra"
1919
metav2 "k8s.io/apimachinery/pkg/api/errors"
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/labels"
22+
"k8s.io/apimachinery/pkg/types"
2123
)
2224

2325
func NewScanCommand() *cobra.Command {
@@ -132,55 +134,39 @@ func NewScanCommand() *cobra.Command {
132134
}
133135

134136
func followScanLogs(ctx context.Context, kubeclient client.Client, namespace, scanName string) error {
135-
// Find the job associated with the scan
136-
jobList := &batchv1.JobList{}
137-
labelSelector := client.MatchingLabels{
138-
"securecodebox.io/job-type": "scanner",
139-
}
140-
141-
fmt.Println("Listing jobs in namespace:", namespace)
137+
fmt.Println("⏰ Waiting for scan job to start...")
138+
const recheckInterval = 250 * time.Millisecond
142139

143140
for {
144-
fmt.Println("Attempting to list jobs...")
145-
err := kubeclient.List(ctx, jobList, client.InNamespace(namespace), labelSelector)
141+
scan := &v1.Scan{}
142+
err := kubeclient.Get(ctx, types.NamespacedName{Name: scanName, Namespace: namespace}, scan)
146143
if err != nil {
147-
return fmt.Errorf("error listing jobs: %s", err)
144+
return fmt.Errorf("error getting scan: %s", err)
148145
}
149146

150-
if len(jobList.Items) == 0 {
151-
fmt.Println("No jobs found, retrying...")
152-
time.Sleep(2 * time.Second)
153-
continue
147+
jobs, err := getJobsForScanOfType(ctx, kubeclient, scan, "scanner")
148+
if err != nil {
149+
return fmt.Errorf("error getting jobs for scan: %s", err)
154150
}
155151

156-
fmt.Printf("Found %d job(s)\n", len(jobList.Items))
157-
158-
for _, j := range jobList.Items {
159-
fmt.Printf("Job: %s, Labels: %v\n", j.Name, j.Labels)
152+
if len(jobs) == 0 {
153+
time.Sleep(recheckInterval)
154+
continue
160155
}
161156

162-
var job *batchv1.Job
163-
for _, j := range jobList.Items {
164-
fmt.Printf(j.Name)
165-
if strings.HasPrefix(j.Name, fmt.Sprintf("scan-%s", scanName)) {
166-
job = &j
167-
break
168-
}
169-
}
157+
job := jobs[0]
170158

171-
if job == nil {
172-
fmt.Println("No matching job found, retrying...")
173-
time.Sleep(2 * time.Second)
159+
// check if job has started or completed yet (checking for completion in case it finished before we got the ready state)
160+
if job.Status.CompletionTime == nil && (job.Status.Ready == nil || (*job.Status.Ready == 0)) {
161+
time.Sleep(recheckInterval)
174162
continue
175163
}
176164

177-
jobName := job.Name
178165
containerName := scanName // Assuming container name matches scan name
179166

180-
fmt.Printf("📡 Streaming logs for job '%s' and container '%s'\n", jobName, containerName)
167+
fmt.Printf("📡 Streaming logs for job '%s' and container '%s'\n", job.Name, containerName)
181168

182-
// Execute kubectl logs command
183-
cmd := exec.CommandContext(ctx, "kubectl", "logs", fmt.Sprintf("job/%s", jobName), containerName, "--follow", "-n", namespace)
169+
cmd := exec.CommandContext(ctx, "kubectl", "logs", fmt.Sprintf("job/%s", job.Name), containerName, "--follow", "--namespace", namespace)
184170
cmd.Stdout = os.Stdout
185171
cmd.Stderr = os.Stderr
186172

@@ -193,3 +179,25 @@ func followScanLogs(ctx context.Context, kubeclient client.Client, namespace, sc
193179

194180
return nil
195181
}
182+
183+
func getJobsForScanOfType(ctx context.Context, kubeclient client.Client, scan *v1.Scan, jobType string) ([]batchv1.Job, error) {
184+
var jobs []batchv1.Job
185+
186+
scanJobs := &batchv1.JobList{}
187+
err := kubeclient.List(ctx, scanJobs, &client.ListOptions{
188+
LabelSelector: labels.SelectorFromSet(map[string]string{"securecodebox.io/job-type": jobType}),
189+
})
190+
if err != nil {
191+
return []batchv1.Job{}, fmt.Errorf("error fetching jobs: %s", err)
192+
}
193+
194+
for _, job := range scanJobs.Items {
195+
for _, jobOwnerReference := range job.GetOwnerReferences() {
196+
if jobOwnerReference.UID == scan.GetUID() {
197+
jobs = append(jobs, job)
198+
}
199+
}
200+
}
201+
202+
return jobs, nil
203+
}

0 commit comments

Comments
 (0)