Skip to content

Commit

Permalink
config: move to use Mode and LogLevel types
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Nov 4, 2021
1 parent 9033bc9 commit 7bcfc20
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 108 deletions.
2 changes: 1 addition & 1 deletion cmd/clair/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func main() {
if err != nil {
golog.Fatalf("failed to decode yaml config: %v", err)
}
conf.Mode = runMode.String()
conf.Mode = runMode.Mode
_, err = config.Validate(&conf)
if err != nil {
golog.Fatalf("failed to validate config: %v", err)
Expand Down
63 changes: 16 additions & 47 deletions cmd/clair/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,55 +37,24 @@ func (v *ConfValue) Set(s string) error {
return nil
}

const (
_ ConfMode = iota
ModeCombo
ModeIndexer
ModeMatcher
ModeNotifier
)

// ConfMode enumerates the arguments that are acceptable "modes".
type ConfMode int

func (v *ConfMode) String() string {
if v == nil {
return ""
}
switch *v {
case ModeCombo:
return config.ComboMode.String()
case ModeIndexer:
return config.IndexerMode.String()
case ModeMatcher:
return config.MatcherMode.String()
case ModeNotifier:
return config.NotifierMode.String()
default:
}
return "invalid"
}

// Get implements flag.Getter
func (v *ConfMode) Get() interface{} {
return *v
// ConfMode enumerates the arguments that are acceptable modes: "combo",
// "indexer", "matcher", "notifier".
//
// See also: github.com/quay/clair/config.ParseMode
type ConfMode struct {
config.Mode
}

// Set implements flag.Value
func (v *ConfMode) Set(s string) error {
switch s {
case "", "dev":
fallthrough
case "combo", "combination", "pizza": // "Pizza", of course, being the best Combos flavor.
*v = ModeCombo
case "index", "indexer":
*v = ModeIndexer
case "match", "matcher":
*v = ModeMatcher
case "notify", "notifier":
*v = ModeNotifier
default:
return fmt.Errorf("unknown mode argument %q", s)
// Set implements flag.Value.
//
// An empty string is interpreted as "combo".
func (v *ConfMode) Set(s string) (err error) {
if s == "" {
s = "combo"
}
v.Mode, err = config.ParseMode(s)
if err != nil {
return err
}
return nil
}
20 changes: 3 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ const DefaultAddress = ":6060"
// Config is the configuration object for the commands in
// github.com/quay/clair/v4/cmd/...
type Config struct {
// One of the following strings
// Sets which mode the clair instances will run in
//
// "indexer": runs just the indexer node
// "matcher": runs just the matcher node
// "combo": will run both indexer and matcher on the same node.
Mode string `yaml:"-" json:"-"`
// Sets which mode the clair instance will run.
Mode Mode `yaml:"-" json:"-"`
// A string in <host>:<port> format where <host> can be an empty string.
//
// exposes Clair node's functionality to the network.
Expand All @@ -28,16 +23,7 @@ type Config struct {
// exposes Clair's metrics and health endpoints.
IntrospectionAddr string `yaml:"introspection_addr" json:"introspection_addr"`
// Set the logging level.
//
// One of the following strings:
// "debug-color"
// "debug"
// "info"
// "warn"
// "error"
// "fatal"
// "panic"
LogLevel string `yaml:"log_level" json:"log_level"`
LogLevel LogLevel `yaml:"log_level" json:"log_level"`
Indexer Indexer `yaml:"indexer" json:"indexer"`
Matcher Matcher `yaml:"matcher" json:"matcher"`
Matchers Matchers `yaml:"matchers" json:"matchers"`
Expand Down
18 changes: 9 additions & 9 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ func TestConfigValidateFailure(t *testing.T) {
{
name: "No Mode",
conf: config.Config{
Mode: "",
Mode: config.Mode(-1),
},
},
{
name: "ComboMode, Malformed Global HTTP Listen Addr",
conf: config.Config{
Mode: config.ComboMode.String(),
Mode: config.ComboMode,
HTTPListenAddr: "xyz",
},
},
{
name: "MatcherMode, No IndexerAddr",
conf: config.Config{
Mode: config.MatcherMode.String(),
Mode: config.MatcherMode,
HTTPListenAddr: "localhost:8080",
Matcher: config.Matcher{
ConnString: "example@example/db",
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestConfigUpateRetention(t *testing.T) {
name: "Retention less than 0",
expectedRetention: 0,
conf: config.Config{
Mode: config.ComboMode.String(),
Mode: config.ComboMode,
HTTPListenAddr: "localhost:8080",
Indexer: config.Indexer{
ConnString: "example@example/db",
Expand All @@ -78,7 +78,7 @@ func TestConfigUpateRetention(t *testing.T) {
name: "Retention of 0",
expectedRetention: 10,
conf: config.Config{
Mode: config.ComboMode.String(),
Mode: config.ComboMode,
HTTPListenAddr: "localhost:8080",
Indexer: config.Indexer{
ConnString: "example@example/db",
Expand All @@ -97,7 +97,7 @@ func TestConfigUpateRetention(t *testing.T) {
name: "Retention less than 2",
expectedRetention: 10,
conf: config.Config{
Mode: config.ComboMode.String(),
Mode: config.ComboMode,
HTTPListenAddr: "localhost:8080",
Indexer: config.Indexer{
ConnString: "example@example/db",
Expand All @@ -116,7 +116,7 @@ func TestConfigUpateRetention(t *testing.T) {
name: "Retention of 2",
expectedRetention: 2,
conf: config.Config{
Mode: config.ComboMode.String(),
Mode: config.ComboMode,
HTTPListenAddr: "localhost:8080",
Indexer: config.Indexer{
ConnString: "example@example/db",
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestConfigDisableUpdaters(t *testing.T) {
{
name: "ComboMode, disable updaters",
conf: config.Config{
Mode: config.ComboMode.String(),
Mode: config.ComboMode,
HTTPListenAddr: "localhost:8080",
Indexer: config.Indexer{
ConnString: "example@example/db",
Expand All @@ -177,7 +177,7 @@ func TestConfigDisableUpdaters(t *testing.T) {
{
name: "MatcherMode, disable updaters",
conf: config.Config{
Mode: config.MatcherMode.String(),
Mode: config.MatcherMode,
HTTPListenAddr: "localhost:8080",
Matcher: config.Matcher{
ConnString: "example@example/db",
Expand Down
10 changes: 5 additions & 5 deletions config/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,24 @@ type Matcher struct {
}

const (
defaultMatcherPeriod = 30 * time.Minute
defaultMacherRetention = 10
DefaultMatcherPeriod = 30 * time.Minute
DefaultMatcherRetention = 10
)

func (m *Matcher) validate(mode Mode) ([]Warning, error) {
if mode != ComboMode && mode != MatcherMode {
return nil, nil
}
if m.Period == 0 {
m.Period = defaultMatcherPeriod
m.Period = DefaultMatcherPeriod
}
switch {
case m.UpdateRetention < 0:
// Less than 0 means GC is off.
m.UpdateRetention = 0
case m.UpdateRetention < 2:
// Anything less than 2 gets the default.
m.UpdateRetention = defaultMacherRetention
m.UpdateRetention = DefaultMatcherRetention
}
if m.CacheAge == 0 {
m.CacheAge = m.Period
Expand Down Expand Up @@ -102,7 +102,7 @@ func (m *Matcher) lint() (ws []Warning, err error) {
ws[i].path = ".connstring"
}

if m.Period < defaultMatcherPeriod {
if m.Period < DefaultMatcherPeriod {
ws = append(ws, Warning{
path: ".period",
msg: "updater period is very aggressive: most sources are updated daily",
Expand Down
6 changes: 1 addition & 5 deletions config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ package config
// Validate confirms the necessary values to support the desired Clair mode
// exist and sets default values.
func Validate(c *Config) ([]Warning, error) {
m, err := ParseMode(c.Mode)
if err != nil {
return nil, err
}
return forEach(c, func(i interface{}) ([]Warning, error) {
if v, ok := i.(validator); ok {
return v.validate(m)
return v.validate(c.Mode)
}
return nil, nil
})
Expand Down
8 changes: 4 additions & 4 deletions httptransport/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,22 @@ func New(ctx context.Context, conf config.Config, indexer indexer.Service, match

var e error
switch conf.Mode {
case config.ComboMode.String():
case config.ComboMode:
e = t.configureComboMode(ctx)
if e != nil {
return nil, e
}
case config.IndexerMode.String():
case config.IndexerMode:
e = t.configureIndexerMode(ctx)
if e != nil {
return nil, e
}
case config.MatcherMode.String():
case config.MatcherMode:
e = t.configureMatcherMode(ctx)
if e != nil {
return nil, e
}
case config.NotifierMode.String():
case config.NotifierMode:
e = t.configureNotifierMode(ctx)
if e != nil {
return nil, e
Expand Down
22 changes: 11 additions & 11 deletions initialize/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package initialize

import (
"context"
"fmt"
"os"
"strings"

"github.com/quay/clair/config"
"github.com/quay/zlog"
Expand All @@ -14,31 +14,31 @@ import (
// Logging configures zlog according to the provided configuration.
func Logging(ctx context.Context, cfg *config.Config) error {
l := zerolog.New(os.Stderr)
switch strings.ToLower(cfg.LogLevel) {
case "debug-color":
switch cfg.LogLevel {
case config.DebugColorLog:
// set logger to use ConsoleWriter for colorized output
l = l.Level(zerolog.DebugLevel).
Output(zerolog.ConsoleWriter{Out: os.Stderr})
case "debug":
case config.DebugLog:
l = l.Level(zerolog.DebugLevel)
case "info":
case config.InfoLog:
l = l.Level(zerolog.InfoLevel)
case "warn":
case config.WarnLog:
l = l.Level(zerolog.WarnLevel)
case "error":
case config.ErrorLog:
l = l.Level(zerolog.ErrorLevel)
case "fatal":
case config.FatalLog:
l = l.Level(zerolog.FatalLevel)
case "panic":
case config.PanicLog:
l = l.Level(zerolog.PanicLevel)
default:
l = l.Level(zerolog.InfoLevel)
return fmt.Errorf("unknown log level: %v", cfg.LogLevel)
}
l = l.With().
Timestamp().
Logger()
zlog.Set(&l)
log.Logger = zerolog.Nop()
l.Debug().Str("component", "initialize/Logging").Msg("logging initialized")
zlog.Debug(ctx).Str("component", "initialize/Logging").Msg("logging initialized")
return nil
}
8 changes: 4 additions & 4 deletions initialize/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Services(ctx context.Context, cfg *config.Config) (*Srv, error) {
var srv Srv
var err error
switch cfg.Mode {
case config.ComboMode.String():
case config.ComboMode:
srv.Indexer, err = localIndexer(ctx, cfg)
if err != nil {
return nil, err
Expand All @@ -75,12 +75,12 @@ func Services(ctx context.Context, cfg *config.Config) (*Srv, error) {
if err != nil {
return nil, err
}
case config.IndexerMode.String():
case config.IndexerMode:
srv.Indexer, err = localIndexer(ctx, cfg)
if err != nil {
return nil, err
}
case config.MatcherMode.String():
case config.MatcherMode:
srv.Matcher, err = localMatcher(ctx, cfg)
if err != nil {
return nil, err
Expand All @@ -89,7 +89,7 @@ func Services(ctx context.Context, cfg *config.Config) (*Srv, error) {
if err != nil {
return nil, err
}
case config.NotifierMode.String():
case config.NotifierMode:
srv.Indexer, err = remoteIndexer(ctx, cfg, cfg.Notifier.IndexerAddr)
if err != nil {
return nil, err
Expand Down
7 changes: 2 additions & 5 deletions introspection/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func New(ctx context.Context, conf config.Config, health func() bool) (*Server,
// sampler
var sampler sdktrace.Sampler
switch {
case i.conf.LogLevel == "debug":
case i.conf.LogLevel == config.DebugLog || i.conf.LogLevel == config.DebugColorLog:
sampler = sdktrace.AlwaysSample()
case i.conf.Trace.Probability != nil:
p := *i.conf.Trace.Probability
Expand Down Expand Up @@ -219,7 +219,7 @@ func (i *Server) withJaeger(ctx context.Context, traceOpts []sdktrace.TracerProv
opts = append(opts, jaeger.WithBufferMaxCount(conf.BufferMax))
}
p := jaeger.Process{
ServiceName: "clairv4/" + i.conf.Mode,
ServiceName: fmt.Sprintf("clairv4/%v", i.conf.Mode),
}
if len(conf.Tags) != 0 {
for k, v := range conf.Tags {
Expand All @@ -236,9 +236,6 @@ func (i *Server) withJaeger(ctx context.Context, traceOpts []sdktrace.TracerProv
i.RegisterOnShutdown(exporter.Flush)

tp := sdktrace.NewTracerProvider(traceOpts...)
if err != nil {
return err
}
otel.SetTracerProvider(tp)
return nil
}
Expand Down

0 comments on commit 7bcfc20

Please sign in to comment.