diff --git a/cmd/es-rollover/app/actions.go b/cmd/es-rollover/app/actions.go index 8dbeff30e95..3b3cf6149cf 100644 --- a/cmd/es-rollover/app/actions.go +++ b/cmd/es-rollover/app/actions.go @@ -4,14 +4,15 @@ package app import ( + "context" "crypto/tls" + "fmt" "net/http" "time" "github.com/spf13/viper" "go.uber.org/zap" - "github.com/jaegertracing/jaeger/pkg/config/tlscfg" "github.com/jaegertracing/jaeger/pkg/es/client" ) @@ -37,10 +38,9 @@ type Action interface { // ActionExecuteOptions are the options passed to the execute action function type ActionExecuteOptions struct { - Args []string - Viper *viper.Viper - Logger *zap.Logger - TLSFlags tlscfg.ClientFlagsConfig + Args []string + Viper *viper.Viper + Logger *zap.Logger } // ActionCreatorFunction type is the function type in charge of create the action to be executed @@ -50,15 +50,12 @@ type ActionCreatorFunction func(client.Client, Config) Action func ExecuteAction(opts ActionExecuteOptions, createAction ActionCreatorFunction) error { cfg := Config{} cfg.InitFromViper(opts.Viper) - tlsOpts, err := opts.TLSFlags.InitFromViper(opts.Viper) - if err != nil { - return err - } - tlsCfg, err := tlsOpts.Config(opts.Logger) + + ctx := context.Background() + tlsCfg, err := cfg.TLSConfig.LoadTLSConfig(ctx) if err != nil { - return err + return fmt.Errorf("TLS configuration failed: %w", err) } - defer tlsOpts.Close() esClient := newESClient(opts.Args[0], &cfg, tlsCfg) action := createAction(esClient, cfg) diff --git a/cmd/es-rollover/app/actions_test.go b/cmd/es-rollover/app/actions_test.go index e4b7a33c34f..bc78ef11827 100644 --- a/cmd/es-rollover/app/actions_test.go +++ b/cmd/es-rollover/app/actions_test.go @@ -5,17 +5,14 @@ package app import ( "errors" - "flag" "net/http" "testing" - "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" - "github.com/jaegertracing/jaeger/pkg/config/tlscfg" + "github.com/jaegertracing/jaeger/pkg/config" "github.com/jaegertracing/jaeger/pkg/es/client" ) @@ -73,22 +70,14 @@ func TestExecuteAction(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - v := viper.New() - tlsFlags := tlscfg.ClientFlagsConfig{Prefix: "es"} - command := cobra.Command{} - flags := &flag.FlagSet{} - tlsFlags.AddFlags(flags) - command.PersistentFlags().AddGoFlagSet(flags) - v.BindPFlags(command.PersistentFlags()) + v, command := config.Viperize(AddFlags) cmdLine := append([]string{"--es.tls.enabled=true"}, test.flags...) - err := command.ParseFlags(cmdLine) - require.NoError(t, err) + require.NoError(t, command.ParseFlags(cmdLine)) executedAction := false - err = ExecuteAction(ActionExecuteOptions{ - Args: args, - Viper: v, - Logger: logger, - TLSFlags: tlsFlags, + err := ExecuteAction(ActionExecuteOptions{ + Args: args, + Viper: v, + Logger: logger, }, func(c client.Client, _ Config) Action { assert.Equal(t, "https://localhost:9300", c.Endpoint) transport, ok := c.Client.Transport.(*http.Transport) @@ -101,7 +90,6 @@ func TestExecuteAction(t *testing.T) { }, } }) - assert.Equal(t, test.expectedExecuteAction, executedAction) if test.configError { require.Error(t, err) diff --git a/cmd/es-rollover/app/flags.go b/cmd/es-rollover/app/flags.go index bf7bfcac1f0..a1456546546 100644 --- a/cmd/es-rollover/app/flags.go +++ b/cmd/es-rollover/app/flags.go @@ -7,8 +7,13 @@ import ( "flag" "github.com/spf13/viper" + "go.opentelemetry.io/collector/config/configtls" + + "github.com/jaegertracing/jaeger/pkg/config/tlscfg" ) +var tlsFlagsCfg = tlscfg.ClientFlagsConfig{Prefix: "es"} + const ( indexPrefix = "index-prefix" archive = "archive" @@ -33,6 +38,7 @@ type Config struct { Timeout int SkipDependencies bool AdaptiveSampling bool + TLSConfig configtls.ClientConfig } // AddFlags adds flags @@ -46,6 +52,7 @@ func AddFlags(flags *flag.FlagSet) { flags.Int(timeout, 120, "Number of seconds to wait for master node response") flags.Bool(skipDependencies, false, "Disable rollover for dependencies index") flags.Bool(adaptiveSampling, false, "Enable rollover for adaptive sampling index") + tlsFlagsCfg.AddFlags(flags) } // InitFromViper initializes config from viper.Viper. @@ -62,4 +69,9 @@ func (c *Config) InitFromViper(v *viper.Viper) { c.Timeout = v.GetInt(timeout) c.SkipDependencies = v.GetBool(skipDependencies) c.AdaptiveSampling = v.GetBool(adaptiveSampling) + opts, err := tlsFlagsCfg.InitFromViper(v) + if err != nil { + panic(err) + } + c.TLSConfig = opts.ToOtelClientConfig() } diff --git a/cmd/es-rollover/main.go b/cmd/es-rollover/main.go index 5fe026d6e68..411d4596d2f 100644 --- a/cmd/es-rollover/main.go +++ b/cmd/es-rollover/main.go @@ -16,7 +16,6 @@ import ( "github.com/jaegertracing/jaeger/cmd/es-rollover/app/lookback" "github.com/jaegertracing/jaeger/cmd/es-rollover/app/rollover" "github.com/jaegertracing/jaeger/pkg/config" - "github.com/jaegertracing/jaeger/pkg/config/tlscfg" "github.com/jaegertracing/jaeger/pkg/es/client" ) @@ -30,8 +29,6 @@ func main() { Long: "Jaeger es-rollover manages Jaeger indices", } - tlsFlags := tlscfg.ClientFlagsConfig{Prefix: "es"} - // Init command initCfg := &initialize.Config{} initCommand := &cobra.Command{ @@ -42,10 +39,9 @@ func main() { SilenceUsage: true, RunE: func(_ *cobra.Command, args []string) error { return app.ExecuteAction(app.ActionExecuteOptions{ - Args: args, - Viper: v, - Logger: logger, - TLSFlags: tlsFlags, + Args: args, + Viper: v, + Logger: logger, }, func(c client.Client, cfg app.Config) app.Action { initCfg.Config = cfg initCfg.InitFromViper(v) @@ -80,10 +76,9 @@ func main() { RunE: func(_ *cobra.Command, args []string) error { rolloverCfg.InitFromViper(v) return app.ExecuteAction(app.ActionExecuteOptions{ - Args: args, - Viper: v, - Logger: logger, - TLSFlags: tlsFlags, + Args: args, + Viper: v, + Logger: logger, }, func(c client.Client, cfg app.Config) app.Action { rolloverCfg.Config = cfg rolloverCfg.InitFromViper(v) @@ -109,10 +104,9 @@ func main() { RunE: func(_ *cobra.Command, args []string) error { lookbackCfg.InitFromViper(v) return app.ExecuteAction(app.ActionExecuteOptions{ - Args: args, - Viper: v, - Logger: logger, - TLSFlags: tlsFlags, + Args: args, + Viper: v, + Logger: logger, }, func(c client.Client, cfg app.Config) app.Action { lookbackCfg.Config = cfg lookbackCfg.InitFromViper(v) @@ -129,7 +123,7 @@ func main() { }, } - addPersistentFlags(v, rootCmd, tlsFlags.AddFlags, app.AddFlags) + addPersistentFlags(v, rootCmd, app.AddFlags) addSubCommand(v, rootCmd, initCommand, initCfg.AddFlags) addSubCommand(v, rootCmd, rolloverCommand, rolloverCfg.AddFlags) addSubCommand(v, rootCmd, lookbackCommand, lookbackCfg.AddFlags)