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,7 +35,16 @@ var SRIOV_NODE_SELECTOR map[string]string = map[string]string{
3435 "feature.node.kubernetes.io/network-sriov.capable" : "true" ,
3536}
3637
37- var SRIOV_MANIFEST_PATH string = "/template/cni-config"
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+ }
3848
3949type SriovPlugin struct {
4050 SriovNetworkHandler * DynamicHandler
@@ -64,30 +74,65 @@ func (p *SriovPlugin) Init(config *rest.Config) error {
6474
6575func (p * SriovPlugin ) GetConfig (net multinicv1.MultiNicNetwork , hifList map [string ]multinicv1.HostInterface ) (string , map [string ]string , error ) {
6676 annotation := make (map [string ]string )
67- name := net .GetName ()
68- namespace := net .GetNamespace ()
77+ name := net .ObjectMeta . Name
78+ namespace := net .ObjectMeta . Namespace
6979 resourceName := ValidateResourceName (name ) // default name
7080 spec := net .Spec .MainPlugin
7181 args := spec .CNIArgs
7282 rootDevices := p .getRootDevices (net , hifList )
7383 // get resource, create new SriovNetworkNodePolicies if resource is not pre-defined
7484 // TO-DO: check configmap to verify pre-defined resourceName is valid
7585 resourceName = p .getResource (name , args , resourceName , rootDevices )
76- var raw * unstructured.Unstructured
77- var err error
78- // create sriov network
86+
87+ // Create sriov network resource for tests and resource management
7988 // TO-DO: support SriovIBNetwork
80- sriovnet , err := p .createSriovNetwork (name , namespace , args , resourceName )
89+ _ , err := p .createSriovNetwork (name , namespace , args , resourceName , & spec )
8190 if err != nil {
8291 return "" , annotation , err
8392 }
84- raw , err = sriovnet .RenderNetAttDef ()
93+
94+ // Create SR-IOV configuration directly instead of using template
95+ conf := & SriovNetConf {}
96+ conf .CNIVersion = spec .CNIVersion
97+ conf .Type = SRIOV_TYPE
98+
99+ // Parse VLAN
100+ val , err := getInt (args , "vlan" )
101+ if err == nil {
102+ conf .Vlan = val
103+ }
104+
105+ // Parse VlanQoS
106+ val , err = getInt (args , "vlanQoS" )
107+ if err == nil {
108+ conf .VlanQoS = val
109+ }
110+
111+ // Parse optional boolean fields
112+ if spoofchk , err := getBoolean (args , "spoofchk" ); err == nil {
113+ conf .SpoofChk = & spoofchk
114+ }
115+
116+ if trust , err := getBoolean (args , "trust" ); err == nil {
117+ conf .Trust = & trust
118+ }
119+
120+ // Parse optional rate limiting fields
121+ if minTxRate , err := getInt (args , "minTxRate" ); err == nil {
122+ conf .MinTxRate = & minTxRate
123+ }
124+
125+ if maxTxRate , err := getInt (args , "maxTxRate" ); err == nil {
126+ conf .MaxTxRate = & maxTxRate
127+ }
128+
129+ confBytes , err := json .Marshal (conf )
85130 if err != nil {
86131 return "" , annotation , err
87132 }
88- config := raw . Object [ "spec" ].( map [ string ] interface {})[ "config" ].( string )
133+
89134 annotation [RESOURCE_ANNOTATION ] = SRIOV_RESOURCE_PREFIX + "/" + resourceName
90- return config , annotation , nil
135+ return string ( confBytes ) , annotation , nil
91136}
92137
93138func (p * SriovPlugin ) getRootDevices (net multinicv1.MultiNicNetwork , hifList map [string ]multinicv1.HostInterface ) []string {
@@ -188,7 +233,7 @@ func (p *SriovPlugin) getResource(name string, args map[string]string, resourceN
188233 return spec .ResourceName
189234}
190235
191- func (p * SriovPlugin ) createSriovNetwork (name string , namespace string , args map [string ]string , resourceName string ) (* SriovNetwork , error ) {
236+ func (p * SriovPlugin ) createSriovNetwork (name string , namespace string , args map [string ]string , resourceName string , pluginSpec * multinicv1. PluginSpec ) (* SriovNetwork , error ) {
192237 spec := & SriovNetworkSpec {}
193238 spec .NetworkNamespace = namespace
194239 spec .ResourceName = resourceName
@@ -208,6 +253,7 @@ func (p *SriovPlugin) createSriovNetwork(name string, namespace string, args map
208253 if err == nil {
209254 spec .MaxTxRate = & val
210255 }
256+
211257 netName := p .SriovnetworkName (name )
212258 metaObj := GetMetaObject (netName , SRIOV_NAMESPACE , make (map [string ]string ))
213259 sriovnet := NewSrioNetwork (metaObj , * spec )
0 commit comments