forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka.go
252 lines (212 loc) · 6.54 KB
/
kafka.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
package utils
import (
"crypto/tls"
"fmt"
"io"
"net/url"
"time"
"github.com/pkg/errors"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config/dialer"
kafka "github.com/segmentio/kafka-go"
)
const (
defaultPartition = 0
)
type kafkaTestWrap struct {
*v3.KafkaConfig
}
func getKafkaTestData() kafka.Message {
return kafka.Message{
Value: []byte(time.Now().Format(time.RFC1123Z) + " " + testMessage),
Time: time.Now(),
}
}
func (w *kafkaTestWrap) TestReachable(dial dialer.Dialer, includeSendTestLog bool) error {
if w.SaslUsername != "" && w.SaslPassword != "" {
//TODO: Now we don't have a out of the box Kafka go client fit our request which both support sasl and could pass conn to it.
//kafka-go has a PR to support sasl, but not merge yet due to the mantainer want support Negotiation and Kerberos as well, we will add test func to sasl after the sasl in kafka-go is stable
return nil
}
if w.ZookeeperEndpoint != "" {
url, err := url.Parse(w.ZookeeperEndpoint)
if err != nil {
return errors.Wrapf(err, "couldn't parse url %s", w.ZookeeperEndpoint)
}
var tlsConfig *tls.Config
if url.Scheme == "https" {
tlsConfig, err = buildTLSConfig(w.Certificate, w.ClientCert, w.ClientKey, "", "", url.Hostname(), true)
if err != nil {
return err
}
}
conn, err := newTCPConn(dial, url.Host, tlsConfig, true)
if err != nil {
return err
}
conn.Close()
return nil
}
if len(w.BrokerEndpoints) == 0 {
return errors.New("broker endpoint is empty")
}
broker0URL, err := url.Parse(w.BrokerEndpoints[0])
if err != nil {
return errors.Wrapf(err, "couldn't parse url %s", w.BrokerEndpoints[0])
}
var broker0TLSConfig *tls.Config
if broker0URL.Scheme == "https" {
if broker0TLSConfig, err = buildTLSConfig(w.Certificate, w.ClientCert, w.ClientKey, "", "", broker0URL.Hostname(), true); err != nil {
return err
}
}
broker0Host := broker0URL.Host
broker0Conn, err := w.kafkaConn(dial, broker0TLSConfig, broker0Host)
if err != nil {
return err
}
defer broker0Conn.Close()
brokers, err := broker0Conn.Brokers()
if err != nil {
return errors.Wrap(err, "couldn't get broker list")
}
topicConf := kafka.TopicConfig{
Topic: w.Topic,
NumPartitions: len(brokers),
ReplicationFactor: 1,
}
brokerController, err := broker0Conn.Controller()
if err != nil {
return errors.Wrap(err, "couldn't get broker controller")
}
brokerControllerHost := brokerHost(brokerController.Host, brokerController.Port)
if brokerControllerHost == broker0Host {
if err := broker0Conn.CreateTopics(topicConf); err != nil {
return errors.Wrapf(err, "couldn't create topic %s", w.Topic)
}
if includeSendTestLog {
if err = w.sendData2Kafka(broker0Conn, dial); err != nil {
return err
}
}
} else {
var brokerControllerTLSConfig *tls.Config
if broker0URL.Scheme == "https" {
if brokerControllerTLSConfig, err = buildTLSConfig(w.Certificate, w.ClientCert, w.ClientKey, "", "", brokerController.Host, true); err != nil {
return err
}
}
brokerControllerConn, err := w.kafkaConn(dial, brokerControllerTLSConfig, brokerControllerHost)
if err != nil {
return err
}
defer brokerControllerConn.Close()
if err := brokerControllerConn.CreateTopics(topicConf); err != nil {
return errors.Wrapf(err, "couldn't create topic %s", w.Topic)
}
if includeSendTestLog {
if err = w.sendData2Kafka(brokerControllerConn, dial); err != nil {
return err
}
}
}
brokerMap := make(map[string]kafka.Broker)
for _, v := range brokers {
host := brokerHost(v.Host, v.Port)
brokerMap[host] = v
}
for _, v := range w.BrokerEndpoints[1:] {
endpointURL, err := url.Parse(v)
if err != nil {
return errors.Wrapf(err, "couldn't parse url %s", v)
}
if endpointURL.Host == brokerControllerHost || endpointURL.Host == broker0Host {
continue
}
if _, ok := brokerMap[endpointURL.Host]; !ok {
return errors.New(v + " isn't included in broker list")
}
if err = w.checkEndpointReachable(endpointURL, dial); err != nil {
return err
}
}
return nil
}
func (w *kafkaTestWrap) checkEndpointReachable(endpointURL *url.URL, dial dialer.Dialer) error {
var tlsConfig *tls.Config
var err error
if endpointURL.Scheme == "https" {
tlsConfig, err = buildTLSConfig(w.Certificate, w.ClientCert, w.ClientKey, "", "", endpointURL.Hostname(), true)
if err != nil {
return err
}
}
conn, err := w.kafkaConn(dial, tlsConfig, endpointURL.Host)
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Controller()
if err != nil {
return errors.Wrapf(err, "couldn't get response from %s", endpointURL.Host)
}
return nil
}
func (w *kafkaTestWrap) getKafkaPartitionLeader(kafkaConn *kafka.Conn) (*kafka.Broker, error) {
partitions, err := kafkaConn.ReadPartitions(w.Topic)
if err != nil {
return nil, errors.Wrap(wrapErrEOF(err), "couldn't read kafka partitions")
}
var patitionLeader kafka.Broker
for _, v := range partitions {
if v.ID == defaultPartition {
patitionLeader = v.Leader
}
}
if &patitionLeader == nil {
return nil, errors.New("couldn't get topic partition leader")
}
return &patitionLeader, nil
}
func (w *kafkaTestWrap) sendData2Kafka(kafkaConn *kafka.Conn, dial dialer.Dialer) error {
patitionLeader, err := w.getKafkaPartitionLeader(kafkaConn)
if err != nil {
return err
}
patitionLeaderHost := brokerHost(patitionLeader.Host, patitionLeader.Port)
if patitionLeaderHost == kafkaConn.RemoteAddr().String() {
if _, err := kafkaConn.WriteMessages(getKafkaTestData()); err != nil {
return errors.Wrap(err, "couldn't write test message to kafka")
}
return nil
}
tlsConfig, err := buildTLSConfig(w.Certificate, w.ClientCert, w.ClientKey, "", "", patitionLeader.Host, true)
if err != nil {
return err
}
patitionLeaderConn, err := w.kafkaConn(dial, tlsConfig, patitionLeaderHost)
if err != nil {
return err
}
defer patitionLeaderConn.Close()
if _, err := patitionLeaderConn.WriteMessages(getKafkaTestData()); err != nil {
return errors.Wrap(err, "couldn't write test message to kafka")
}
return nil
}
func (w *kafkaTestWrap) kafkaConn(dial dialer.Dialer, config *tls.Config, smartHost string) (*kafka.Conn, error) {
conn, err := newTCPConn(dial, smartHost, config, false)
if err != nil {
return nil, err
}
return kafka.NewConn(conn, w.Topic, defaultPartition), nil
}
func wrapErrEOF(err error) error {
if err == io.EOF {
return errors.New("unexpected EOF, connection closed by remote server")
}
return err
}
func brokerHost(hostName string, port int) string {
return fmt.Sprintf("%s:%d", hostName, port)
}