From 1f9b56577957ce28044b221af58b160328a671a2 Mon Sep 17 00:00:00 2001 From: Hank Donnay Date: Thu, 22 Apr 2021 09:19:49 -0500 Subject: [PATCH] config: validate based on combo mode or not Fixes PROJQUAY-1910 Signed-off-by: Hank Donnay --- config/config.go | 14 +++++++------- config/indexer.go | 4 ++-- config/matcher.go | 21 +++++++++++---------- config/notifier.go | 16 +++++++++------- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/config/config.go b/config/config.go index 777baa8c1f..495694fad7 100644 --- a/config/config.go +++ b/config/config.go @@ -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: diff --git a/config/indexer.go b/config/indexer.go index 72301e4430..fc1da5d933 100644 --- a/config/indexer.go +++ b/config/indexer.go @@ -37,7 +37,7 @@ type Indexer struct { Airgap bool `yaml:"airgap" json:"airgap"` } -func (i *Indexer) Validate() error { +func (i *Indexer) Validate(combo bool) error { const ( DefaultScanLockRetry = 1 ) @@ -45,7 +45,7 @@ func (i *Indexer) Validate() error { return fmt.Errorf("indexer mode requires a database connection string") } if i.ScanLockRetry == 0 { - i.ScanLockRetry = 1 + i.ScanLockRetry = DefaultScanLockRetry } return nil } diff --git a/config/matcher.go b/config/matcher.go index 0fce28829d..3e0c27d8cf 100644 --- a/config/matcher.go +++ b/config/matcher.go @@ -48,21 +48,13 @@ 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 @@ -70,5 +62,14 @@ func (m *Matcher) Validate() error { 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 } diff --git a/config/notifier.go b/config/notifier.go index 5fce102ccb..19dca371fa 100644 --- a/config/notifier.go +++ b/config/notifier.go @@ -65,7 +65,7 @@ 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 @@ -73,17 +73,19 @@ func (n *Notifier) Validate() error { 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 }