Skip to content

Commit 4c5f5af

Browse files
authored
Merge pull request #902 from SchSeba/create_platform_and_orchestrator_packages
Create platform and orchestrator packages
2 parents 3d1a472 + f9637c1 commit 4c5f5af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2607
-2036
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ image: ; $(info Building images...)
7575

7676
# Run tests
7777
test: generate lint manifests envtest
78-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir=/tmp -p path)" HOME="$(shell pwd)" go test -coverprofile cover.out -v ${TESTPKGS}
78+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use --bin-dir=/tmp -p path)" HOME="$(shell pwd)" go test -coverprofile cover.out -v ${TESTPKGS}
7979

8080
# Build manager binary
8181
manager: generate _build-manager
@@ -216,7 +216,7 @@ test-e2e-validation-only: ginkgo
216216
SUITE=./test/validation ./hack/run-e2e-conformance.sh
217217

218218
test-e2e: generate manifests skopeo envtest
219-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir=/tmp -p path)"; source hack/env.sh; HOME="$(shell pwd)" go test ./test/e2e/... -timeout 60m -coverprofile cover.out -v
219+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use --bin-dir=/tmp -p path)"; source hack/env.sh; HOME="$(shell pwd)" go test ./test/e2e/... -timeout 60m -coverprofile cover.out -v
220220

221221
test-e2e-k8s: export NAMESPACE=sriov-network-operator
222222
test-e2e-k8s: test-e2e
@@ -225,7 +225,7 @@ test-bindata-scripts: fakechroot
225225
fakechroot ./test/scripts/kargs_test.sh
226226

227227
test-%: generate manifests envtest
228-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir=/tmp -p path)" HOME="$(shell pwd)" go test `go list ./$*/... | grep -v "/mock" | grep -v "/pkg/client"` -coverprofile cover-$*-$(CLUSTER_TYPE).out -coverpkg ./... -v
228+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use --bin-dir=/tmp -p path)" HOME="$(shell pwd)" go test `go list ./$*/... | grep -v "/mock" | grep -v "/pkg/client"` -coverprofile cover-$*-$(CLUSTER_TYPE).out -coverpkg ./... -v
229229

230230
GOCOVMERGE = $(BIN_DIR)/gocovmerge
231231
gocovmerge: ## Download gocovmerge locally if necessary.

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

Lines changed: 58 additions & 88 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,23 +54,22 @@ 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.
7063
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
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

7670
func 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

8175
func newServiceConfig(setupLog logr.Logger) (*ServiceConfig, error) {
@@ -84,11 +78,29 @@ func newServiceConfig(setupLog logr.Logger) (*ServiceConfig, error) {
8478
return nil, fmt.Errorf("failed to create host helpers: %v", err)
8579
}
8680

87-
return &ServiceConfig{
88-
hostHelpers,
89-
setupLog,
90-
nil,
91-
}, nil
81+
// Read config first to get platform type
82+
83+
sriovConfig, err := readConf(hostHelpers, setupLog)
84+
if err != nil {
85+
return nil, fmt.Errorf("failed to read configuration: %w", err)
86+
}
87+
88+
// init globals
89+
vars.PlatformType = sriovConfig.PlatformType
90+
platformInterface, err := newPlatformFunc(vars.PlatformType, hostHelpers)
91+
if err != nil {
92+
return nil, fmt.Errorf("failed to create serviceConfig: %w", err)
93+
}
94+
95+
// Create final ServiceConfig with all fields initialized
96+
sc := &ServiceConfig{
97+
hostHelper: hostHelpers,
98+
platformInterface: platformInterface,
99+
log: setupLog,
100+
sriovConfig: sriovConfig,
101+
}
102+
103+
return sc, nil
92104
}
93105

94106
// The service supports two configuration phases:
@@ -102,8 +114,8 @@ func newServiceConfig(setupLog logr.Logger) (*ServiceConfig, error) {
102114
// If the result of the "pre" phase is different than "InProgress", then the "post" phase will not be executed
103115
// and the execution result will be forcefully set to "Failed".
104116
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)
117+
if phaseArg != consts.PhasePre && phaseArg != consts.PhasePost {
118+
return fmt.Errorf("invalid value for \"--phase\" argument, valid values are: %s, %s", consts.PhasePre, consts.PhasePost)
107119
}
108120
// init logger
109121
snolog.InitLog()
@@ -115,18 +127,14 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
115127
// Mark that we are running on host
116128
vars.UsingSystemdMode = true
117129
vars.InChroot = true
130+
vars.Destdir = "/tmp"
118131

119132
sc, err := newServiceConfig(setupLog)
120133
if err != nil {
121134
setupLog.Error(err, "failed to create the service configuration controller, Exiting")
122135
return err
123136
}
124137

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

139147
sc.waitForDevicesInitialization()
140148

141-
if phaseArg == PhasePre {
149+
err = sc.platformInterface.Init()
150+
if err != nil {
151+
return sc.updateSriovResultErr(phaseArg, fmt.Errorf("failed to init platform configuration: %w", err))
152+
}
153+
154+
if phaseArg == consts.PhasePre {
142155
err = sc.phasePre()
143156
} else {
144157
err = sc.phasePost()
@@ -149,21 +162,20 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
149162
return sc.updateSriovResultOk(phaseArg)
150163
}
151164

152-
func (s *ServiceConfig) readConf() error {
153-
nodeStateSpec, err := s.hostHelper.ReadConfFile()
165+
func readConf(hostHelper helper.HostHelpersInterface, log logr.Logger) (*hosttypes.SriovConfig, error) {
166+
nodeStateSpec, err := hostHelper.ReadConfFile()
154167
if err != nil {
155168
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)
169+
return nil, fmt.Errorf("failed to read the sriov configuration file in path %s: %v", utils.GetHostExtensionPath(consts.SriovSystemdConfigPath), err)
157170
}
158-
s.log.Info("configuration file not found, use default config")
171+
log.Info("configuration file not found, use default config")
159172
nodeStateSpec = &hosttypes.SriovConfig{
160173
Spec: sriovv1.SriovNetworkNodeStateSpec{},
161174
UnsupportedNics: false,
162175
PlatformType: consts.Baremetal,
163176
}
164177
}
165-
s.sriovConfig = nodeStateSpec
166-
return nil
178+
return nodeStateSpec, nil
167179
}
168180

169181
func (s *ServiceConfig) initSupportedNics() error {
@@ -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.SystemdGetVendorPlugin(phase)
267250
}
268251

269252
func (s *ServiceConfig) getNetworkNodeState(phase string) (*sriovv1.SriovNetworkNodeState, error) {
@@ -272,33 +255,20 @@ 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()
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+
if vars.ManageSoftwareBridges {
265+
bridges, err = s.platformInterface.DiscoverBridges()
284266
if err != nil {
285267
return nil, fmt.Errorf("failed to discover managed bridges on the host: %v", err)
286268
}
287269
}
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()
298-
if err != nil {
299-
return nil, fmt.Errorf("failed to discover devices: %v", err)
300-
}
301270
}
271+
302272
return &sriovv1.SriovNetworkNodeState{
303273
Spec: s.sriovConfig.Spec,
304274
Status: sriovv1.SriovNetworkNodeStateStatus{Interfaces: ifaceStatuses, Bridges: bridges},
@@ -317,7 +287,7 @@ func (s *ServiceConfig) updateSriovResultErr(phase string, origErr error) error
317287
func (s *ServiceConfig) updateSriovResultOk(phase string) error {
318288
s.log.V(0).Info("service call succeed")
319289
syncStatus := consts.SyncStatusSucceeded
320-
if phase == PhasePre {
290+
if phase == consts.PhasePre {
321291
syncStatus = consts.SyncStatusInProgress
322292
}
323293
return s.updateResult(syncStatus, "")

0 commit comments

Comments
 (0)