Skip to content

Commit 0ab75b1

Browse files
authored
feat(migrations): Add migration for inputs.zookeeper TLS option (#19098)
1 parent 2e3a1c1 commit 0ab75b1

8 files changed

Lines changed: 190 additions & 2 deletions

File tree

migrations/all/inputs_zookeeper.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build !custom || (migrations && (inputs || inputs.zookeeper))
2+
3+
package all
4+
5+
import _ "github.com/influxdata/telegraf/migrations/inputs_zookeeper" // register migration
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package inputs_zookeeper
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/influxdata/toml"
8+
"github.com/influxdata/toml/ast"
9+
10+
"github.com/influxdata/telegraf/migrations"
11+
)
12+
13+
// Migration function to migrate the deprecated 'enable_tls' option to its
14+
// replacement 'tls_enable'.
15+
func migrate(tbl *ast.Table) ([]byte, string, error) {
16+
// Decode the old data structure
17+
var plugin map[string]interface{}
18+
if err := toml.UnmarshalTable(tbl, &plugin); err != nil {
19+
return nil, "", err
20+
}
21+
22+
// Check for the deprecated option and migrate it
23+
var applied bool
24+
if rawOldValue, found := plugin["enable_tls"]; found {
25+
applied = true
26+
27+
// Convert to the actual type
28+
oldValue, ok := rawOldValue.(bool)
29+
if !ok {
30+
return nil, "", fmt.Errorf("unexpected type %T for 'enable_tls'", rawOldValue)
31+
}
32+
33+
// Check if the new option already exists and if it has a contradicting
34+
// value. If the new option is not present, migrate the old value.
35+
if rawNewValue, found := plugin["tls_enable"]; found {
36+
if newValue, ok := rawNewValue.(bool); !ok {
37+
return nil, "", fmt.Errorf("unexpected type %T for 'tls_enable'", rawNewValue)
38+
} else if newValue != oldValue {
39+
return nil, "", errors.New("contradicting setting for 'tls_enable' and 'enable_tls'")
40+
}
41+
} else {
42+
plugin["tls_enable"] = oldValue
43+
}
44+
45+
// Remove the deprecated setting
46+
delete(plugin, "enable_tls")
47+
}
48+
49+
// No options migrated so we can exit early
50+
if !applied {
51+
return nil, "", migrations.ErrNotApplicable
52+
}
53+
54+
// Create the corresponding plugin configuration
55+
cfg := migrations.CreateTOMLStruct("inputs", "zookeeper")
56+
cfg.Add("inputs", "zookeeper", plugin)
57+
58+
output, err := toml.Marshal(cfg)
59+
return output, "", err
60+
}
61+
62+
// Register the migration function for the plugin type
63+
func init() {
64+
migrations.AddPluginOptionMigration("inputs.zookeeper", migrate)
65+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package inputs_zookeeper_test
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/influxdata/telegraf/config"
11+
_ "github.com/influxdata/telegraf/migrations/inputs_zookeeper" // register migration
12+
"github.com/influxdata/telegraf/plugins/inputs/zookeeper" // register plugin
13+
)
14+
15+
func TestNoMigration(t *testing.T) {
16+
plugin := &zookeeper.Zookeeper{}
17+
defaultCfg := plugin.SampleConfig()
18+
19+
// Migrate and check that nothing changed
20+
output, n, err := config.ApplyMigrations([]byte(defaultCfg))
21+
require.NoError(t, err)
22+
require.NotEmpty(t, output)
23+
require.Zero(t, n)
24+
require.Equal(t, defaultCfg, string(output))
25+
}
26+
27+
func TestEnableTLSConflict(t *testing.T) {
28+
cfg := []byte(`
29+
[[inputs.zookeeper]]
30+
servers = ["localhost:2181"]
31+
enable_tls = true
32+
tls_enable = false
33+
`)
34+
// Migrate and check that the contradicting settings are caught
35+
output, n, err := config.ApplyMigrations(cfg)
36+
require.ErrorContains(t, err, "contradicting setting for 'tls_enable' and 'enable_tls'")
37+
require.Empty(t, output)
38+
require.Zero(t, n)
39+
}
40+
41+
func TestCases(t *testing.T) {
42+
// Get all directories in testcases
43+
folders, err := os.ReadDir("testcases")
44+
require.NoError(t, err)
45+
46+
for _, f := range folders {
47+
// Only handle folders
48+
if !f.IsDir() {
49+
continue
50+
}
51+
52+
t.Run(f.Name(), func(t *testing.T) {
53+
testcasePath := filepath.Join("testcases", f.Name())
54+
inputFile := filepath.Join(testcasePath, "telegraf.conf")
55+
expectedFile := filepath.Join(testcasePath, "expected.conf")
56+
57+
// Read the expected output
58+
expected := config.NewConfig()
59+
require.NoError(t, expected.LoadConfig(expectedFile))
60+
require.NotEmpty(t, expected.Inputs)
61+
62+
// Read the input data
63+
input, remote, err := config.LoadConfigFile(inputFile)
64+
require.NoError(t, err)
65+
require.False(t, remote)
66+
require.NotEmpty(t, input)
67+
68+
// Migrate
69+
output, n, err := config.ApplyMigrations(input)
70+
require.NoError(t, err)
71+
require.NotEmpty(t, output)
72+
require.GreaterOrEqual(t, n, uint64(1))
73+
actual := config.NewConfig()
74+
require.NoError(t, actual.LoadConfigData(output, config.EmptySourcePath))
75+
76+
// Test the output
77+
require.Len(t, actual.Inputs, len(expected.Inputs))
78+
actualIDs := make([]string, 0, len(expected.Inputs))
79+
expectedIDs := make([]string, 0, len(expected.Inputs))
80+
for i := range actual.Inputs {
81+
actualIDs = append(actualIDs, actual.Inputs[i].ID())
82+
expectedIDs = append(expectedIDs, expected.Inputs[i].ID())
83+
}
84+
require.ElementsMatch(t, expectedIDs, actualIDs, string(output))
85+
})
86+
}
87+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Reads 'mntr' stats from one or many zookeeper servers
2+
[[inputs.zookeeper]]
3+
## An array of address to gather stats about. Specify an ip or hostname
4+
## with port. ie localhost:2181, 10.0.0.1:2181, etc.
5+
servers = ["localhost:2181"]
6+
7+
## Both the deprecated and the new option set to the same value
8+
tls_enable = true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Reads 'mntr' stats from one or many zookeeper servers
2+
[[inputs.zookeeper]]
3+
## An array of address to gather stats about. Specify an ip or hostname
4+
## with port. ie localhost:2181, 10.0.0.1:2181, etc.
5+
servers = ["localhost:2181"]
6+
7+
## Both the deprecated and the new option set to the same value
8+
enable_tls = true
9+
tls_enable = true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Reads 'mntr' stats from one or many zookeeper servers
2+
[[inputs.zookeeper]]
3+
## An array of address to gather stats about. Specify an ip or hostname
4+
## with port. ie localhost:2181, 10.0.0.1:2181, etc.
5+
servers = ["localhost:2181"]
6+
7+
## Enable TLS using the deprecated option
8+
tls_enable = true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Reads 'mntr' stats from one or many zookeeper servers
2+
[[inputs.zookeeper]]
3+
## An array of address to gather stats about. Specify an ip or hostname
4+
## with port. ie localhost:2181, 10.0.0.1:2181, etc.
5+
servers = ["localhost:2181"]
6+
7+
## Enable TLS using the deprecated option
8+
enable_tls = true

plugins/inputs/zookeeper/zookeeper.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ type Zookeeper struct {
3030
Timeout config.Duration `toml:"timeout"`
3131
ParseFloats string `toml:"parse_floats"`
3232

33-
EnableTLS bool `toml:"enable_tls" deprecated:"1.37.0;1.40.0;use 'tls_enable' instead"`
3433
common_tls.ClientConfig
3534

3635
tlsConfig *tls.Config
@@ -41,7 +40,6 @@ func (*Zookeeper) SampleConfig() string {
4140
}
4241

4342
func (z *Zookeeper) Init() error {
44-
z.ClientConfig.Enable = &z.EnableTLS
4543
tlsConfig, err := z.ClientConfig.TLSConfig()
4644
if err != nil {
4745
return err

0 commit comments

Comments
 (0)