Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpk: small improvements for rpk-trim #11957

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ help text for more details on why.`, broker)
err = cl.DecommissionBroker(cmd.Context(), broker)
out.MaybeDie(err, "unable to decommission broker: %v", err)

fmt.Printf("Success, broker %d decommission started. Use `rpk redpanda admin brokers decommission-status %d` to monitor data movement.`", broker, broker)
fmt.Printf("Success, broker %d decommission started. Use `rpk redpanda admin brokers decommission-status %d` to monitor data movement.\n`", broker, broker)
},
}

Expand Down
21 changes: 18 additions & 3 deletions src/go/rpk/pkg/cli/topic/trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -80,6 +81,9 @@ Trim records from a JSON file
if fromFile != "" {
o, err = parseOffsetFile(fs, fromFile, args)
out.MaybeDie(err, "unable to parse file %q: %v", fromFile, err)
if len(o) == 0 {
out.Die("offset file is empty")
}
} else {
if len(args) == 0 {
out.Die("Error: required arg 'topic' not set\n%v", cmd.UsageString())
Expand All @@ -90,6 +94,9 @@ Trim records from a JSON file
topic := args[0]
o, err = parseOffsetArgs(cmd.Context(), adm, topic, offset, partitions)
out.MaybeDieErr(err)
if len(o) == 0 {
out.Die("topic %q does not exists", topic)
}
}
if !noConfirm {
err := printDeleteRecordRequest(cmd.Context(), adm, o)
Expand All @@ -104,7 +111,10 @@ Trim records from a JSON file
}
drr, err := adm.DeleteRecords(cmd.Context(), o)
out.MaybeDie(err, "unable to trim records: %v", err)
printDeleteRecordResponse(drr)
ok := printDeleteRecordResponse(drr)
if !ok { // This means that at least 1 row contained an error.
os.Exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not out.Die?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to exit with code 1, we won't print any message since it's already in the table created by printDeleteRecordResponse

}
},
}

Expand Down Expand Up @@ -182,6 +192,9 @@ func parseOffsetArgs(ctx context.Context, adm *kadm.Client, topic, offset string
if !ok {
return nil, fmt.Errorf("unable to find offset %q for topic %q in partition %v: %v", offset, topic, p, err)
}
if l.Err != nil {
return nil, l.Err
}
parsedOffset = l.Offset
}
o.Add(kadm.Offset{
Expand Down Expand Up @@ -265,15 +278,17 @@ func printDeleteRecordRequest(ctx context.Context, adm *kadm.Client, o kadm.Offs
return rerr
}

func printDeleteRecordResponse(drr kadm.DeleteRecordsResponses) {
func printDeleteRecordResponse(drr kadm.DeleteRecordsResponses) (ok bool) {
tw := out.NewTable("TOPIC", "PARTITION", "NEW-START-OFFSET", "ERROR")
defer tw.Flush()

ok = true
drr.Each(func(r kadm.DeleteRecordsResponse) {
if r.Err != nil {
tw.Print(r.Topic, r.Partition, "-", r.Err)
ok = false
} else {
tw.Print(r.Topic, r.Partition, r.LowWatermark, "")
}
})
return
}