Skip to content

Commit

Permalink
config: struct ordering and simplifications
Browse files Browse the repository at this point in the history
This makes struct-size gains where the proper ordering was easy to spot.

Also fixes some test using `log.Fatal` instead of `t.Fatal`.

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Nov 3, 2021
1 parent 579719a commit eb64aa5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 54 deletions.
25 changes: 12 additions & 13 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config_test

import (
"log"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -10,10 +9,10 @@ import (
"github.com/quay/clair/v4/config"
)

func Test_Config_Validate_Failure(t *testing.T) {
var table = []struct {
name string
func TestConfigValidateFailure(t *testing.T) {
table := []struct {
conf config.Config
name string
}{
{
name: "No Mode",
Expand Down Expand Up @@ -64,16 +63,16 @@ func Test_Config_Validate_Failure(t *testing.T) {
for _, tab := range table {
t.Run(tab.name, func(t *testing.T) {
if err := config.Validate(&tab.conf); err == nil {
log.Fatalf("expected error for test case: %s", tab.name)
t.Fatalf("expected error for test case: %s", tab.name)
}
})
}
}

func TestConfigUpateRetention(t *testing.T) {
var table = []struct {
name string
table := []struct {
conf config.Config
name string
expectedRetention int
}{
{
Expand Down Expand Up @@ -157,7 +156,7 @@ func TestConfigUpateRetention(t *testing.T) {
t.Run(tab.name, func(t *testing.T) {
err := config.Validate(&tab.conf)
if err != nil {
log.Fatalf("expected no errors but got: %s, for test case: %s", err, tab.name)
t.Fatalf("expected no errors but got: %s, for test case: %s", err, tab.name)
}
if tab.conf.Matcher.UpdateRetention != tab.expectedRetention {
t.Fatalf("expected UpdateRetention of %d but got %d", tab.expectedRetention, tab.conf.Matcher.UpdateRetention)
Expand All @@ -167,7 +166,7 @@ func TestConfigUpateRetention(t *testing.T) {
}

func TestConfigDisableUpdaters(t *testing.T) {
var table = []struct {
table := []struct {
name string
conf config.Config
}{
Expand Down Expand Up @@ -219,10 +218,10 @@ func TestConfigDisableUpdaters(t *testing.T) {
t.Run(tab.name, func(t *testing.T) {
err := config.Validate(&tab.conf)
if err != nil {
log.Fatalf("expected no errors but got: %s, for test case: %s", err, tab.name)
t.Fatalf("expected no errors but got: %s, for test case: %s", err, tab.name)
}
if len(tab.conf.Updaters.Sets) != 0 {
log.Fatalf("expected updaters sets to be empty but was: %s, for test case: %s", tab.conf.Updaters.Sets, tab.name)
t.Fatalf("expected updaters sets to be empty but was: %s, for test case: %s", tab.conf.Updaters.Sets, tab.name)
}
})
}
Expand All @@ -234,7 +233,7 @@ func TestAuthUnmarshal(t *testing.T) {
In string
Want config.AuthPSK
}
var tt = []testcase{
tt := []testcase{
{
In: `---
key: >-
Expand Down Expand Up @@ -268,7 +267,7 @@ iss:
In string
Want config.AuthKeyserver
}
var tt = []testcase{
tt := []testcase{
{
In: `---
api: quay/keys
Expand Down
4 changes: 2 additions & 2 deletions config/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

// Indexer provides Clair Indexer node configuration
type Indexer struct {
// Scanner allows for passing configuration options to layer scanners.
Scanner ScannerConfig `yaml:"scanner" json:"scanner"`
// A Postgres connection string.
//
// formats
Expand Down Expand Up @@ -35,8 +37,6 @@ type Indexer struct {
//
// Whether Indexer nodes handle migrations to their database.
Migrations bool `yaml:"migrations" json:"migrations"`
// Scanner allows for passing configuration options to layer scanners.
Scanner ScannerConfig `yaml:"scanner" json:"scanner"`
// Airgap disables scanners that have signaled they expect to talk to the
// Internet.
Airgap bool `yaml:"airgap" json:"airgap"`
Expand Down
10 changes: 5 additions & 5 deletions config/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ type Trace struct {

// Jager specific distributed tracing configuration.
type Jaeger struct {
Tags map[string]string `yaml:"tags" json:"tags"`
Agent struct {
Endpoint string `yaml:"endpoint" json:"endpoint"`
} `yaml:"agent" json:"agent"`
Collector struct {
Endpoint string `yaml:"endpoint" json:"endpoint"`
Username *string `yaml:"username" json:"username"`
Password *string `yaml:"password" json:"password"`
Endpoint string `yaml:"endpoint" json:"endpoint"`
} `yaml:"collector" json:"collector"`
ServiceName string `yaml:"service_name" json:"service_name"`
Tags map[string]string `yaml:"tags" json:"tags"`
BufferMax int `yaml:"buffer_max" json:"buffer_max"`
ServiceName string `yaml:"service_name" json:"service_name"`
BufferMax int `yaml:"buffer_max" json:"buffer_max"`
}

// Configure Metrics.
type Metrics struct {
Name string `yaml:"name" json:"name"`
Prometheus Prometheus `yaml:"prometheus" json:"prometheus"`
Name string `yaml:"name" json:"name"`
}

// Prometheus specific metrics configuration
Expand Down
30 changes: 15 additions & 15 deletions config/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,15 @@ type Matcher struct {
// or
// string: "user=pqgotest dbname=pqgotest sslmode=verify-full"
ConnString string `yaml:"connstring" json:"connstring"`
// A positive integer
//
// Clair allows for a custom connection pool size.
// This number will directly set how many active sql
// connections are allowed concurrently.
MaxConnPool int `yaml:"max_conn_pool" json:"max_conn_pool"`
// A string in <host>:<port> format where <host> can be an empty string.
//
// A Matcher contacts an Indexer to create a VulnerabilityReport.
// The location of this Indexer is required.
IndexerAddr string `yaml:"indexer_addr" json:"indexer_addr"`
// A "true" or "false" value
//
// Whether Matcher nodes handle migrations to their databases.
Migrations bool `yaml:"migrations" json:"migrations"`
// Period controls how often updaters are run.
//
// The default is 30 minutes.
Period time.Duration `yaml:"period" json:"period"`
// DisableUpdaters disables the updater's running of matchers.
//
// This should be toggled on if vulnerabilities are being provided by
// another mechanism.
DisableUpdaters bool `yaml:"disable_updaters" json:"disable_updaters"`
// UpdateRetention controls the number of updates to retain between
// garbage collection periods.
//
Expand All @@ -46,12 +31,27 @@ type Matcher struct {
//
// A value of 0 disables GC.
UpdateRetention int `yaml:"update_retention" json:"update_retention"`
// A positive integer
//
// Clair allows for a custom connection pool size.
// This number will directly set how many active sql
// connections are allowed concurrently.
MaxConnPool int `yaml:"max_conn_pool" json:"max_conn_pool"`
// CacheAge controls how long clients should be hinted to cache responses
// for.
//
// If empty, the duration set in "Period" will be used. This means client
// may cache "stale" results for 2(Period) - 1 seconds.
CacheAge time.Duration `yaml:"cache_age,omitempty" json:"cache_age,omitempty"`
// A "true" or "false" value
//
// Whether Matcher nodes handle migrations to their databases.
Migrations bool `yaml:"migrations" json:"migrations"`
// DisableUpdaters disables the updater's running of matchers.
//
// This should be toggled on if vulnerabilities are being provided by
// another mechanism.
DisableUpdaters bool `yaml:"disable_updaters" json:"disable_updaters"`
}

func (m *Matcher) Validate(combo bool) error {
Expand Down
6 changes: 3 additions & 3 deletions config/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
)

type Matchers struct {
// Config holds configuration blocks for MatcherFactories and Matchers,
// keyed by name.
Config map[string]yaml.Node `yaml:"config" json:"config"`
// A slice of strings representing which
// matchers will be used.
//
Expand All @@ -22,7 +25,4 @@ type Matchers struct {
// "ubuntu"
// "crda" - remotematcher calls hosted api via RPC.
Names []string `yaml:"names" json:"names"`
// Config holds configuration blocks for MatcherFactories and Matchers,
// keyed by name.
Config map[string]yaml.Node `yaml:"config" json:"config"`
}
8 changes: 4 additions & 4 deletions config/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ type Notifier struct {
// or
// string: "user=pqgotest dbname=pqgotest sslmode=verify-full"
ConnString string `yaml:"connstring" json:"connstring"`
// A "true" or "false" value
//
// Whether Notifier nodes handle migrations to their database.
Migrations bool `yaml:"migrations" json:"migrations"`
// A string in <host>:<port> format where <host> can be an empty string.
//
// A Notifier contacts an Indexer to create obtain manifests affected by vulnerabilities.
Expand Down Expand Up @@ -63,6 +59,10 @@ type Notifier struct {
AMQP *amqp.Config `yaml:"amqp" json:"amqp"`
// Configures the notifier for STOMP delivery.
STOMP *stomp.Config `yaml:"stomp" json:"stomp"`
// A "true" or "false" value
//
// Whether Notifier nodes handle migrations to their database.
Migrations bool `yaml:"migrations" json:"migrations"`
}

func (n *Notifier) Validate(combo bool) error {
Expand Down
24 changes: 12 additions & 12 deletions config/updaters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import (

// Updaters configures updater behavior.
type Updaters struct {
// Filter is a regexp that disallows updaters that do not match from
// running.
// TODO(louis): this is only used in clairctl, should we keep this?
// it may offer an escape hatch for a particular updater name
// from running, vs disabling the updater set completely.
Filter string `yaml:"filter" json:"filter"`
// Config holds configuration blocks for UpdaterFactories and Updaters,
// keyed by name.
//
// These are defined by the updater implementation and can't be documented
// here. Improving the documentation for these is an open issue.
Config map[string]yaml.Node `yaml:"config" json:"config"`
// A slice of strings representing which
// updaters will be used.
//
Expand All @@ -22,16 +34,4 @@ type Updaters struct {
// "suse"
// "ubuntu"
Sets []string `yaml:"sets,omitempty" json:"sets,omitempty"`
// Config holds configuration blocks for UpdaterFactories and Updaters,
// keyed by name.
//
// These are defined by the updater implementation and can't be documented
// here. Improving the documentation for these is an open issue.
Config map[string]yaml.Node `yaml:"config" json:"config"`
// Filter is a regexp that disallows updaters that do not match from
// running.
// TODO(louis): this is only used in clairctl, should we keep this?
// it may offer an escape hatch for a particular updater name
// from running, vs disabling the updater set completely.
Filter string `yaml:"filter" json:"filter"`
}

0 comments on commit eb64aa5

Please sign in to comment.