Skip to content

Commit 838fc5e

Browse files
committed
Create a platform package
This creates an interface for platform adding baremetal and openstack with a preparation for additional ones Signed-off-by: Sebastian Sch <[email protected]>
1 parent 9b1902e commit 838fc5e

File tree

31 files changed

+1394
-818
lines changed

31 files changed

+1394
-818
lines changed

cmd/sriov-network-config-daemon/service.go

Lines changed: 50 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4240
const (
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,52 @@ var (
5954
}
6055
phaseArg string
6156

62-
newGenericPluginFunc = generic.NewGenericPlugin
63-
newVirtualPluginFunc = virtual.NewVirtualPlugin
64-
newHostHelpersFunc = helper.NewDefaultHostHelpers
65-
newPlatformHelperFunc = platforms.NewDefaultPlatformHelper
57+
newPlatformFunc = func(hostHelpers helper.HostHelpersInterface) (platform.Interface, error) {
58+
return platform.New(hostHelpers)
59+
}
60+
newHostHelpersFunc = helper.NewDefaultHostHelpers
6661
)
6762

6863
// ServiceConfig is a struct that encapsulates the configuration and dependencies
6964
// needed by the SriovNetworkConfigDaemon systemd service.
7065
type 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
66+
hostHelper helper.HostHelpersInterface // Provides host-specific helper functions
67+
platformInterface platform.Interface // Provides platform helpers function
68+
log logr.Logger // Handles logging for the service
69+
sriovConfig *hosttypes.SriovConfig // Contains the SR-IOV network configuration settings
7470
}
7571

7672
func init() {
7773
rootCmd.AddCommand(serviceCmd)
78-
serviceCmd.Flags().StringVarP(&phaseArg, "phase", "p", PhasePre, fmt.Sprintf("configuration phase, supported values are: %s, %s", PhasePre, PhasePost))
74+
serviceCmd.Flags().StringVarP(&phaseArg, "phase", "p", consts.PhasePre, fmt.Sprintf("configuration phase, supported values are: %s, %s", consts.PhasePre, consts.PhasePost))
7975
}
8076

8177
func newServiceConfig(setupLog logr.Logger) (*ServiceConfig, error) {
8278
hostHelpers, err := newHostHelpersFunc()
8379
if err != nil {
8480
return nil, fmt.Errorf("failed to create host helpers: %v", err)
8581
}
86-
87-
return &ServiceConfig{
82+
sc := &ServiceConfig{
8883
hostHelpers,
84+
nil,
8985
setupLog,
9086
nil,
91-
}, nil
87+
}
88+
89+
err = sc.readConf()
90+
if err != nil {
91+
return nil, sc.updateSriovResultErr(phaseArg, err)
92+
}
93+
94+
// init globals
95+
vars.PlatformType = sc.sriovConfig.PlatformType
96+
platformInterface, err := newPlatformFunc(hostHelpers)
97+
if err != nil {
98+
return nil, fmt.Errorf("failed to creeate serviceConfig: %w", err)
99+
}
100+
sc.platformInterface = platformInterface
101+
102+
return sc, nil
92103
}
93104

94105
// The service supports two configuration phases:
@@ -102,8 +113,8 @@ func newServiceConfig(setupLog logr.Logger) (*ServiceConfig, error) {
102113
// If the result of the "pre" phase is different than "InProgress", then the "post" phase will not be executed
103114
// and the execution result will be forcefully set to "Failed".
104115
func 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)
116+
if phaseArg != consts.PhasePre && phaseArg != consts.PhasePost {
117+
return fmt.Errorf("invalid value for \"--phase\" argument, valid values are: %s, %s", consts.PhasePre, consts.PhasePost)
107118
}
108119
// init logger
109120
snolog.InitLog()
@@ -115,18 +126,14 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
115126
// Mark that we are running on host
116127
vars.UsingSystemdMode = true
117128
vars.InChroot = true
129+
vars.Destdir = "/tmp"
118130

119131
sc, err := newServiceConfig(setupLog)
120132
if err != nil {
121133
setupLog.Error(err, "failed to create the service configuration controller, Exiting")
122134
return err
123135
}
124136

125-
err = sc.readConf()
126-
if err != nil {
127-
return sc.updateSriovResultErr(phaseArg, err)
128-
}
129-
130137
setupLog.V(2).Info("sriov-config-service", "config", sc.sriovConfig)
131138
vars.DevMode = sc.sriovConfig.UnsupportedNics
132139
vars.ManageSoftwareBridges = sc.sriovConfig.ManageSoftwareBridges
@@ -138,7 +145,12 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
138145

139146
sc.waitForDevicesInitialization()
140147

141-
if phaseArg == PhasePre {
148+
err = sc.platformInterface.Init()
149+
if err != nil {
150+
return sc.updateSriovResultErr(phaseArg, fmt.Errorf("failed to init platform configuration: %w", err))
151+
}
152+
153+
if phaseArg == consts.PhasePre {
142154
err = sc.phasePre()
143155
} else {
144156
err = sc.phasePost()
@@ -189,7 +201,7 @@ func (s *ServiceConfig) phasePre() error {
189201
s.hostHelper.TryEnableTun()
190202
s.hostHelper.TryEnableVhostNet()
191203

192-
return s.callPlugin(PhasePre)
204+
return s.callPlugin(consts.PhasePre)
193205
}
194206

195207
func (s *ServiceConfig) phasePost() error {
@@ -203,7 +215,7 @@ func (s *ServiceConfig) phasePost() error {
203215
}
204216
s.log.V(0).Info("Pre phase succeed, continue execution")
205217

206-
return s.callPlugin(PhasePost)
218+
return s.callPlugin(consts.PhasePost)
207219
}
208220

209221
func (s *ServiceConfig) callPlugin(phase string) error {
@@ -234,36 +246,7 @@ func (s *ServiceConfig) callPlugin(phase string) error {
234246
}
235247

236248
func (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
249+
return s.platformInterface.SystemdGetPlugin(phase)
267250
}
268251

269252
func (s *ServiceConfig) getNetworkNodeState(phase string) (*sriovv1.SriovNetworkNodeState, error) {
@@ -272,33 +255,18 @@ func (s *ServiceConfig) getNetworkNodeState(phase string) (*sriovv1.SriovNetwork
272255
bridges sriovv1.Bridges
273256
err error
274257
)
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()
258+
ifaceStatuses, err = s.platformInterface.DiscoverSriovDevices()
259+
if err != nil {
260+
return nil, fmt.Errorf("failed to discover sriov devices on the host: %v", err)
261+
}
262+
if phase != consts.PhasePre && vars.ManageSoftwareBridges {
263+
// openvswitch is not available during the pre-phase
264+
bridges, err = s.platformInterface.DiscoverBridges()
298265
if err != nil {
299-
return nil, fmt.Errorf("failed to discover devices: %v", err)
266+
return nil, fmt.Errorf("failed to discover managed bridges on the host: %v", err)
300267
}
301268
}
269+
302270
return &sriovv1.SriovNetworkNodeState{
303271
Spec: s.sriovConfig.Spec,
304272
Status: sriovv1.SriovNetworkNodeStateStatus{Interfaces: ifaceStatuses, Bridges: bridges},
@@ -317,7 +285,7 @@ func (s *ServiceConfig) updateSriovResultErr(phase string, origErr error) error
317285
func (s *ServiceConfig) updateSriovResultOk(phase string) error {
318286
s.log.V(0).Info("service call succeed")
319287
syncStatus := consts.SyncStatusSucceeded
320-
if phase == PhasePre {
288+
if phase == consts.PhasePre {
321289
syncStatus = consts.SyncStatusInProgress
322290
}
323291
return s.updateResult(syncStatus, "")

0 commit comments

Comments
 (0)