/
config.go
248 lines (201 loc) · 7.34 KB
/
config.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
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package adapter
import (
"encoding/json"
"os"
"strconv"
"time"
"go.uber.org/zap"
duckv1 "knative.dev/pkg/apis/duck/v1"
kle "knative.dev/pkg/leaderelection"
"knative.dev/pkg/logging"
"knative.dev/pkg/metrics"
"knative.dev/pkg/tracing"
tracingconfig "knative.dev/pkg/tracing/config"
)
type EnvConfigConstructor func() EnvConfigAccessor
const (
EnvConfigComponent = "K_COMPONENT"
EnvConfigNamespace = "NAMESPACE"
EnvConfigName = "NAME"
EnvConfigResourceGroup = "K_RESOURCE_GROUP"
EnvConfigSink = "K_SINK"
EnvConfigCEOverrides = "K_CE_OVERRIDES"
EnvConfigMetricsConfig = "K_METRICS_CONFIG"
EnvConfigLoggingConfig = "K_LOGGING_CONFIG"
EnvConfigTracingConfig = "K_TRACING_CONFIG"
EnvConfigLeaderElectionConfig = "K_LEADER_ELECTION_CONFIG"
EnvSinkTimeout = "K_SINK_TIMEOUT"
)
// EnvConfig is the minimal set of configuration parameters
// source adapters should support.
type EnvConfig struct {
// Component is the kind of this adapter.
Component string `envconfig:"K_COMPONENT"`
// Environment variable containing the namespace of the adapter.
Namespace string `envconfig:"NAMESPACE"`
// Environment variable containing the name of the adapter.
Name string `envconfig:"NAME" default:"adapter"`
// Environment variable containing the resource group of the adapter for metrics.
ResourceGroup string `envconfig:"K_RESOURCE_GROUP" default:"adapter.sources.knative.dev"`
// Sink is the URI messages will be sent.
Sink string `envconfig:"K_SINK"`
// CEOverrides are the CloudEvents overrides to be applied to the outbound event.
CEOverrides string `envconfig:"K_CE_OVERRIDES"`
// MetricsConfigJson is a json string of metrics.ExporterOptions.
// This is used to configure the metrics exporter options,
// the config is stored in a config map inside the controllers
// namespace and copied here.
MetricsConfigJson string `envconfig:"K_METRICS_CONFIG" default:"{}"`
// LoggingConfigJson is a json string of logging.Config.
// This is used to configure the logging config, the config is stored in
// a config map inside the controllers namespace and copied here.
LoggingConfigJson string `envconfig:"K_LOGGING_CONFIG" default:"{}"`
// TracingConfigJson is a json string of tracing.Config.
// This is used to configure the tracing config, the config is stored in
// a config map inside the controllers namespace and copied here.
// Default is no-op.
TracingConfigJson string `envconfig:"K_TRACING_CONFIG"`
// LeaderElectionConfigJson is the leader election component configuration.
LeaderElectionConfigJson string `envconfig:"K_LEADER_ELECTION_CONFIG"`
// Time in seconds to wait for sink to respond
EnvSinkTimeout string `envconfig:"K_SINK_TIMEOUT"`
// cached zap logger
logger *zap.SugaredLogger
}
// EnvConfigAccessor defines accessors for the minimal
// set of source adapter configuration parameters.
type EnvConfigAccessor interface {
// Set the component name.
SetComponent(string)
// Get the URI where messages will be forwarded to.
GetSink() string
// Get the namespace of the adapter.
GetNamespace() string
// Get the name of the adapter.
GetName() string
// Get the parsed metrics.ExporterOptions.
GetMetricsConfig() (*metrics.ExporterOptions, error)
// Get the parsed logger.
GetLogger() *zap.SugaredLogger
SetupTracing(*zap.SugaredLogger) (tracing.Tracer, error)
GetCloudEventOverrides() (*duckv1.CloudEventOverrides, error)
// GetLeaderElectionConfig returns leader election configuration.
GetLeaderElectionConfig() (*kle.ComponentConfig, error)
// Get the timeout to apply on a request to a sink
GetSinktimeout() int
}
var _ EnvConfigAccessor = (*EnvConfig)(nil)
func (e *EnvConfig) SetComponent(component string) {
e.Component = component
}
func (e *EnvConfig) GetMetricsConfig() (*metrics.ExporterOptions, error) {
// Convert json metrics.ExporterOptions to metrics.ExporterOptions.
metricsConfig, err := metrics.JSONToOptions(e.MetricsConfigJson)
if err != nil {
return nil, err
}
return metricsConfig, err
}
func (e *EnvConfig) GetLogger() *zap.SugaredLogger {
if e.logger == nil {
loggingConfig, err := logging.JSONToConfig(e.LoggingConfigJson)
if err != nil {
// Use default logging config.
if loggingConfig, err = logging.NewConfigFromMap(map[string]string{}); err != nil {
// If this fails, there is no recovering.
panic(err)
}
}
logger, _ := logging.NewLoggerFromConfig(loggingConfig, e.Component)
e.logger = logger
}
return e.logger
}
func (e *EnvConfig) GetSink() string {
return e.Sink
}
func (e *EnvConfig) GetNamespace() string {
return e.Namespace
}
func (e *EnvConfig) GetName() string {
return e.Name
}
func (e *EnvConfig) GetSinktimeout() int {
if duration, err := strconv.Atoi(e.EnvSinkTimeout); err == nil {
return duration
}
e.GetLogger().Warn("Sink timeout configuration is invalid, default to -1 (no timeout)")
return -1
}
func (e *EnvConfig) SetupTracing(logger *zap.SugaredLogger) (tracing.Tracer, error) {
config, err := tracingconfig.JSONToTracingConfig(e.TracingConfigJson)
if err != nil {
logger.Warn("Tracing configuration is invalid, using the no-op default", zap.Error(err))
}
return tracing.SetupPublishingWithStaticConfig(logger, "", config)
}
func (e *EnvConfig) GetCloudEventOverrides() (*duckv1.CloudEventOverrides, error) {
var ceOverrides duckv1.CloudEventOverrides
if len(e.CEOverrides) > 0 {
err := json.Unmarshal([]byte(e.CEOverrides), &ceOverrides)
if err != nil {
return nil, err
}
}
return &ceOverrides, nil
}
func (e *EnvConfig) GetLeaderElectionConfig() (*kle.ComponentConfig, error) {
if e.LeaderElectionConfigJson == "" {
return e.defaultLeaderElectionConfig(), nil
}
var config kle.ComponentConfig
if err := json.Unmarshal([]byte(e.LeaderElectionConfigJson), &config); err != nil {
return e.defaultLeaderElectionConfig(), err
}
config.Component = e.Component
return &config, nil
}
func (e *EnvConfig) defaultLeaderElectionConfig() *kle.ComponentConfig {
return &kle.ComponentConfig{
Component: e.Component,
Buckets: 1,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
}
}
// LeaderElectionComponentConfigToJSON converts a ComponentConfig to a json string.
func LeaderElectionComponentConfigToJSON(cfg *kle.ComponentConfig) (string, error) {
if cfg == nil {
return "", nil
}
jsonCfg, err := json.Marshal(cfg)
return string(jsonCfg), err
}
func GetSinkTimeout(logger *zap.SugaredLogger) int {
str := os.Getenv(EnvSinkTimeout)
if str != "" {
var err error
duration, err := strconv.Atoi(str)
if err != nil || duration < 0 {
if logger != nil {
logger.Errorf("%s environment value is invalid. It must be a integer greater than zero. (got %s)", EnvSinkTimeout, str)
}
return -1
}
return duration
}
return -1
}