Skip to content

Commit a6914e7

Browse files
authored
Merge pull request #8 from danielpacak/issue_7_async_processing
feat: Process scan requests asynchronously
2 parents 9c4b36c + 871e289 commit a6914e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1671
-704
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ install:
1717
script:
1818
- make lint
1919
- make test
20+
- make test-integration
2021
- curl -sL https://git.io/goreleaser | bash -s -- --snapshot --skip-publish --rm-dist
2122
after_success:
2223
- bash <(curl -s https://codecov.io/bash)

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ lint:
1919
test: $(SOURCES)
2020
GO111MODULE=on go test -v -short -race -timeout 30s -coverprofile=coverage.txt -covermode=atomic ./...
2121

22+
test-integration: build
23+
GO111MODULE=on go test -v -tags=integration ./test/integration/...
24+
2225
container: build
2326
docker build --no-cache -t $(IMAGE) .

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ Configuration of the adapter is done via environment variables at startup.
3131
| `SCANNER_TLS_CLIENTCAS` | | An array of absolute paths to x509 CA files that will be added to host's root CA set. |
3232
| `SCANNER_API_SERVER_READ_TIMEOUT` | `15s` | The maximum duration for reading the entire request, including the body. |
3333
| `SCANNER_API_SERVER_WRITE_TIMEOUT` | `15s` | The maximum duration before timing out writes of the response. |
34+
| `SCANNER_API_SERVER_IDLE_TIMEOUT` | `60s` | The maximum amount of time to wait for the next request when keep-alives are enabled. |
3435
| `SCANNER_CLAIR_URL` | `http://harbor-harbor-clair:6060` | Clair URL |
36+
| `SCANNER_STORE_REDIS_URL` | `redis://harbor-harbor-redis:6379` | Redis server URI for a redis store. |
37+
| `SCANNER_STORE_REDIS_NAMESPACE` | `harbor.scanner.clair:store` | A namespace for keys in a redis store. |
38+
| `SCANNER_STORE_REDIS_POOL_MAX_ACTIVE` | `5` | The max number of connections allocated by the pool for a redis store. |
39+
| `SCANNER_STORE_REDIS_POOL_MAX_IDLE` | `5` | The max number of idle connections in the pool for a redis store. |
40+
| `SCANNER_STORE_REDIS_SCAN_JOB_TTL` | `1h` | The time to live for persisting scan jobs and associated scan reports. |
3541

3642
## Deploy to minikube
3743

cmd/harbor-scanner-clair/main.go

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package main
22

33
import (
44
"context"
5+
"github.com/goharbor/harbor-scanner-clair/pkg/clair"
56
"github.com/goharbor/harbor-scanner-clair/pkg/etc"
67
"github.com/goharbor/harbor-scanner-clair/pkg/http/api"
78
"github.com/goharbor/harbor-scanner-clair/pkg/http/api/v1"
8-
"github.com/goharbor/harbor-scanner-clair/pkg/model"
9+
"github.com/goharbor/harbor-scanner-clair/pkg/persistence/redis"
910
"github.com/goharbor/harbor-scanner-clair/pkg/registry"
10-
"github.com/goharbor/harbor-scanner-clair/pkg/scanner/clair"
11+
"github.com/goharbor/harbor-scanner-clair/pkg/scanner"
12+
"github.com/goharbor/harbor-scanner-clair/pkg/work"
1113
log "github.com/sirupsen/logrus"
12-
"net/http"
1314
"os"
1415
"os/signal"
1516
"syscall"
@@ -28,55 +29,46 @@ func main() {
2829
log.SetReportCaller(false)
2930
log.SetFormatter(&log.JSONFormatter{})
3031

32+
config, err := etc.GetConfig()
33+
if err != nil {
34+
log.Fatalf("Error: %v", err)
35+
}
36+
37+
store := redis.NewStore(config.Store)
38+
39+
workPool := work.New()
40+
3141
log.WithFields(log.Fields{
3242
"version": version,
3343
"commit": commit,
3444
"built_at": date,
3545
}).Info("Starting harbor-scanner-clair")
3646

37-
clairConfig, err := etc.GetClairConfig()
38-
if err != nil {
39-
log.Fatalf("Error: %v", err)
40-
}
47+
registryClientFactory := registry.NewClientFactory(config.TLS)
48+
clairClient := clair.NewClient(config.TLS, config.Clair)
49+
adapter := scanner.NewAdapter(registryClientFactory, clairClient, scanner.NewTransformer())
4150

42-
tlsConfig, err := etc.GetTLSConfig()
43-
if err != nil {
44-
log.Fatalf("Error: %v", err)
45-
}
46-
47-
registryClientFactory := registry.NewClientFactory(tlsConfig)
48-
clairClient := clair.NewClient(tlsConfig, clairConfig)
49-
scanner := clair.NewScanner(registryClientFactory, clairClient, model.NewTransformer())
50-
51-
apiConfig, err := etc.GetAPIConfig()
52-
if err != nil {
53-
log.Fatalf("Error: %v", err)
54-
}
51+
enqueuer := scanner.NewEnqueuer(workPool, adapter, store)
5552

56-
apiHandler := v1.NewAPIHandler(scanner)
53+
apiHandler := v1.NewAPIHandler(enqueuer, store)
5754

58-
server := api.NewServer(apiConfig, apiHandler)
55+
apiServer := api.NewServer(config.API, apiHandler)
5956

6057
shutdownComplete := make(chan struct{})
6158
go func() {
6259
sigint := make(chan os.Signal, 1)
63-
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
60+
signal.Notify(sigint, syscall.SIGINT, syscall.SIGTERM)
6461
captured := <-sigint
6562
log.WithField("signal", captured.String()).Debug("Trapped os signal")
6663

67-
log.Debug("API server shutdown started")
68-
if err := server.Shutdown(context.Background()); err != nil {
69-
log.WithError(err).Error("Error while shutting down server")
70-
}
71-
log.Debug("API server shutdown completed")
64+
apiServer.Shutdown(context.Background())
65+
workPool.Shutdown()
66+
7267
close(shutdownComplete)
7368
}()
7469

75-
go func() {
76-
if err = server.ListenAndServe(); err != http.ErrServerClosed {
77-
log.Fatalf("Error: %v", err)
78-
}
79-
log.Debug("ListenAndServe returned")
80-
}()
70+
workPool.Start()
71+
apiServer.ListenAndServe()
72+
8173
<-shutdownComplete
8274
}

go.mod

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ module github.com/goharbor/harbor-scanner-clair
33
go 1.13
44

55
require (
6+
github.com/Microsoft/hcsshim v0.8.6 // indirect
67
github.com/caarlos0/env/v6 v6.0.0
78
github.com/docker/distribution v2.7.1+incompatible
9+
github.com/gomodule/redigo v2.0.0+incompatible
10+
github.com/google/uuid v1.1.1
811
github.com/gorilla/mux v1.7.2
9-
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
10-
github.com/opencontainers/image-spec v1.0.1 // indirect
12+
github.com/prometheus/client_golang v1.2.1
1113
github.com/sirupsen/logrus v1.4.2
1214
github.com/stretchr/testify v1.3.0
15+
github.com/testcontainers/testcontainers-go v0.0.8
16+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898
1317
)

0 commit comments

Comments
 (0)