@@ -2108,6 +2108,157 @@ var _ = Describe("Pod controller integration test suite", func() {
2108
2108
})
2109
2109
})
2110
2110
2111
+ Context ("Specify IP pool for stateful pod and check reserved ip instances" , func () {
2112
+ var podName string
2113
+ var ownerReference = statefulOwnerReferenceRender ()
2114
+ var idx = 0
2115
+ var ipPool = []string {
2116
+ "100.10.0.151" ,
2117
+ "100.10.0.161" ,
2118
+ }
2119
+
2120
+ BeforeEach (func () {
2121
+ podName = fmt .Sprintf ("pod-sts-%d" , idx )
2122
+ })
2123
+
2124
+ It ("Change assigned IP for stateful pod" , func () {
2125
+ By ("create a stateful pod with special annotations" )
2126
+ pod := simplePodRender (podName , node1Name )
2127
+ pod .OwnerReferences = []metav1.OwnerReference {ownerReference }
2128
+ pod .Annotations = map [string ]string {
2129
+ constants .AnnotationSpecifiedNetwork : overlayNetworkName ,
2130
+ constants .AnnotationIPPool : strings .Join (ipPool , "," ),
2131
+ }
2132
+ Expect (k8sClient .Create (context .Background (), pod )).Should (Succeed ())
2133
+
2134
+ By ("check the allocated ip instance" )
2135
+ Eventually (
2136
+ func (g Gomega ) {
2137
+ ipInstances , err := utils .ListAllocatedIPInstancesOfPod (context .Background (), k8sClient , pod )
2138
+ g .Expect (err ).NotTo (HaveOccurred ())
2139
+ g .Expect (ipInstances ).To (HaveLen (1 ))
2140
+
2141
+ ipInstance := ipInstances [0 ]
2142
+ g .Expect (ipInstance .Spec .Binding .PodUID ).To (Equal (pod .UID ))
2143
+ g .Expect (ipInstance .Spec .Binding .PodName ).To (Equal (pod .Name ))
2144
+ g .Expect (ipInstance .Spec .Binding .NodeName ).To (Equal (node1Name ))
2145
+ g .Expect (ipInstance .Spec .Binding .ReferredObject ).To (Equal (networkingv1.ObjectMeta {
2146
+ Kind : ownerReference .Kind ,
2147
+ Name : ownerReference .Name ,
2148
+ UID : ownerReference .UID ,
2149
+ }))
2150
+
2151
+ g .Expect (ipInstance .Spec .Binding .Stateful ).NotTo (BeNil ())
2152
+ g .Expect (ipInstance .Spec .Binding .Stateful .Index ).NotTo (BeNil ())
2153
+
2154
+ idx := * ipInstance .Spec .Binding .Stateful .Index
2155
+ g .Expect (pod .Name ).To (Equal (fmt .Sprintf ("pod-sts-%d" , idx )))
2156
+
2157
+ g .Expect (ipInstance .Spec .Network ).To (Equal (overlayNetworkName ))
2158
+ g .Expect (ipInstance .Spec .Subnet ).To (Equal (overlayIPv4SubnetName ))
2159
+
2160
+ g .Expect (ipInstance .Spec .Address .Version ).To (Equal (networkingv1 .IPv4 ))
2161
+ g .Expect (ipInstance .Spec .Address .IP ).To (Equal (ipPool [idx ] + "/24" ))
2162
+ }).
2163
+ WithTimeout (30 * time .Second ).
2164
+ WithPolling (time .Second ).
2165
+ Should (Succeed ())
2166
+
2167
+ By ("remove stateful pod" )
2168
+ Expect (k8sClient .Delete (context .Background (), pod , client .GracePeriodSeconds (0 ))).NotTo (HaveOccurred ())
2169
+
2170
+ By ("make sure the pod is cleaned" )
2171
+ Eventually (
2172
+ func (g Gomega ) {
2173
+ err := k8sClient .Get (context .Background (),
2174
+ types.NamespacedName {
2175
+ Namespace : "default" ,
2176
+ Name : podName ,
2177
+ },
2178
+ & corev1.Pod {})
2179
+ g .Expect (err ).NotTo (BeNil ())
2180
+ g .Expect (errors .IsNotFound (err )).To (BeTrue ())
2181
+ }).
2182
+ WithTimeout (30 * time .Second ).
2183
+ WithPolling (time .Second ).
2184
+ Should (Succeed ())
2185
+
2186
+ By ("change specified IP and recreate the pod" )
2187
+ ipPool [idx ] = "100.10.0.152"
2188
+ pod = simplePodRender (podName , node1Name )
2189
+ pod .OwnerReferences = []metav1.OwnerReference {ownerReference }
2190
+ pod .Annotations = map [string ]string {
2191
+ constants .AnnotationSpecifiedNetwork : overlayNetworkName ,
2192
+ constants .AnnotationIPPool : strings .Join (ipPool , "," ),
2193
+ }
2194
+ Expect (k8sClient .Create (context .Background (), pod )).Should (Succeed ())
2195
+
2196
+ By ("check the allocated ip instance again" )
2197
+ Eventually (
2198
+ func (g Gomega ) {
2199
+ ipInstances , err := utils .ListAllocatedIPInstancesOfPod (context .Background (), k8sClient , pod )
2200
+ g .Expect (err ).NotTo (HaveOccurred ())
2201
+ g .Expect (ipInstances ).To (HaveLen (1 ))
2202
+
2203
+ ipInstance := ipInstances [0 ]
2204
+ g .Expect (ipInstance .Spec .Binding .PodUID ).To (Equal (pod .UID ))
2205
+ g .Expect (ipInstance .Spec .Binding .PodName ).To (Equal (pod .Name ))
2206
+ g .Expect (ipInstance .Spec .Binding .NodeName ).To (Equal (node1Name ))
2207
+ g .Expect (ipInstance .Spec .Binding .ReferredObject ).To (Equal (networkingv1.ObjectMeta {
2208
+ Kind : ownerReference .Kind ,
2209
+ Name : ownerReference .Name ,
2210
+ UID : ownerReference .UID ,
2211
+ }))
2212
+
2213
+ g .Expect (ipInstance .Spec .Binding .Stateful ).NotTo (BeNil ())
2214
+ g .Expect (ipInstance .Spec .Binding .Stateful .Index ).NotTo (BeNil ())
2215
+
2216
+ idx := * ipInstance .Spec .Binding .Stateful .Index
2217
+ g .Expect (pod .Name ).To (Equal (fmt .Sprintf ("pod-sts-%d" , idx )))
2218
+
2219
+ g .Expect (ipInstance .Spec .Network ).To (Equal (overlayNetworkName ))
2220
+ g .Expect (ipInstance .Spec .Subnet ).To (Equal (overlayIPv4SubnetName ))
2221
+
2222
+ g .Expect (ipInstance .Spec .Address .Version ).To (Equal (networkingv1 .IPv4 ))
2223
+ g .Expect (ipInstance .Spec .Address .IP ).To (Equal (ipPool [idx ] + "/24" ))
2224
+ }).
2225
+ WithTimeout (30 * time .Second ).
2226
+ WithPolling (time .Second ).
2227
+ Should (Succeed ())
2228
+
2229
+ By ("clean up stateful pod" )
2230
+ Expect (k8sClient .Delete (context .Background (), pod , client .GracePeriodSeconds (0 ))).NotTo (HaveOccurred ())
2231
+ })
2232
+
2233
+ AfterEach (func () {
2234
+ By ("make sure test ip instances cleaned up" )
2235
+ Expect (k8sClient .DeleteAllOf (
2236
+ context .Background (),
2237
+ & networkingv1.IPInstance {},
2238
+ client.MatchingLabels {
2239
+ constants .LabelPod : transform .TransferPodNameForLabelValue (podName ),
2240
+ },
2241
+ client .InNamespace ("default" ),
2242
+ )).NotTo (HaveOccurred ())
2243
+
2244
+ By ("make sure test pod cleaned up" )
2245
+ Eventually (
2246
+ func (g Gomega ) {
2247
+ err := k8sClient .Get (context .Background (),
2248
+ types.NamespacedName {
2249
+ Namespace : "default" ,
2250
+ Name : podName ,
2251
+ },
2252
+ & corev1.Pod {})
2253
+ g .Expect (err ).NotTo (BeNil ())
2254
+ g .Expect (errors .IsNotFound (err )).To (BeTrue ())
2255
+ }).
2256
+ WithTimeout (30 * time .Second ).
2257
+ WithPolling (time .Second ).
2258
+ Should (Succeed ())
2259
+ })
2260
+ })
2261
+
2111
2262
Context ("Specify MAC address pool for pod" , func () {
2112
2263
var podName string
2113
2264
var ownerReference metav1.OwnerReference
0 commit comments