Skip to content

Commit 412c1cd

Browse files
committed
chore: sriov validation
Signed-off-by: Min Zhang <minzhang@redhat.com>
1 parent 81ba74c commit 412c1cd

File tree

7 files changed

+382
-40
lines changed

7 files changed

+382
-40
lines changed

config/samples/multinicnetwork/sriov.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ spec:
1717
type: sriov
1818
args:
1919
numVfs: "2"
20+
vlan: "1134"
2021
isRdma: "true"

internal/plugin/sriov.go

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
package plugin
77

88
import (
9+
"encoding/json"
910
"fmt"
1011

12+
"github.com/containernetworking/cni/pkg/types"
1113
multinicv1 "github.com/foundation-model-stack/multi-nic-cni/api/v1"
1214
"github.com/foundation-model-stack/multi-nic-cni/internal/vars"
1315
k8serrors "k8s.io/apimachinery/pkg/api/errors"
14-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1516
"k8s.io/client-go/dynamic"
1617
"k8s.io/client-go/rest"
1718

@@ -34,6 +35,17 @@ var SRIOV_NODE_SELECTOR map[string]string = map[string]string{
3435
"feature.node.kubernetes.io/network-sriov.capable": "true",
3536
}
3637

38+
// SriovNetConf represents SR-IOV CNI configuration for multi-nic wrapper mode
39+
type SriovNetConf struct {
40+
types.NetConf
41+
Vlan int `json:"vlan,omitempty"`
42+
VlanQoS int `json:"vlanQoS,omitempty"`
43+
SpoofChk *bool `json:"spoofchk,omitempty"`
44+
Trust *bool `json:"trust,omitempty"`
45+
MinTxRate *int `json:"min_tx_rate,omitempty"`
46+
MaxTxRate *int `json:"max_tx_rate,omitempty"`
47+
}
48+
3749
var SRIOV_MANIFEST_PATH string = "/template/cni-config"
3850

3951
type SriovPlugin struct {
@@ -64,30 +76,65 @@ func (p *SriovPlugin) Init(config *rest.Config) error {
6476

6577
func (p *SriovPlugin) GetConfig(net multinicv1.MultiNicNetwork, hifList map[string]multinicv1.HostInterface) (string, map[string]string, error) {
6678
annotation := make(map[string]string)
67-
name := net.GetName()
68-
namespace := net.GetNamespace()
79+
name := net.ObjectMeta.Name
80+
namespace := net.ObjectMeta.Namespace
6981
resourceName := ValidateResourceName(name) // default name
7082
spec := net.Spec.MainPlugin
7183
args := spec.CNIArgs
7284
rootDevices := p.getRootDevices(net, hifList)
7385
// get resource, create new SriovNetworkNodePolicies if resource is not pre-defined
7486
// TO-DO: check configmap to verify pre-defined resourceName is valid
7587
resourceName = p.getResource(name, args, resourceName, rootDevices)
76-
var raw *unstructured.Unstructured
77-
var err error
78-
// create sriov network
88+
89+
// Create sriov network resource for tests and resource management
7990
// TO-DO: support SriovIBNetwork
80-
sriovnet, err := p.createSriovNetwork(name, namespace, args, resourceName)
91+
_, err := p.createSriovNetwork(name, namespace, args, resourceName, &spec)
8192
if err != nil {
8293
return "", annotation, err
8394
}
84-
raw, err = sriovnet.RenderNetAttDef()
95+
96+
// Create SR-IOV configuration directly instead of using template
97+
conf := &SriovNetConf{}
98+
conf.CNIVersion = spec.CNIVersion
99+
conf.Type = SRIOV_TYPE
100+
101+
// Parse VLAN
102+
val, err := getInt(args, "vlan")
103+
if err == nil {
104+
conf.Vlan = val
105+
}
106+
107+
// Parse VlanQoS
108+
val, err = getInt(args, "vlanQoS")
109+
if err == nil {
110+
conf.VlanQoS = val
111+
}
112+
113+
// Parse optional boolean fields
114+
if spoofchk, err := getBoolean(args, "spoofchk"); err == nil {
115+
conf.SpoofChk = &spoofchk
116+
}
117+
118+
if trust, err := getBoolean(args, "trust"); err == nil {
119+
conf.Trust = &trust
120+
}
121+
122+
// Parse optional rate limiting fields
123+
if minTxRate, err := getInt(args, "minTxRate"); err == nil {
124+
conf.MinTxRate = &minTxRate
125+
}
126+
127+
if maxTxRate, err := getInt(args, "maxTxRate"); err == nil {
128+
conf.MaxTxRate = &maxTxRate
129+
}
130+
131+
confBytes, err := json.Marshal(conf)
85132
if err != nil {
86133
return "", annotation, err
87134
}
88-
config := raw.Object["spec"].(map[string]interface{})["config"].(string)
135+
89136
annotation[RESOURCE_ANNOTATION] = SRIOV_RESOURCE_PREFIX + "/" + resourceName
90-
return config, annotation, nil
137+
return string(confBytes), annotation, nil
91138
}
92139

93140
func (p *SriovPlugin) getRootDevices(net multinicv1.MultiNicNetwork, hifList map[string]multinicv1.HostInterface) []string {
@@ -188,7 +235,7 @@ func (p *SriovPlugin) getResource(name string, args map[string]string, resourceN
188235
return spec.ResourceName
189236
}
190237

191-
func (p *SriovPlugin) createSriovNetwork(name string, namespace string, args map[string]string, resourceName string) (*SriovNetwork, error) {
238+
func (p *SriovPlugin) createSriovNetwork(name string, namespace string, args map[string]string, resourceName string, pluginSpec *multinicv1.PluginSpec) (*SriovNetwork, error) {
192239
spec := &SriovNetworkSpec{}
193240
spec.NetworkNamespace = namespace
194241
spec.ResourceName = resourceName
@@ -208,6 +255,7 @@ func (p *SriovPlugin) createSriovNetwork(name string, namespace string, args map
208255
if err == nil {
209256
spec.MaxTxRate = &val
210257
}
258+
211259
netName := p.SriovnetworkName(name)
212260
metaObj := GetMetaObject(netName, SRIOV_NAMESPACE, make(map[string]string))
213261
sriovnet := NewSrioNetwork(metaObj, *spec)

internal/plugin/sriov_resource.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,15 @@ type SriovNetworkNicSelector struct {
169169
}
170170

171171
// RenderNetAttDef renders a net-att-def for sriov CNI
172-
func (cr *SriovNetwork) RenderNetAttDef() (*unstructured.Unstructured, error) {
172+
func (cr *SriovNetwork) RenderNetAttDef(pluginCniVersion string) (*unstructured.Unstructured, error) {
173173
// render RawCNIConfig manifests
174174
data := MakeRenderData()
175-
data.Data["MetaPluginsConfigured"] = false
175+
if cr.Spec.MetaPluginsConfig != "" {
176+
data.Data["MetaPluginsConfigured"] = true
177+
data.Data["MetaPlugins"] = cr.Spec.MetaPluginsConfig
178+
} else {
179+
data.Data["MetaPluginsConfigured"] = false
180+
}
176181
data.Data["CniType"] = "sriov"
177182
data.Data["SriovNetworkName"] = cr.Name
178183
if cr.Spec.NetworkNamespace == "" {
@@ -182,6 +187,12 @@ func (cr *SriovNetwork) RenderNetAttDef() (*unstructured.Unstructured, error) {
182187
}
183188
data.Data["SriovCniResourceName"] = os.Getenv("RESOURCE_PREFIX") + "/" + cr.Spec.ResourceName
184189
data.Data["SriovCniVlan"] = cr.Spec.Vlan
190+
// Set CNI version from parameter
191+
if pluginCniVersion != "" {
192+
data.Data["SriovCniVersion"] = pluginCniVersion
193+
} else {
194+
data.Data["SriovCniVersion"] = "0.3.1"
195+
}
185196

186197
if cr.Spec.VlanQoS <= 7 && cr.Spec.VlanQoS >= 0 {
187198
data.Data["VlanQoSConfigured"] = true
@@ -246,9 +257,9 @@ func (cr *SriovNetwork) RenderNetAttDef() (*unstructured.Unstructured, error) {
246257
}
247258

248259
if cr.Spec.IPAM != "" {
249-
data.Data["SriovCniIpam"] = "\"ipam\":" + strings.Join(strings.Fields(cr.Spec.IPAM), "")
260+
data.Data["SriovCniIpam"] = "\"ipam\": " + strings.Join(strings.Fields(cr.Spec.IPAM), "")
250261
} else {
251-
data.Data["SriovCniIpam"] = "\"ipam\":{}"
262+
data.Data["SriovCniIpam"] = "\"ipam\": {}"
252263
}
253264

254265
objs, err := RenderDir(SRIOV_MANIFEST_PATH, &data)

0 commit comments

Comments
 (0)