@@ -30,19 +30,14 @@ import (
3030 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/helper"
3131 hosttypes "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
3232 snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log"
33- "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms "
33+ "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platform "
3434 plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins"
35- "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/generic"
36- "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/virtual"
3735 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
3836 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
3937 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/version"
4038)
4139
4240const (
43- PhasePre = "pre"
44- PhasePost = "post"
45-
4641 // InitializationDeviceDiscoveryTimeoutSec constant defines the number of
4742 // seconds to wait for devices to be registered in the system with the expected name.
4843 InitializationDeviceDiscoveryTimeoutSec = 60
@@ -59,36 +54,50 @@ var (
5954 }
6055 phaseArg string
6156
62- newGenericPluginFunc = generic .NewGenericPlugin
63- newVirtualPluginFunc = virtual .NewVirtualPlugin
64- newHostHelpersFunc = helper .NewDefaultHostHelpers
65- newPlatformHelperFunc = platforms .NewDefaultPlatformHelper
57+ newPlatformFunc = platform .New
58+ newHostHelpersFunc = helper .NewDefaultHostHelpers
6659)
6760
6861// ServiceConfig is a struct that encapsulates the configuration and dependencies
6962// needed by the SriovNetworkConfigDaemon systemd service.
7063type ServiceConfig struct {
71- hostHelper helper.HostHelpersInterface // Provides host-specific helper functions
72- log logr.Logger // Handles logging for the service
73- sriovConfig * hosttypes.SriovConfig // Contains the SR-IOV network configuration settings
64+ hostHelper helper.HostHelpersInterface // Provides host-specific helper functions
65+ platformInterface platform.Interface // Provides platform helpers function
66+ log logr.Logger // Handles logging for the service
67+ sriovConfig * hosttypes.SriovConfig // Contains the SR-IOV network configuration settings
7468}
7569
7670func init () {
7771 rootCmd .AddCommand (serviceCmd )
78- serviceCmd .Flags ().StringVarP (& phaseArg , "phase" , "p" , PhasePre , fmt .Sprintf ("configuration phase, supported values are: %s, %s" , PhasePre , PhasePost ))
72+ serviceCmd .Flags ().StringVarP (& phaseArg , "phase" , "p" , consts . PhasePre , fmt .Sprintf ("configuration phase, supported values are: %s, %s" , consts . PhasePre , consts . PhasePost ))
7973}
8074
8175func newServiceConfig (setupLog logr.Logger ) (* ServiceConfig , error ) {
8276 hostHelpers , err := newHostHelpersFunc ()
8377 if err != nil {
8478 return nil , fmt .Errorf ("failed to create host helpers: %v" , err )
8579 }
86-
87- return & ServiceConfig {
80+ sc := & ServiceConfig {
8881 hostHelpers ,
82+ nil ,
8983 setupLog ,
9084 nil ,
91- }, nil
85+ }
86+
87+ err = sc .readConf ()
88+ if err != nil {
89+ return nil , sc .updateSriovResultErr (phaseArg , err )
90+ }
91+
92+ // init globals
93+ vars .PlatformType = sc .sriovConfig .PlatformType
94+ platformInterface , err := newPlatformFunc (hostHelpers )
95+ if err != nil {
96+ return nil , fmt .Errorf ("failed to creeate serviceConfig: %w" , err )
97+ }
98+ sc .platformInterface = platformInterface
99+
100+ return sc , nil
92101}
93102
94103// The service supports two configuration phases:
@@ -102,8 +111,8 @@ func newServiceConfig(setupLog logr.Logger) (*ServiceConfig, error) {
102111// If the result of the "pre" phase is different than "InProgress", then the "post" phase will not be executed
103112// and the execution result will be forcefully set to "Failed".
104113func runServiceCmd (cmd * cobra.Command , args []string ) error {
105- if phaseArg != PhasePre && phaseArg != PhasePost {
106- return fmt .Errorf ("invalid value for \" --phase\" argument, valid values are: %s, %s" , PhasePre , PhasePost )
114+ if phaseArg != consts . PhasePre && phaseArg != consts . PhasePost {
115+ return fmt .Errorf ("invalid value for \" --phase\" argument, valid values are: %s, %s" , consts . PhasePre , consts . PhasePost )
107116 }
108117 // init logger
109118 snolog .InitLog ()
@@ -115,18 +124,14 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
115124 // Mark that we are running on host
116125 vars .UsingSystemdMode = true
117126 vars .InChroot = true
127+ vars .Destdir = "/tmp"
118128
119129 sc , err := newServiceConfig (setupLog )
120130 if err != nil {
121131 setupLog .Error (err , "failed to create the service configuration controller, Exiting" )
122132 return err
123133 }
124134
125- err = sc .readConf ()
126- if err != nil {
127- return sc .updateSriovResultErr (phaseArg , err )
128- }
129-
130135 setupLog .V (2 ).Info ("sriov-config-service" , "config" , sc .sriovConfig )
131136 vars .DevMode = sc .sriovConfig .UnsupportedNics
132137 vars .ManageSoftwareBridges = sc .sriovConfig .ManageSoftwareBridges
@@ -138,7 +143,12 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
138143
139144 sc .waitForDevicesInitialization ()
140145
141- if phaseArg == PhasePre {
146+ err = sc .platformInterface .Init ()
147+ if err != nil {
148+ return sc .updateSriovResultErr (phaseArg , fmt .Errorf ("failed to init platform configuration: %w" , err ))
149+ }
150+
151+ if phaseArg == consts .PhasePre {
142152 err = sc .phasePre ()
143153 } else {
144154 err = sc .phasePost ()
@@ -189,7 +199,7 @@ func (s *ServiceConfig) phasePre() error {
189199 s .hostHelper .TryEnableTun ()
190200 s .hostHelper .TryEnableVhostNet ()
191201
192- return s .callPlugin (PhasePre )
202+ return s .callPlugin (consts . PhasePre )
193203}
194204
195205func (s * ServiceConfig ) phasePost () error {
@@ -203,7 +213,7 @@ func (s *ServiceConfig) phasePost() error {
203213 }
204214 s .log .V (0 ).Info ("Pre phase succeed, continue execution" )
205215
206- return s .callPlugin (PhasePost )
216+ return s .callPlugin (consts . PhasePost )
207217}
208218
209219func (s * ServiceConfig ) callPlugin (phase string ) error {
@@ -234,36 +244,7 @@ func (s *ServiceConfig) callPlugin(phase string) error {
234244}
235245
236246func (s * ServiceConfig ) getPlugin (phase string ) (plugin.VendorPlugin , error ) {
237- var (
238- configPlugin plugin.VendorPlugin
239- err error
240- )
241- switch s .sriovConfig .PlatformType {
242- case consts .Baremetal :
243- switch phase {
244- case PhasePre :
245- configPlugin , err = newGenericPluginFunc (s .hostHelper ,
246- generic .WithSkipVFConfiguration (),
247- generic .WithSkipBridgeConfiguration ())
248- case PhasePost :
249- configPlugin , err = newGenericPluginFunc (s .hostHelper )
250- }
251- if err != nil {
252- return nil , fmt .Errorf ("failed to create generic plugin for %v" , err )
253- }
254- case consts .VirtualOpenStack :
255- switch phase {
256- case PhasePre :
257- configPlugin , err = newVirtualPluginFunc (s .hostHelper )
258- if err != nil {
259- return nil , fmt .Errorf ("failed to create virtual plugin %v" , err )
260- }
261- case PhasePost :
262- s .log .Info ("skip post configuration phase for virtual cluster" )
263- return nil , nil
264- }
265- }
266- return configPlugin , nil
247+ return s .platformInterface .SystemdGetPlugin (phase )
267248}
268249
269250func (s * ServiceConfig ) getNetworkNodeState (phase string ) (* sriovv1.SriovNetworkNodeState , error ) {
@@ -272,33 +253,18 @@ func (s *ServiceConfig) getNetworkNodeState(phase string) (*sriovv1.SriovNetwork
272253 bridges sriovv1.Bridges
273254 err error
274255 )
275- switch s .sriovConfig .PlatformType {
276- case consts .Baremetal :
277- ifaceStatuses , err = s .hostHelper .DiscoverSriovDevices (s .hostHelper )
278- if err != nil {
279- return nil , fmt .Errorf ("failed to discover sriov devices on the host: %v" , err )
280- }
281- if phase != PhasePre && vars .ManageSoftwareBridges {
282- // openvswitch is not available during the pre phase
283- bridges , err = s .hostHelper .DiscoverBridges ()
284- if err != nil {
285- return nil , fmt .Errorf ("failed to discover managed bridges on the host: %v" , err )
286- }
287- }
288- case consts .VirtualOpenStack :
289- platformHelper , err := newPlatformHelperFunc ()
290- if err != nil {
291- return nil , fmt .Errorf ("failed to create platformHelpers" )
292- }
293- err = platformHelper .CreateOpenstackDevicesInfo ()
294- if err != nil {
295- return nil , fmt .Errorf ("failed to read OpenStack data: %v" , err )
296- }
297- ifaceStatuses , err = platformHelper .DiscoverSriovDevicesVirtual ()
256+ ifaceStatuses , err = s .platformInterface .DiscoverSriovDevices ()
257+ if err != nil {
258+ return nil , fmt .Errorf ("failed to discover sriov devices on the host: %v" , err )
259+ }
260+ if phase != consts .PhasePre && vars .ManageSoftwareBridges {
261+ // openvswitch is not available during the pre-phase
262+ bridges , err = s .platformInterface .DiscoverBridges ()
298263 if err != nil {
299- return nil , fmt .Errorf ("failed to discover devices: %v" , err )
264+ return nil , fmt .Errorf ("failed to discover managed bridges on the host: %v" , err )
300265 }
301266 }
267+
302268 return & sriovv1.SriovNetworkNodeState {
303269 Spec : s .sriovConfig .Spec ,
304270 Status : sriovv1.SriovNetworkNodeStateStatus {Interfaces : ifaceStatuses , Bridges : bridges },
@@ -317,7 +283,7 @@ func (s *ServiceConfig) updateSriovResultErr(phase string, origErr error) error
317283func (s * ServiceConfig ) updateSriovResultOk (phase string ) error {
318284 s .log .V (0 ).Info ("service call succeed" )
319285 syncStatus := consts .SyncStatusSucceeded
320- if phase == PhasePre {
286+ if phase == consts . PhasePre {
321287 syncStatus = consts .SyncStatusInProgress
322288 }
323289 return s .updateResult (syncStatus , "" )
0 commit comments