@@ -28,12 +28,13 @@ import (
2828 sriovv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
2929 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
3030 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/helper"
31+ hosttypes "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
3132 snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log"
3233 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms"
3334 plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins"
3435 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/generic"
3536 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/virtual"
36- "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/systemd "
37+ "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils "
3738 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
3839 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/version"
3940)
@@ -64,11 +65,32 @@ var (
6465 newPlatformHelperFunc = platforms .NewDefaultPlatformHelper
6566)
6667
68+ // ServiceConfig is a struct that encapsulates the configuration and dependencies
69+ // needed by the SriovNetworkConfigDaemon systemd service.
70+ type ServiceConfig struct {
71+ helper.HostHelpersInterface // Provides host-specific helper functions
72+ logr.Logger // Handles logging for the service
73+ * hosttypes.SriovConfig // Contains the SR-IOV network configuration settings
74+ }
75+
6776func init () {
6877 rootCmd .AddCommand (serviceCmd )
6978 serviceCmd .Flags ().StringVarP (& phaseArg , "phase" , "p" , PhasePre , fmt .Sprintf ("configuration phase, supported values are: %s, %s" , PhasePre , PhasePost ))
7079}
7180
81+ func newServiceConfig (setupLog logr.Logger ) (* ServiceConfig , error ) {
82+ hostHelpers , err := newHostHelpersFunc ()
83+ if err != nil {
84+ return nil , fmt .Errorf ("failed to create host helpers: %v" , err )
85+ }
86+
87+ return & ServiceConfig {
88+ hostHelpers ,
89+ setupLog ,
90+ nil ,
91+ }, nil
92+ }
93+
7294// The service supports two configuration phases:
7395// * pre(default) - before the NetworkManager or systemd-networkd
7496// * post - after the NetworkManager or systemd-networkd
@@ -94,105 +116,108 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
94116 vars .UsingSystemdMode = true
95117 vars .InChroot = true
96118
97- sriovConf , err := readConf (setupLog )
119+ sc , err := newServiceConfig (setupLog )
98120 if err != nil {
99- return updateSriovResultErr (setupLog , phaseArg , err )
121+ setupLog .Error (err , "failed to create the service configuration controller, Exiting" )
122+ return err
100123 }
101- setupLog .V (2 ).Info ("sriov-config-service" , "config" , sriovConf )
102- vars .DevMode = sriovConf .UnsupportedNics
103- vars .ManageSoftwareBridges = sriovConf .ManageSoftwareBridges
104- vars .OVSDBSocketPath = sriovConf .OVSDBSocketPath
105124
106- if err := initSupportedNics (); err != nil {
107- return updateSriovResultErr (setupLog , phaseArg , fmt .Errorf ("failed to initialize list of supported NIC ids: %v" , err ))
125+ err = sc .readConf ()
126+ if err != nil {
127+ return sc .updateSriovResultErr (phaseArg , err )
108128 }
109129
110- hostHelpers , err := newHostHelpersFunc ()
111- if err != nil {
112- return updateSriovResultErr (setupLog , phaseArg , fmt .Errorf ("failed to create hostHelpers: %v" , err ))
130+ setupLog .V (2 ).Info ("sriov-config-service" , "config" , sc .SriovConfig )
131+ vars .DevMode = sc .SriovConfig .UnsupportedNics
132+ vars .ManageSoftwareBridges = sc .SriovConfig .ManageSoftwareBridges
133+ vars .OVSDBSocketPath = sc .SriovConfig .OVSDBSocketPath
134+
135+ if err := sc .initSupportedNics (); err != nil {
136+ return sc .updateSriovResultErr (phaseArg , fmt .Errorf ("failed to initialize list of supported NIC ids: %v" , err ))
113137 }
114138
115- waitForDevicesInitialization (setupLog , sriovConf , hostHelpers )
139+ sc . waitForDevicesInitialization ()
116140
117141 if phaseArg == PhasePre {
118- err = phasePre (setupLog , sriovConf , hostHelpers )
142+ err = sc . phasePre ()
119143 } else {
120- err = phasePost (setupLog , sriovConf , hostHelpers )
144+ err = sc . phasePost ()
121145 }
122146 if err != nil {
123- return updateSriovResultErr (setupLog , phaseArg , err )
147+ return sc . updateSriovResultErr (phaseArg , err )
124148 }
125- return updateSriovResultOk (setupLog , phaseArg )
149+ return sc . updateSriovResultOk (phaseArg )
126150}
127151
128- func readConf ( setupLog logr. Logger ) ( * systemd. SriovConfig , error ) {
129- nodeStateSpec , err := systemd .ReadConfFile ()
152+ func ( s * ServiceConfig ) readConf () error {
153+ nodeStateSpec , err := s .ReadConfFile ()
130154 if err != nil {
131- if _ , err := os .Stat (systemd . SriovSystemdConfigPath ); ! errors .Is (err , os .ErrNotExist ) {
132- return nil , fmt .Errorf ("failed to read the sriov configuration file in path %s: %v" , systemd . SriovSystemdConfigPath , err )
155+ if _ , err := os .Stat (utils . GetHostExtensionPath ( consts . SriovSystemdConfigPath ) ); ! errors .Is (err , os .ErrNotExist ) {
156+ return fmt .Errorf ("failed to read the sriov configuration file in path %s: %v" , utils . GetHostExtensionPath ( consts . SriovSystemdConfigPath ) , err )
133157 }
134- setupLog .Info ("configuration file not found, use default config" )
135- nodeStateSpec = & systemd .SriovConfig {
158+ s .Info ("configuration file not found, use default config" )
159+ nodeStateSpec = & hosttypes .SriovConfig {
136160 Spec : sriovv1.SriovNetworkNodeStateSpec {},
137161 UnsupportedNics : false ,
138162 PlatformType : consts .Baremetal ,
139163 }
140164 }
141- return nodeStateSpec , nil
165+ s .SriovConfig = nodeStateSpec
166+ return nil
142167}
143168
144- func initSupportedNics () error {
145- supportedNicIds , err := systemd .ReadSriovSupportedNics ()
169+ func ( s * ServiceConfig ) initSupportedNics () error {
170+ supportedNicIds , err := s .ReadSriovSupportedNics ()
146171 if err != nil {
147172 return fmt .Errorf ("failed to read list of supported nic ids: %v" , err )
148173 }
149174 sriovv1 .InitNicIDMapFromList (supportedNicIds )
150175 return nil
151176}
152177
153- func phasePre ( setupLog logr. Logger , conf * systemd. SriovConfig , hostHelpers helper. HostHelpersInterface ) error {
178+ func ( s * ServiceConfig ) phasePre ( ) error {
154179 // make sure there is no stale result file to avoid situation when we
155180 // read outdated info in the Post phase when the Pre silently failed (should not happen)
156- if err := systemd .RemoveSriovResult (); err != nil {
181+ if err := s .RemoveSriovResult (); err != nil {
157182 return fmt .Errorf ("failed to remove sriov result file: %v" , err )
158183 }
159184
160- _ , err := hostHelpers .CheckRDMAEnabled ()
185+ _ , err := s .CheckRDMAEnabled ()
161186 if err != nil {
162- setupLog .Error (err , "warning, failed to check RDMA state" )
187+ s .Error (err , "warning, failed to check RDMA state" )
163188 }
164- hostHelpers .TryEnableTun ()
165- hostHelpers .TryEnableVhostNet ()
189+ s .TryEnableTun ()
190+ s .TryEnableVhostNet ()
166191
167- return callPlugin (setupLog , PhasePre , conf , hostHelpers )
192+ return s . callPlugin (PhasePre )
168193}
169194
170- func phasePost ( setupLog logr. Logger , conf * systemd. SriovConfig , hostHelpers helper. HostHelpersInterface ) error {
171- setupLog .V (0 ).Info ("check result of the Pre phase" )
172- prePhaseResult , _ , err := systemd .ReadSriovResult ()
195+ func ( s * ServiceConfig ) phasePost ( ) error {
196+ s .V (0 ).Info ("check result of the Pre phase" )
197+ prePhaseResult , _ , err := s .ReadSriovResult ()
173198 if err != nil {
174199 return fmt .Errorf ("failed to read result of the pre phase: %v" , err )
175200 }
176201 if prePhaseResult .SyncStatus != consts .SyncStatusInProgress {
177202 return fmt .Errorf ("unexpected result of the pre phase: %s, syncError: %s" , prePhaseResult .SyncStatus , prePhaseResult .LastSyncError )
178203 }
179- setupLog .V (0 ).Info ("Pre phase succeed, continue execution" )
204+ s .V (0 ).Info ("Pre phase succeed, continue execution" )
180205
181- return callPlugin (setupLog , PhasePost , conf , hostHelpers )
206+ return s . callPlugin (PhasePost )
182207}
183208
184- func callPlugin ( setupLog logr. Logger , phase string , conf * systemd. SriovConfig , hostHelpers helper. HostHelpersInterface ) error {
185- configPlugin , err := getPlugin (setupLog , phase , conf , hostHelpers )
209+ func ( s * ServiceConfig ) callPlugin ( phase string ) error {
210+ configPlugin , err := s . getPlugin (phase )
186211 if err != nil {
187212 return err
188213 }
189214
190215 if configPlugin == nil {
191- setupLog .V (0 ).Info ("no plugin for the platform for the current phase, skip calling" , "platform" , conf .PlatformType )
216+ s .V (0 ).Info ("no plugin for the platform for the current phase, skip calling" , "platform" , s . SriovConfig .PlatformType )
192217 return nil
193218 }
194219
195- nodeState , err := getNetworkNodeState (setupLog , conf , phase , hostHelpers )
220+ nodeState , err := s . getNetworkNodeState (phase )
196221 if err != nil {
197222 return err
198223 }
@@ -204,60 +229,58 @@ func callPlugin(setupLog logr.Logger, phase string, conf *systemd.SriovConfig, h
204229 if err = configPlugin .Apply (); err != nil {
205230 return fmt .Errorf ("failed to apply configuration: %v" , err )
206231 }
207- setupLog .V (0 ).Info ("plugin call succeed" )
232+ s .V (0 ).Info ("plugin call succeed" )
208233 return nil
209234}
210235
211- func getPlugin (setupLog logr.Logger , phase string ,
212- conf * systemd.SriovConfig , hostHelpers helper.HostHelpersInterface ) (plugin.VendorPlugin , error ) {
236+ func (s * ServiceConfig ) getPlugin (phase string ) (plugin.VendorPlugin , error ) {
213237 var (
214238 configPlugin plugin.VendorPlugin
215239 err error
216240 )
217- switch conf .PlatformType {
241+ switch s . SriovConfig .PlatformType {
218242 case consts .Baremetal :
219243 switch phase {
220244 case PhasePre :
221- configPlugin , err = newGenericPluginFunc (hostHelpers ,
245+ configPlugin , err = newGenericPluginFunc (s ,
222246 generic .WithSkipVFConfiguration (),
223247 generic .WithSkipBridgeConfiguration ())
224248 case PhasePost :
225- configPlugin , err = newGenericPluginFunc (hostHelpers )
249+ configPlugin , err = newGenericPluginFunc (s )
226250 }
227251 if err != nil {
228252 return nil , fmt .Errorf ("failed to create generic plugin for %v" , err )
229253 }
230254 case consts .VirtualOpenStack :
231255 switch phase {
232256 case PhasePre :
233- configPlugin , err = newVirtualPluginFunc (hostHelpers )
257+ configPlugin , err = newVirtualPluginFunc (s )
234258 if err != nil {
235259 return nil , fmt .Errorf ("failed to create virtual plugin %v" , err )
236260 }
237261 case PhasePost :
238- setupLog .Info ("skip post configuration phase for virtual cluster" )
262+ s .Info ("skip post configuration phase for virtual cluster" )
239263 return nil , nil
240264 }
241265 }
242266 return configPlugin , nil
243267}
244268
245- func getNetworkNodeState (setupLog logr.Logger , conf * systemd.SriovConfig , phase string ,
246- hostHelpers helper.HostHelpersInterface ) (* sriovv1.SriovNetworkNodeState , error ) {
269+ func (s * ServiceConfig ) getNetworkNodeState (phase string ) (* sriovv1.SriovNetworkNodeState , error ) {
247270 var (
248271 ifaceStatuses []sriovv1.InterfaceExt
249272 bridges sriovv1.Bridges
250273 err error
251274 )
252- switch conf .PlatformType {
275+ switch s . SriovConfig .PlatformType {
253276 case consts .Baremetal :
254- ifaceStatuses , err = hostHelpers .DiscoverSriovDevices (hostHelpers )
277+ ifaceStatuses , err = s .DiscoverSriovDevices (s )
255278 if err != nil {
256279 return nil , fmt .Errorf ("failed to discover sriov devices on the host: %v" , err )
257280 }
258281 if phase != PhasePre && vars .ManageSoftwareBridges {
259282 // openvswitch is not available during the pre phase
260- bridges , err = hostHelpers .DiscoverBridges ()
283+ bridges , err = s .DiscoverBridges ()
261284 if err != nil {
262285 return nil , fmt .Errorf ("failed to discover managed bridges on the host: %v" , err )
263286 }
@@ -277,40 +300,40 @@ func getNetworkNodeState(setupLog logr.Logger, conf *systemd.SriovConfig, phase
277300 }
278301 }
279302 return & sriovv1.SriovNetworkNodeState {
280- Spec : conf .Spec ,
303+ Spec : s . SriovConfig .Spec ,
281304 Status : sriovv1.SriovNetworkNodeStateStatus {Interfaces : ifaceStatuses , Bridges : bridges },
282305 }, nil
283306}
284307
285- func updateSriovResultErr ( setupLog logr. Logger , phase string , origErr error ) error {
286- setupLog .Error (origErr , "service call failed" )
287- err := updateResult (setupLog , consts .SyncStatusFailed , fmt .Sprintf ("%s: %v" , phase , origErr ))
308+ func ( s * ServiceConfig ) updateSriovResultErr ( phase string , origErr error ) error {
309+ s .Error (origErr , "service call failed" )
310+ err := s . updateResult (consts .SyncStatusFailed , fmt .Sprintf ("%s: %v" , phase , origErr ))
288311 if err != nil {
289312 return err
290313 }
291314 return origErr
292315}
293316
294- func updateSriovResultOk ( setupLog logr. Logger , phase string ) error {
295- setupLog .V (0 ).Info ("service call succeed" )
317+ func ( s * ServiceConfig ) updateSriovResultOk ( phase string ) error {
318+ s .V (0 ).Info ("service call succeed" )
296319 syncStatus := consts .SyncStatusSucceeded
297320 if phase == PhasePre {
298321 syncStatus = consts .SyncStatusInProgress
299322 }
300- return updateResult (setupLog , syncStatus , "" )
323+ return s . updateResult (syncStatus , "" )
301324}
302325
303- func updateResult ( setupLog logr. Logger , result , msg string ) error {
304- sriovResult := & systemd .SriovResult {
326+ func ( s * ServiceConfig ) updateResult ( result , msg string ) error {
327+ sriovResult := & hosttypes .SriovResult {
305328 SyncStatus : result ,
306329 LastSyncError : msg ,
307330 }
308- err := systemd .WriteSriovResult (sriovResult )
331+ err := s .WriteSriovResult (sriovResult )
309332 if err != nil {
310- setupLog .Error (err , "failed to write sriov result file" , "content" , * sriovResult )
333+ s .Error (err , "failed to write sriov result file" , "content" , * sriovResult )
311334 return fmt .Errorf ("sriov-config-service failed to write sriov result file with content %v error: %v" , * sriovResult , err )
312335 }
313- setupLog .V (0 ).Info ("result file updated" , "SyncStatus" , sriovResult .SyncStatus , "LastSyncError" , msg )
336+ s .V (0 ).Info ("result file updated" , "SyncStatus" , sriovResult .SyncStatus , "LastSyncError" , msg )
314337 return nil
315338}
316339
@@ -331,20 +354,20 @@ func updateResult(setupLog logr.Logger, result, msg string) error {
331354//
332355// Note: Currently, this function handles only Baremetal clusters. We do not have evidence that
333356// this logic is required for virtual clusters.
334- func waitForDevicesInitialization ( setupLog logr. Logger , conf * systemd. SriovConfig , hostHelpers helper. HostHelpersInterface ) {
335- if conf .PlatformType != consts .Baremetal {
357+ func ( s * ServiceConfig ) waitForDevicesInitialization ( ) {
358+ if s . SriovConfig .PlatformType != consts .Baremetal {
336359 // skip waiting on virtual cluster
337360 return
338361 }
339362 // wait for devices from the spec to be registered in the system with expected names
340- devicesToWait := make (map [string ]string , len (conf .Spec .Interfaces ))
341- for _ , d := range conf .Spec .Interfaces {
363+ devicesToWait := make (map [string ]string , len (s . SriovConfig .Spec .Interfaces ))
364+ for _ , d := range s . SriovConfig .Spec .Interfaces {
342365 devicesToWait [d .PciAddress ] = d .Name
343366 }
344367 deadline := time .Now ().Add (time .Second * time .Duration (InitializationDeviceDiscoveryTimeoutSec ))
345368 for time .Now ().Before (deadline ) {
346369 for pciAddr , name := range devicesToWait {
347- if hostHelpers .TryGetInterfaceName (pciAddr ) == name {
370+ if s .TryGetInterfaceName (pciAddr ) == name {
348371 delete (devicesToWait , pciAddr )
349372 }
350373 }
@@ -354,10 +377,10 @@ func waitForDevicesInitialization(setupLog logr.Logger, conf *systemd.SriovConfi
354377 time .Sleep (time .Second )
355378 }
356379 if len (devicesToWait ) != 0 {
357- setupLog .Info ("WARNING: some devices were not initialized" , "devices" , devicesToWait , "timeout" , InitializationDeviceDiscoveryTimeoutSec )
380+ s .Info ("WARNING: some devices were not initialized" , "devices" , devicesToWait , "timeout" , InitializationDeviceDiscoveryTimeoutSec )
358381 }
359- if err := hostHelpers .WaitUdevEventsProcessed (InitializationDeviceUdevProcessingTimeoutSec ); err != nil {
360- setupLog .Info ("WARNING: failed to wait for udev events processing" , "reason" , err .Error (),
382+ if err := s .WaitUdevEventsProcessed (InitializationDeviceUdevProcessingTimeoutSec ); err != nil {
383+ s .Info ("WARNING: failed to wait for udev events processing" , "reason" , err .Error (),
361384 "timeout" , InitializationDeviceUdevProcessingTimeoutSec )
362385 }
363386}
0 commit comments