/
broker_configs_command.go
250 lines (204 loc) · 8.05 KB
/
broker_configs_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/landoop/lenses-go"
"github.com/landoop/bite"
"github.com/spf13/cobra"
)
func init() {
// Hidden: true
// app.AddCommand(newDynamicClusterConfigsGroupCommand())
// app.AddCommand(newDynamicBrokerConfigsGroupCommand())
}
func newDynamicClusterConfigsGroupCommand() *cobra.Command {
root := &cobra.Command{
// currently we don't have an API to retrieve all brokers' ids(...),
// and no cluster "static" configs,
// so the only thing it will print is the dynamic updated configs for all brokers for a kafka cluster.
Use: "cluster",
Short: "Work with the dynamic updated configurations for a kafka cluster",
Example: `cluster configs`,
SilenceErrors: true,
TraverseChildren: true,
}
root.AddCommand(newGetDynamicClusterConfigsCommand())
return root
}
func newGetDynamicClusterConfigsCommand() *cobra.Command {
rootSub := &cobra.Command{
Use: "configs",
Short: "List the dynamic updated configurations for a kafka cluster",
Example: `cluster configs`,
SilenceErrors: true,
TraverseChildren: true,
RunE: func(cmd *cobra.Command, args []string) error {
configs, err := client.GetDynamicClusterConfigs()
if err != nil {
return err
}
return bite.PrintObject(cmd, configs)
},
}
rootSub.AddCommand(newSetDynamicClusterConfigsCommand())
rootSub.AddCommand(newDeleteDynamicClusterConfigsCommand())
return rootSub
}
func newSetDynamicClusterConfigsCommand() *cobra.Command {
var (
configsRaw string
configs lenses.BrokerConfig
)
cmd := &cobra.Command{
Use: "set",
Aliases: []string{"add", "update"},
Short: "Add or update configuration for a kafka cluster dynamically",
Example: `cluster configs set --configs=file.yml/json or --configs="{\"log.cleaner.threads\": 2, \"compression.type\": \"snappy\"}"`,
SilenceErrors: true,
TraverseChildren: true,
RunE: func(cmd *cobra.Command, args []string) error {
bite.FriendlyError(cmd, errResourceInternal, "failed to retrieve cluster configurations")
bite.FriendlyError(cmd, errResourceNotGoodMessage, "unknown configurations where provided: %#+v", configs)
if err := bite.TryReadFile(configsRaw, &configs); err != nil {
// from flag as json.
if err = json.Unmarshal([]byte(configsRaw), &configs); err != nil {
return fmt.Errorf("unable to unmarshal the configs: %v. Try using a yaml or json file instead", err)
}
}
err := client.UpdateDynamicClusterConfigs(configs)
if err != nil {
return err
}
return bite.PrintInfo(cmd, "Cluster configs updated")
},
}
cmd.Flags().StringVar(&configsRaw, "configs", "", `--configs="{\"log.cleaner.threads\": 2, \"compression.type\": \"snappy\"}`)
cmd.MarkFlagRequired("configs")
bite.CanBeSilent(cmd)
return cmd
}
func newDeleteDynamicClusterConfigsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"reset"},
Short: "Delete/reset cluster configuration dynamically, separate them by space",
Example: `cluster configs delete log.cleaner.threads compression.type snappy`,
SilenceErrors: true,
TraverseChildren: true,
RunE: func(cmd *cobra.Command, keysToBeReseted []string) error {
if len(keysToBeReseted) == 0 {
return bite.PrintInfo(cmd, "keys are required, pass the config's keys to be removed/reset to their default through command's arguments separated by space")
}
bite.FriendlyError(cmd, errResourceInternal, "failed to retrieve cluster configurations")
keysStr := strings.Join(keysToBeReseted, ", ")
bite.FriendlyError(cmd, errResourceNotGoodMessage, "unknown keys where provided: %s", keysStr)
err := client.DeleteDynamicClusterConfigs(keysToBeReseted...)
if err != nil {
return err
}
return bite.PrintInfo(cmd, "Cluster configs %s reseted", keysStr)
},
}
bite.CanBeSilent(cmd)
return cmd
}
func newDynamicBrokerConfigsGroupCommand() *cobra.Command {
root := &cobra.Command{
Use: "broker",
Short: "Work with broker configurations",
Example: `broker configs --broker=brokerID`,
SilenceErrors: true,
TraverseChildren: true,
}
root.AddCommand(newGetDynamicBrokerConfigsCommand())
return root
}
func newGetDynamicBrokerConfigsCommand() *cobra.Command {
var brokerID int
rootSub := &cobra.Command{
Use: "configs",
Short: "List the dynamic updated configurations for a kafka broker",
Example: `broker configs --broker=brokerID`,
SilenceErrors: true,
TraverseChildren: true,
RunE: func(cmd *cobra.Command, args []string) error {
bite.FriendlyError(cmd, errResourceInternal, "could not retrieve configurations for broker: %d", brokerID)
bite.FriendlyError(cmd, errResourceNotFoundMessage, "could not retrieve broker: %d", brokerID)
configs, err := client.GetDynamicBrokerConfigs(brokerID)
if err != nil {
return err
}
return bite.PrintObject(cmd, configs)
},
}
rootSub.Flags().IntVar(&brokerID, "broker", 0, "--broker=42")
rootSub.MarkFlagRequired("broker")
rootSub.AddCommand(newSetDynamicBrokerConfigsCommand())
rootSub.AddCommand(newDeleteDynamicBrokerConfigsCommand())
return rootSub
}
func newSetDynamicBrokerConfigsCommand() *cobra.Command {
var (
brokerID int
configsRaw string
configs lenses.BrokerConfig
)
cmd := &cobra.Command{
Use: "set",
Aliases: []string{"add", "update"},
Short: "Add or update broker configuration dynamically",
Example: `broker configs set --broker=brokerID --configs=file.yml/json or --configs="{\"log.cleaner.threads\": 2, \"compression.type\": \"snappy\"}"`,
SilenceErrors: true,
TraverseChildren: true,
RunE: func(cmd *cobra.Command, args []string) error {
bite.FriendlyError(cmd, errResourceInternal, "failed to retrieve broker configurations", brokerID)
bite.FriendlyError(cmd, errResourceNotGoodMessage, "unknown configurations where provided: %#+v", configs)
if err := bite.TryReadFile(configsRaw, &configs); err != nil {
// from flag as json.
if err = json.Unmarshal([]byte(configsRaw), &configs); err != nil {
return fmt.Errorf("unable to unmarshal the configs: %v. Try using a yaml or json file instead", err)
}
}
err := client.UpdateDynamicBrokerConfigs(brokerID, configs)
if err != nil {
return err
}
return bite.PrintInfo(cmd, "Configs updated for broker with id: %d", brokerID)
},
}
cmd.Flags().IntVar(&brokerID, "broker", 0, "--broker=brokerID")
cmd.MarkFlagRequired("broker")
cmd.Flags().StringVar(&configsRaw, "configs", "", `--configs="{\"log.cleaner.threads\": 2, \"compression.type\": \"snappy\"}`)
cmd.MarkFlagRequired("configs")
bite.CanBeSilent(cmd)
return cmd
}
func newDeleteDynamicBrokerConfigsCommand() *cobra.Command {
var brokerID int
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"reset"},
Short: "Delete/reset broker configuration dynamically, separate them by space",
Example: `broker configs delete --broker=brokerID log.cleaner.threads compression.type snappy`,
SilenceErrors: true,
TraverseChildren: true,
RunE: func(cmd *cobra.Command, keysToBeReseted []string) error {
if len(keysToBeReseted) == 0 {
return bite.PrintInfo(cmd, "keys are required, pass the config's keys to be removed/reseted to their default values through command's arguments separated by space")
}
bite.FriendlyError(cmd, errResourceInternal, "could not retrieve configurations for broker: %d", brokerID)
keysStr := strings.Join(keysToBeReseted, ", ")
bite.FriendlyError(cmd, errResourceNotGoodMessage, "unknown keys where provided: %s", keysStr)
err := client.DeleteDynamicBrokerConfigs(brokerID, keysToBeReseted...)
if err != nil {
return err
}
return bite.PrintInfo(cmd, "Configs %s reseted for broker with id: %d", keysStr, brokerID)
},
}
cmd.Flags().IntVar(&brokerID, "broker", 0, "--broker=brokerID")
cmd.MarkFlagRequired("broker")
bite.CanBeSilent(cmd)
return cmd
}