Skip to content

Commit cf27354

Browse files
authored
Merge pull request #312 from minmzzhang/sriov-validation
chore: sriov validation
2 parents b460c9c + a221de9 commit cf27354

File tree

10 files changed

+115
-344
lines changed

10 files changed

+115
-344
lines changed

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager
2727
FROM gcr.io/distroless/static:nonroot
2828
WORKDIR /
2929
COPY --from=builder /workspace/manager .
30-
COPY --from=builder /workspace/internal/plugin/template/ template/
3130
USER 65532:65532
3231

33-
ENTRYPOINT ["/manager"]
32+
ENTRYPOINT ["/manager"]

config/samples/multinicnetwork/sriov.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ spec:
1616
cniVersion: "0.3.0"
1717
type: sriov
1818
args:
19+
# Modify the fields below according to your SR-IOV environment
1920
numVfs: "2"
21+
vlan: "999"
2022
isRdma: "true"

controllers/suite_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ var _ = BeforeSuite(func() {
259259
},
260260
}
261261

262-
plugin.SRIOV_MANIFEST_PATH = "../internal/plugin/template/cni-config"
263262
Expect(K8sClient.Create(context.TODO(), &sriovNamespace)).Should(Succeed())
264263
err = SriovPlugin.Init(cfg)
265264
Expect(err).ToNot(HaveOccurred())

go.mod

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ module github.com/foundation-model-stack/multi-nic-cni
33
go 1.24.1
44

55
require (
6-
github.com/Masterminds/sprig v2.22.0+incompatible
76
github.com/containernetworking/cni v1.0.1
87
github.com/go-logr/logr v1.4.2
98
github.com/onsi/ginkgo/v2 v2.21.0
109
github.com/onsi/gomega v1.36.1
1110
github.com/operator-framework/operator-lib v0.11.0
12-
github.com/pkg/errors v0.9.1
1311
go.uber.org/zap v1.27.0
1412
k8s.io/api v0.32.0
1513
k8s.io/apimachinery v0.32.0
@@ -20,8 +18,6 @@ require (
2018

2119
require (
2220
cel.dev/expr v0.18.0 // indirect
23-
github.com/Masterminds/goutils v1.1.1 // indirect
24-
github.com/Masterminds/semver v1.5.0 // indirect
2521
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
2622
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
2723
github.com/beorn7/perks v1.0.1 // indirect
@@ -50,17 +46,14 @@ require (
5046
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db // indirect
5147
github.com/google/uuid v1.6.0 // indirect
5248
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
53-
github.com/huandu/xstrings v1.5.0 // indirect
54-
github.com/imdario/mergo v0.3.12 // indirect
5549
github.com/inconshreveable/mousetrap v1.1.0 // indirect
5650
github.com/josharian/intern v1.0.0 // indirect
5751
github.com/json-iterator/go v1.1.12 // indirect
5852
github.com/mailru/easyjson v0.7.7 // indirect
59-
github.com/mitchellh/copystructure v1.2.0 // indirect
60-
github.com/mitchellh/reflectwalk v1.0.2 // indirect
6153
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
6254
github.com/modern-go/reflect2 v1.0.2 // indirect
6355
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
56+
github.com/pkg/errors v0.9.1 // indirect
6457
github.com/prometheus/client_golang v1.19.1 // indirect
6558
github.com/prometheus/client_model v0.6.1 // indirect
6659
github.com/prometheus/common v0.55.0 // indirect
@@ -78,7 +71,6 @@ require (
7871
go.opentelemetry.io/otel/trace v1.28.0 // indirect
7972
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
8073
go.uber.org/multierr v1.11.0 // indirect
81-
golang.org/x/crypto v0.36.0 // indirect
8274
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
8375
golang.org/x/net v0.38.0 // indirect
8476
golang.org/x/oauth2 v0.23.0 // indirect

internal/plugin/plugin_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ var _ = Describe("Test GetConfig of main plugins", func() {
151151
hifList := generateHostInterfaceList(nodes)
152152

153153
BeforeAll(func() {
154-
SRIOV_MANIFEST_PATH = "./template/cni-config"
155154
err := sriovPlugin.Init(Cfg)
156155
Expect(err).ToNot(HaveOccurred())
157156
})

internal/plugin/sriov.go

Lines changed: 58 additions & 12 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,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

3949
type SriovPlugin struct {
4050
SriovNetworkHandler *DynamicHandler
@@ -64,30 +74,65 @@ func (p *SriovPlugin) Init(config *rest.Config) error {
6474

6575
func (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

93138
func (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

Comments
 (0)