Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
fishkerez committed Jun 28, 2023
1 parent 4d832a6 commit db822b3
Show file tree
Hide file tree
Showing 10 changed files with 489 additions and 217 deletions.
1 change: 1 addition & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,7 @@ components:
name:
type: string
zones:
example: 'us-central1-a'
type: array
items:
type: string
Expand Down
201 changes: 101 additions & 100 deletions api/server/server.gen.go

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions runtime_scan/pkg/orchestrator/scanconfigwatcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func (w *Watcher) reconcileUnscheduled(ctx context.Context, scanConfig *models.S
}

func (w *Watcher) reconcileDue(ctx context.Context, scanConfig *models.ScanConfig, schedule *ScanConfigSchedule) error {
// TODO createScanIfNotExist
if err := w.createScan(ctx, scanConfig); err != nil {
return fmt.Errorf("failed to reconcile new Scan for ScanConfig. ScanConfigID=%s: %w", *scanConfig.Id, err)
}
Expand Down Expand Up @@ -199,7 +198,6 @@ func (w *Watcher) createScan(ctx context.Context, scanConfig *models.ScanConfig)
Count: utils.PointerTo(true),
})
if err != nil || scans == nil {
// TODO not found error is also an error here?
return fmt.Errorf("failed to fetch scans for ScanConfig. ScanConfigID=%s: %w", *scanConfig.Id, err)
}

Expand Down
2 changes: 1 addition & 1 deletion runtime_scan/pkg/provider/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ func (c *Client) RemoveTargetScan(ctx context.Context, config *provider.ScanJobC
func (c *Client) GetInstances(ctx context.Context, filters []ec2types.Filter, excludeTags []models.Tag, regionID string) ([]Instance, error) {
ret := make([]Instance, 0)

out, err := c.ec2Client.DescribeInstances(ctx, &ec2.DescribeInstancesInput {
out, err := c.ec2Client.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
Filters: filters,
MaxResults: utils.PointerTo[int32](maxResults), // TODO what will be a good number?
}, func(options *ec2.Options) {
Expand Down
53 changes: 22 additions & 31 deletions runtime_scan/pkg/provider/gcp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import (

compute "cloud.google.com/go/compute/apiv1"
"cloud.google.com/go/compute/apiv1/computepb"
// "github.com/google/martian/log"
"github.com/sirupsen/logrus"
"google.golang.org/api/iterator"

"github.com/openclarity/vmclarity/api/models"
"github.com/openclarity/vmclarity/runtime_scan/pkg/provider"
log2 "github.com/openclarity/vmclarity/shared/pkg/log"
"github.com/openclarity/vmclarity/shared/pkg/log"
"github.com/openclarity/vmclarity/shared/pkg/utils"
)

Expand Down Expand Up @@ -96,8 +95,8 @@ func (c *Client) RunTargetScan(ctx context.Context, config *provider.ScanJobConf
return provider.FatalErrorf("unable to get vminfo from target: %w", err)
}

logger := log2.GetLoggerFromContextOrDefault(ctx).WithFields(logrus.Fields{
"scanResultID": config.ScanResultID,
logger := log.GetLoggerFromContextOrDefault(ctx).WithFields(logrus.Fields{
"ScanResultID": config.ScanResultID,
"TargetLocation": vminfo.Location,
"InstanceID": vminfo.InstanceID,
"ScannerZone": c.gcpConfig.ScannerZone,
Expand Down Expand Up @@ -161,7 +160,7 @@ func (c *Client) RunTargetScan(ctx context.Context, config *provider.ScanJobConf
}

func (c *Client) RemoveTargetScan(ctx context.Context, config *provider.ScanJobConfig) error {
logger := log2.GetLoggerFromContextOrDefault(ctx).WithFields(logrus.Fields{
logger := log.GetLoggerFromContextOrDefault(ctx).WithFields(logrus.Fields{
"scanResultID": config.ScanResultID,
"ScannerZone": c.gcpConfig.ScannerZone,
"Provider": string(c.Kind()),
Expand Down Expand Up @@ -379,18 +378,7 @@ func hasIncludeTags(vm *computepb.Instance, tags *[]models.Tag) bool {
return false
}

instanceTags := convertTagsToMap(vm.Tags)

for _, tag := range *tags {
val, ok := instanceTags[tag.Key]
if !ok {
return false
}
if !(strings.Compare(val, tag.Value) == 0) {
return false
}
}
return true
return hasTags(vm.Tags, tags)
}

// AND logic - if tags = {tag1:val1, tag2:val2},
Expand All @@ -406,9 +394,13 @@ func hasExcludeTags(vm *computepb.Instance, tags *[]models.Tag) bool {
return false
}

instanceTags := convertTagsToMap(vm.Tags)
return hasTags(vm.Tags, tags)
}

func hasTags(vmTags *computepb.Tags, modelsTags *[]models.Tag) bool {
instanceTags := convertTagsToMap(vmTags)

for _, tag := range *tags {
for _, tag := range *modelsTags {
val, ok := instanceTags[tag.Key]
if !ok {
return false
Expand Down Expand Up @@ -490,12 +482,7 @@ func (c *Client) getVMInfoFromVirtualMachine(vm *computepb.Instance) (models.Tar
func convertTags(tags *computepb.Tags) *[]models.Tag {
ret := make([]models.Tag, 0, len(tags.Items))
for _, item := range tags.Items {
spl := strings.Split(item, "=")
var key, val string
key = spl[0]
if len(spl) > 1 {
val = spl[1]
}
key, val := getKeyValue(item)
ret = append(ret, models.Tag{
Key: key,
Value: val,
Expand All @@ -507,13 +494,17 @@ func convertTags(tags *computepb.Tags) *[]models.Tag {
func convertTagsToMap(tags *computepb.Tags) map[string]string {
ret := make(map[string]string, len(tags.Items))
for _, item := range tags.Items {
spl := strings.Split(item, "=")
var key, val string
key = spl[0]
if len(spl) > 1 {
val = spl[1]
}
key, val := getKeyValue(item)
ret[key] = val
}
return ret
}

func getKeyValue(str string) (key, value string) {
spl := strings.Split(str, "=")
key = spl[0]
if len(spl) > 1 {
value = spl[1]
}
return
}
Loading

0 comments on commit db822b3

Please sign in to comment.