Skip to content

Commit

Permalink
[refactor] Seperated the parts of ES-Rollover Config common with ES C…
Browse files Browse the repository at this point in the history
…onfig

Signed-off-by: Manik2708 <[email protected]>
  • Loading branch information
Manik2708 committed Jan 9, 2025
1 parent 980dc31 commit 3b76f4b
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 85 deletions.
49 changes: 18 additions & 31 deletions cmd/es-rollover/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,52 @@ import (
"go.opentelemetry.io/collector/config/configtls"

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

var tlsFlagsCfg = tlscfg.ClientFlagsConfig{Prefix: "es"}
var (
tlsFlagsCfg = tlscfg.ClientFlagsConfig{Prefix: "es"}
esrolloverCfg = config.EsRolloverFlagConfig{}
)

const (
indexPrefix = "index-prefix"
archive = "archive"
username = "es.username"
password = "es.password"
useILM = "es.use-ilm"
ilmPolicyName = "es.ilm-policy-name"
timeout = "timeout"
skipDependencies = "skip-dependencies"
adaptiveSampling = "adaptive-sampling"
indexPrefix = "index-prefix"
username = "es.username"
password = "es.password"
useILM = "es.use-ilm"
)

// Config holds the global configurations for the es rollover, common to all actions
type Config struct {
IndexPrefix string
Archive bool
Username string
Password string
TLSEnabled bool
ILMPolicyName string
UseILM bool
Timeout int
SkipDependencies bool
AdaptiveSampling bool
TLSConfig configtls.ClientConfig
config.RolloverOptions
IndexPrefix string
Username string
Password string
TLSEnabled bool
UseILM bool
TLSConfig configtls.ClientConfig
}

// AddFlags adds flags
func AddFlags(flags *flag.FlagSet) {
esrolloverCfg.AddFlagsForRolloverOptions(flags)
flags.String(indexPrefix, "", "Index prefix")
flags.Bool(archive, false, "Handle archive indices")
flags.String(username, "", "The username required by storage")
flags.String(password, "", "The password required by storage")
flags.Bool(useILM, false, "Use ILM to manage jaeger indices")
flags.String(ilmPolicyName, "jaeger-ilm-policy", "The name of the ILM policy to use if ILM is active")
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) {
c.RolloverOptions = esrolloverCfg.InitRolloverOptionsFromViper(v)
c.IndexPrefix = v.GetString(indexPrefix)
if c.IndexPrefix != "" {
c.IndexPrefix += "-"
}
c.Archive = v.GetBool(archive)
c.Username = v.GetString(username)
c.Password = v.GetString(password)
c.ILMPolicyName = v.GetString(ilmPolicyName)
c.UseILM = v.GetBool(useILM)
c.Timeout = v.GetInt(timeout)
c.SkipDependencies = v.GetBool(skipDependencies)
c.AdaptiveSampling = v.GetBool(adaptiveSampling)
tlsCfg, err := tlsFlagsCfg.InitFromViper(v)
if err != nil {
panic(err)
Expand Down
40 changes: 19 additions & 21 deletions cmd/es-rollover/app/init/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/es-rollover/app"
"github.com/jaegertracing/jaeger/pkg/es/client"
"github.com/jaegertracing/jaeger/pkg/es/client/mocks"
"github.com/jaegertracing/jaeger/pkg/es/config"
)

func TestIndexCreateIfNotExist(t *testing.T) {
Expand Down Expand Up @@ -90,8 +91,8 @@ func TestRolloverAction(t *testing.T) {
},
config: Config{
Config: app.Config{
Archive: true,
UseILM: true,
RolloverOptions: config.RolloverOptions{Archive: true},
UseILM: true,
},
},
expectedErr: errors.New("ILM is supported only for ES version 7+"),
Expand All @@ -104,8 +105,8 @@ func TestRolloverAction(t *testing.T) {
expectedErr: errors.New("version error"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: true,
RolloverOptions: config.RolloverOptions{Archive: true},
UseILM: true,
},
},
},
Expand All @@ -118,9 +119,8 @@ func TestRolloverAction(t *testing.T) {
expectedErr: errors.New("ILM policy myilmpolicy doesn't exist in Elasticsearch. Please create it and re-run init"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: true,
ILMPolicyName: "myilmpolicy",
RolloverOptions: config.RolloverOptions{Archive: true, ILMPolicyName: "myilmpolicy"},
UseILM: true,
},
},
},
Expand All @@ -133,9 +133,8 @@ func TestRolloverAction(t *testing.T) {
expectedErr: errors.New("error getting ilm policy"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: true,
ILMPolicyName: "myilmpolicy",
RolloverOptions: config.RolloverOptions{Archive: true, ILMPolicyName: "myilmpolicy"},
UseILM: true,
},
},
},
Expand All @@ -148,8 +147,8 @@ func TestRolloverAction(t *testing.T) {
expectedErr: errors.New("error creating template"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: false,
RolloverOptions: config.RolloverOptions{Archive: true},
UseILM: false,
},
},
},
Expand All @@ -164,8 +163,8 @@ func TestRolloverAction(t *testing.T) {
expectedErr: errors.New("error getting jaeger indices"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: false,
RolloverOptions: config.RolloverOptions{Archive: true},
UseILM: false,
},
},
},
Expand All @@ -184,8 +183,8 @@ func TestRolloverAction(t *testing.T) {
expectedErr: errors.New("error creating aliases"),
config: Config{
Config: app.Config{
Archive: true,
UseILM: false,
RolloverOptions: config.RolloverOptions{Archive: true},
UseILM: false,
},
},
},
Expand All @@ -204,8 +203,8 @@ func TestRolloverAction(t *testing.T) {
expectedErr: nil,
config: Config{
Config: app.Config{
Archive: true,
UseILM: false,
RolloverOptions: config.RolloverOptions{Archive: true},
UseILM: false,
},
},
},
Expand All @@ -225,9 +224,8 @@ func TestRolloverAction(t *testing.T) {
expectedErr: nil,
config: Config{
Config: app.Config{
Archive: true,
UseILM: true,
ILMPolicyName: "jaeger-ilm",
RolloverOptions: config.RolloverOptions{Archive: true, ILMPolicyName: "jaeger-ilm"},
UseILM: true,
},
},
},
Expand Down
31 changes: 19 additions & 12 deletions cmd/es-rollover/app/lookback/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/es-rollover/app"
"github.com/jaegertracing/jaeger/pkg/es/client"
"github.com/jaegertracing/jaeger/pkg/es/client/mocks"
"github.com/jaegertracing/jaeger/pkg/es/config"
)

func TestLookBackAction(t *testing.T) {
Expand Down Expand Up @@ -81,11 +82,13 @@ func TestLookBackAction(t *testing.T) {
}).Return(nil)
},
config: Config{
Unit: "days",
UnitCount: 1,
LookBackOptions: config.LookBackOptions{
Unit: "days",
UnitCount: 1,
},
Config: app.Config{
Archive: true,
UseILM: true,
RolloverOptions: config.RolloverOptions{Archive: true},
UseILM: true,
},
},
expectedErr: nil,
Expand All @@ -96,11 +99,13 @@ func TestLookBackAction(t *testing.T) {
indexClient.On("GetJaegerIndices", "").Return(indices, errors.New("get indices error"))
},
config: Config{
Unit: "days",
UnitCount: 1,
LookBackOptions: config.LookBackOptions{
Unit: "days",
UnitCount: 1,
},
Config: app.Config{
Archive: true,
UseILM: true,
RolloverOptions: config.RolloverOptions{Archive: true},
UseILM: true,
},
},
expectedErr: errors.New("get indices error"),
Expand All @@ -111,11 +116,13 @@ func TestLookBackAction(t *testing.T) {
indexClient.On("GetJaegerIndices", "").Return([]client.Index{}, nil)
},
config: Config{
Unit: "days",
UnitCount: 1,
LookBackOptions: config.LookBackOptions{
Unit: "days",
UnitCount: 1,
},
Config: app.Config{
Archive: true,
UseILM: true,
RolloverOptions: config.RolloverOptions{Archive: true},
UseILM: true,
},
},
expectedErr: nil,
Expand Down
17 changes: 5 additions & 12 deletions cmd/es-rollover/app/lookback/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,23 @@ import (
"github.com/spf13/viper"

"github.com/jaegertracing/jaeger/cmd/es-rollover/app"
"github.com/jaegertracing/jaeger/pkg/es/config"
)

const (
unit = "unit"
unitCount = "unit-count"
defaultUnit = "days"
defaultUnitCount = 1
)
var esrolloverCfg = config.EsRolloverFlagConfig{}

// Config holds configuration for index cleaner binary.
type Config struct {
app.Config
Unit string
UnitCount int
config.LookBackOptions
}

// AddFlags adds flags for TLS to the FlagSet.
func (*Config) AddFlags(flags *flag.FlagSet) {
flags.String(unit, defaultUnit, "used with lookback to remove indices from read alias e.g, days, weeks, months, years")
flags.Int(unitCount, defaultUnitCount, "count of UNITs")
esrolloverCfg.AddFlagsForLookBackOptions(flags)
}

// InitFromViper initializes config from viper.Viper.
func (c *Config) InitFromViper(v *viper.Viper) {
c.Unit = v.GetString(unit)
c.UnitCount = v.GetInt(unitCount)
c.LookBackOptions = esrolloverCfg.InitLookBackFromViper(v)
}
5 changes: 3 additions & 2 deletions cmd/es-rollover/app/rollover/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/es-rollover/app"
"github.com/jaegertracing/jaeger/pkg/es/client"
"github.com/jaegertracing/jaeger/pkg/es/client/mocks"
"github.com/jaegertracing/jaeger/pkg/es/config"
)

func TestRolloverAction(t *testing.T) {
Expand Down Expand Up @@ -115,9 +116,9 @@ func TestRolloverAction(t *testing.T) {

rolloverAction := Action{
Config: Config{
Conditions: test.conditions,
RollBackOptions: config.RollBackOptions{Conditions: test.conditions},
Config: app.Config{
Archive: true,
RolloverOptions: config.RolloverOptions{Archive: true},
},
},
IndicesClient: indexClient,
Expand Down
12 changes: 5 additions & 7 deletions cmd/es-rollover/app/rollover/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@ import (
"github.com/spf13/viper"

"github.com/jaegertracing/jaeger/cmd/es-rollover/app"
"github.com/jaegertracing/jaeger/pkg/es/config"
)

const (
conditions = "conditions"
defaultRollbackCondition = "{\"max_age\": \"2d\"}"
)
var esrolloverCfg = config.EsRolloverFlagConfig{}

// Config holds configuration for index cleaner binary.
type Config struct {
app.Config
Conditions string
config.RollBackOptions
}

// AddFlags adds flags for TLS to the FlagSet.
func (*Config) AddFlags(flags *flag.FlagSet) {
flags.String(conditions, defaultRollbackCondition, "conditions used to rollover to a new write index")
esrolloverCfg.AddFlagsForRollBackOptions(flags)
}

// InitFromViper initializes config from viper.Viper.
func (c *Config) InitFromViper(v *viper.Viper) {
c.Conditions = v.GetString(conditions)
c.RollBackOptions = esrolloverCfg.InitRollBackFromViper(v)
}
25 changes: 25 additions & 0 deletions pkg/es/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ type BearerTokenAuthentication struct {
AllowFromContext bool `mapstructure:"from_context"`
}

type RolloverOptions struct {
// Archive if set to true will handle archive indices also
Archive bool `mapstructure:"archive"`
// The name of the ILM policy to use if ILM is active
ILMPolicyName string `mapstructure:"ilm_policy_name"`
// This stores number of seconds to wait for master node response. By default, it is set to 120
Timeout int `mapstructure:"timeout"`
// SkipDependencies if set to true will disable rollover for dependencies index
SkipDependencies bool `mapstructure:"skip_dependencies"`
// AdaptiveSampling if set to true will enable rollover for adaptive sampling index
AdaptiveSampling bool `mapstructure:"adaptive_sampling"`
}

type LookBackOptions struct {
// Unit is used with lookback to remove indices from read alias e.g, days, weeks, months, years
Unit string `mapstructure:"unit"`
// UnitCount is the count of units for which look-up is performed
UnitCount int `mapstructure:"unit-count"`
}

type RollBackOptions struct {
// Conditions stores the conditions on which index writing should be performed, for example: "{\"max_age\": \"2d\"}"
Conditions string `mapstructure:"conditions"`
}

// NewClient creates a new ElasticSearch client
func NewClient(c *Configuration, logger *zap.Logger, metricsFactory metrics.Factory) (es.Client, error) {
if len(c.Servers) < 1 {
Expand Down
Loading

0 comments on commit 3b76f4b

Please sign in to comment.