Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[es-rollover] Use OTEL helpers for TLS config instead of tlscfg #6238

Merged
merged 9 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions cmd/es-rollover/app/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
package app

import (
"context"
"crypto/tls"
"fmt"
"net/http"
"time"

"github.com/spf13/viper"
"go.opentelemetry.io/collector/config/configtls"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
"github.com/jaegertracing/jaeger/pkg/es/client"
)

Expand All @@ -37,28 +39,38 @@ 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
TLSConfig configtls.ClientConfig
}

// ActionCreatorFunction type is the function type in charge of create the action to be executed
type ActionCreatorFunction func(client.Client, Config) Action

func getTLSConfig(tlsConfig configtls.ClientConfig, logger *zap.Logger) (*tls.Config, error) {
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved
if tlsConfig.Insecure {
logger.Info("TLS is disabled")
return nil, nil
}

ctx := context.Background()
tlsCfg, err := tlsConfig.LoadTLSConfig(ctx)
if err != nil {
logger.Error("Failed to load TLS configuration", zap.Error(err))
return nil, fmt.Errorf("error loading TLS config: %w", err)
}
return tlsCfg, nil
}

// ExecuteAction execute the action returned by the createAction function
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)
tlsCfg, err := getTLSConfig(opts.TLSConfig, opts.Logger)
if err != nil {
return err
}
defer tlsOpts.Close()

esClient := newESClient(opts.Args[0], &cfg, tlsCfg)
action := createAction(esClient, cfg)
Expand Down
12 changes: 5 additions & 7 deletions cmd/es-rollover/app/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
"github.com/jaegertracing/jaeger/pkg/es/client"
)

Expand Down Expand Up @@ -74,21 +73,20 @@ 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)
AddTLSFlags(flags)
AddFlags(flags)
command.PersistentFlags().AddGoFlagSet(flags)
v.BindPFlags(command.PersistentFlags())
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved
cmdLine := append([]string{"--es.tls.enabled=true"}, test.flags...)
err := command.ParseFlags(cmdLine)
require.NoError(t, err)
executedAction := false
err = ExecuteAction(ActionExecuteOptions{
Args: args,
Viper: v,
Logger: logger,
TLSFlags: tlsFlags,
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)
Expand Down
18 changes: 17 additions & 1 deletion cmd/es-rollover/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,6 +38,7 @@ type Config struct {
Timeout int
SkipDependencies bool
AdaptiveSampling bool
TLSConfig configtls.ClientConfig
}

// AddFlags adds flags
Expand All @@ -48,8 +54,12 @@ func AddFlags(flags *flag.FlagSet) {
flags.Bool(adaptiveSampling, false, "Enable rollover for adaptive sampling index")
}

func AddTLSFlags(flags *flag.FlagSet) {
tlsFlagsCfg.AddFlags(flags)
}
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved

// InitFromViper initializes config from viper.Viper.
func (c *Config) InitFromViper(v *viper.Viper) {
func (c *Config) InitFromViper(v *viper.Viper) error {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
c.IndexPrefix = v.GetString(indexPrefix)
if c.IndexPrefix != "" {
c.IndexPrefix += "-"
Expand All @@ -62,4 +72,10 @@ 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 {
return err
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
}
c.TLSConfig = opts.ToOtelClientConfig()
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
26 changes: 10 additions & 16 deletions cmd/es-rollover/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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{
Expand All @@ -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)
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -129,7 +123,7 @@ func main() {
},
}

addPersistentFlags(v, rootCmd, tlsFlags.AddFlags, app.AddFlags)
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved
addPersistentFlags(v, rootCmd, app.AddTLSFlags, app.AddFlags)
addSubCommand(v, rootCmd, initCommand, initCfg.AddFlags)
addSubCommand(v, rootCmd, rolloverCommand, rolloverCfg.AddFlags)
addSubCommand(v, rootCmd, lookbackCommand, lookbackCfg.AddFlags)
Expand Down
Loading