Skip to content

Commit 9e9f7f8

Browse files
committed
Remove dead test code
1 parent 3257018 commit 9e9f7f8

File tree

2 files changed

+71
-40
lines changed

2 files changed

+71
-40
lines changed

pkg/agent/lease_counter_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
coordinationv1 "k8s.io/api/coordination/v1"
1616
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17-
coordinationv1listers "k8s.io/client-go/listers/coordination/v1"
1817
)
1918

2019
type leaseTemplate struct {
@@ -118,47 +117,8 @@ func TestIsLeaseValid(t *testing.T) {
118117
}
119118
}
120119

121-
type fakeLeaseLister struct {
122-
leases []*coordinationv1.Lease
123-
calls []labels.Selector
124-
err error
125-
}
126-
127120
type labelMap map[string]string
128121

129-
func (l labelMap) Has(label string) bool {
130-
_, exists := l[label]
131-
return exists
132-
}
133-
func (l labelMap) Get(label string) string {
134-
value, exists := l[label]
135-
if !exists {
136-
return ""
137-
}
138-
139-
return value
140-
}
141-
142-
func (lister *fakeLeaseLister) List(selector labels.Selector) ([]*coordinationv1.Lease, error) {
143-
lister.calls = append(lister.calls, selector)
144-
145-
if lister.err != nil {
146-
return nil, lister.err
147-
}
148-
149-
res := []*coordinationv1.Lease{}
150-
for _, lease := range lister.leases {
151-
if selector.Matches(labelMap(lease.Labels)) {
152-
res = append(res, lease)
153-
}
154-
}
155-
156-
return res, nil
157-
}
158-
func (lister *fakeLeaseLister) Leases(_ string) coordinationv1listers.LeaseNamespaceLister {
159-
panic("not implemented")
160-
}
161-
162122
func TestServerLeaseCounter(t *testing.T) {
163123
testCases := []struct {
164124
name string

pkg/server/leases/gc_controller.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package leases
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
coordinationv1api "k8s.io/api/coordination/v1"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/util/wait"
10+
"k8s.io/client-go/kubernetes"
11+
coordinationv1 "k8s.io/client-go/kubernetes/typed/coordination/v1"
12+
"k8s.io/klog/v2"
13+
)
14+
15+
var timeNow = time.Now
16+
17+
type GarbageCollectionController struct {
18+
leaseInterface coordinationv1.LeaseInterface
19+
20+
labelSelector string
21+
gcCheckPeriod time.Duration
22+
}
23+
24+
func NewGarbageCollectionController(k8sclient kubernetes.Interface, namespace string, gcCheckPeriod time.Duration, leaseSelector string) *GarbageCollectionController {
25+
return &GarbageCollectionController{
26+
leaseInterface: k8sclient.CoordinationV1().Leases(namespace),
27+
gcCheckPeriod: gcCheckPeriod,
28+
labelSelector: leaseSelector,
29+
}
30+
}
31+
32+
func (c *GarbageCollectionController) Run(stopCh <-chan struct{}) {
33+
go wait.Until(c.gc, c.gcCheckPeriod, stopCh)
34+
35+
<-stopCh
36+
}
37+
38+
func (c *GarbageCollectionController) gc() {
39+
ctx := context.Background()
40+
leases, err := c.leaseInterface.List(ctx, metav1.ListOptions{LabelSelector: c.labelSelector})
41+
if err != nil {
42+
klog.Errorf("could not list leases to garbage collect: %v", err)
43+
return
44+
}
45+
46+
for _, lease := range leases.Items {
47+
if isLeaseValid(lease) {
48+
continue
49+
}
50+
51+
// Optimistic concurrency: if a lease has a different resourceVersion than
52+
// when we got it, it may have been renewed.
53+
54+
}
55+
}
56+
57+
func isLeaseValid(lease coordinationv1api.Lease) bool {
58+
var lastRenewTime time.Time
59+
if lease.Spec.RenewTime != nil {
60+
lastRenewTime = lease.Spec.RenewTime.Time
61+
} else if lease.Spec.AcquireTime != nil {
62+
lastRenewTime = lease.Spec.AcquireTime.Time
63+
} else {
64+
klog.Warningf("lease %v has neither a renew time or an acquire time, marking as expired: %v", lease.Name, lease)
65+
return false
66+
}
67+
68+
duration := time.Duration(*lease.Spec.LeaseDurationSeconds) * time.Second
69+
70+
return lastRenewTime.Add(duration).After(timeNow()) // renewTime+duration > time.Now()
71+
}

0 commit comments

Comments
 (0)