Debian Codegraph @

pkg/testing/leases.go

raw symbols SCIP
1 package testing
2
3 import (
4 "time"
5
6 "github.com/google/uuid"
7 coordinationv1api "k8s.io/api/coordination/v1"
8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9 "k8s.io/utils/clock"
10 )
11
12 type LeaseTemplate struct {
13 DurationSecs int32
14 TimeSinceAcquire time.Duration
15 TimeSinceRenew time.Duration
16 Labels map[string]string
17 }
18
19 func NewLeaseFromTemplate(pc clock.PassiveClock, template LeaseTemplate) *coordinationv1api.Lease {
20 lease := &coordinationv1api.Lease{
21 TypeMeta: metav1.TypeMeta{},
22 ObjectMeta: metav1.ObjectMeta{
23 Name: uuid.New().String(),
24 Labels: template.Labels,
25 },
26 Spec: coordinationv1api.LeaseSpec{},
27 }
28
29 if template.DurationSecs != 0 {
30 lease.Spec.LeaseDurationSeconds = &template.DurationSecs
31 }
32 if template.TimeSinceAcquire != time.Duration(0) {
33 acquireTime := metav1.NewMicroTime(pc.Now().Add(-template.TimeSinceAcquire))
34 lease.Spec.AcquireTime = &acquireTime
35 }
36 if template.TimeSinceRenew != time.Duration(0) {
37 renewTime := metav1.NewMicroTime(pc.Now().Add(-template.TimeSinceRenew))
38 lease.Spec.RenewTime = &renewTime
39 }
40
41 return lease
42 }