66package plugin
77
88import (
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+
3749var SRIOV_MANIFEST_PATH string = "/template/cni-config"
3850
3951type SriovPlugin struct {
@@ -64,30 +76,65 @@ func (p *SriovPlugin) Init(config *rest.Config) error {
6476
6577func (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
93140func (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 )
0 commit comments