Skip to content

Commit 4b41f0b

Browse files
subcmd/apply: Support fail-safe approach (#185)
Up until now, the `apply` command implemented a fail-fast strategy. This means that an attempt to configure a batch of topics (by passing a path to a folder contains a list of topics configurations files) will be terminated upon the first failure (w/o even trying to apply the configuration for the next-in-line topics). This commit introduces the fail-safe support: It allows the user to run the `apply` command in a fail-safe manner, which means that instead of failing upon the first failure, we'll simply continue to the next topic configuration, while aggregating all the errors along the way. Eventually, if there were any errors, all of them will be shown. Signed-off-by: shimon-armis <[email protected]>
1 parent 25a6f37 commit 4b41f0b

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

cmd/topicctl/subcmd/apply.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type applyCmdConfig struct {
3737
skipConfirm bool
3838
ignoreFewerPartitionsError bool
3939
sleepLoopDuration time.Duration
40+
failFast bool
4041

4142
shared sharedOptions
4243

@@ -112,6 +113,12 @@ func init() {
112113
10*time.Second,
113114
"Amount of time to wait between partition checks",
114115
)
116+
applyCmd.Flags().BoolVar(
117+
&applyConfig.failFast,
118+
"fail-fast",
119+
true,
120+
"Fail upon the first error encountered during apply process",
121+
)
115122

116123
addSharedConfigOnlyFlags(applyCmd, &applyConfig.shared)
117124
RootCmd.AddCommand(applyCmd)
@@ -132,6 +139,14 @@ func applyPreRun(cmd *cobra.Command, args []string) error {
132139
return nil
133140
}
134141

142+
func appendError(aggregatedErr error, err error) error {
143+
if aggregatedErr == nil {
144+
return err
145+
}
146+
147+
return fmt.Errorf("%v\n%v", aggregatedErr, err)
148+
}
149+
135150
func applyRun(cmd *cobra.Command, args []string) error {
136151
ctx, cancel := context.WithCancel(context.Background())
137152
defer cancel()
@@ -145,6 +160,8 @@ func applyRun(cmd *cobra.Command, args []string) error {
145160

146161
// Keep a cache of the admin clients with the cluster config path as the key
147162
adminClients := map[string]admin.Client{}
163+
// Keep track of any errors that occur during the apply process
164+
var errs error
148165

149166
defer func() {
150167
for _, adminClient := range adminClients {
@@ -167,7 +184,10 @@ func applyRun(cmd *cobra.Command, args []string) error {
167184
for _, match := range matches {
168185
matchCount++
169186
if err := applyTopic(ctx, match, adminClients); err != nil {
170-
return err
187+
if applyConfig.failFast {
188+
return err
189+
}
190+
errs = appendError(errs, err)
171191
}
172192
}
173193
}
@@ -176,7 +196,7 @@ func applyRun(cmd *cobra.Command, args []string) error {
176196
return fmt.Errorf("No topic configs match the provided args (%+v)", args)
177197
}
178198

179-
return nil
199+
return errs
180200
}
181201

182202
func applyTopic(

0 commit comments

Comments
 (0)