-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathkafka_scaler.go
373 lines (306 loc) · 10.8 KB
/
kafka_scaler.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package scalers
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/Shopify/sarama"
v2beta1 "k8s.io/api/autoscaling/v2beta1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
type kafkaScaler struct {
metadata kafkaMetadata
client sarama.Client
admin sarama.ClusterAdmin
}
type kafkaMetadata struct {
bootstrapServers []string
group string
topic string
lagThreshold int64
// auth
authMode kafkaAuthMode
username string
password string
// ssl
cert string
key string
ca string
}
type kafkaAuthMode string
const (
kafkaAuthModeForNone kafkaAuthMode = "none"
kafkaAuthModeForSaslPlaintext kafkaAuthMode = "sasl_plaintext"
kafkaAuthModeForSaslScramSha256 kafkaAuthMode = "sasl_scram_sha256"
kafkaAuthModeForSaslScramSha512 kafkaAuthMode = "sasl_scram_sha512"
kafkaAuthModeForSaslSSL kafkaAuthMode = "sasl_ssl"
kafkaAuthModeForSaslSSLPlain kafkaAuthMode = "sasl_ssl_plain"
)
const (
lagThresholdMetricName = "lagThreshold"
kafkaMetricType = "External"
defaultKafkaLagThreshold = 10
)
var kafkaLog = logf.Log.WithName("kafka_scaler")
// NewKafkaScaler creates a new kafkaScaler
func NewKafkaScaler(resolvedEnv, metadata, authParams map[string]string) (Scaler, error) {
kafkaMetadata, err := parseKafkaMetadata(resolvedEnv, metadata, authParams)
if err != nil {
return nil, fmt.Errorf("error parsing kafka metadata: %s", err)
}
client, admin, err := getKafkaClients(kafkaMetadata)
if err != nil {
return nil, err
}
return &kafkaScaler{
client: client,
admin: admin,
metadata: kafkaMetadata,
}, nil
}
func parseKafkaMetadata(resolvedEnv, metadata, authParams map[string]string) (kafkaMetadata, error) {
meta := kafkaMetadata{}
// brokerList marked as deprecated, bootstrapServers is the new one to use
if metadata["brokerList"] != "" && metadata["bootstrapServers"] != "" {
return meta, errors.New("cannot specify both bootstrapServers and brokerList (deprecated)")
}
if metadata["brokerList"] == "" && metadata["bootstrapServers"] == "" {
return meta, errors.New("no bootstrapServers or brokerList (deprecated) given")
}
if metadata["bootstrapServers"] != "" {
meta.bootstrapServers = strings.Split(metadata["bootstrapServers"], ",")
}
if metadata["brokerList"] != "" {
kafkaLog.V(0).Info("WARNING: usage of brokerList is deprecated. use bootstrapServers instead.")
meta.bootstrapServers = strings.Split(metadata["brokerList"], ",")
}
if metadata["consumerGroup"] == "" {
return meta, errors.New("no consumer group given")
}
meta.group = metadata["consumerGroup"]
if metadata["topic"] == "" {
return meta, errors.New("no topic given")
}
meta.topic = metadata["topic"]
meta.lagThreshold = defaultKafkaLagThreshold
if val, ok := metadata[lagThresholdMetricName]; ok {
t, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return meta, fmt.Errorf("error parsing %s: %s", lagThresholdMetricName, err)
}
meta.lagThreshold = t
}
meta.authMode = kafkaAuthModeForNone
if val, ok := authParams["authMode"]; ok {
val = strings.TrimSpace(val)
mode := kafkaAuthMode(val)
if mode != kafkaAuthModeForNone && mode != kafkaAuthModeForSaslPlaintext && mode != kafkaAuthModeForSaslSSL && mode != kafkaAuthModeForSaslSSLPlain && mode != kafkaAuthModeForSaslScramSha256 && mode != kafkaAuthModeForSaslScramSha512 {
return meta, fmt.Errorf("err auth mode %s given", mode)
}
meta.authMode = mode
}
if meta.authMode != kafkaAuthModeForNone && meta.authMode != kafkaAuthModeForSaslSSL {
if authParams["username"] == "" {
return meta, errors.New("no username given")
}
meta.username = strings.TrimSpace(authParams["username"])
if authParams["password"] == "" {
return meta, errors.New("no password given")
}
meta.password = strings.TrimSpace(authParams["password"])
}
if meta.authMode == kafkaAuthModeForSaslSSL {
if authParams["ca"] == "" {
return meta, errors.New("no ca given")
}
meta.ca = authParams["ca"]
if authParams["cert"] == "" {
return meta, errors.New("no cert given")
}
meta.cert = authParams["cert"]
if authParams["key"] == "" {
return meta, errors.New("no key given")
}
meta.key = authParams["key"]
}
return meta, nil
}
// IsActive determines if we need to scale from zero
func (s *kafkaScaler) IsActive(ctx context.Context) (bool, error) {
partitions, err := s.getPartitions()
if err != nil {
return false, err
}
offsets, err := s.getOffsets(partitions)
if err != nil {
return false, err
}
for _, partition := range partitions {
lag := s.getLagForPartition(partition, offsets)
kafkaLog.V(1).Info(fmt.Sprintf("Group %s has a lag of %d for topic %s and partition %d\n", s.metadata.group, lag, s.metadata.topic, partition))
// Return as soon as a lag was detected for any partition
if lag > 0 {
return true, nil
}
}
return false, nil
}
func getKafkaClients(metadata kafkaMetadata) (sarama.Client, sarama.ClusterAdmin, error) {
config := sarama.NewConfig()
config.Version = sarama.V1_0_0_0
if ok := metadata.authMode == kafkaAuthModeForSaslPlaintext || metadata.authMode == kafkaAuthModeForSaslSSLPlain || metadata.authMode == kafkaAuthModeForSaslScramSha256 || metadata.authMode == kafkaAuthModeForSaslScramSha512; ok {
config.Net.SASL.Enable = true
config.Net.SASL.User = metadata.username
config.Net.SASL.Password = metadata.password
}
if metadata.authMode == kafkaAuthModeForSaslSSLPlain {
config.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypePlaintext)
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
ClientAuth: 0,
}
config.Net.TLS.Enable = true
config.Net.TLS.Config = tlsConfig
config.Net.DialTimeout = 10 * time.Second
}
if metadata.authMode == kafkaAuthModeForSaslSSL {
cert, err := tls.X509KeyPair([]byte(metadata.cert), []byte(metadata.key))
if err != nil {
return nil, nil, fmt.Errorf("error parse X509KeyPair: %s", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(metadata.ca))
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
config.Net.TLS.Enable = true
config.Net.TLS.Config = tlsConfig
}
if metadata.authMode == kafkaAuthModeForSaslScramSha256 {
config.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA256} }
config.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA256)
}
if metadata.authMode == kafkaAuthModeForSaslScramSha512 {
config.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA512} }
config.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA512)
}
if metadata.authMode == kafkaAuthModeForSaslPlaintext {
config.Net.SASL.Mechanism = sarama.SASLTypePlaintext
config.Net.TLS.Enable = true
}
client, err := sarama.NewClient(metadata.bootstrapServers, config)
if err != nil {
return nil, nil, fmt.Errorf("error creating kafka client: %s", err)
}
admin, err := sarama.NewClusterAdmin(metadata.bootstrapServers, config)
if err != nil {
return nil, nil, fmt.Errorf("error creating kafka admin: %s", err)
}
return client, admin, nil
}
func (s *kafkaScaler) getPartitions() ([]int32, error) {
topicsMetadata, err := s.admin.DescribeTopics([]string{s.metadata.topic})
if err != nil {
return nil, fmt.Errorf("error describing topics: %s", err)
}
if len(topicsMetadata) != 1 {
return nil, fmt.Errorf("expected only 1 topic metadata, got %d", len(topicsMetadata))
}
partitionMetadata := topicsMetadata[0].Partitions
partitions := make([]int32, len(partitionMetadata))
for i, p := range partitionMetadata {
partitions[i] = p.ID
}
return partitions, nil
}
func (s *kafkaScaler) getOffsets(partitions []int32) (*sarama.OffsetFetchResponse, error) {
offsets, err := s.admin.ListConsumerGroupOffsets(s.metadata.group, map[string][]int32{
s.metadata.topic: partitions,
})
if err != nil {
return nil, fmt.Errorf("error listing consumer group offsets: %s", err)
}
return offsets, nil
}
func (s *kafkaScaler) getLagForPartition(partition int32, offsets *sarama.OffsetFetchResponse) int64 {
block := offsets.GetBlock(s.metadata.topic, partition)
if block == nil {
kafkaLog.Error(fmt.Errorf("error finding offset block for topic %s and partition %d", s.metadata.topic, partition), "")
return 0
}
consumerOffset := block.Offset
latestOffset, err := s.client.GetOffset(s.metadata.topic, partition, sarama.OffsetNewest)
if err != nil {
kafkaLog.Error(err, fmt.Sprintf("error finding latest offset for topic %s and partition %d\n", s.metadata.topic, partition))
return 0
}
var lag int64
// For now, assume a consumer group that has no committed
// offset will read all messages from the topic. This may be
// something we want to allow users to configure.
if consumerOffset == sarama.OffsetNewest || consumerOffset == sarama.OffsetOldest {
lag = latestOffset
} else {
lag = latestOffset - consumerOffset
}
return lag
}
// Close closes the kafka admin and client
func (s *kafkaScaler) Close() error {
err := s.client.Close()
if err != nil {
return err
}
err = s.admin.Close()
if err != nil {
return err
}
return nil
}
func (s *kafkaScaler) GetMetricSpecForScaling() []v2beta1.MetricSpec {
return []v2beta1.MetricSpec{
{
External: &v2beta1.ExternalMetricSource{
MetricName: lagThresholdMetricName,
TargetAverageValue: resource.NewQuantity(s.metadata.lagThreshold, resource.DecimalSI),
},
Type: kafkaMetricType,
},
}
}
//GetMetrics returns value for a supported metric and an error if there is a problem getting the metric
func (s *kafkaScaler) GetMetrics(ctx context.Context, metricName string, metricSelector labels.Selector) ([]external_metrics.ExternalMetricValue, error) {
partitions, err := s.getPartitions()
if err != nil {
return []external_metrics.ExternalMetricValue{}, err
}
offsets, err := s.getOffsets(partitions)
if err != nil {
return []external_metrics.ExternalMetricValue{}, err
}
totalLag := int64(0)
for _, partition := range partitions {
lag := s.getLagForPartition(partition, offsets)
totalLag += lag
}
kafkaLog.V(1).Info(fmt.Sprintf("Kafka scaler: Providing metrics based on totalLag %v, partitions %v, threshold %v", totalLag, len(partitions), s.metadata.lagThreshold))
// don't scale out beyond the number of partitions
if (totalLag / s.metadata.lagThreshold) > int64(len(partitions)) {
totalLag = int64(len(partitions)) * s.metadata.lagThreshold
}
metric := external_metrics.ExternalMetricValue{
MetricName: metricName,
Value: *resource.NewQuantity(int64(totalLag), resource.DecimalSI),
Timestamp: metav1.Now(),
}
return append([]external_metrics.ExternalMetricValue{}, metric), nil
}