@@ -247,6 +247,16 @@ 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+ restartHNS (ctx )
255+ log .Printf ("HNS service restarted successfully" )
256+ return nil
257+ }
258+
259+ func setSDNRemoteARPRegKey () error {
250260 log .Printf ("Setting SDNRemoteArpMacAddress regKey" )
251261 // open the registry key
252262 k , err := registry .OpenKey (registry .LOCAL_MACHINE , `SYSTEM\CurrentControlSet\Services\hns\State` , registry .READ | registry .SET_VALUE )
@@ -265,7 +275,10 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
265275 if err = k .SetStringValue ("SDNRemoteArpMacAddress" , SDNRemoteArpMacAddress ); err != nil {
266276 return errors .Wrap (err , "could not set registry key" )
267277 }
268- log .Printf ("SDNRemoteArpMacAddress regKey set successfully" )
278+ return nil
279+ }
280+
281+ func restartHNS (ctx context.Context ) error {
269282 log .Printf ("Restarting HNS service" )
270283 // connect to the service manager
271284 m , err := mgr .Connect ()
@@ -279,24 +292,16 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
279292 return errors .Wrap (err , "could not access service" )
280293 }
281294 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 {
290295 // Stop the service
291- _ , err := s .Control (svc .Stop )
296+ _ , err = service .Control (svc .Stop )
292297 if err != nil {
293298 return errors .Wrap (err , "could not stop service" )
294299 }
295300 // Wait for the service to stop
296301 ticker := time .NewTicker (500 * time .Millisecond ) //nolint:gomnd // 500ms
297302 defer ticker .Stop ()
298303 for { // hacky cancellable do-while
299- status , err := s .Query ()
304+ status , err := service .Query ()
300305 if err != nil {
301306 return errors .Wrap (err , "could not query service status" )
302307 }
@@ -310,7 +315,7 @@ func restartService(ctx context.Context, s *mgr.Service) error {
310315 }
311316 }
312317 // Start the service again
313- if err := s .Start (); err != nil {
318+ if err := service .Start (); err != nil {
314319 return errors .Wrap (err , "could not start service" )
315320 }
316321 return nil
0 commit comments