Skip to content

Commit

Permalink
Create the new systemd cmd and implementation changes in the daemon pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Sch <[email protected]>
  • Loading branch information
SchSeba committed Jun 20, 2023
1 parent cfb0e3d commit 4e0b661
Show file tree
Hide file tree
Showing 14 changed files with 797 additions and 122 deletions.
15 changes: 15 additions & 0 deletions cmd/sriov-network-config-daemon/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import (
Expand Down
182 changes: 182 additions & 0 deletions cmd/sriov-network-config-daemon/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import (
"errors"
"flag"
"fmt"
"os"

"github.com/golang/glog"
"github.com/spf13/cobra"

sriovv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host"
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/systemd"
"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: "",
RunE: runServiceCmd,
}
)

func init() {
rootCmd.AddCommand(serviceCmd)
}

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

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

glog.V(0).Info("Starting sriov-config-service")
supportedNicIds, err := systemd.ReadSriovSupportedNics()
if err != nil {
glog.Errorf("failed to read list of supported nic ids")
sriovResult := &systemd.SriovResult{
SyncStatus: "Failed",
LastSyncError: fmt.Sprintf("failed to read list of supported nic ids: %v", err),
}
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 fmt.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err)
}
return fmt.Errorf("sriov-config-service failed to read list of supported nic ids: %v", err)
}
sriovv1.InitNicIDMapFromList(supportedNicIds)

nodeStateSpec, err := systemd.ReadConfFile()
if err != nil {
if _, err := os.Stat(systemd.SriovSystemdConfigPath); !errors.Is(err, os.ErrNotExist) {
glog.Errorf("failed to read the sriov configuration file in path %s: %v", systemd.SriovSystemdConfigPath, err)
sriovResult := &systemd.SriovResult{
SyncStatus: "Failed",
LastSyncError: fmt.Sprintf("failed to read the sriov configuration file in path %s: %v", systemd.SriovSystemdConfigPath, err),
}
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 fmt.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err)
}
}

nodeStateSpec = &systemd.SriovConfig{
Spec: sriovv1.SriovNetworkNodeStateSpec{},
UnsupportedNics: false,
PlatformType: utils.Baremetal,
}
}

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()

var configPlugin plugin.VendorPlugin
var ifaceStatuses []sriovv1.InterfaceExt
if nodeStateSpec.PlatformType == utils.Baremetal {
// Bare metal support
ifaceStatuses, err = utils.DiscoverSriovDevices(nodeStateSpec.UnsupportedNics)
if err != nil {
glog.Errorf("sriov-config-service: failed to discover sriov devices on the host: %v", err)
return fmt.Errorf("sriov-config-service: failed to discover sriov devices on the host: %v", err)
}

// Create the generic plugin
configPlugin, err = generic.NewGenericPlugin(true)
if err != nil {
glog.Errorf("sriov-config-service: failed to create generic plugin %v", err)
return fmt.Errorf("sriov-config-service failed to create generic plugin %v", err)
}
} else if nodeStateSpec.PlatformType == utils.VirtualOpenStack {
// Openstack support
metaData, networkData, err := utils.GetOpenstackData(false)
if err != nil {
glog.Errorf("sriov-config-service: failed to read OpenStack data: %v", err)
return fmt.Errorf("sriov-config-service failed to read OpenStack data: %v", err)
}

openStackDevicesInfo, err := utils.CreateOpenstackDevicesInfo(metaData, networkData)
if err != nil {
glog.Errorf("failed to read OpenStack data: %v", err)
return fmt.Errorf("sriov-config-service failed to read OpenStack data: %v", err)
}

ifaceStatuses, err = utils.DiscoverSriovDevicesVirtual(openStackDevicesInfo)
if err != nil {
glog.Errorf("sriov-config-service:failed to read OpenStack data: %v", err)
return fmt.Errorf("sriov-config-service: failed to read OpenStack data: %v", err)
}

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

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

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

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

err = configPlugin.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 fmt.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err)
}

glog.V(0).Info("Shutting down sriov-config-service")
return nil
}
23 changes: 21 additions & 2 deletions cmd/sriov-network-config-daemon/start.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import (
Expand Down Expand Up @@ -39,13 +54,15 @@ var (
startOpts struct {
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().StringVar(&startOpts.nodeName, "node-name", "", "kubernetes node name daemon is managing")
startCmd.PersistentFlags().BoolVar(&startOpts.systemd, "use-systemd-service", false, "use config daemon in systemd mode")
}

func runStartCmd(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -165,7 +182,7 @@ func runStartCmd(cmd *cobra.Command, args []string) {
glog.V(0).Infof("Running on platform: %s", platformType.String())

var namespace = os.Getenv("NAMESPACE")
if err := sriovnetworkv1.InitNicIDMap(kubeclient, namespace); err != nil {
if err := sriovnetworkv1.InitNicIDMapFromConfigMap(kubeclient, namespace); err != nil {
glog.Errorf("failed to run init NicIdMap: %v", err)
panic(err.Error())
}
Expand All @@ -189,6 +206,8 @@ func runStartCmd(cmd *cobra.Command, args []string) {
syncCh,
refreshCh,
platformType,
startOpts.systemd,
devMode,
).Run(stopCh, exitCh)
if err != nil {
glog.Errorf("failed to run daemon: %v", err)
Expand Down
36 changes: 19 additions & 17 deletions pkg/consts/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ package consts
import "time"

const (
ResyncPeriod = 5 * time.Minute
DefaultConfigName = "default"
ConfigDaemonPath = "./bindata/manifests/daemon"
InjectorWebHookPath = "./bindata/manifests/webhook"
OperatorWebHookPath = "./bindata/manifests/operator-webhook"
ServiceCAConfigMapAnnotation = "service.beta.openshift.io/inject-cabundle"
InjectorWebHookName = "network-resources-injector-config"
OperatorWebHookName = "sriov-operator-webhook-config"
DeprecatedOperatorWebHookName = "operator-webhook-config"
PluginPath = "./bindata/manifests/plugins"
DaemonPath = "./bindata/manifests/daemon"
DefaultPolicyName = "default"
ConfigMapName = "device-plugin-config"
DaemonSet = "DaemonSet"
ServiceAccount = "ServiceAccount"
DPConfigFileName = "config.json"
OVSHWOLMachineConfigNameSuffix = "ovs-hw-offload"
ResyncPeriod = 5 * time.Minute
DefaultConfigName = "default"
ConfigDaemonPath = "./bindata/manifests/daemon"
InjectorWebHookPath = "./bindata/manifests/webhook"
OperatorWebHookPath = "./bindata/manifests/operator-webhook"
SystemdServiceOcpPath = "./bindata/manifests/sriov-config-service/openshift"
SystemdServiceOcpMachineConfigName = "sriov-config-service"
ServiceCAConfigMapAnnotation = "service.beta.openshift.io/inject-cabundle"
InjectorWebHookName = "network-resources-injector-config"
OperatorWebHookName = "sriov-operator-webhook-config"
DeprecatedOperatorWebHookName = "operator-webhook-config"
PluginPath = "./bindata/manifests/plugins"
DaemonPath = "./bindata/manifests/daemon"
DefaultPolicyName = "default"
ConfigMapName = "device-plugin-config"
DaemonSet = "DaemonSet"
ServiceAccount = "ServiceAccount"
DPConfigFileName = "config.json"
OVSHWOLMachineConfigNameSuffix = "ovs-hw-offload"

LinkTypeEthernet = "ether"
LinkTypeInfiniband = "infiniband"
Expand Down
Loading

0 comments on commit 4e0b661

Please sign in to comment.