Skip to content

Commit f1c3a89

Browse files
committed
Drop event recorder from shard lease lock
1 parent 7664eb6 commit f1c3a89

File tree

6 files changed

+15
-50
lines changed

6 files changed

+15
-50
lines changed

cmd/checksum-controller/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (o *options) run(ctx context.Context) error {
125125
}
126126

127127
log.Info("Setting up shard lease")
128-
shardLease, err := shardlease.NewResourceLock(restConfig, nil, shardlease.Options{
128+
shardLease, err := shardlease.NewResourceLock(restConfig, shardlease.Options{
129129
ControllerRingName: o.controllerRingName,
130130
LeaseNamespace: o.leaseNamespace, // optional, can be empty
131131
ShardName: o.shardName, // optional, can be empty

docs/implement-sharding.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ import (
135135
func run() error {
136136
restConfig := config.GetConfigOrDie()
137137
138-
shardLease, err := shardlease.NewResourceLock(restConfig, nil, shardlease.Options{
138+
shardLease, err := shardlease.NewResourceLock(restConfig, shardlease.Options{
139139
ControllerRingName: "my-controllerring",
140140
})
141141
if err != nil {

pkg/shard/lease/lease.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"os"
2626

2727
coordinationv1 "k8s.io/api/coordination/v1"
28-
corev1 "k8s.io/api/core/v1"
2928
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3029
coordinationv1client "k8s.io/client-go/kubernetes/typed/coordination/v1"
3130
"k8s.io/client-go/rest"
@@ -50,7 +49,7 @@ type Options struct {
5049
// NewResourceLock returns a new resource lock that implements the shard lease.
5150
// Pass this to the leader elector, e.g., leaderelection.LeaderElectionConfig.Lock (if using plain client-go) or
5251
// manager.Options.LeaderElectionResourceLockInterface (if using controller-runtime).
53-
func NewResourceLock(config *rest.Config, eventRecorder resourcelock.EventRecorder, options Options) (resourcelock.Interface, error) {
52+
func NewResourceLock(config *rest.Config, options Options) (resourcelock.Interface, error) {
5453
// Construct client for leader election
5554
rest.AddUserAgent(config, "shard-lease")
5655

@@ -89,8 +88,6 @@ func NewResourceLock(config *rest.Config, eventRecorder resourcelock.EventRecord
8988
Client: coordinationClient,
9089
LockConfig: resourcelock.ResourceLockConfig{
9190
Identity: options.ShardName,
92-
// eventRecorder is optional
93-
EventRecorder: eventRecorder,
9491
},
9592
Labels: map[string]string{
9693
shardingv1alpha1.LabelControllerRing: options.ControllerRingName,
@@ -158,18 +155,8 @@ func (ll *LeaseLock) Update(ctx context.Context, ler resourcelock.LeaderElection
158155
return nil
159156
}
160157

161-
// RecordEvent in leader election while adding meta-data
162-
func (ll *LeaseLock) RecordEvent(s string) {
163-
if ll.LockConfig.EventRecorder == nil {
164-
return
165-
}
166-
events := fmt.Sprintf("%v %v", ll.LockConfig.Identity, s)
167-
subject := &coordinationv1.Lease{ObjectMeta: ll.lease.ObjectMeta}
168-
// Populate the type meta, so we don't have to get it from the schema
169-
subject.Kind = "Lease"
170-
subject.APIVersion = coordinationv1.SchemeGroupVersion.String()
171-
ll.LockConfig.EventRecorder.Eventf(subject, corev1.EventTypeNormal, "LeaderElection", events)
172-
}
158+
// RecordEvent does nothing, as recording events on shard leases is not meaningful.
159+
func (ll *LeaseLock) RecordEvent(string) {}
173160

174161
// Describe is used to convert details on current resource lock
175162
// into a string

pkg/shard/lease/lease_test.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
coordinationv1client "k8s.io/client-go/kubernetes/typed/coordination/v1"
3030
"k8s.io/client-go/rest"
3131
"k8s.io/client-go/tools/leaderelection/resourcelock"
32-
"k8s.io/client-go/tools/record"
3332
"k8s.io/utils/ptr"
3433
"sigs.k8s.io/controller-runtime/pkg/client"
3534

@@ -75,11 +74,11 @@ var _ = Describe("LeaseLock", func() {
7574
It("should fail if ControllerRingName is empty", func() {
7675
options.ControllerRingName = ""
7776

78-
Expect(NewResourceLock(restConfig, nil, options)).Error().To(MatchError("ControllerRingName is required"))
77+
Expect(NewResourceLock(restConfig, options)).Error().To(MatchError("ControllerRingName is required"))
7978
})
8079

8180
It("should use the configured namespace and name", func() {
82-
resourceLock, err := NewResourceLock(restConfig, nil, options)
81+
resourceLock, err := NewResourceLock(restConfig, options)
8382
Expect(err).NotTo(HaveOccurred())
8483

8584
leaseLock := resourceLock.(*LeaseLock)
@@ -93,7 +92,7 @@ var _ = Describe("LeaseLock", func() {
9392
hostname, err := os.Hostname()
9493
Expect(err).NotTo(HaveOccurred())
9594

96-
resourceLock, err := NewResourceLock(restConfig, nil, options)
95+
resourceLock, err := NewResourceLock(restConfig, options)
9796
Expect(err).NotTo(HaveOccurred())
9897

9998
leaseLock := resourceLock.(*LeaseLock)
@@ -104,7 +103,7 @@ var _ = Describe("LeaseLock", func() {
104103
It("should default the namespace to the in-cluster namespace", func() {
105104
options.LeaseNamespace = ""
106105

107-
resourceLock, err := NewResourceLock(restConfig, nil, options)
106+
resourceLock, err := NewResourceLock(restConfig, options)
108107
Expect(err).NotTo(HaveOccurred())
109108

110109
leaseLock := resourceLock.(*LeaseLock)
@@ -115,7 +114,7 @@ var _ = Describe("LeaseLock", func() {
115114
options.LeaseNamespace = ""
116115
fsys = fstest.MapFS{}
117116

118-
Expect(NewResourceLock(restConfig, nil, options)).Error().To(MatchError(And(
117+
Expect(NewResourceLock(restConfig, options)).Error().To(MatchError(And(
119118
ContainSubstring("not running in cluster"),
120119
ContainSubstring("please specify LeaseNamespace"),
121120
)))
@@ -132,7 +131,7 @@ var _ = Describe("LeaseLock", func() {
132131

133132
BeforeEach(func() {
134133
var err error
135-
lock, err = NewResourceLock(&rest.Config{}, nil, Options{
134+
lock, err = NewResourceLock(&rest.Config{}, Options{
136135
ControllerRingName: controllerRingName,
137136
LeaseNamespace: namespace,
138137
ShardName: shardName,
@@ -235,29 +234,8 @@ var _ = Describe("LeaseLock", func() {
235234
})
236235

237236
Describe("#RecordEvent", func() {
238-
Context("no EventRecorder configured", func() {
239-
It("should do nothing", func() {
240-
lock.RecordEvent("foo")
241-
})
242-
})
243-
244-
Context("EventRecorder configured", func() {
245-
var recorder *record.FakeRecorder
246-
247-
BeforeEach(func() {
248-
recorder = record.NewFakeRecorder(1)
249-
lock.(*LeaseLock).LockConfig.EventRecorder = recorder
250-
})
251-
252-
It("should send the event", func() {
253-
Expect(lock.Get(ctx)).Error().To(Succeed())
254-
255-
lock.RecordEvent("foo")
256-
257-
Eventually(recorder.Events).Should(Receive(
258-
Equal("Normal LeaderElection " + shardName + " foo"),
259-
))
260-
})
237+
It("should do nothing", func() {
238+
lock.RecordEvent("foo")
261239
})
262240
})
263241

test/integration/shard/lease/lease_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var _ = Describe("Shard lease", func() {
6868
}, OncePerOrdered)
6969

7070
JustBeforeEach(func() {
71-
shardLease, err := shardlease.NewResourceLock(restConfig, nil, leaseOptions)
71+
shardLease, err := shardlease.NewResourceLock(restConfig, leaseOptions)
7272
Expect(err).NotTo(HaveOccurred())
7373
mgrOptions.LeaderElectionResourceLockInterface = shardLease
7474

webhosting-operator/cmd/webhosting-operator/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func (o *options) applyOptionsOverrides() error {
273273

274274
// SHARD LEASE
275275
o.controllerRingName = "webhosting-operator"
276-
shardLease, err := shardlease.NewResourceLock(o.restConfig, nil, shardlease.Options{
276+
shardLease, err := shardlease.NewResourceLock(o.restConfig, shardlease.Options{
277277
ControllerRingName: o.controllerRingName,
278278
})
279279
if err != nil {

0 commit comments

Comments
 (0)