-
Notifications
You must be signed in to change notification settings - Fork 72
/
consume.go
298 lines (237 loc) · 8.65 KB
/
consume.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package consume
import (
"fmt"
"net/http"
"strconv"
"time"
kafkaflagutil "github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil"
"strings"
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/kafkacmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
kafkainstanceclient "github.com/redhat-developer/app-services-sdk-go/kafkainstance/apiv1internal/client"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
"github.com/spf13/cobra"
)
const (
DefaultOffset = ""
DefaultLimit = 20
DefaultTimestamp = ""
FormatKeyValue = "key-value"
)
var outputFormatTypes = []string{dump.JSONFormat, dump.YAMLFormat, FormatKeyValue}
type options struct {
topicName string
kafkaID string
partition int32
from string
unix bool
limit int32
offset string
wait bool
outputFormat string
f *factory.Factory
}
// row is the details of a record produced needed to print to a table
type kafkaRow struct {
Topic string `json:"topic" header:"Topic"`
Key string `json:"key" header:"Key"`
Value string `json:"value" header:"Value"`
Partition int32 `json:"partition" header:"Partition"`
Offset int64 `json:"offset" header:"Offset"`
}
// NewComsumeTopicCommand creates a new command for producing to a kafka topic.
func NewConsumeTopicCommand(f *factory.Factory) *cobra.Command {
opts := &options{
f: f,
}
cmd := &cobra.Command{
Use: "consume",
Short: f.Localizer.MustLocalize("kafka.topic.consume.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("kafka.topic.consume.cmd.longDescription"),
Example: f.Localizer.MustLocalize("kafka.topic.consume.cmd.example"),
Hidden: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if opts.kafkaID == "" {
kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f)
if err != nil {
return err
}
opts.kafkaID = kafkaInstance.GetId()
}
return runCmd(opts)
},
}
flags := kafkaflagutil.NewFlagSet(cmd, f.Localizer)
flags.StringVar(&opts.topicName, "name", "", f.Localizer.MustLocalize("kafka.topic.common.flag.name.description"))
flags.Int32Var(&opts.partition, "partition", 0, f.Localizer.MustLocalize("kafka.topic.consume.flag.partition.description"))
flags.StringVar(&opts.from, "from", DefaultTimestamp, f.Localizer.MustLocalize("kafka.topic.consume.flag.from.description"))
flags.BoolVar(&opts.unix, "unix-time", false, "Use unix timestamp")
flags.BoolVar(&opts.wait, "wait", false, f.Localizer.MustLocalize("kafka.topic.consume.flag.wait.description"))
flags.StringVar(&opts.offset, "offset", DefaultOffset, f.Localizer.MustLocalize("kafka.topic.consume.flag.offset.description"))
flags.Int32Var(&opts.limit, "limit", DefaultLimit, f.Localizer.MustLocalize("kafka.topic.consume.flag.limit.description"))
flags.StringVar(&opts.outputFormat, "format", FormatKeyValue, f.Localizer.MustLocalize("kafka.topic.produce.flag.format.description"))
_ = cmd.MarkFlagRequired("name")
_ = cmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return kafkacmdutil.FilterValidTopicNameArgs(f, toComplete)
})
flags.AddInstanceID(&opts.kafkaID)
flagutil.EnableStaticFlagCompletion(cmd, "format", outputFormatTypes)
return cmd
}
func runCmd(opts *options) error {
conn, err := opts.f.Connection(connection.DefaultConfigRequireMasAuth)
if err != nil {
return err
}
api, kafkaInstance, err := conn.API().KafkaAdmin(opts.kafkaID)
if err != nil {
return err
}
// check for flags that are exclusive to eachother
if opts.offset != DefaultOffset && opts.from != DefaultTimestamp {
return opts.f.Localizer.MustLocalizeError("kafka.topic.consume.error.offsetAndFromConflict")
}
if opts.wait {
err := consumeAndWait(opts, api, kafkaInstance)
if err != nil {
return err
}
} else {
records, err := consume(opts, api, kafkaInstance)
if err != nil {
return err
}
outputRecords(opts, records)
}
return nil
}
func consumeAndWait(opts *options, api *kafkainstanceclient.APIClient, kafkaInstance *kafkamgmtclient.KafkaRequest) error {
if opts.limit != DefaultLimit {
opts.f.Logger.Info(opts.f.Localizer.MustLocalize("kafka.topic.consume.log.info.limitIgnored", localize.NewEntry("Limit", DefaultLimit)))
opts.limit = DefaultLimit
}
if opts.offset != DefaultOffset {
opts.f.Logger.Info(opts.f.Localizer.MustLocalize("kafka.topic.consume.log.info.offsetIgnored", localize.NewEntry("Offset", DefaultOffset)))
opts.offset = DefaultOffset
}
if opts.from == DefaultTimestamp {
// get current time in ISO 8601
opts.from = time.Now().Format(time.RFC3339)
}
var max_offset int64
first_consume := true
for true {
records, err := consume(opts, api, kafkaInstance)
if err != nil {
return err
}
record_count := len(records.Items)
if record_count > 0 {
max_offset = *(records.Items[record_count-1].Offset) + 1
outputRecords(opts, records)
if first_consume {
// reset timestamp after first consume as it will conflict with
// the max offset we are setting to only get new records
opts.from = DefaultTimestamp
first_consume = false
}
opts.offset = fmt.Sprint(max_offset)
}
time.Sleep(1 * time.Second)
}
return nil
}
func consume(opts *options, api *kafkainstanceclient.APIClient, kafkaInstance *kafkamgmtclient.KafkaRequest) (*kafkainstanceclient.RecordList, error) {
request := api.RecordsApi.ConsumeRecords(opts.f.Context, opts.topicName).Limit(opts.limit).Partition(opts.partition)
if opts.offset != DefaultOffset {
intOffset, err := strconv.ParseInt(opts.offset, 10, 64)
if err != nil {
return nil, opts.f.Localizer.MustLocalizeError("kafka.topic.comman.error.offsetInvalid", localize.NewEntry("Offset", opts.offset))
}
if intOffset < 0 {
return nil, opts.f.Localizer.MustLocalizeError("kafka.topic.comman.error.offsetNegative")
}
request = request.Offset(int32(intOffset))
}
if opts.from != DefaultTimestamp {
if opts.unix {
digits, err := strconv.ParseInt(opts.from, 10, 64)
if err != nil {
return nil, err
}
opts.from = time.Unix(digits, 0).Format(time.RFC3339)
opts.f.Logger.Info(opts.from)
} else {
_, err := time.Parse(time.RFC3339, opts.from)
if err != nil {
return nil, opts.f.Localizer.MustLocalizeError("kafka.topic.comman.error.timeFormat", localize.NewEntry("Time", opts.from))
}
}
request = request.Timestamp(opts.from)
}
list, httpRes, err := request.Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
if err != nil {
if httpRes == nil {
return nil, err
}
if httpRes.StatusCode == http.StatusNotFound {
return nil, opts.f.Localizer.MustLocalizeError("kafka.topic.common.error.topicNotFoundError",
localize.NewEntry("TopicName", opts.topicName),
localize.NewEntry("InstanceName", kafkaInstance.GetName()))
}
if httpRes.StatusCode == 400 {
return nil, opts.f.Localizer.MustLocalizeError("kafka.topic.common.error.partitionNotFoundError",
localize.NewEntry("Topic", opts.topicName),
localize.NewEntry("Partition", opts.partition))
}
return nil, err
}
return &list, nil
}
func outputRecords(opts *options, records *kafkainstanceclient.RecordList) {
recordsAsRows := mapRecordsToRows(opts.topicName, &records.Items)
if len(records.Items) == 0 {
opts.f.Logger.Info(opts.f.Localizer.MustLocalize("kafka.common.log.info.noRecords"))
return
}
format := opts.outputFormat
if format == dump.EmptyFormat {
format = dump.JSONFormat
}
for i := 0; i < len(recordsAsRows); i++ {
row := &recordsAsRows[i]
if format == FormatKeyValue {
if row.Key == "" {
opts.f.Logger.Info(fmt.Sprintf("Message: %v", row.Value))
} else {
opts.f.Logger.Info(fmt.Sprintf("Key: %v\nMessage: %v", row.Key, row.Value))
}
} else {
_ = dump.Formatted(opts.f.IOStreams.Out, format, row)
}
}
}
func mapRecordsToRows(topic string, records *[]kafkainstanceclient.Record) []kafkaRow {
rows := make([]kafkaRow, len(*records))
for i := 0; i < len(*records); i++ {
record := &(*records)[i]
row := kafkaRow{
Topic: topic,
Key: *record.Key,
Value: strings.TrimSuffix(record.Value, "\n"), // trailing new line gives weird printing of table
Partition: *record.Partition,
Offset: *record.Offset,
}
rows[i] = row
}
return rows
}