Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scan guids #838

Closed
Closed
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
103 changes: 102 additions & 1 deletion api/v1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ var log = logf.Log.WithName("sriovnetwork")
// NicIDMap contains supported mapping of IDs with each in the format of:
// Vendor ID, Physical Function Device ID, Virtual Function Device ID
var NicIDMap = []string{}

var InitialState SriovNetworkNodeState

// NetFilterType Represents the NetFilter tags to be used
Expand Down Expand Up @@ -220,6 +219,27 @@ func IsSwitchdevModeSpec(spec SriovNetworkNodeStateSpec) bool {
return ContainsSwitchdevInterface(spec.Interfaces)
}

func GetGUIDFromSriovNetworkNodeStateStatus(status SriovNetworkNodeStateStatus, interfaceName string) string {
// Loop through all interfaces
for _, iface := range status.Interfaces {
// Only process InfiniBand interfaces with matching name
if strings.EqualFold(iface.LinkType, consts.LinkTypeIB) && iface.Name == interfaceName {
// If interface has VFs and at least one VF has a GUID
if len(iface.VFs) > 0 {
for _, vf := range iface.VFs {
// Return first valid GUID found
// Skip uninitialized GUIDs
if vf.GUID != "" && vf.GUID != consts.UninitializedNodeGUID {
return vf.GUID
}
}
}
break // Found the interface but no valid GUID
}
}
return ""
}

// ContainsSwitchdevInterface returns true if provided interface list contains interface
// with switchdev configuration
func ContainsSwitchdevInterface(interfaces []Interface) bool {
Expand Down Expand Up @@ -687,6 +707,87 @@ func (s *SriovNetworkNodeState) GetDriverByPciAddress(addr string) string {
return ""
}

// RenderNetAttDefWithGUID renders a net-att-def with GUID for ib-sriov CNI
func (cr *SriovIBNetwork) RenderNetAttDefWithGUID(status SriovNetworkNodeStateStatus) (*uns.Unstructured, error) {
logger := log.WithName("RenderNetAttDef")
logger.Info("Start to render IB SRIOV CNI NetworkAttachmentDefinition")

// render RawCNIConfig manifests
data := render.MakeRenderData()
data.Data["CniType"] = "ib-sriov"
data.Data["pKey"] = cr.Spec.Pkey
data.Data["interfaceName"] = cr.Spec.InterfaceName
data.Data["SriovNetworkName"] = cr.Name
if cr.Spec.NetworkNamespace == "" {
data.Data["SriovNetworkNamespace"] = cr.Namespace
} else {
data.Data["SriovNetworkNamespace"] = cr.Spec.NetworkNamespace
}

data.Data["SriovCniResourceName"] = os.Getenv("RESOURCE_PREFIX") + "/" + cr.Spec.ResourceName
if cr.Spec.ScanGUIDs {
if guid := GetGUIDFromSriovNetworkNodeStateStatus(status, cr.Spec.InterfaceName); guid != "" {
data.Data["GUID"] = guid
}
}

data.Data["StateConfigured"] = true
switch cr.Spec.LinkState {
case SriovCniStateEnable:
data.Data["SriovCniState"] = SriovCniStateEnable
case SriovCniStateDisable:
data.Data["SriovCniState"] = SriovCniStateDisable
case SriovCniStateAuto:
data.Data["SriovCniState"] = SriovCniStateAuto
default:
data.Data["StateConfigured"] = false
}

if cr.Spec.Capabilities == "" {
data.Data["CapabilitiesConfigured"] = false
} else {
data.Data["CapabilitiesConfigured"] = true
data.Data["SriovCniCapabilities"] = cr.Spec.Capabilities
}

if cr.Spec.IPAM != "" {
data.Data["SriovCniIpam"] = SriovCniIpam + ":" + strings.Join(strings.Fields(cr.Spec.IPAM), "")
} else {
data.Data["SriovCniIpam"] = SriovCniIpamEmpty
}

// metaplugins for the infiniband cni
data.Data["MetaPluginsConfigured"] = false
if cr.Spec.MetaPluginsConfig != "" {
data.Data["MetaPluginsConfigured"] = true
data.Data["MetaPlugins"] = cr.Spec.MetaPluginsConfig
}

// logLevel and logFile are currently not supported by the ib-sriov-cni
data.Data["LogLevelConfigured"] = false
data.Data["LogFileConfigured"] = false

objs, err := render.RenderDir(filepath.Join(ManifestsPath, "sriov"), &data)
if err != nil {
return nil, err
}
for _, obj := range objs {
raw, _ := json.Marshal(obj)
logger.Info("render NetworkAttachmentDefinition output", "raw", string(raw))
}
return objs[0], nil
}

func (cr *SriovNetwork) RenderNetAttDefWithGUID(status SriovNetworkNodeStateStatus) (*uns.Unstructured, error) {
// Not implemented
return cr.RenderNetAttDef()
}

func (cr *OVSNetwork) RenderNetAttDefWithGUID(status SriovNetworkNodeStateStatus) (*uns.Unstructured, error) {
// Not implemented
return cr.RenderNetAttDef()
}

// RenderNetAttDef renders a net-att-def for ib-sriov CNI
func (cr *SriovIBNetwork) RenderNetAttDef() (*uns.Unstructured, error) {
logger := log.WithName("RenderNetAttDef")
Expand Down
3 changes: 3 additions & 0 deletions api/v1/sriovibnetwork_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type SriovIBNetworkSpec struct {
// MetaPluginsConfig configuration to be used in order to chain metaplugins to the sriov interface returned
// by the operator.
MetaPluginsConfig string `json:"metaPlugins,omitempty"`
InterfaceName string `json:"interfaceName,omitempty"`
Pkey string `json:"pkey,omitempty"`
ScanGUIDs bool `json:"scanGuids,omitempty"`
}

// SriovIBNetworkStatus defines the observed state of SriovIBNetwork
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ spec:
resourceName:
description: SRIOV Network device plugin endpoint resource name
type: string
pkey:
description: pKey for the Infiniband network.
type: string
scanGUIDs:
description: Whether to scan for GUIDs.
type: boolean
InterfaceName:
description: Name of the IB interface
type: string
required:
- resourceName
type: object
Expand Down
21 changes: 20 additions & 1 deletion controllers/generic_network_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"reflect"
"time"

netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -44,6 +45,8 @@ type networkCRInstance interface {
client.Object
// renders NetAttDef from the network instance
RenderNetAttDef() (*uns.Unstructured, error)
// RenderNetAttDefWithGUID renders NetAttDef with GUID (if available)
RenderNetAttDefWithGUID(status sriovnetworkv1.SriovNetworkNodeStateStatus) (*uns.Unstructured, error)
// return name of the target namespace for the network
NetworkNamespace() string
}
Expand Down Expand Up @@ -124,7 +127,23 @@ func (r *genericNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
return reconcile.Result{}, err
}
raw, err := instance.RenderNetAttDef()

// Get list of all SriovNetworkNodeStates
nodeStateList := &sriovnetworkv1.SriovNetworkNodeStateList{}
if err := r.List(ctx, nodeStateList, &client.ListOptions{
Namespace: vars.Namespace,
}); err != nil {
return ctrl.Result{}, err
}

// If no node states exist yet, requeue
if len(nodeStateList.Items) == 0 {
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
}

nodeState := nodeStateList.Items[0]
raw, err := instance.RenderNetAttDefWithGUID(nodeState.Status)

if err != nil {
return reconcile.Result{}, err
}
Expand Down