Skip to content

Commit 4d1c7d8

Browse files
[es-rollover] Use OTEL helpers for TLS config instead of tlscfg (#6238)
## 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]>
1 parent f6899b0 commit 4d1c7d8

File tree

4 files changed

+38
-47
lines changed

4 files changed

+38
-47
lines changed

cmd/es-rollover/app/actions.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
package app
55

66
import (
7+
"context"
78
"crypto/tls"
9+
"fmt"
810
"net/http"
911
"time"
1012

1113
"github.com/spf13/viper"
1214
"go.uber.org/zap"
1315

14-
"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
1516
"github.com/jaegertracing/jaeger/pkg/es/client"
1617
)
1718

@@ -37,10 +38,9 @@ type Action interface {
3738

3839
// ActionExecuteOptions are the options passed to the execute action function
3940
type ActionExecuteOptions struct {
40-
Args []string
41-
Viper *viper.Viper
42-
Logger *zap.Logger
43-
TLSFlags tlscfg.ClientFlagsConfig
41+
Args []string
42+
Viper *viper.Viper
43+
Logger *zap.Logger
4444
}
4545

4646
// 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
5050
func ExecuteAction(opts ActionExecuteOptions, createAction ActionCreatorFunction) error {
5151
cfg := Config{}
5252
cfg.InitFromViper(opts.Viper)
53-
tlsOpts, err := opts.TLSFlags.InitFromViper(opts.Viper)
54-
if err != nil {
55-
return err
56-
}
57-
tlsCfg, err := tlsOpts.Config(opts.Logger)
53+
54+
ctx := context.Background()
55+
tlsCfg, err := cfg.TLSConfig.LoadTLSConfig(ctx)
5856
if err != nil {
59-
return err
57+
return fmt.Errorf("TLS configuration failed: %w", err)
6058
}
61-
defer tlsOpts.Close()
6259

6360
esClient := newESClient(opts.Args[0], &cfg, tlsCfg)
6461
action := createAction(esClient, cfg)

cmd/es-rollover/app/actions_test.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ package app
55

66
import (
77
"errors"
8-
"flag"
98
"net/http"
109
"testing"
1110

12-
"github.com/spf13/cobra"
13-
"github.com/spf13/viper"
1411
"github.com/stretchr/testify/assert"
1512
"github.com/stretchr/testify/require"
1613
"go.uber.org/zap"
1714

18-
"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
15+
"github.com/jaegertracing/jaeger/pkg/config"
1916
"github.com/jaegertracing/jaeger/pkg/es/client"
2017
)
2118

@@ -73,22 +70,14 @@ func TestExecuteAction(t *testing.T) {
7370

7471
for _, test := range tests {
7572
t.Run(test.name, func(t *testing.T) {
76-
v := viper.New()
77-
tlsFlags := tlscfg.ClientFlagsConfig{Prefix: "es"}
78-
command := cobra.Command{}
79-
flags := &flag.FlagSet{}
80-
tlsFlags.AddFlags(flags)
81-
command.PersistentFlags().AddGoFlagSet(flags)
82-
v.BindPFlags(command.PersistentFlags())
73+
v, command := config.Viperize(AddFlags)
8374
cmdLine := append([]string{"--es.tls.enabled=true"}, test.flags...)
84-
err := command.ParseFlags(cmdLine)
85-
require.NoError(t, err)
75+
require.NoError(t, command.ParseFlags(cmdLine))
8676
executedAction := false
87-
err = ExecuteAction(ActionExecuteOptions{
88-
Args: args,
89-
Viper: v,
90-
Logger: logger,
91-
TLSFlags: tlsFlags,
77+
err := ExecuteAction(ActionExecuteOptions{
78+
Args: args,
79+
Viper: v,
80+
Logger: logger,
9281
}, func(c client.Client, _ Config) Action {
9382
assert.Equal(t, "https://localhost:9300", c.Endpoint)
9483
transport, ok := c.Client.Transport.(*http.Transport)
@@ -101,7 +90,6 @@ func TestExecuteAction(t *testing.T) {
10190
},
10291
}
10392
})
104-
10593
assert.Equal(t, test.expectedExecuteAction, executedAction)
10694
if test.configError {
10795
require.Error(t, err)

cmd/es-rollover/app/flags.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ import (
77
"flag"
88

99
"github.com/spf13/viper"
10+
"go.opentelemetry.io/collector/config/configtls"
11+
12+
"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
1013
)
1114

15+
var tlsFlagsCfg = tlscfg.ClientFlagsConfig{Prefix: "es"}
16+
1217
const (
1318
indexPrefix = "index-prefix"
1419
archive = "archive"
@@ -33,6 +38,7 @@ type Config struct {
3338
Timeout int
3439
SkipDependencies bool
3540
AdaptiveSampling bool
41+
TLSConfig configtls.ClientConfig
3642
}
3743

3844
// AddFlags adds flags
@@ -46,6 +52,7 @@ func AddFlags(flags *flag.FlagSet) {
4652
flags.Int(timeout, 120, "Number of seconds to wait for master node response")
4753
flags.Bool(skipDependencies, false, "Disable rollover for dependencies index")
4854
flags.Bool(adaptiveSampling, false, "Enable rollover for adaptive sampling index")
55+
tlsFlagsCfg.AddFlags(flags)
4956
}
5057

5158
// InitFromViper initializes config from viper.Viper.
@@ -62,4 +69,9 @@ func (c *Config) InitFromViper(v *viper.Viper) {
6269
c.Timeout = v.GetInt(timeout)
6370
c.SkipDependencies = v.GetBool(skipDependencies)
6471
c.AdaptiveSampling = v.GetBool(adaptiveSampling)
72+
opts, err := tlsFlagsCfg.InitFromViper(v)
73+
if err != nil {
74+
panic(err)
75+
}
76+
c.TLSConfig = opts.ToOtelClientConfig()
6577
}

cmd/es-rollover/main.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/jaegertracing/jaeger/cmd/es-rollover/app/lookback"
1717
"github.com/jaegertracing/jaeger/cmd/es-rollover/app/rollover"
1818
"github.com/jaegertracing/jaeger/pkg/config"
19-
"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
2019
"github.com/jaegertracing/jaeger/pkg/es/client"
2120
)
2221

@@ -30,8 +29,6 @@ func main() {
3029
Long: "Jaeger es-rollover manages Jaeger indices",
3130
}
3231

33-
tlsFlags := tlscfg.ClientFlagsConfig{Prefix: "es"}
34-
3532
// Init command
3633
initCfg := &initialize.Config{}
3734
initCommand := &cobra.Command{
@@ -42,10 +39,9 @@ func main() {
4239
SilenceUsage: true,
4340
RunE: func(_ *cobra.Command, args []string) error {
4441
return app.ExecuteAction(app.ActionExecuteOptions{
45-
Args: args,
46-
Viper: v,
47-
Logger: logger,
48-
TLSFlags: tlsFlags,
42+
Args: args,
43+
Viper: v,
44+
Logger: logger,
4945
}, func(c client.Client, cfg app.Config) app.Action {
5046
initCfg.Config = cfg
5147
initCfg.InitFromViper(v)
@@ -80,10 +76,9 @@ func main() {
8076
RunE: func(_ *cobra.Command, args []string) error {
8177
rolloverCfg.InitFromViper(v)
8278
return app.ExecuteAction(app.ActionExecuteOptions{
83-
Args: args,
84-
Viper: v,
85-
Logger: logger,
86-
TLSFlags: tlsFlags,
79+
Args: args,
80+
Viper: v,
81+
Logger: logger,
8782
}, func(c client.Client, cfg app.Config) app.Action {
8883
rolloverCfg.Config = cfg
8984
rolloverCfg.InitFromViper(v)
@@ -109,10 +104,9 @@ func main() {
109104
RunE: func(_ *cobra.Command, args []string) error {
110105
lookbackCfg.InitFromViper(v)
111106
return app.ExecuteAction(app.ActionExecuteOptions{
112-
Args: args,
113-
Viper: v,
114-
Logger: logger,
115-
TLSFlags: tlsFlags,
107+
Args: args,
108+
Viper: v,
109+
Logger: logger,
116110
}, func(c client.Client, cfg app.Config) app.Action {
117111
lookbackCfg.Config = cfg
118112
lookbackCfg.InitFromViper(v)
@@ -129,7 +123,7 @@ func main() {
129123
},
130124
}
131125

132-
addPersistentFlags(v, rootCmd, tlsFlags.AddFlags, app.AddFlags)
126+
addPersistentFlags(v, rootCmd, app.AddFlags)
133127
addSubCommand(v, rootCmd, initCommand, initCfg.AddFlags)
134128
addSubCommand(v, rootCmd, rolloverCommand, rolloverCfg.AddFlags)
135129
addSubCommand(v, rootCmd, lookbackCommand, lookbackCfg.AddFlags)

0 commit comments

Comments
 (0)