-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
195 lines (159 loc) · 5.47 KB
/
adapter.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
package kafkasource
import (
"context"
"crypto/sha256"
"crypto/sha512"
"crypto/tls"
"crypto/x509"
"time"
"go.uber.org/zap"
"github.com/Shopify/sarama"
cloudevents "github.com/cloudevents/sdk-go/v2"
pkgadapter "knative.dev/eventing/pkg/adapter/v2"
"knative.dev/pkg/logging"
"github.com/zeiss/typhoon/pkg/apis/sources"
)
const (
// Errors for the last 40 seconds will be taken into consideration.
errorAccumulationTolerance = time.Second * 40
// First 10 errors will be retried right away.
errorAccumulationSupportedCount = 10
// Next 10 errors will be delayed, after a total of
// 20 errors have been accumulated for the tolerance perior, the
// adapter will exit.
errorAccumulationDelayedCount = 20
// Delayed consumer retries will wait this duration.
errorAccumulationDelay = time.Second
)
var _ pkgadapter.Adapter = (*kafkasourceAdapter)(nil)
type kafkasourceAdapter struct {
ceClient cloudevents.Client
logger *zap.SugaredLogger
mt *pkgadapter.MetricTag
kafkaClient sarama.ConsumerGroup
topic string
}
// NewAdapter satisfies pkgadapter.AdapterConstructor.
// nolint:gocyclo
func NewAdapter(ctx context.Context, envAcc pkgadapter.EnvConfigAccessor, ceClient cloudevents.Client) pkgadapter.Adapter {
logger := logging.FromContext(ctx)
sarama.Logger = zap.NewStdLog(logger.Named("sarama").Desugar())
mt := &pkgadapter.MetricTag{
ResourceGroup: sources.CloudEventsSourceResource.String(),
Namespace: envAcc.GetNamespace(),
Name: envAcc.GetName(),
}
env := envAcc.(*envAccessor)
var err error
config := sarama.NewConfig()
if env.SASLEnable {
mechanism := sarama.SASLMechanism(env.SecurityMechanisms)
// If the SASL SCRAM mechanism a SCRAM generator must be provided pointing
// to a corresponding hash generator function.
switch mechanism {
case sarama.SASLTypeSCRAMSHA256:
config.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: sha256.New} }
case sarama.SASLTypeSCRAMSHA512:
config.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: sha512.New} }
}
config.Net.SASL.Enable = env.SASLEnable
config.Net.SASL.Mechanism = mechanism
config.Net.SASL.User = env.Username
config.Net.SASL.Password = env.Password
}
if env.TLSEnable {
config.Net.TLS.Enable = env.TLSEnable
tlsCfg := &tls.Config{
MinVersion: tls.VersionTLS12,
}
if env.CA != "" {
addCAConfig(tlsCfg, env.CA)
}
if env.ClientCert != "" || env.ClientKey != "" {
if err := addTLSCerts(tlsCfg, env.ClientCert, env.ClientKey); err != nil {
logger.Panicw("Could not parse the TLS Certificates", zap.Error(err))
}
}
config.Net.TLS.Config = tlsCfg
config.Net.TLS.Config.InsecureSkipVerify = env.SkipVerify
}
if env.SecurityMechanisms == "GSSAPI" {
kerberosConfig := sarama.GSSAPIConfig{
KerberosConfigPath: env.KerberosConfigPath,
ServiceName: env.KerberosServiceName,
Username: env.KerberosUsername,
Password: env.KerberosPassword,
Realm: env.KerberosRealm,
DisablePAFXFAST: true,
}
if env.KerberosKeytabPath != "" {
kerberosConfig.AuthType = sarama.KRB5_KEYTAB_AUTH
kerberosConfig.KeyTabPath = env.KerberosKeytabPath
} else {
kerberosConfig.AuthType = sarama.KRB5_USER_AUTH
}
config.Net.SASL.GSSAPI = kerberosConfig
}
err = config.Validate()
if err != nil {
logger.Panicw("Config not valid", zap.Error(err))
}
kc, err := sarama.NewConsumerGroup(
env.BootstrapServers,
env.GroupID, config)
if err != nil {
logger.Panicw("Error creating Kafka Consumer Group", zap.Error(err))
}
return &kafkasourceAdapter{
kafkaClient: kc,
topic: env.Topic,
ceClient: ceClient,
logger: logger,
mt: mt,
}
}
func (a *kafkasourceAdapter) Start(ctx context.Context) error {
a.logger.Info("Starting Kafka Source Adapter")
consumerGroup := consumerGroupHandler{
adapter: a,
}
errorList := NewStaleList(errorAccumulationTolerance)
// while the context is not done, run the loop.
for ctx.Err() == nil {
// `Consume` should be called inside an infinite loop, when a
// server-side rebalance happens, the consumer session will need to be
// recreated to get the new claims
if err := a.kafkaClient.Consume(ctx, []string{a.topic}, consumerGroup); err != nil {
a.logger.Error("Error setting up the consumer client", zap.Error(err))
// Safety net mechanism, we try to re-consume and avoid exiting the adapter.
// This is mainly due to the adapter not being used at environments where
// a restart can be configured (?)
errNum := errorList.AddAndCount(err)
switch {
case errNum < errorAccumulationSupportedCount:
// If errors are occasional let it retry to consume fast.
continue
case errNum < errorAccumulationDelayedCount:
// If errors pile up, we add pauses between retries
a.logger.Info("Slowing down consumer connection loop, too many errors")
time.Sleep(errorAccumulationDelay)
default:
a.logger.Info("Giving up on consumer connection retries, too many errors")
return err
}
}
}
return nil
}
func addCAConfig(tlsConfig *tls.Config, caCert string) {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(caCert))
tlsConfig.RootCAs = caCertPool
}
func addTLSCerts(tlsConfig *tls.Config, clientCert, clientKey string) error {
cert, err := tls.X509KeyPair([]byte(clientCert), []byte(clientKey))
if err == nil {
tlsConfig.Certificates = []tls.Certificate{cert}
}
return err
}