mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Merge pull request #127910 from leonzz/leonzz-tmp
fix node start time inconsistency in kubelet
This commit is contained in:
commit
a2b19b3edb
@ -21,17 +21,54 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetBootTime returns the time at which the machine was started, truncated to the nearest second
|
// GetBootTime returns the time at which the machine was started, truncated to the nearest second.
|
||||||
|
// It uses /proc/stat first, which is more accurate, and falls back to the less accurate
|
||||||
|
// unix.Sysinfo if /proc/stat failed.
|
||||||
func GetBootTime() (time.Time, error) {
|
func GetBootTime() (time.Time, error) {
|
||||||
|
bootTime, err := getBootTimeWithProcStat()
|
||||||
|
if err != nil {
|
||||||
|
klog.InfoS("Failed to get boot time from /proc/uptime. Will retry with unix.Sysinfo.", "error", err)
|
||||||
|
return getBootTimeWithSysinfo()
|
||||||
|
}
|
||||||
|
return bootTime, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBootTimeWithProcStat() (time.Time, error) {
|
||||||
|
raw, err := os.ReadFile("/proc/stat")
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, fmt.Errorf("error getting boot time: %w", err)
|
||||||
|
}
|
||||||
|
rawFields := strings.Fields(string(raw))
|
||||||
|
for i, v := range rawFields {
|
||||||
|
if v == "btime" {
|
||||||
|
if len(rawFields) > i+1 {
|
||||||
|
sec, err := strconv.ParseInt(rawFields[i+1], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, fmt.Errorf("error parsing boot time %s: %w", rawFields[i+1], err)
|
||||||
|
}
|
||||||
|
return time.Unix(sec, 0), nil
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.Time{}, fmt.Errorf("can not find btime from /proc/stat: %s", raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBootTimeWithSysinfo() (time.Time, error) {
|
||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
var info unix.Sysinfo_t
|
var info unix.Sysinfo_t
|
||||||
if err := unix.Sysinfo(&info); err != nil {
|
if err := unix.Sysinfo(&info); err != nil {
|
||||||
return time.Time{}, fmt.Errorf("error getting system uptime: %s", err)
|
return time.Time{}, fmt.Errorf("error getting system uptime: %w", err)
|
||||||
}
|
}
|
||||||
return currentTime.Add(-time.Duration(info.Uptime) * time.Second).Truncate(time.Second), nil
|
return currentTime.Add(-time.Duration(info.Uptime) * time.Second).Truncate(time.Second), nil
|
||||||
}
|
}
|
||||||
|
@ -35,3 +35,18 @@ func TestGetBootTime(t *testing.T) {
|
|||||||
t.Errorf("Invalid system uptime")
|
t.Errorf("Invalid system uptime")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestVariationBetweenGetBootTimeMethods(t *testing.T) {
|
||||||
|
boottime1, err := getBootTimeWithProcStat()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unable to get boot time from /proc/uptime")
|
||||||
|
}
|
||||||
|
boottime2, err := getBootTimeWithSysinfo()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unable to get boot time from unix.Sysinfo")
|
||||||
|
}
|
||||||
|
diff := boottime1.Sub(boottime2)
|
||||||
|
if diff > time.Second || diff < -time.Second {
|
||||||
|
t.Errorf("boot time produced by 2 methods should not vary more than a second")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user