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 6 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
26 changes: 14 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 @@

// 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 @@ -49,16 +49,18 @@
// 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
if err := cfg.InitFromViper(opts.Viper); err != nil {
opts.Logger.Error("Failed to initialize config from viper",
zap.Error(err),
)
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("failed to initialize config: %w", err)

Check warning on line 56 in cmd/es-rollover/app/actions.go

View check run for this annotation

Codecov / codecov/patch

cmd/es-rollover/app/actions.go#L53-L56

Added lines #L53 - L56 were not covered by tests
}
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
12 changes: 4 additions & 8 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,19 @@ 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)
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,
}, 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 +98,6 @@ func TestExecuteAction(t *testing.T) {
},
}
})

assert.Equal(t, test.expectedExecuteAction, executedAction)
if test.configError {
require.Error(t, err)
Expand Down
16 changes: 15 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 @@
"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 @@
Timeout int
SkipDependencies bool
AdaptiveSampling bool
TLSConfig configtls.ClientConfig
}

// AddFlags adds flags
Expand All @@ -46,10 +52,11 @@
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.
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 +69,11 @@
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
}

Check warning on line 75 in cmd/es-rollover/app/flags.go

View check run for this annotation

Codecov / codecov/patch

cmd/es-rollover/app/flags.go#L74-L75

Added lines #L74 - L75 were not covered by tests
c.TLSConfig = opts.ToOtelClientConfig()

return nil
}
3 changes: 3 additions & 0 deletions cmd/es-rollover/app/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@

// InitFromViper initializes config from viper.Viper.
func (c *Config) InitFromViper(v *viper.Viper) {
if v == nil {
panic("viper instance cannot be nil")

Check warning on line 44 in cmd/es-rollover/app/init/flags.go

View check run for this annotation

Codecov / codecov/patch

cmd/es-rollover/app/init/flags.go#L44

Added line #L44 was not covered by tests
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
}
c.Indices.Spans.Shards = v.GetInt64(shards)
c.Indices.Services.Shards = v.GetInt64(shards)
c.Indices.Dependencies.Shards = v.GetInt64(shards)
Expand Down
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.AddFlags)
addSubCommand(v, rootCmd, initCommand, initCfg.AddFlags)
addSubCommand(v, rootCmd, rolloverCommand, rolloverCfg.AddFlags)
addSubCommand(v, rootCmd, lookbackCommand, lookbackCfg.AddFlags)
Expand Down
Loading