@@ -247,6 +247,18 @@ func (p *execClient) ExecutePowershellCommandWithContext(ctx context.Context, co
247247
248248// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy if hns is enabled
249249func SetSdnRemoteArpMacAddress (ctx context.Context ) error {
250+ if err := setSDNRemoteARPRegKey (); err != nil {
251+ return err
252+ }
253+ log .Printf ("SDNRemoteArpMacAddress regKey set successfully" )
254+ if err := restartHNS (ctx ); err != nil {
255+ return err
256+ }
257+ log .Printf ("HNS service restarted successfully" )
258+ return nil
259+ }
260+
261+ func setSDNRemoteARPRegKey () error {
250262 log .Printf ("Setting SDNRemoteArpMacAddress regKey" )
251263 // open the registry key
252264 k , err := registry .OpenKey (registry .LOCAL_MACHINE , `SYSTEM\CurrentControlSet\Services\hns\State` , registry .READ | registry .SET_VALUE )
@@ -265,7 +277,10 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
265277 if err = k .SetStringValue ("SDNRemoteArpMacAddress" , SDNRemoteArpMacAddress ); err != nil {
266278 return errors .Wrap (err , "could not set registry key" )
267279 }
268- log .Printf ("SDNRemoteArpMacAddress regKey set successfully" )
280+ return nil
281+ }
282+
283+ func restartHNS (ctx context.Context ) error {
269284 log .Printf ("Restarting HNS service" )
270285 // connect to the service manager
271286 m , err := mgr .Connect ()
@@ -279,24 +294,16 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
279294 return errors .Wrap (err , "could not access service" )
280295 }
281296 defer service .Close ()
282- if err := restartService (ctx , service ); err != nil {
283- return errors .Wrap (err , "could not restart service" )
284- }
285- log .Printf ("HNS service restarted successfully" )
286- return nil
287- }
288-
289- func restartService (ctx context.Context , s * mgr.Service ) error {
290297 // Stop the service
291- _ , err := s .Control (svc .Stop )
298+ _ , err = service .Control (svc .Stop )
292299 if err != nil {
293300 return errors .Wrap (err , "could not stop service" )
294301 }
295302 // Wait for the service to stop
296303 ticker := time .NewTicker (500 * time .Millisecond ) //nolint:gomnd // 500ms
297304 defer ticker .Stop ()
298305 for { // hacky cancellable do-while
299- status , err := s .Query ()
306+ status , err := service .Query ()
300307 if err != nil {
301308 return errors .Wrap (err , "could not query service status" )
302309 }
@@ -310,7 +317,7 @@ func restartService(ctx context.Context, s *mgr.Service) error {
310317 }
311318 }
312319 // Start the service again
313- if err := s .Start (); err != nil {
320+ if err := service .Start (); err != nil {
314321 return errors .Wrap (err , "could not start service" )
315322 }
316323 return nil
0 commit comments