Skip to content

Commit

Permalink
config: validate based on combo mode or not
Browse files Browse the repository at this point in the history
Fixes PROJQUAY-1910

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay authored and ldelossa committed Apr 22, 2021
1 parent cc4a10f commit 1f9b565
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
14 changes: 7 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,32 @@ type Config struct {
}

// Validate confirms the necessary values to support
// the desired Clair Mode exist
// the desired Clair mode exist.
func Validate(conf *Config) error {
if conf.HTTPListenAddr == "" {
conf.HTTPListenAddr = DefaultAddress
}
switch strings.ToLower(conf.Mode) {
case ComboMode:
if err := conf.Indexer.Validate(); err != nil {
if err := conf.Indexer.Validate(true); err != nil {
return err
}
if err := conf.Matcher.Validate(); err != nil {
if err := conf.Matcher.Validate(true); err != nil {
return err
}
if err := conf.Notifier.Validate(); err != nil {
if err := conf.Notifier.Validate(true); err != nil {
return err
}
case IndexerMode:
if err := conf.Indexer.Validate(); err != nil {
if err := conf.Indexer.Validate(false); err != nil {
return err
}
case MatcherMode:
if err := conf.Matcher.Validate(); err != nil {
if err := conf.Matcher.Validate(false); err != nil {
return err
}
case NotifierMode:
if err := conf.Notifier.Validate(); err != nil {
if err := conf.Notifier.Validate(false); err != nil {
return err
}
default:
Expand Down
4 changes: 2 additions & 2 deletions config/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ type Indexer struct {
Airgap bool `yaml:"airgap" json:"airgap"`
}

func (i *Indexer) Validate() error {
func (i *Indexer) Validate(combo bool) error {
const (
DefaultScanLockRetry = 1
)
if i.ConnString == "" {
return fmt.Errorf("indexer mode requires a database connection string")
}
if i.ScanLockRetry == 0 {
i.ScanLockRetry = 1
i.ScanLockRetry = DefaultScanLockRetry
}
return nil
}
Expand Down
21 changes: 11 additions & 10 deletions config/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,28 @@ type Matcher struct {
UpdateRetention int `yaml:"update_retention" json:"update_retention"`
}

func (m *Matcher) Validate() error {
func (m *Matcher) Validate(combo bool) error {
const (
DefaultPeriod = 30 * time.Minute
DefaultRetention = 10
)
if m.ConnString == "" {
return fmt.Errorf("matcher requies a database connection string")
}
if m.IndexerAddr == "" {
return fmt.Errorf("matcher mode requires a remote Indexer address")
}

_, err := url.Parse(m.IndexerAddr)
if err != nil {
return fmt.Errorf("failed to url parse matcher mode IndexAddr string: %v", err)
return fmt.Errorf("matcher requires a database connection string")
}
if m.Period == 0 {
m.Period = DefaultPeriod
}
if m.UpdateRetention == 1 || m.UpdateRetention < 0 {
m.UpdateRetention = DefaultRetention
}
if !combo {
if m.IndexerAddr == "" {
return fmt.Errorf("matcher mode requires a remote Indexer address")
}
_, err := url.Parse(m.IndexerAddr)
if err != nil {
return fmt.Errorf("failed to parse matcher mode IndexerAddr string: %v", err)
}
}
return nil
}
16 changes: 9 additions & 7 deletions config/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,27 @@ type Notifier struct {
STOMP *stomp.Config `yaml:"stomp" json:"stomp"`
}

func (n *Notifier) Validate() error {
func (n *Notifier) Validate(combo bool) error {
const (
DefaultPollInterval = 5 * time.Second
DefaultDeliveryInterval = 5 * time.Second
)
if n.ConnString == "" {
return fmt.Errorf("notifier mode requires a database connection string")
}
if n.IndexerAddr == "" {
return fmt.Errorf("notifier mode requires a remote Indexer")
}
if n.MatcherAddr == "" {
return fmt.Errorf("notifier mode requires a remote Matcher")
}
if n.PollInterval < 1*time.Second {
n.PollInterval = DefaultPollInterval
}
if n.DeliveryInterval < 1*time.Second {
n.DeliveryInterval = DefaultDeliveryInterval
}
if !combo {
if n.IndexerAddr == "" {
return fmt.Errorf("notifier mode requires a remote Indexer")
}
if n.MatcherAddr == "" {
return fmt.Errorf("notifier mode requires a remote Matcher")
}
}
return nil
}

0 comments on commit 1f9b565

Please sign in to comment.