Skip to content

Commit

Permalink
add the new server command for the config daemon
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Sch <sebassch@gmail.com>
  • Loading branch information
SchSeba committed Sep 29, 2022
1 parent 1468ffa commit 67439ab
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 6 deletions.
116 changes: 116 additions & 0 deletions cmd/sriov-network-config-daemon/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"errors"
"flag"
"github.com/golang/glog"
sriovv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/generic"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/systemd"
"github.com/spf13/cobra"
"os"

"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/version"
)

var (
serviceCmd = &cobra.Command{
Use: "service",
Short: "Starts SR-IOV service Config",
Long: "",
Run: runServiceCmd,
}

serviceOpts struct {
kubeconfig string
nodeName string
}
)

func init() {
rootCmd.AddCommand(serviceCmd)
serviceCmd.PersistentFlags().StringVar(&startOpts.kubeconfig, "kubeconfig", "", "Kubeconfig file to access a remote cluster (testing only)")
serviceCmd.PersistentFlags().StringVar(&startOpts.nodeName, "node-name", "", "kubernetes node name daemon is managing.")
}

func runServiceCmd(cmd *cobra.Command, args []string) {
flag.Set("logtostderr", "true")
flag.Set("stderrthreshold", "INFO")
flag.Parse()

// To help debugging, immediately log version
glog.V(2).Infof("Version: %+v", version.Version)

glog.V(0).Info("Starting sriov-config-service")
nodeStateSpec, err := systemd.ReadConfFile()
if err != nil {
if _, err := os.Stat(utils.SriovSwitchDevConfPath); errors.Is(err, os.ErrNotExist) {
glog.Infof("sriov configuration file on path %s doesn't exist nothing todo", utils.SriovSwitchDevConfPath)
glog.V(0).Info("Shutting down sriov-config-service")
return
} else {
glog.Fatalf("failed to check if sriov configuration file exist in path %s: %v", utils.SriovSwitchDevConfPath, err)
}
glog.Fatalf("failed to read the sriov configuration file in path %s: %v", utils.SriovSwitchDevConfPath, err)
}

glog.V(2).Infof("sriov-config-service read config: %v", nodeStateSpec)

// Load kernel modules
hostManager := host.NewHostManager(true)
_, err = hostManager.TryEnableRdma()
if err != nil {
glog.Warningf("failed to enable RDMA: %v", err)
}
hostManager.TryEnableTun()
hostManager.TryEnableVhostNet()

// TODO: implement OpenStack support
// TODO: check the unsupport method (maybe save the config file)
ifaceStatuses, err := utils.DiscoverSriovDevices(true)
if err != nil {
glog.V(0).Infof("sriov-config-service failed %v", err)
return
}

// Create the generic plugin
genericPlugin, err := generic.NewGenericPlugin(true)
if err != nil {
glog.Errorf("sriov-config-service failed to create generic plugin %v", err)
return
}

nodeState := &sriovv1.SriovNetworkNodeState{
Spec: nodeStateSpec.Spec,
Status: sriovv1.SriovNetworkNodeStateStatus{Interfaces: ifaceStatuses},
}

_, _, err = genericPlugin.OnNodeStateChange(nodeState)
if err != nil {
glog.Errorf("sriov-config-service failed to run OnNodeStateChange to update the generic plugin status %v", err)
return
}

sriovResult := &systemd.SriovResult{
Generation: nodeStateSpec.Generation,
SyncStatus: "Succeeded",
LastSyncError: "",
}

err = genericPlugin.Apply()
if err != nil {
glog.Errorf("sriov-config-service failed to run apply node configuration %v", err)
sriovResult.SyncStatus = "Failed"
sriovResult.LastSyncError = err.Error()
}

err = systemd.WriteSriovResult(sriovResult)
if err != nil {
glog.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err)
return
}

glog.V(0).Info("Shutting down sriov-config-service")
}
12 changes: 6 additions & 6 deletions cmd/sriov-network-config-daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ var (
}

startOpts struct {
kubeconfig string
nodeName string
useSystemdService bool
kubeconfig string
nodeName string
systemd bool
}
)

func init() {
rootCmd.AddCommand(startCmd)
startCmd.PersistentFlags().StringVar(&startOpts.kubeconfig, "kubeconfig", "", "Kubeconfig file to access a remote cluster (testing only)")
startCmd.PersistentFlags().StringVar(&startOpts.nodeName, "node-name", "", "kubernetes node name daemon is managing")
startCmd.PersistentFlags().BoolVar(&startOpts.useSystemdService, "use-systemd-service", false, "Configure SR-IOV using systemd service (false by default)")
startCmd.PersistentFlags().BoolVar(&startOpts.systemd, "systemd", false, "use config daemon in systemd mode")
}

func runStartCmd(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -90,7 +90,7 @@ func runStartCmd(cmd *cobra.Command, args []string) {
var config *rest.Config
var err error

if os.Getenv("CLUSTER_TYPE") == utils.ClusterTypeOpenshift {
if utils.ClusterType == utils.ClusterTypeOpenshift {
kubeconfig, err := clientcmd.LoadFromFile("/host/etc/kubernetes/kubeconfig")
if err != nil {
glog.Errorf("failed to load kubelet kubeconfig: %v", err)
Expand Down Expand Up @@ -210,7 +210,7 @@ func runStartCmd(cmd *cobra.Command, args []string) {
syncCh,
refreshCh,
platformType,
startOpts.useSystemdService,
startOpts.systemd,
).Run(stopCh, exitCh)
if err != nil {
glog.Errorf("failed to run daemon: %v", err)
Expand Down

0 comments on commit 67439ab

Please sign in to comment.