Debian Codegraph @

pkg/util/leases.go

raw
1 /*
2 Copyright 2024 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16 package util //nolint:revive
17
18 import (
19 "time"
20
21 coordinationv1api "k8s.io/api/coordination/v1"
22 "k8s.io/klog/v2"
23 "k8s.io/utils/clock"
24 )
25
26 func IsLeaseValid(pc clock.PassiveClock, lease coordinationv1api.Lease) bool {
27 var lastRenewTime time.Time
28 if lease.Spec.RenewTime != nil {
29 lastRenewTime = lease.Spec.RenewTime.Time
30 } else if lease.Spec.AcquireTime != nil {
31 lastRenewTime = lease.Spec.AcquireTime.Time
32 } else {
33 klog.Warningf("lease %v has neither a renew time or an acquire time, marking as expired: %v", lease.Name, lease)
34 return false
35 }
36
37 duration := time.Duration(*lease.Spec.LeaseDurationSeconds) * time.Second
38
39 return lastRenewTime.Add(duration).After(pc.Now()) // renewTime+duration > time.Now()
40 }