Skip to content

Commit 96b4401

Browse files
ashvindeodharYongli Chen
authored andcommitted
Detect reboot for windows and cleanup network config (#281)
1 parent 136f03c commit 96b4401

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

ipam/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func (am *addressManager) restore() error {
110110
log.Printf("[ipam] reboot time %v store mod time %v", rebootTime, modTime)
111111

112112
if err == nil && rebootTime.After(modTime) {
113+
log.Printf("[ipam] Detected Reboot")
113114
rebooted = true
114115
}
115116
}

network/manager.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,22 @@ func (nm *networkManager) restore() error {
121121
rebootTime, err := platform.GetLastRebootTime()
122122
log.Printf("[net] reboot time %v store mod time %v", rebootTime, modTime)
123123
if err == nil && rebootTime.After(modTime) {
124+
log.Printf("[net] Detected Reboot")
124125
rebooted = true
126+
if clearNwConfig, err := platform.ClearNetworkConfiguration(); clearNwConfig {
127+
if err != nil {
128+
log.Printf("[net] Failed to clear network configuration, err:%v\n", err)
129+
return err
130+
}
131+
132+
// Clear networkManager contents
133+
nm.TimeStamp = time.Time{}
134+
for extIfName := range nm.ExternalInterfaces {
135+
delete(nm.ExternalInterfaces, extIfName)
136+
}
137+
138+
return nil
139+
}
125140
}
126141
}
127142

platform/os_linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,9 @@ func SetOutboundSNAT(subnet string) error {
8888
}
8989
return nil
9090
}
91+
92+
// ClearNetworkConfiguration clears the azure-vnet.json contents.
93+
// This will be called only when reboot is detected - This is windows specific
94+
func ClearNetworkConfiguration() (bool, error) {
95+
return false, nil
96+
}

platform/os_windows.go

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
package platform
55

66
import (
7+
"fmt"
8+
"os/exec"
9+
"strconv"
10+
"strings"
711
"time"
12+
13+
"github.com/Azure/azure-container-networking/log"
814
)
915

1016
const (
@@ -32,8 +38,60 @@ func GetOSInfo() string {
3238

3339
// GetLastRebootTime returns the last time the system rebooted.
3440
func GetLastRebootTime() (time.Time, error) {
35-
var rebootTime time.Time
36-
return rebootTime, nil
41+
var systemBootTime string
42+
out, err := exec.Command("cmd", "/c", "systeminfo").Output()
43+
if err != nil {
44+
log.Printf("Failed to query systeminfo, err: %v", err)
45+
return time.Time{}.UTC(), err
46+
}
47+
48+
systemInfo := strings.Split(string(out), "\n")
49+
for _, systemProperty := range systemInfo {
50+
if strings.Contains(systemProperty, "Boot Time") {
51+
systemBootTime = strings.TrimSpace(strings.Split(systemProperty, "System Boot Time:")[1])
52+
}
53+
}
54+
55+
if len(strings.TrimSpace(systemBootTime)) == 0 {
56+
log.Printf("Failed to retrieve boot time from systeminfo")
57+
return time.Time{}.UTC(), fmt.Errorf("Failed to retrieve boot time from systeminfo")
58+
}
59+
60+
log.Printf("Boot time: %s", systemBootTime)
61+
// The System Boot Time is in the following format "01/02/2006, 03:04:05 PM"
62+
// Formulate the Boot Time in the format: "2006-01-02 15:04:05"
63+
bootDate := strings.Split(systemBootTime, " ")[0]
64+
bootTime := strings.Split(systemBootTime, " ")[1]
65+
bootPM := strings.Contains(strings.Split(systemBootTime, " ")[2], "PM")
66+
67+
month := strings.Split(bootDate, "/")[0]
68+
day := strings.Split(bootDate, "/")[1]
69+
year := strings.Split(bootDate, "/")[2]
70+
year = strings.Trim(year, ",")
71+
hour := strings.Split(bootTime, ":")[0]
72+
hourInt, _ := strconv.Atoi(hour)
73+
min := strings.Split(bootTime, ":")[1]
74+
sec := strings.Split(bootTime, ":")[2]
75+
76+
if bootPM && hourInt < 12 {
77+
hourInt += 12
78+
} else if !bootPM && hourInt == 12 {
79+
hourInt = 0
80+
}
81+
82+
hour = strconv.Itoa(hourInt)
83+
systemBootTime = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
84+
log.Printf("Formatted Boot time: %s", systemBootTime)
85+
86+
// Parse the boot time.
87+
layout := "2006-01-02 15:04:05"
88+
rebootTime, err := time.ParseInLocation(layout, systemBootTime, time.Local)
89+
if err != nil {
90+
log.Printf("Failed to parse boot time, err:%v", err)
91+
return time.Time{}.UTC(), err
92+
}
93+
94+
return rebootTime.UTC(), nil
3795
}
3896

3997
func ExecuteCommand(command string) (string, error) {
@@ -43,3 +101,18 @@ func ExecuteCommand(command string) (string, error) {
43101
func SetOutboundSNAT(subnet string) error {
44102
return nil
45103
}
104+
105+
// ClearNetworkConfiguration clears the azure-vnet.json contents.
106+
// This will be called only when reboot is detected - This is windows specific
107+
func ClearNetworkConfiguration() (bool, error) {
108+
jsonStore := CNIRuntimePath + "azure-vnet.json"
109+
log.Printf("Deleting the json store %s", jsonStore)
110+
cmd := exec.Command("cmd", "/c", "del", jsonStore)
111+
112+
if err := cmd.Run(); err != nil {
113+
log.Printf("Error deleting the json store %s", jsonStore)
114+
return true, err
115+
}
116+
117+
return true, nil
118+
}

0 commit comments

Comments
 (0)