Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ image: ; $(info Building images...)

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

# Build manager binary
manager: generate _build-manager
Expand Down Expand Up @@ -216,7 +216,7 @@ test-e2e-validation-only: ginkgo
SUITE=./test/validation ./hack/run-e2e-conformance.sh

test-e2e: generate manifests skopeo envtest
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
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

test-e2e-k8s: export NAMESPACE=sriov-network-operator
test-e2e-k8s: test-e2e
Expand All @@ -225,7 +225,7 @@ test-bindata-scripts: fakechroot
fakechroot ./test/scripts/kargs_test.sh

test-%: generate manifests envtest
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
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

GOCOVMERGE = $(BIN_DIR)/gocovmerge
gocovmerge: ## Download gocovmerge locally if necessary.
Expand Down
146 changes: 58 additions & 88 deletions cmd/sriov-network-config-daemon/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,14 @@ import (
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/helper"
hosttypes "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platform"
plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/generic"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/virtual"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/version"
)

const (
PhasePre = "pre"
PhasePost = "post"

// InitializationDeviceDiscoveryTimeoutSec constant defines the number of
// seconds to wait for devices to be registered in the system with the expected name.
InitializationDeviceDiscoveryTimeoutSec = 60
Expand All @@ -59,23 +54,22 @@ var (
}
phaseArg string

newGenericPluginFunc = generic.NewGenericPlugin
newVirtualPluginFunc = virtual.NewVirtualPlugin
newHostHelpersFunc = helper.NewDefaultHostHelpers
newPlatformHelperFunc = platforms.NewDefaultPlatformHelper
newPlatformFunc = platform.New
newHostHelpersFunc = helper.NewDefaultHostHelpers
)

// ServiceConfig is a struct that encapsulates the configuration and dependencies
// needed by the SriovNetworkConfigDaemon systemd service.
type ServiceConfig struct {
hostHelper helper.HostHelpersInterface // Provides host-specific helper functions
log logr.Logger // Handles logging for the service
sriovConfig *hosttypes.SriovConfig // Contains the SR-IOV network configuration settings
hostHelper helper.HostHelpersInterface // Provides host-specific helper functions
platformInterface platform.Interface // Provides platform helpers function
log logr.Logger // Handles logging for the service
sriovConfig *hosttypes.SriovConfig // Contains the SR-IOV network configuration settings
}

func init() {
rootCmd.AddCommand(serviceCmd)
serviceCmd.Flags().StringVarP(&phaseArg, "phase", "p", PhasePre, fmt.Sprintf("configuration phase, supported values are: %s, %s", PhasePre, PhasePost))
serviceCmd.Flags().StringVarP(&phaseArg, "phase", "p", consts.PhasePre, fmt.Sprintf("configuration phase, supported values are: %s, %s", consts.PhasePre, consts.PhasePost))
}

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

return &ServiceConfig{
hostHelpers,
setupLog,
nil,
}, nil
// Read config first to get platform type

sriovConfig, err := readConf(hostHelpers, setupLog)
if err != nil {
return nil, fmt.Errorf("failed to read configuration: %w", err)
}

// init globals
vars.PlatformType = sriovConfig.PlatformType
platformInterface, err := newPlatformFunc(vars.PlatformType, hostHelpers)
if err != nil {
return nil, fmt.Errorf("failed to create serviceConfig: %w", err)
}

// Create final ServiceConfig with all fields initialized
sc := &ServiceConfig{
hostHelper: hostHelpers,
platformInterface: platformInterface,
log: setupLog,
sriovConfig: sriovConfig,
}

return sc, nil
}

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

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

err = sc.readConf()
if err != nil {
return sc.updateSriovResultErr(phaseArg, err)
}

setupLog.V(2).Info("sriov-config-service", "config", sc.sriovConfig)
vars.DevMode = sc.sriovConfig.UnsupportedNics
vars.ManageSoftwareBridges = sc.sriovConfig.ManageSoftwareBridges
Expand All @@ -138,7 +146,12 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {

sc.waitForDevicesInitialization()

if phaseArg == PhasePre {
err = sc.platformInterface.Init()
if err != nil {
return sc.updateSriovResultErr(phaseArg, fmt.Errorf("failed to init platform configuration: %w", err))
}

if phaseArg == consts.PhasePre {
err = sc.phasePre()
} else {
err = sc.phasePost()
Expand All @@ -149,21 +162,20 @@ func runServiceCmd(cmd *cobra.Command, args []string) error {
return sc.updateSriovResultOk(phaseArg)
}

func (s *ServiceConfig) readConf() error {
nodeStateSpec, err := s.hostHelper.ReadConfFile()
func readConf(hostHelper helper.HostHelpersInterface, log logr.Logger) (*hosttypes.SriovConfig, error) {
nodeStateSpec, err := hostHelper.ReadConfFile()
if err != nil {
if _, err := os.Stat(utils.GetHostExtensionPath(consts.SriovSystemdConfigPath)); !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("failed to read the sriov configuration file in path %s: %v", utils.GetHostExtensionPath(consts.SriovSystemdConfigPath), err)
return nil, fmt.Errorf("failed to read the sriov configuration file in path %s: %v", utils.GetHostExtensionPath(consts.SriovSystemdConfigPath), err)
}
s.log.Info("configuration file not found, use default config")
log.Info("configuration file not found, use default config")
nodeStateSpec = &hosttypes.SriovConfig{
Spec: sriovv1.SriovNetworkNodeStateSpec{},
UnsupportedNics: false,
PlatformType: consts.Baremetal,
}
}
s.sriovConfig = nodeStateSpec
return nil
return nodeStateSpec, nil
}

func (s *ServiceConfig) initSupportedNics() error {
Expand All @@ -189,7 +201,7 @@ func (s *ServiceConfig) phasePre() error {
s.hostHelper.TryEnableTun()
s.hostHelper.TryEnableVhostNet()

return s.callPlugin(PhasePre)
return s.callPlugin(consts.PhasePre)
}

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

return s.callPlugin(PhasePost)
return s.callPlugin(consts.PhasePost)
}

func (s *ServiceConfig) callPlugin(phase string) error {
Expand Down Expand Up @@ -234,36 +246,7 @@ func (s *ServiceConfig) callPlugin(phase string) error {
}

func (s *ServiceConfig) getPlugin(phase string) (plugin.VendorPlugin, error) {
var (
configPlugin plugin.VendorPlugin
err error
)
switch s.sriovConfig.PlatformType {
case consts.Baremetal:
switch phase {
case PhasePre:
configPlugin, err = newGenericPluginFunc(s.hostHelper,
generic.WithSkipVFConfiguration(),
generic.WithSkipBridgeConfiguration())
case PhasePost:
configPlugin, err = newGenericPluginFunc(s.hostHelper)
}
if err != nil {
return nil, fmt.Errorf("failed to create generic plugin for %v", err)
}
case consts.VirtualOpenStack:
switch phase {
case PhasePre:
configPlugin, err = newVirtualPluginFunc(s.hostHelper)
if err != nil {
return nil, fmt.Errorf("failed to create virtual plugin %v", err)
}
case PhasePost:
s.log.Info("skip post configuration phase for virtual cluster")
return nil, nil
}
}
return configPlugin, nil
return s.platformInterface.SystemdGetVendorPlugin(phase)
}

func (s *ServiceConfig) getNetworkNodeState(phase string) (*sriovv1.SriovNetworkNodeState, error) {
Expand All @@ -272,33 +255,20 @@ func (s *ServiceConfig) getNetworkNodeState(phase string) (*sriovv1.SriovNetwork
bridges sriovv1.Bridges
err error
)
switch s.sriovConfig.PlatformType {
case consts.Baremetal:
ifaceStatuses, err = s.hostHelper.DiscoverSriovDevices(s.hostHelper)
if err != nil {
return nil, fmt.Errorf("failed to discover sriov devices on the host: %v", err)
}
if phase != PhasePre && vars.ManageSoftwareBridges {
// openvswitch is not available during the pre phase
bridges, err = s.hostHelper.DiscoverBridges()
ifaceStatuses, err = s.platformInterface.DiscoverSriovDevices()
if err != nil {
return nil, fmt.Errorf("failed to discover sriov devices on the host: %v", err)
}
if phase != consts.PhasePre && vars.ManageSoftwareBridges {
// openvswitch is not available during the pre-phase
if vars.ManageSoftwareBridges {
bridges, err = s.platformInterface.DiscoverBridges()
if err != nil {
return nil, fmt.Errorf("failed to discover managed bridges on the host: %v", err)
}
}
case consts.VirtualOpenStack:
platformHelper, err := newPlatformHelperFunc()
if err != nil {
return nil, fmt.Errorf("failed to create platformHelpers")
}
err = platformHelper.CreateOpenstackDevicesInfo()
if err != nil {
return nil, fmt.Errorf("failed to read OpenStack data: %v", err)
}
ifaceStatuses, err = platformHelper.DiscoverSriovDevicesVirtual()
if err != nil {
return nil, fmt.Errorf("failed to discover devices: %v", err)
}
}

return &sriovv1.SriovNetworkNodeState{
Spec: s.sriovConfig.Spec,
Status: sriovv1.SriovNetworkNodeStateStatus{Interfaces: ifaceStatuses, Bridges: bridges},
Expand All @@ -317,7 +287,7 @@ func (s *ServiceConfig) updateSriovResultErr(phase string, origErr error) error
func (s *ServiceConfig) updateSriovResultOk(phase string) error {
s.log.V(0).Info("service call succeed")
syncStatus := consts.SyncStatusSucceeded
if phase == PhasePre {
if phase == consts.PhasePre {
syncStatus = consts.SyncStatusInProgress
}
return s.updateResult(syncStatus, "")
Expand Down
Loading
Loading