From a65636db2dac762768e935c929ceef9b7f6deb87 Mon Sep 17 00:00:00 2001 From: Timon Wong Date: Tue, 17 Aug 2021 21:14:24 +0800 Subject: [PATCH] feat!: Remove support for legacy flags --- cmd/prometheus-webhook-dingtalk/main.go | 23 +---------- config/coordinator.go | 31 ++++++--------- internal/compat030/builder.go | 52 ------------------------- internal/compat030/flag.go | 50 ------------------------ 4 files changed, 14 insertions(+), 142 deletions(-) delete mode 100644 internal/compat030/builder.go delete mode 100644 internal/compat030/flag.go diff --git a/cmd/prometheus-webhook-dingtalk/main.go b/cmd/prometheus-webhook-dingtalk/main.go index ea08b3e8..60a1b5bc 100644 --- a/cmd/prometheus-webhook-dingtalk/main.go +++ b/cmd/prometheus-webhook-dingtalk/main.go @@ -18,7 +18,6 @@ import ( "gopkg.in/alecthomas/kingpin.v2" "github.com/timonwong/prometheus-webhook-dingtalk/config" - "github.com/timonwong/prometheus-webhook-dingtalk/internal/compat030" "github.com/timonwong/prometheus-webhook-dingtalk/template" "github.com/timonwong/prometheus-webhook-dingtalk/web" ) @@ -44,12 +43,11 @@ func run() int { configFile = kingpin.Flag( "config.file", "Path to the configuration file.", - ).Default("config.yml").String() + ).Default("config.yml").ExistingFile() ) // DO NOT REMOVE. For compatibility purpose kingpin.Flag("web.ui-enabled", "").Hidden().BoolVar(enableWebUI) - compat030Builder := compat030.NewBuilder(kingpin.CommandLine) promlogConfig := &promlog.Config{} flag.AddFlags(kingpin.CommandLine, promlogConfig) @@ -93,25 +91,8 @@ func run() int { Flags: flagsMap, }) - var compatConf *config.Config - if compat030Builder.IsCompatibleMode() { - var err error - compatConf, err = compat030Builder.BuildConfig() - if err != nil { - level.Error(logger).Log("msg", "Failed to build configuration from legacy commandline flags", "err", err) - return 1 - } - - level.Warn(logger).Log("msg", "DEPRECATION: Detected one of the following flags: --ding.profile, --ding.timeout, --template.file") - level.Warn(logger).Log("msg", "DEPRECATION: Now working in compatibility mode, please consider upgrading your configurations") - } - configLogger := log.With(logger, "component", "configuration") - configCoordinator := config.NewCoordinator( - *configFile, - compatConf, - configLogger, - ) + configCoordinator := config.NewCoordinator(*configFile, configLogger) configCoordinator.Subscribe(func(conf *config.Config) error { // Parse templates level.Info(configLogger).Log("msg", "Loading templates", "templates", strings.Join(conf.Templates, ";")) diff --git a/config/coordinator.go b/config/coordinator.go index 4f9f9834..2240becc 100644 --- a/config/coordinator.go +++ b/config/coordinator.go @@ -27,20 +27,18 @@ type Coordinator struct { logger log.Logger // Protects config and subscribers - mutex sync.Mutex - config *Config - frozenConfig *Config - subscribers []func(*Config) error + mutex sync.Mutex + config *Config + subscribers []func(*Config) error } // NewCoordinator returns a new coordinator with the given configuration file // path. It does not yet load the configuration from file. This is done in // `Reload()`. -func NewCoordinator(configFilePath string, frozenConfig *Config, l log.Logger) *Coordinator { +func NewCoordinator(configFilePath string, l log.Logger) *Coordinator { c := &Coordinator{ configFilePath: configFilePath, logger: l, - frozenConfig: frozenConfig, } return c @@ -82,20 +80,15 @@ func (c *Coordinator) Reload() error { defer c.mutex.Unlock() logger := log.With(c.logger, "file", c.configFilePath) - if c.frozenConfig != nil { - logger = c.logger - c.config = c.frozenConfig - } else { - level.Info(logger).Log("msg", "Loading configuration file") - if err := c.loadFromFile(); err != nil { - level.Error(logger).Log( - "msg", "Loading configuration file failed", - "err", err, - ) - return err - } - level.Info(logger).Log("msg", "Completed loading of configuration file") + level.Info(logger).Log("msg", "Loading configuration file") + if err := c.loadFromFile(); err != nil { + level.Error(logger).Log( + "msg", "Loading configuration file failed", + "err", err, + ) + return err } + level.Info(logger).Log("msg", "Completed loading of configuration file") if err := c.notifySubscribers(); err != nil { logger.Log("msg", "one or more config change subscribers failed to apply new config", "err", err) diff --git a/internal/compat030/builder.go b/internal/compat030/builder.go deleted file mode 100644 index 46734cff..00000000 --- a/internal/compat030/builder.go +++ /dev/null @@ -1,52 +0,0 @@ -package compat030 - -import ( - "time" - - "gopkg.in/alecthomas/kingpin.v2" - - "github.com/timonwong/prometheus-webhook-dingtalk/config" -) - -type Builder struct { - dingTalkProfiles *dingTalkProfilesValue - requestTimeout *time.Duration - templateFile *string - isCompat bool -} - -func NewBuilder(a *kingpin.Application) *Builder { - b := &Builder{} - action := func(ctx *kingpin.ParseContext) error { - b.isCompat = true - return nil - } - b.dingTalkProfiles = asDingTalkProfiles(a.Flag("ding.profile", "").Action(action).Hidden()) - b.requestTimeout = a.Flag("ding.timeout", "").Hidden().Action(action).Duration() - b.templateFile = a.Flag("template.file", "").Hidden().Action(action).String() - return b -} - -func (b *Builder) IsCompatibleMode() bool { - return b.isCompat -} - -func (b *Builder) BuildConfig() (*config.Config, error) { - conf := config.DefaultConfig - if *b.requestTimeout != 0 { - conf.Timeout = *b.requestTimeout - } - - if *b.templateFile != "" { - conf.Templates = []string{*b.templateFile} - } - - conf.Targets = make(map[string]config.Target) - for name, targetURL := range *b.dingTalkProfiles { - targetConfig := config.DefaultTarget - targetConfig.URL = targetURL - conf.Targets[name] = targetConfig - } - - return &conf, nil -} diff --git a/internal/compat030/flag.go b/internal/compat030/flag.go deleted file mode 100644 index eec749ae..00000000 --- a/internal/compat030/flag.go +++ /dev/null @@ -1,50 +0,0 @@ -package compat030 - -import ( - "fmt" - "strings" - - "gopkg.in/alecthomas/kingpin.v2" - - "github.com/timonwong/prometheus-webhook-dingtalk/config" -) - -type dingTalkProfilesValue map[string]*config.SecretURL - -func asDingTalkProfiles(s kingpin.Settings) (target *dingTalkProfilesValue) { - target = &dingTalkProfilesValue{} - s.SetValue(target) - return -} - -func (s *dingTalkProfilesValue) Set(value string) error { - parts := strings.SplitN(value, "=", 2) - profile, webhookURL := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]) - - // Validate profile name - if !config.TargetValidNameRE.MatchString(profile) { - return fmt.Errorf("invalid profile name: %q", profile) - } - - // Validate webhook url - url, err := config.ParseURL(webhookURL) - if err != nil { - return fmt.Errorf("invalid webhook url: %s", err) - } - - targetURL := config.SecretURL(*url) - (*s)[profile] = &targetURL - return nil -} - -func (s *dingTalkProfilesValue) Get() interface{} { - return *s -} - -func (s *dingTalkProfilesValue) String() string { - return fmt.Sprintf("%s", *s) -} - -func (s *dingTalkProfilesValue) IsCumulative() bool { - return true -}