Skip to content

Commit a8573ab

Browse files
committed
address feedback
Signed-off-by: Evan Baker <[email protected]>
1 parent 2fe0601 commit a8573ab

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

cns/service/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ func main() {
787787
}
788788

789789
// Setting the remote ARP MAC address to 12-34-56-78-9a-bc on windows for external traffic if HNS is enabled
790-
err = platform.SetSdnRemoteArpMacAddress()
790+
err = platform.SetSdnRemoteArpMacAddress(rootCtx)
791791
if err != nil {
792792
logger.Errorf("Failed to set remote ARP MAC address: %v", err)
793793
return

platform/os_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (p *execClient) KillProcessByName(processName string) error {
179179

180180
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy
181181
// This operation is specific to windows OS
182-
func SetSdnRemoteArpMacAddress() error {
182+
func SetSdnRemoteArpMacAddress(context.Context) error {
183183
return nil
184184
}
185185

platform/os_windows.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ const (
6565
SDNRemoteArpMacAddress = "12-34-56-78-9a-bc"
6666

6767
// Command to fetch netadapter and pnp id
68-
// TODO can we replace this (and things in endpoint_windows) with "golang.org/x/sys/windows"
69-
// var adapterInfo windows.IpAdapterInfo
70-
// var bufferSize uint32 = uint32(unsafe.Sizeof(adapterInfo))
68+
// TODO: can we replace this (and things in endpoint_windows) with other utils from "golang.org/x/sys/windows"?
7169
GetMacAddressVFPPnpIDMapping = "Get-NetAdapter | Select-Object MacAddress, PnpDeviceID| Format-Table -HideTableHeaders"
7270

7371
// Interval between successive checks for mellanox adapter's PriorityVLANTag value
@@ -248,7 +246,8 @@ func (p *execClient) ExecutePowershellCommandWithContext(ctx context.Context, co
248246
}
249247

250248
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy if hns is enabled
251-
func SetSdnRemoteArpMacAddress() error {
249+
func SetSdnRemoteArpMacAddress(ctx context.Context) error {
250+
log.Printf("Setting SDNRemoteArpMacAddress regKey")
252251
// open the registry key
253252
k, err := registry.OpenKey(registry.LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\hns\\State", registry.READ|registry.SET_VALUE)
254253
if err != nil {
@@ -262,6 +261,8 @@ func SetSdnRemoteArpMacAddress() error {
262261
if err = setSDNRemoteARPMACAddress(k); err != nil {
263262
return errors.Wrap(err, "could not set registry key")
264263
}
264+
log.Printf("SDNRemoteArpMacAddress regKey set successfully")
265+
log.Printf("Restarting HNS service")
265266
// connect to the service manager
266267
m, err := mgr.Connect()
267268
if err != nil {
@@ -274,7 +275,11 @@ func SetSdnRemoteArpMacAddress() error {
274275
return errors.Wrap(err, "could not access service")
275276
}
276277
defer service.Close()
277-
return errors.Wrap(restartService(service), "could not restart service")
278+
if err := restartService(ctx, service); err != nil {
279+
return errors.Wrap(err, "could not restart service")
280+
}
281+
log.Printf("HNS service restarted successfully")
282+
return nil
278283
}
279284

280285
type key interface {
@@ -287,31 +292,37 @@ func setSDNRemoteARPMACAddress(k key) error {
287292
if err := k.SetStringValue("SDNRemoteArpMacAddress", SDNRemoteArpMacAddress); err != nil {
288293
return errors.Wrap(err, "could not set registry key")
289294
}
290-
log.Printf("[Azure CNS] SDNRemoteArpMacAddress regKey set successfully. Restarting hns service.")
291295
return nil
292296
}
293297

294-
type serv interface {
295-
Control(code svc.Cmd) (svc.Status, error)
296-
Query() (svc.Status, error)
297-
Start(...string) error
298-
}
299-
300-
func restartService(s serv) error {
298+
func restartService(ctx context.Context, s *mgr.Service) error {
301299
// Stop the service
302300
_, err := s.Control(svc.Stop)
303301
if err != nil {
304302
return errors.Wrap(err, "could not stop service")
305303
}
306304
// Wait for the service to stop
307-
for status, err := s.Query(); status.State != svc.Stopped; status, err = s.Query() {
305+
ticker := time.NewTicker(500 * time.Millisecond) //nolint:gomnd // 500ms
306+
defer ticker.Stop()
307+
for { // hacky cancellable do-while
308+
status, err := s.Query()
308309
if err != nil {
309310
return errors.Wrap(err, "could not query service status")
310311
}
311-
time.Sleep(500 * time.Millisecond) //nolint:gomnd // 500ms
312+
if status.State == svc.Stopped {
313+
break
314+
}
315+
select {
316+
case <-ctx.Done():
317+
return errors.New("context cancelled")
318+
case <-ticker.C:
319+
}
312320
}
313321
// Start the service again
314-
return errors.Wrap(s.Start(), "could not start service")
322+
if err := s.Start(); err != nil {
323+
return errors.Wrap(err, "could not start service")
324+
}
325+
return nil
315326
}
316327

317328
func HasMellanoxAdapter() bool {
@@ -384,7 +395,6 @@ func GetProcessNameByID(pidstr string) (string, error) {
384395
pidstr = strings.Trim(pidstr, "\r\n")
385396
cmd := fmt.Sprintf("Get-Process -Id %s|Format-List", pidstr)
386397
p := NewExecClient(nil)
387-
// TODO not riemovign this because it seems to only be called in test?
388398
out, err := p.ExecutePowershellCommand(cmd)
389399
if err != nil {
390400
log.Printf("Process is not running. Output:%v, Error %v", out, err)

0 commit comments

Comments
 (0)