File: leases.go

package info (click to toggle)
golang-k8s-sigs-apiserver-network-proxy 0.33.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,068 kB
  • sloc: makefile: 220; sh: 118
file content (42 lines) | stat: -rw-r--r-- 1,094 bytes parent folder | download
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
package testing

import (
	"time"

	"github.com/google/uuid"
	coordinationv1api "k8s.io/api/coordination/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/utils/clock"
)

type LeaseTemplate struct {
	DurationSecs     int32
	TimeSinceAcquire time.Duration
	TimeSinceRenew   time.Duration
	Labels           map[string]string
}

func NewLeaseFromTemplate(pc clock.PassiveClock, template LeaseTemplate) *coordinationv1api.Lease {
	lease := &coordinationv1api.Lease{
		TypeMeta: metav1.TypeMeta{},
		ObjectMeta: metav1.ObjectMeta{
			Name:   uuid.New().String(),
			Labels: template.Labels,
		},
		Spec: coordinationv1api.LeaseSpec{},
	}

	if template.DurationSecs != 0 {
		lease.Spec.LeaseDurationSeconds = &template.DurationSecs
	}
	if template.TimeSinceAcquire != time.Duration(0) {
		acquireTime := metav1.NewMicroTime(pc.Now().Add(-template.TimeSinceAcquire))
		lease.Spec.AcquireTime = &acquireTime
	}
	if template.TimeSinceRenew != time.Duration(0) {
		renewTime := metav1.NewMicroTime(pc.Now().Add(-template.TimeSinceRenew))
		lease.Spec.RenewTime = &renewTime
	}

	return lease
}