@@ -166,6 +166,18 @@ func ExecutePowershellCommand(command string) (string, error) {
166166
167167// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy if hns is enabled
168168func SetSdnRemoteArpMacAddress (ctx context.Context ) error {
169+ if err := setSDNRemoteARPRegKey (); err != nil {
170+ return err
171+ }
172+ log .Printf ("SDNRemoteArpMacAddress regKey set successfully" )
173+ if err := restartHNS (ctx ); err != nil {
174+ return err
175+ }
176+ log .Printf ("HNS service restarted successfully" )
177+ return nil
178+ }
179+
180+ func setSDNRemoteARPRegKey () error {
169181 log .Printf ("Setting SDNRemoteArpMacAddress regKey" )
170182 // open the registry key
171183 k , err := registry .OpenKey (registry .LOCAL_MACHINE , `SYSTEM\CurrentControlSet\Services\hns\State` , registry .READ | registry .SET_VALUE )
@@ -184,7 +196,10 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
184196 if err = k .SetStringValue ("SDNRemoteArpMacAddress" , SDNRemoteArpMacAddress ); err != nil {
185197 return errors .Wrap (err , "could not set registry key" )
186198 }
187- log .Printf ("SDNRemoteArpMacAddress regKey set successfully" )
199+ return nil
200+ }
201+
202+ func restartHNS (ctx context.Context ) error {
188203 log .Printf ("Restarting HNS service" )
189204 // connect to the service manager
190205 m , err := mgr .Connect ()
@@ -198,24 +213,16 @@ func SetSdnRemoteArpMacAddress(ctx context.Context) error {
198213 return errors .Wrap (err , "could not access service" )
199214 }
200215 defer service .Close ()
201- if err := restartService (ctx , service ); err != nil {
202- return errors .Wrap (err , "could not restart service" )
203- }
204- log .Printf ("HNS service restarted successfully" )
205- return nil
206- }
207-
208- func restartService (ctx context.Context , s * mgr.Service ) error {
209216 // Stop the service
210- _ , err := s .Control (svc .Stop )
217+ _ , err = service .Control (svc .Stop )
211218 if err != nil {
212219 return errors .Wrap (err , "could not stop service" )
213220 }
214221 // Wait for the service to stop
215222 ticker := time .NewTicker (500 * time .Millisecond ) //nolint:gomnd // 500ms
216223 defer ticker .Stop ()
217224 for { // hacky cancellable do-while
218- status , err := s .Query ()
225+ status , err := service .Query ()
219226 if err != nil {
220227 return errors .Wrap (err , "could not query service status" )
221228 }
@@ -229,7 +236,7 @@ func restartService(ctx context.Context, s *mgr.Service) error {
229236 }
230237 }
231238 // Start the service again
232- if err := s .Start (); err != nil {
239+ if err := service .Start (); err != nil {
233240 return errors .Wrap (err , "could not start service" )
234241 }
235242 return nil
0 commit comments