Skip to content

Commit

Permalink
[es-rollover] Use OTEL helpers for TLS config instead of tlscfg (#6238)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- Part of #4316 

---------

Signed-off-by: chahatsagarmain <[email protected]>
Signed-off-by: Yuri Shkuro <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
  • Loading branch information
3 people authored Nov 24, 2024
1 parent f6899b0 commit 4d1c7d8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 47 deletions.
21 changes: 9 additions & 12 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"
"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"
)

Expand All @@ -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
Expand All @@ -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)
Expand Down
26 changes: 7 additions & 19 deletions cmd/es-rollover/app/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -101,7 +90,6 @@ func TestExecuteAction(t *testing.T) {
},
}
})

assert.Equal(t, test.expectedExecuteAction, executedAction)
if test.configError {
require.Error(t, err)
Expand Down
12 changes: 12 additions & 0 deletions 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 @@ -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.
Expand All @@ -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()
}
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)
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)
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

0 comments on commit 4d1c7d8

Please sign in to comment.