Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): Limit the number of concurrent scan jobs (#232)
Resolves: #202 Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>
- Loading branch information
1 parent
cb78a84
commit 50d179c
Showing
13 changed files
with
427 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/aquasecurity/starboard/pkg/kube" | ||
"github.com/aquasecurity/starboard/pkg/operator/etc" | ||
"github.com/aquasecurity/starboard/pkg/vulnerabilityreport" | ||
batchv1 "k8s.io/api/batch/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type Analyzer interface { | ||
HasVulnerabilityReports(ctx context.Context, owner kube.Object, images kube.ContainerImages, hash string) (bool, error) | ||
GetActiveScanJob(ctx context.Context, owner kube.Object, hash string) (*batchv1.Job, error) | ||
IsConcurrentScanJobsLimitExceeded(ctx context.Context) (bool, int, error) | ||
} | ||
|
||
func NewAnalyzer(config etc.Operator, store vulnerabilityreport.StoreInterface, client client.Client) Analyzer { | ||
return &analyzer{ | ||
config: config, | ||
store: store, | ||
client: client, | ||
} | ||
} | ||
|
||
type analyzer struct { | ||
config etc.Operator | ||
client client.Client | ||
store vulnerabilityreport.StoreInterface | ||
} | ||
|
||
func (a *analyzer) HasVulnerabilityReports(ctx context.Context, owner kube.Object, images kube.ContainerImages, hash string) (bool, error) { | ||
list, err := a.store.FindByOwner(ctx, owner) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
actual := map[string]bool{} | ||
for _, report := range list { | ||
if containerName, ok := report.Labels[kube.LabelContainerName]; ok { | ||
if hash == report.Labels[kube.LabelPodSpecHash] { | ||
actual[containerName] = true | ||
} | ||
} | ||
} | ||
|
||
expected := map[string]bool{} | ||
for containerName, _ := range images { | ||
expected[containerName] = true | ||
} | ||
|
||
return reflect.DeepEqual(actual, expected), nil | ||
} | ||
|
||
func (a *analyzer) GetActiveScanJob(ctx context.Context, owner kube.Object, hash string) (*batchv1.Job, error) { | ||
jobList := &batchv1.JobList{} | ||
err := a.client.List(ctx, jobList, client.MatchingLabels{ | ||
kube.LabelResourceNamespace: owner.Namespace, | ||
kube.LabelResourceKind: string(owner.Kind), | ||
kube.LabelResourceName: owner.Name, | ||
kube.LabelPodSpecHash: hash, | ||
}, client.InNamespace(a.config.Namespace)) | ||
if err != nil { | ||
return nil, fmt.Errorf("listing scan jobs: %w", err) | ||
} | ||
if len(jobList.Items) > 0 { | ||
return jobList.Items[0].DeepCopy(), nil | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (a *analyzer) IsConcurrentScanJobsLimitExceeded(ctx context.Context) (bool, int, error) { | ||
scanJobsCount, err := a.countScanJobs(ctx, a.config.Namespace) | ||
if err != nil { | ||
return false, 0, err | ||
} | ||
|
||
return scanJobsCount >= a.config.ConcurrentScanJobsLimit, scanJobsCount, nil | ||
} | ||
|
||
func (a *analyzer) countScanJobs(ctx context.Context, namespace string) (int, error) { | ||
var scanJobs batchv1.JobList | ||
err := a.client.List(ctx, &scanJobs, client.MatchingLabels{ | ||
"app.kubernetes.io/managed-by": "starboard-operator", | ||
}, client.InNamespace(namespace)) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return len(scanJobs.Items), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package controller_test |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.