@@ -8,8 +8,12 @@ package controllers
8
8
import (
9
9
"fmt"
10
10
11
+ multinicv1 "github.com/foundation-model-stack/multi-nic-cni/api/v1"
12
+ "github.com/foundation-model-stack/multi-nic-cni/controllers"
13
+ "github.com/foundation-model-stack/multi-nic-cni/plugin"
11
14
. "github.com/onsi/ginkgo"
12
15
. "github.com/onsi/gomega"
16
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
17
//+kubebuilder:scaffold:imports
14
18
)
15
19
@@ -33,3 +37,164 @@ var _ = Describe("Test deploying MultiNicNetwork", func() {
33
37
Expect (err ).NotTo (HaveOccurred ())
34
38
})
35
39
})
40
+
41
+ var _ = Describe ("Test definition changes check" , func () {
42
+ cniVersion := "0.3.0"
43
+ cniType := "ipvlan"
44
+ mode := "l2"
45
+ mtu := 1500
46
+ cniArgs := make (map [string ]string )
47
+ cniArgs ["mode" ] = mode
48
+ cniArgs ["mtu" ] = fmt .Sprintf ("%d" , mtu )
49
+
50
+ multinicnetwork := getMultiNicCNINetwork ("test-mn" , cniVersion , cniType , cniArgs )
51
+ It ("detect no change" , func () {
52
+ mainPlugin , annotations , err := multinicnetworkReconciler .GetMainPluginConf (multinicnetwork )
53
+ Expect (err ).NotTo (HaveOccurred ())
54
+ def , err := plugin .NetToDef ("" , multinicnetwork , mainPlugin , annotations )
55
+ Expect (err ).NotTo (HaveOccurred ())
56
+ defCopy , err := plugin .NetToDef ("" , multinicnetwork , mainPlugin , annotations )
57
+ Expect (err ).NotTo (HaveOccurred ())
58
+ changed := plugin .CheckDefChanged (defCopy , def )
59
+ Expect (changed ).To (BeFalse ())
60
+ })
61
+
62
+ It ("detect annotation change" , func () {
63
+ mainPlugin , annotations , err := multinicnetworkReconciler .GetMainPluginConf (multinicnetwork )
64
+ Expect (err ).NotTo (HaveOccurred ())
65
+ def , err := plugin .NetToDef ("" , multinicnetwork , mainPlugin , annotations )
66
+ Expect (err ).NotTo (HaveOccurred ())
67
+
68
+ newAnnotations := map [string ]string {"resource" : "new" }
69
+ defWithNewAnnotation , err := plugin .NetToDef ("" , multinicnetwork , mainPlugin , newAnnotations )
70
+ Expect (err ).NotTo (HaveOccurred ())
71
+ changed := plugin .CheckDefChanged (defWithNewAnnotation , def )
72
+ Expect (changed ).To (BeTrue ())
73
+ })
74
+
75
+ It ("detect config change" , func () {
76
+ mainPlugin , annotations , err := multinicnetworkReconciler .GetMainPluginConf (multinicnetwork )
77
+ Expect (err ).NotTo (HaveOccurred ())
78
+ def , err := plugin .NetToDef ("" , multinicnetwork , mainPlugin , annotations )
79
+ Expect (err ).NotTo (HaveOccurred ())
80
+
81
+ newCniArgs := make (map [string ]string )
82
+ newCniArgs ["mode" ] = "l3"
83
+ newCniArgs ["mtu" ] = fmt .Sprintf ("%d" , mtu )
84
+ changedArgsNetwork := getMultiNicCNINetwork ("test-mn" , cniVersion , cniType , newCniArgs )
85
+ newMainPlugin , annotations , err := multinicnetworkReconciler .GetMainPluginConf (changedArgsNetwork )
86
+ Expect (err ).NotTo (HaveOccurred ())
87
+ defWithNewArgs , err := plugin .NetToDef ("" , changedArgsNetwork , newMainPlugin , annotations )
88
+ Expect (err ).NotTo (HaveOccurred ())
89
+ changed := plugin .CheckDefChanged (defWithNewArgs , def )
90
+ Expect (changed ).To (BeTrue ())
91
+ })
92
+ })
93
+
94
+ func getNetStatus (computeResults []multinicv1.NicNetworkResult , discoverStatus multinicv1.DiscoverStatus , netConfigStatus multinicv1.NetConfigStatus , routeStatus multinicv1.RouteStatus ) multinicv1.MultiNicNetworkStatus {
95
+ return multinicv1.MultiNicNetworkStatus {
96
+ ComputeResults : computeResults ,
97
+ DiscoverStatus : discoverStatus ,
98
+ NetConfigStatus : netConfigStatus ,
99
+ RouteStatus : routeStatus ,
100
+ Message : "" ,
101
+ LastSyncTime : metav1 .Now (),
102
+ }
103
+ }
104
+
105
+ func testNewNetStatus (multinicnetwork * multinicv1.MultiNicNetwork , newStatus multinicv1.MultiNicNetworkStatus , expectedChange bool ) * multinicv1.MultiNicNetwork {
106
+ if expectedChange {
107
+ updated := controllers .NetStatusUpdated (multinicnetwork , newStatus )
108
+ // check new update
109
+ Expect (updated ).To (Equal (expectedChange ))
110
+ // update status
111
+ multinicnetwork .Status = newStatus
112
+ }
113
+ updated := controllers .NetStatusUpdated (multinicnetwork , newStatus )
114
+ // expect no update
115
+ Expect (updated ).To (BeFalse ())
116
+ return multinicnetwork
117
+ }
118
+
119
+ var _ = Describe ("Test multinicnetwork status change check" , func () {
120
+ cniVersion := "0.3.0"
121
+ cniType := "ipvlan"
122
+ mode := "l3"
123
+ mtu := 1500
124
+ cniArgs := make (map [string ]string )
125
+ cniArgs ["mode" ] = mode
126
+ cniArgs ["mtu" ] = fmt .Sprintf ("%d" , mtu )
127
+
128
+ It ("detect change from no status" , func () {
129
+ multinicnetwork := getMultiNicCNINetwork ("test-mn" , cniVersion , cniType , cniArgs )
130
+ initStatus := getNetStatus ([]multinicv1.NicNetworkResult {}, multinicv1.DiscoverStatus {}, multinicv1 .WaitForConfig , multinicv1 .ApplyingRoute )
131
+ updated := controllers .NetStatusUpdated (multinicnetwork , initStatus )
132
+ Expect (updated ).To (BeTrue ())
133
+ })
134
+
135
+ It ("detect change on compute results" , func () {
136
+ multinicnetwork := getMultiNicCNINetwork ("test-mn" , cniVersion , cniType , cniArgs )
137
+ multinicnetwork .Status = getNetStatus ([]multinicv1.NicNetworkResult {}, multinicv1.DiscoverStatus {}, multinicv1 .WaitForConfig , multinicv1 .ApplyingRoute )
138
+
139
+ net1 := multinicv1.NicNetworkResult {
140
+ NetAddress : "192.168.0.0/24" ,
141
+ NumOfHost : 1 ,
142
+ }
143
+ net2 := multinicv1.NicNetworkResult {
144
+ NetAddress : "192.168.1.0/24" ,
145
+ NumOfHost : 2 ,
146
+ }
147
+
148
+ computeResults := []multinicv1.NicNetworkResult {net1 }
149
+ newStatus := getNetStatus (computeResults , multinicv1.DiscoverStatus {}, multinicv1 .WaitForConfig , multinicv1 .ApplyingRoute )
150
+ expectedChange := true
151
+ multinicnetwork = testNewNetStatus (multinicnetwork , newStatus , expectedChange )
152
+
153
+ // add new compute result
154
+ computeResults = []multinicv1.NicNetworkResult {net1 , net2 }
155
+ newStatus = getNetStatus (computeResults , multinicv1.DiscoverStatus {}, multinicv1 .WaitForConfig , multinicv1 .ApplyingRoute )
156
+ expectedChange = true
157
+ multinicnetwork = testNewNetStatus (multinicnetwork , newStatus , expectedChange )
158
+
159
+ // change order
160
+ computeResults = []multinicv1.NicNetworkResult {net2 , net1 }
161
+ newStatus = getNetStatus (computeResults , multinicv1.DiscoverStatus {}, multinicv1 .WaitForConfig , multinicv1 .ApplyingRoute )
162
+ expectedChange = false
163
+ multinicnetwork = testNewNetStatus (multinicnetwork , newStatus , expectedChange )
164
+
165
+ // change values
166
+ net1 .NetAddress = "192.168.0.2/24"
167
+ computeResults = []multinicv1.NicNetworkResult {net1 , net2 }
168
+ newStatus = getNetStatus (computeResults , multinicv1.DiscoverStatus {}, multinicv1 .WaitForConfig , multinicv1 .ApplyingRoute )
169
+ expectedChange = true
170
+ testNewNetStatus (multinicnetwork , newStatus , expectedChange )
171
+ net1 .NumOfHost = 3
172
+ computeResults = []multinicv1.NicNetworkResult {net1 , net2 }
173
+ newStatus = getNetStatus (computeResults , multinicv1.DiscoverStatus {}, multinicv1 .WaitForConfig , multinicv1 .ApplyingRoute )
174
+ expectedChange = true
175
+ testNewNetStatus (multinicnetwork , newStatus , expectedChange )
176
+ })
177
+
178
+ It ("detect change on simple status" , func () {
179
+ multinicnetwork := getMultiNicCNINetwork ("test-mn" , cniVersion , cniType , cniArgs )
180
+ multinicnetwork .Status = getNetStatus ([]multinicv1.NicNetworkResult {}, multinicv1.DiscoverStatus {}, multinicv1 .WaitForConfig , multinicv1 .ApplyingRoute )
181
+
182
+ // change discover status
183
+ discoverStatus := multinicv1.DiscoverStatus {
184
+ ExistDaemon : 10 ,
185
+ }
186
+ newStatus := getNetStatus ([]multinicv1.NicNetworkResult {}, discoverStatus , multinicv1 .WaitForConfig , multinicv1 .ApplyingRoute )
187
+ expectedChange := true
188
+ testNewNetStatus (multinicnetwork , newStatus , expectedChange )
189
+
190
+ // change config status
191
+ newStatus = getNetStatus ([]multinicv1.NicNetworkResult {}, discoverStatus , multinicv1 .ConfigComplete , multinicv1 .ApplyingRoute )
192
+ expectedChange = true
193
+ testNewNetStatus (multinicnetwork , newStatus , expectedChange )
194
+
195
+ // change route status
196
+ newStatus = getNetStatus ([]multinicv1.NicNetworkResult {}, discoverStatus , multinicv1 .ConfigComplete , multinicv1 .AllRouteApplied )
197
+ expectedChange = true
198
+ testNewNetStatus (multinicnetwork , newStatus , expectedChange )
199
+ })
200
+ })
0 commit comments