forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka.go
136 lines (110 loc) · 3.17 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
package utils
import (
"crypto/tls"
"fmt"
"net/url"
"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"
)
var (
kafkaTestData = kafka.Message{Value: []byte(testMessage)}
)
type kafkaTestWrap struct {
*v3.KafkaConfig
}
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, "parse url %s failed", url)
}
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
}
for _, v := range w.BrokerEndpoints {
url, err := url.Parse(v)
if err != nil {
return errors.Wrapf(err, "parse url %s failed", url)
}
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
}
}
if includeSendTestLog {
if err := w.sendData2Kafka(url.Host, dial, tlsConfig); err != nil {
return err
}
}
}
return nil
}
func (w *kafkaTestWrap) sendData2Kafka(smartHost string, dial dialer.Dialer, tlsConfig *tls.Config) error {
leaderConn, err := w.kafkaConn(dial, tlsConfig, smartHost)
if err != nil {
return err
}
defer leaderConn.Close()
if _, err := leaderConn.WriteMessages(kafkaTestData); err != nil {
return errors.Wrap(err, "write test message to kafka failed")
}
return nil
}
func (w *kafkaTestWrap) kafkaConn(dial dialer.Dialer, config *tls.Config, smartHost string) (*kafka.Conn, error) {
defaultPartition := 0
conn, err := newTCPConn(dial, smartHost, config, false)
if err != nil {
return nil, err
}
kafkaConn := kafka.NewConn(conn, w.Topic, defaultPartition)
topicConf := kafka.TopicConfig{
Topic: w.Topic,
NumPartitions: 1,
ReplicationFactor: 1,
}
if err := kafkaConn.CreateTopics(topicConf); err != nil {
kafkaConn.Close()
return nil, err
}
partitions, err := kafkaConn.ReadPartitions(w.Topic)
if err != nil {
kafkaConn.Close()
return nil, err
}
var leader kafka.Broker
for _, v := range partitions {
if v.ID == defaultPartition {
leader = v.Leader
}
}
leaderSmartHost := fmt.Sprintf("%s:%d", leader.Host, leader.Port)
if leaderSmartHost == smartHost {
return kafkaConn, nil
}
kafkaConn.Close()
LeaderConn, err := newTCPConn(dial, leaderSmartHost, config, false)
if err != nil {
return nil, err
}
return kafka.NewConn(LeaderConn, w.Topic, defaultPartition), nil
}