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 2 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
38 changes: 27 additions & 11 deletions cmd/es-rollover/app/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
package app

import (
"context"
"crypto/tls"
"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 @@ -35,30 +36,45 @@ type Action interface {
Do() error
}

type ClientConfig struct {
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved
configtls.ClientConfig `mapstructure:",squash"`
Enabled bool
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 *ClientConfig
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 *ClientConfig, logger *zap.Logger) (*tls.Config, error) {
if tlsConfig == nil {
return nil, nil
}

if tlsConfig.Insecure {
logger.Info("TLS is disabled")
return nil, nil
}

ctx := context.Background()

return tlsConfig.LoadTLSConfig(ctx)
}

// 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
14 changes: 7 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,7 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"

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

Expand Down Expand Up @@ -74,21 +74,21 @@ 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"}
tlfConfig := &ClientConfig{}
command := cobra.Command{}
flags := &flag.FlagSet{}
tlsFlags.AddFlags(flags)
app.AddFlags(flags)
command.PersistentFlags().AddGoFlagSet(flags)
v.BindPFlags(command.PersistentFlags())
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,
TLSConfig: tlfConfig,
}, func(c client.Client, _ Config) Action {
assert.Equal(t, "https://localhost:9300", c.Endpoint)
transport, ok := c.Client.Transport.(*http.Transport)
Expand Down
5 changes: 5 additions & 0 deletions cmd/es-rollover/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"flag"

"github.com/spf13/viper"

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

const (
Expand Down Expand Up @@ -35,6 +37,8 @@ type Config struct {
AdaptiveSampling bool
}

var tlsFlagsConfig = tlscfg.ClientFlagsConfig{Prefix: "es"}

// AddFlags adds flags
func AddFlags(flags *flag.FlagSet) {
flags.String(indexPrefix, "", "Index prefix")
Expand All @@ -46,6 +50,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")
tlsFlagsConfig.AddFlags(flags)
}

// InitFromViper initializes config from viper.Viper.
Expand Down
29 changes: 14 additions & 15 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,7 +29,7 @@ func main() {
Long: "Jaeger es-rollover manages Jaeger indices",
}

tlsFlags := tlscfg.ClientFlagsConfig{Prefix: "es"}
tlsConfig := &app.ClientConfig{}
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved

// Init command
initCfg := &initialize.Config{}
Expand All @@ -42,10 +41,10 @@ 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,
TLSConfig: tlsConfig,
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved
}, 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 +79,10 @@ 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,
TLSConfig: tlsConfig,
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
}, func(c client.Client, cfg app.Config) app.Action {
rolloverCfg.Config = cfg
rolloverCfg.InitFromViper(v)
Expand All @@ -109,10 +108,10 @@ 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,
TLSConfig: tlsConfig,
chahatsagarmain marked this conversation as resolved.
Show resolved Hide resolved
}, func(c client.Client, cfg app.Config) app.Action {
lookbackCfg.Config = cfg
lookbackCfg.InitFromViper(v)
Expand All @@ -129,7 +128,7 @@ func main() {
},
}

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