Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable HA by default #3564

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"knative.dev/pkg/controller"
"knative.dev/pkg/injection"
"knative.dev/pkg/injection/sharedmain"
kle "knative.dev/pkg/leaderelection"
"knative.dev/pkg/leaderelection"
"knative.dev/pkg/logging"
"knative.dev/pkg/signals"
tracingconfig "knative.dev/pkg/tracing/config"
Expand Down Expand Up @@ -55,7 +55,6 @@ import (
"knative.dev/eventing/pkg/apis/sources"
sourcesv1alpha1 "knative.dev/eventing/pkg/apis/sources/v1alpha1"
sourcesv1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
"knative.dev/eventing/pkg/leaderelection"
"knative.dev/eventing/pkg/logconfig"
"knative.dev/eventing/pkg/reconciler/sinkbinding"
)
Expand Down Expand Up @@ -185,8 +184,8 @@ func NewConfigValidationController(ctx context.Context, _ configmap.Watcher) *co
configmap.Constructors{
tracingconfig.ConfigName: tracingconfig.NewTracingConfigFromConfigMap,
// metrics.ConfigMapName(): metricsconfig.NewObservabilityConfigFromConfigMap,
logging.ConfigMapName(): logging.NewConfigFromConfigMap,
kle.ConfigMapName(): leaderelection.ValidateConfig,
logging.ConfigMapName(): logging.NewConfigFromConfigMap,
leaderelection.ConfigMapName(): leaderelection.NewConfigFromConfigMap,
},
)
}
Expand Down
12 changes: 1 addition & 11 deletions config/core/configmaps/leader-election.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ metadata:
labels:
eventing.knative.dev/release: devel
annotations:
knative.dev/example-checksum: "f7320456"
knative.dev/example-checksum: "6cf53e10"
data:
_example: |
################################
Expand Down Expand Up @@ -49,13 +49,3 @@ data:
# retryPeriod is how long the leader election client waits between tries of
# actions; 2 seconds is the value used by core kuberntes controllers.
retryPeriod: "2s"

# enabledComponents is a comma-delimited list of component names for which
# leader election is enabled. Valid values are:
#
# - controller
# - broker-controller
# - inmemorychannel-dispatcher
# - inmemorychannel-controller
# - pingsource-mt-adapter
enabledComponents: "controller,broker-controller,inmemorychannel-dispatcher,inmemorychannel-controller,webhook,pingsource-mt-adapter"
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ require (
k8s.io/apiserver v0.17.6
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible
k8s.io/utils v0.0.0-20200124190032-861946025e34
knative.dev/pkg v0.0.0-20200708171447-5358179e7499
knative.dev/test-infra v0.0.0-20200708165947-2cd922769fa4
knative.dev/pkg v0.0.0-20200713194318-a81727701f66
knative.dev/test-infra v0.0.0-20200713185018-6b52776d44a4
sigs.k8s.io/yaml v1.2.0
)

Expand Down
34 changes: 21 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+
cloud.google.com/go/pubsub v1.2.0 h1:Lpy6hKgdcl7a3WGSfJIFmxmcdjSpP6OmBEfcOv1Y680=
cloud.google.com/go/pubsub v1.2.0/go.mod h1:jhfEVHT8odbXTkndysNHCcx0awwzvfOlguIAii9o8iA=
cloud.google.com/go/pubsub v1.3.1/go.mod h1:i+ucay31+CNRpDW4Lu78I4xXG+O1r/MAHgjpRVR+TSU=
cloud.google.com/go/pubsub v1.4.0/go.mod h1:LFrqilwgdw4X2cJS9ALgzYmMu+ULyrUN6IHV3CPK4TM=
cloud.google.com/go/pubsub v1.5.0/go.mod h1:ZEwJccE3z93Z2HWvstpri00jOg7oO4UZDtKhwDwqF0w=
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos=
cloud.google.com/go/storage v1.6.0 h1:UDpwYIwla4jHGzZJaEJYx1tOejbgSoNqsAfHAUYe2r8=
Expand Down Expand Up @@ -542,7 +542,7 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/licenseclassifier v0.0.0-20190926221455-842c0d70d702/go.mod h1:qsqn2hxC+vURpyBRygGUuinTO42MFRLcsmQ/P8v94+M=
github.com/google/licenseclassifier v0.0.0-20200402202327-879cb1424de0/go.mod h1:qsqn2hxC+vURpyBRygGUuinTO42MFRLcsmQ/P8v94+M=
github.com/google/licenseclassifier v0.0.0-20200708223521-3d09a0ea2f39/go.mod h1:qsqn2hxC+vURpyBRygGUuinTO42MFRLcsmQ/P8v94+M=
github.com/google/mako v0.0.0-20190821191249-122f8dcef9e3 h1:/o5e44nTD/QEEiWPGSFT3bSqcq3Qg7q27N9bv4gKh5M=
github.com/google/mako v0.0.0-20190821191249-122f8dcef9e3/go.mod h1:YzLcVlL+NqWnmUEPuhS1LxDDwGO9WNbVlEXaF4IH35g=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
Expand Down Expand Up @@ -1075,6 +1075,7 @@ github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1/go.mod h1:QcJo0QPSf
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA=
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=
Expand Down Expand Up @@ -1144,6 +1145,8 @@ golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 h1:DZhuSZLsGlFL4CmhA8BcRA0mnthyA/nZ00AqCUo7vHg=
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -1230,6 +1233,8 @@ golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU=
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -1405,8 +1410,9 @@ golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200527183253-8e7acdbce89d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200626171337-aa94e735be7f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200701000337-a32c0cb1d5b2 h1:xs+dSrelqXhHGIwIftyT5DHxJKH8hbDQnHc5KZ6i/u8=
golang.org/x/tools v0.0.0-20200701000337-a32c0cb1d5b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200706234117-b22de6825cf7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200710042808-f1c4188a97a1 h1:rD1FcWVsRaMY+l8biE9jbWP5MS/CJJ/90a9TMkMgNrM=
golang.org/x/tools v0.0.0-20200710042808-f1c4188a97a1/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down Expand Up @@ -1444,6 +1450,8 @@ google.golang.org/api v0.24.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0M
google.golang.org/api v0.25.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE=
google.golang.org/api v0.28.0 h1:jMF5hhVfMkTZwHW1SDpKq5CkgWLXOb31Foaca9Zr3oM=
google.golang.org/api v0.28.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE=
google.golang.org/api v0.29.0 h1:BaiDisFir8O4IJxvAabCGGkQ6yCJegNQqSVoYUNAnbk=
google.golang.org/api v0.29.0/go.mod h1:Lcubydp8VUV7KeIHD9z2Bys/sm/vGKnG1UHuDBSrHWM=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down Expand Up @@ -1488,11 +1496,11 @@ google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380/go.mod h1:55QSHmfG
google.golang.org/genproto v0.0.0-20200515170657-fc4c6c6a6587/go.mod h1:YsZOwe1myG/8QRHRsmBRE1LrgQY60beZKjly0O1fX9U=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/genproto v0.0.0-20200528110217-3d3490e7e671/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/genproto v0.0.0-20200626011028-ee7919e894b5/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200701001935-0939c5918c31 h1:Of4QP8bfRqzDROen6+s2j/p0jCPgzvQRd9nHiactfn4=
google.golang.org/genproto v0.0.0-20200701001935-0939c5918c31/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200707001353-8e8330bf89df/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200710124503-20a17af7bd0e h1:k+p/u26/lVeNEpdxSeUrm7rTvoFckBKaf7gTzgmHyDA=
google.golang.org/genproto v0.0.0-20200710124503-20a17af7bd0e/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/grpc v0.0.0-20160317175043-d3ddb4469d5a/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
Expand Down Expand Up @@ -1634,18 +1642,18 @@ k8s.io/legacy-cloud-providers v0.17.4/go.mod h1:FikRNoD64ECjkxO36gkDgJeiQWwyZTuB
k8s.io/metrics v0.17.2/go.mod h1:3TkNHET4ROd+NfzNxkjoVfQ0Ob4iZnaHmSEA4vYpwLw=
k8s.io/test-infra v0.0.0-20200514184223-ba32c8aae783/go.mod h1:bW6thaPZfL2hW7ecjx2WYwlP9KQLM47/xIJyttkVk5s=
k8s.io/test-infra v0.0.0-20200617221206-ea73eaeab7ff/go.mod h1:L3+cRvwftUq8IW1TrHji5m3msnc4uck/7LsE/GR/aZk=
k8s.io/test-infra v0.0.0-20200630233406-1dca6122872e/go.mod h1:L3+cRvwftUq8IW1TrHji5m3msnc4uck/7LsE/GR/aZk=
k8s.io/test-infra v0.0.0-20200710134549-5891a1a4cc17/go.mod h1:L3+cRvwftUq8IW1TrHji5m3msnc4uck/7LsE/GR/aZk=
k8s.io/utils v0.0.0-20191114184206-e782cd3c129f/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
k8s.io/utils v0.0.0-20200124190032-861946025e34 h1:HjlUD6M0K3P8nRXmr2B9o4F9dUy9TCj/aEpReeyi6+k=
k8s.io/utils v0.0.0-20200124190032-861946025e34/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
knative.dev/caching v0.0.0-20200116200605-67bca2c83dfa/go.mod h1:dHXFU6CGlLlbzaWc32g80cR92iuBSpsslDNBWI8C7eg=
knative.dev/eventing-contrib v0.11.2/go.mod h1:SnXZgSGgMSMLNFTwTnpaOH7hXDzTFtw0J8OmHflNx3g=
knative.dev/pkg v0.0.0-20200207155214-fef852970f43/go.mod h1:pgODObA1dTyhNoFxPZTTjNWfx6F0aKsKzn+vaT9XO/Q=
knative.dev/pkg v0.0.0-20200708171447-5358179e7499 h1:hmwZWMiH/EZ+DYrBVC4IXEAKqQyQGJlj1oDETXvdsgA=
knative.dev/pkg v0.0.0-20200708171447-5358179e7499/go.mod h1:AqAJV6rYi8IGikDjJ/9ZQd9qKdkXVlesVnVjwx62YB8=
knative.dev/test-infra v0.0.0-20200707183444-aed09e56ddc7/go.mod h1:RjYAhXnZqeHw9+B0zsbqSPlae0lCvjekO/nw5ZMpLCs=
knative.dev/test-infra v0.0.0-20200708165947-2cd922769fa4 h1:FI6tYvlxya3RiA9pqV9h2mk7Rkyc7qvi9QTA4Fguv2I=
knative.dev/test-infra v0.0.0-20200708165947-2cd922769fa4/go.mod h1:RjYAhXnZqeHw9+B0zsbqSPlae0lCvjekO/nw5ZMpLCs=
knative.dev/pkg v0.0.0-20200713194318-a81727701f66 h1:H9s47uSb5NCRvnsyIQQpWo5q/cRJ5qEDpm/5pwdnPEg=
knative.dev/pkg v0.0.0-20200713194318-a81727701f66/go.mod h1:2xVLIH5SNUripobZvOEz3w/Ta9xqMkw7QmFIa2cbDFY=
knative.dev/test-infra v0.0.0-20200713045417-850e4e37918d/go.mod h1:vtT6dLs/iNj8pKcfag8CSVqHKNMgyCFtU/g1pV7Bovs=
knative.dev/test-infra v0.0.0-20200713185018-6b52776d44a4 h1:BYNKY0hC5wsq533k6XbJXi+sb9LNNhM8NQV4mGljR2c=
knative.dev/test-infra v0.0.0-20200713185018-6b52776d44a4/go.mod h1:vtT6dLs/iNj8pKcfag8CSVqHKNMgyCFtU/g1pV7Bovs=
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=
modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
Expand Down
7 changes: 6 additions & 1 deletion pkg/adapter/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package adapter

import (
"encoding/json"
"time"

"go.uber.org/zap"

Expand Down Expand Up @@ -190,6 +191,10 @@ func (e *EnvConfig) GetLeaderElectionConfig() (*kle.ComponentConfig, error) {

func defaultLeaderElectionConfig() *kle.ComponentConfig {
return &kle.ComponentConfig{
LeaderElect: false,
ResourceLock: "leases",
mattmoor marked this conversation as resolved.
Show resolved Hide resolved
Buckets: 1,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
}
}
23 changes: 21 additions & 2 deletions pkg/adapter/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,26 @@ type Adapter interface {
type AdapterConstructor func(ctx context.Context, env EnvConfigAccessor, client cloudevents.Client) Adapter

func Main(component string, ector EnvConfigConstructor, ctor AdapterConstructor) {
MainWithContext(signals.NewContext(), component, ector, ctor)

ctx := signals.NewContext()

// TODO(mattmoor): expose a flag that gates this?
// ctx = WithHAEnabled(ctx)
mattmoor marked this conversation as resolved.
Show resolved Hide resolved

MainWithContext(ctx, component, ector, ctor)
}

type haEnabledKey struct{}

// WithHAEnabled signals to MainWithContext that it should set up an appropriate leader elector for this component.
func WithHAEnabled(ctx context.Context) context.Context {
return context.WithValue(ctx, haEnabledKey{}, struct{}{})
}

// IsHAEnabled checks the context for the desire to enable leader elector.
func IsHAEnabled(ctx context.Context) bool {
val := ctx.Value(haEnabledKey{})
return val != nil
}

func MainWithContext(ctx context.Context, component string, ector EnvConfigConstructor, ctor AdapterConstructor) {
Expand Down Expand Up @@ -124,7 +143,7 @@ func MainWithContext(ctx context.Context, component string, ector EnvConfigConst
logger.Error("Error loading the leader election configuration", zap.Error(err))
}

if leConfig.LeaderElect {
if IsHAEnabled(ctx) {
// Signal that we are executing in a context with leader election.
ctx = leaderelection.WithStandardLeaderElectorBuilder(ctx, kubeclient.Get(ctx), *leConfig)
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/adapter/v2/main_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ type MessageAdapter interface {
type MessageAdapterConstructor func(ctx context.Context, env EnvConfigAccessor, adapter *kncloudevents.HttpMessageSender, reporter source.StatsReporter) MessageAdapter

func MainMessageAdapter(component string, ector EnvConfigConstructor, ctor MessageAdapterConstructor) {
MainMessageAdapterWithContext(signals.NewContext(), component, ector, ctor)
ctx := signals.NewContext()

// TODO(mattmoor): expose a flag that gates this?
// ctx = WithHAEnabled(ctx)

MainMessageAdapterWithContext(ctx, component, ector, ctor)
}

func MainMessageAdapterWithContext(ctx context.Context, component string, ector EnvConfigConstructor, ctor MessageAdapterConstructor) {
Expand Down Expand Up @@ -122,7 +127,7 @@ func MainMessageAdapterWithContext(ctx context.Context, component string, ector
logger.Error("Error loading the leader election configuration", zap.Error(err))
}

if leConfig.LeaderElect {
if IsHAEnabled(ctx) {
// Signal that we are executing in a context with leader election.
ctx = leaderelection.WithStandardLeaderElectorBuilder(ctx, kubeclient.Get(ctx), *leConfig)
}
Expand Down
9 changes: 3 additions & 6 deletions pkg/adapter/v2/main_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"go.opencensus.io/stats/view"
"knative.dev/pkg/leaderelection"
"knative.dev/pkg/metrics"
"knative.dev/pkg/source"

Expand Down Expand Up @@ -60,12 +61,8 @@ func TestMainMessageAdapter(t *testing.T) {
t.Errorf("Expected sinkURI http://sink, got: %s", env.Sink)
}

leConfig, err := env.GetLeaderElectionConfig()
if err != nil {
t.Errorf("Expected no error: %v", err)
}
if leConfig.LeaderElect {
t.Errorf("Expected LeaderElect to be false, got: %t", leConfig.LeaderElect)
if leaderelection.HasLeaderElection(ctx) {
t.Error("Expected no leader election, but got leader election")
}
return &myAdapterBindings{}
})
Expand Down
13 changes: 6 additions & 7 deletions pkg/adapter/v2/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ import (

cloudevents "github.com/cloudevents/sdk-go/v2"
"go.opencensus.io/stats/view"
"knative.dev/pkg/leaderelection"
"knative.dev/pkg/metrics"

_ "knative.dev/pkg/system/testing"
)

type myAdapter struct{}

func TestMainWithContext(t *testing.T) {
func TestMainWithContextNoLeaderElection(t *testing.T) {
os.Setenv("K_SINK", "http://sink")
os.Setenv("NAMESPACE", "ns")
os.Setenv("K_METRICS_CONFIG", "metrics")
Expand Down Expand Up @@ -59,12 +62,8 @@ func TestMainWithContext(t *testing.T) {
t.Errorf("Expected sinkURI http://sink, got: %s", env.Sink)
}

leConfig, err := env.GetLeaderElectionConfig()
if err != nil {
t.Errorf("Expected no error: %v", err)
}
if leConfig.LeaderElect {
t.Errorf("Expected LeaderElect to be false, got: %t", leConfig.LeaderElect)
if leaderelection.HasLeaderElection(ctx) {
t.Error("Expected no leader election, but got leader election")
}
return &myAdapter{}
})
Expand Down
5 changes: 1 addition & 4 deletions pkg/leaderelection/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ func BuildAdapterElector(ctx context.Context, adapter Adapter) (Elector, error)
if val := ctx.Value(builderKey{}); val != nil {
switch builder := val.(type) {
case *standardBuilder:
// Only use the standard elector is leader election is enabled
if builder.lec.LeaderElect {
return builder.BuildElector(ctx, adapter)
}
return builder.BuildElector(ctx, adapter)
}
}

Expand Down
1 change: 0 additions & 1 deletion pkg/leaderelection/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func (a *fakeAdapter) Start(ctx context.Context) error {
func TestWithBuilder(t *testing.T) {
cc := leaderelection.ComponentConfig{
Component: "component",
LeaderElect: true,
ResourceLock: "leases",
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
Expand Down
31 changes: 1 addition & 30 deletions pkg/leaderelection/leader_election.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,7 @@ limitations under the License.
package leaderelection

import (
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"

kle "knative.dev/pkg/leaderelection"
)

var (
validComponents = sets.NewString(
"controller",
"broker-controller",
"inmemorychannel-dispatcher",
"inmemorychannel-controller",
"webhook",
"pingsource-mt-adapter",
)
)

func ValidateConfig(configMap *corev1.ConfigMap) (*kle.Config, error) {
config, err := kle.NewConfigFromMap(configMap.Data)
if err != nil {
return nil, err
}

for component := range config.EnabledComponents {
if !validComponents.Has(component) {
return nil, fmt.Errorf("invalid enabledComponent %q: valid values are %q", component, validComponents.List())
}
}

return config, nil
}
var ValidateConfig = kle.NewConfigFromConfigMap
Loading