Skip to content

Commit

Permalink
initialize: wire through new configuration options
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Aug 31, 2020
1 parent 28957dc commit 98c8ffd
Showing 1 changed file with 79 additions and 12 deletions.
91 changes: 79 additions & 12 deletions initialize/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import (
notifier "github.com/quay/clair/v4/notifier/service"
"github.com/quay/claircore/libindex"
"github.com/quay/claircore/libvuln"
"github.com/quay/claircore/libvuln/driver"
"github.com/rs/zerolog"

clairerror "github.com/quay/clair/v4/clair-error"
"github.com/quay/clair/v4/config"
"github.com/quay/clair/v4/httptransport/client"
)

const (
// DefaultUpdatePeriod is the default period used for running updaters
// within matcher processes.
DefaultUpdatePeriod = 30 * time.Minute
)

// Services will initialize the correct ClairCore services
// dependent on operation mode.
//
Expand All @@ -25,20 +32,50 @@ func (i *Init) Services() error {
switch i.conf.Mode {
case config.ComboMode:
// configure two local services via claircore libraries
libI, err := libindex.New(i.GlobalCTX, &libindex.Opts{
opts := libindex.Opts{
ConnString: i.conf.Indexer.ConnString,
ScanLockRetry: time.Duration(i.conf.Indexer.ScanLockRetry) * time.Second,
LayerScanConcurrency: i.conf.Indexer.LayerScanConcurrency,
Migrations: i.conf.Indexer.Migrations,
})
Airgap: i.conf.Indexer.Airgap,
}
if i.conf.Indexer.Scanner.Package != nil {
opts.ScannerConfig.Package = make(map[string]func(interface{}) error, len(i.conf.Indexer.Scanner.Package))
for name, node := range i.conf.Indexer.Scanner.Package {
opts.ScannerConfig.Package[name] = node.Decode
}
}
if i.conf.Indexer.Scanner.Dist != nil {
opts.ScannerConfig.Dist = make(map[string]func(interface{}) error, len(i.conf.Indexer.Scanner.Dist))
for name, node := range i.conf.Indexer.Scanner.Dist {
opts.ScannerConfig.Dist[name] = node.Decode
}
}
if i.conf.Indexer.Scanner.Repo != nil {
opts.ScannerConfig.Repo = make(map[string]func(interface{}) error, len(i.conf.Indexer.Scanner.Repo))
for name, node := range i.conf.Indexer.Scanner.Repo {
opts.ScannerConfig.Repo[name] = node.Decode
}
}
libI, err := libindex.New(i.GlobalCTX, &opts)
if err != nil {
return clairerror.ErrNotInitialized{"failed to initialize libindex: " + err.Error()}
}
per := DefaultUpdatePeriod
if p := i.conf.Matcher.Period; p != nil {
per = *p
}
updaterConfigs := make(map[string]driver.ConfigUnmarshaler)
for name, node := range i.conf.Updaters.Config {
updaterConfigs[name] = node.Decode
}
libV, err := libvuln.New(i.GlobalCTX, &libvuln.Opts{
MaxConnPool: int32(i.conf.Matcher.MaxConnPool),
ConnString: i.conf.Matcher.ConnString,
Migrations: i.conf.Matcher.Migrations,
UpdaterSets: i.conf.Matcher.UpdaterSets,
MaxConnPool: int32(i.conf.Matcher.MaxConnPool),
ConnString: i.conf.Matcher.ConnString,
Migrations: i.conf.Matcher.Migrations,
UpdaterSets: i.conf.Updaters.Sets,
UpdateInterval: per,
UpdaterConfigs: updaterConfigs,
})
if err != nil {
return fmt.Errorf("failed to initialize libvuln: %v", err)
Expand Down Expand Up @@ -80,24 +117,54 @@ func (i *Init) Services() error {
i.Notifier = n
case config.IndexerMode:
// configure just a local indexer
libI, err := libindex.New(i.GlobalCTX, &libindex.Opts{
opts := libindex.Opts{
ConnString: i.conf.Indexer.ConnString,
ScanLockRetry: time.Duration(i.conf.Indexer.ScanLockRetry) * time.Second,
LayerScanConcurrency: i.conf.Indexer.LayerScanConcurrency,
Migrations: i.conf.Indexer.Migrations,
})
Airgap: i.conf.Indexer.Airgap,
}
if i.conf.Indexer.Scanner.Package != nil {
opts.ScannerConfig.Package = make(map[string]func(interface{}) error, len(i.conf.Indexer.Scanner.Package))
for name, node := range i.conf.Indexer.Scanner.Package {
opts.ScannerConfig.Package[name] = node.Decode
}
}
if i.conf.Indexer.Scanner.Dist != nil {
opts.ScannerConfig.Dist = make(map[string]func(interface{}) error, len(i.conf.Indexer.Scanner.Dist))
for name, node := range i.conf.Indexer.Scanner.Dist {
opts.ScannerConfig.Dist[name] = node.Decode
}
}
if i.conf.Indexer.Scanner.Repo != nil {
opts.ScannerConfig.Repo = make(map[string]func(interface{}) error, len(i.conf.Indexer.Scanner.Repo))
for name, node := range i.conf.Indexer.Scanner.Repo {
opts.ScannerConfig.Repo[name] = node.Decode
}
}
libI, err := libindex.New(i.GlobalCTX, &opts)
if err != nil {
return clairerror.ErrNotInitialized{"failed to initialize libindex: " + err.Error()}
}
i.Indexer = libI
i.Matcher = nil
case config.MatcherMode:
per := DefaultUpdatePeriod
if p := i.conf.Matcher.Period; p != nil {
per = *p
}
updaterConfigs := make(map[string]driver.ConfigUnmarshaler)
for name, node := range i.conf.Updaters.Config {
updaterConfigs[name] = node.Decode
}
// configure a local matcher but a remote indexer
libV, err := libvuln.New(i.GlobalCTX, &libvuln.Opts{
MaxConnPool: int32(i.conf.Matcher.MaxConnPool),
ConnString: i.conf.Matcher.ConnString,
Migrations: i.conf.Matcher.Migrations,
UpdaterSets: i.conf.Matcher.UpdaterSets,
MaxConnPool: int32(i.conf.Matcher.MaxConnPool),
ConnString: i.conf.Matcher.ConnString,
Migrations: i.conf.Matcher.Migrations,
UpdaterSets: i.conf.Updaters.Sets,
UpdateInterval: per,
UpdaterConfigs: updaterConfigs,
})
if err != nil {
return fmt.Errorf("failed to initialize libvuln: %v", err)
Expand Down

0 comments on commit 98c8ffd

Please sign in to comment.