@@ -61,6 +61,7 @@ func TestPoolDeletionWithExistingIPAddresses(t *testing.T) {
61
61
Kind : namespacedPool .GetObjectKind ().GroupVersionKind ().Kind ,
62
62
Name : namespacedPool .GetName (),
63
63
},
64
+ Address : "10.0.0.10" ,
64
65
},
65
66
},
66
67
& ipamv1.IPAddress {
@@ -77,6 +78,7 @@ func TestPoolDeletionWithExistingIPAddresses(t *testing.T) {
77
78
Kind : globalPool .GetObjectKind ().GroupVersionKind ().Kind ,
78
79
Name : globalPool .GetName (),
79
80
},
81
+ Address : "10.0.0.10" ,
80
82
},
81
83
},
82
84
}
@@ -98,6 +100,91 @@ func TestPoolDeletionWithExistingIPAddresses(t *testing.T) {
98
100
99
101
g .Expect (webhook .ValidateDelete (ctx , namespacedPool )).To (Succeed (), "should allow deletion when no claims exist" )
100
102
g .Expect (webhook .ValidateDelete (ctx , globalPool )).To (Succeed (), "should allow deletion when no claims exist" )
103
+
104
+ }
105
+
106
+ func TestUpdatingPoolInUseAddresses (t * testing.T ) {
107
+ g := NewWithT (t )
108
+
109
+ scheme := runtime .NewScheme ()
110
+ g .Expect (ipamv1 .AddToScheme (scheme )).To (Succeed ())
111
+
112
+ namespacedPool := & v1alpha1.InClusterIPPool {
113
+ ObjectMeta : metav1.ObjectMeta {
114
+ Name : "my-pool" ,
115
+ },
116
+ Spec : v1alpha1.InClusterIPPoolSpec {
117
+ Addresses : []string {"10.0.0.10-10.0.0.20" },
118
+ Prefix : 24 ,
119
+ Gateway : "10.0.0.1" ,
120
+ },
121
+ }
122
+
123
+ globalPool := & v1alpha1.InClusterIPPool {
124
+ ObjectMeta : metav1.ObjectMeta {
125
+ Name : "my-pool" ,
126
+ },
127
+ Spec : v1alpha1.InClusterIPPoolSpec {
128
+ Addresses : []string {"10.0.0.10-10.0.0.20" },
129
+ Prefix : 24 ,
130
+ Gateway : "10.0.0.1" ,
131
+ },
132
+ }
133
+
134
+ ips := []client.Object {
135
+ & ipamv1.IPAddress {
136
+ TypeMeta : metav1.TypeMeta {
137
+ Kind : "IPAddress" ,
138
+ APIVersion : "ipam.cluster.x-k8s.io/v1alpha1" ,
139
+ },
140
+ ObjectMeta : metav1.ObjectMeta {
141
+ Name : "my-ip" ,
142
+ },
143
+ Spec : ipamv1.IPAddressSpec {
144
+ PoolRef : corev1.TypedLocalObjectReference {
145
+ APIGroup : pointer .String (namespacedPool .GetObjectKind ().GroupVersionKind ().Group ),
146
+ Kind : namespacedPool .GetObjectKind ().GroupVersionKind ().Kind ,
147
+ Name : namespacedPool .GetName (),
148
+ },
149
+ Address : "10.0.0.10" ,
150
+ },
151
+ },
152
+ & ipamv1.IPAddress {
153
+ TypeMeta : metav1.TypeMeta {
154
+ Kind : "IPAddress" ,
155
+ APIVersion : "ipam.cluster.x-k8s.io/v1alpha1" ,
156
+ },
157
+ ObjectMeta : metav1.ObjectMeta {
158
+ Name : "my-ip-2" ,
159
+ },
160
+ Spec : ipamv1.IPAddressSpec {
161
+ PoolRef : corev1.TypedLocalObjectReference {
162
+ APIGroup : pointer .String (globalPool .GetObjectKind ().GroupVersionKind ().Group ),
163
+ Kind : globalPool .GetObjectKind ().GroupVersionKind ().Kind ,
164
+ Name : globalPool .GetName (),
165
+ },
166
+ Address : "10.0.0.10" ,
167
+ },
168
+ },
169
+ }
170
+
171
+ fakeClient := fake .NewClientBuilder ().
172
+ WithScheme (scheme ).
173
+ WithObjects (ips ... ).
174
+ WithIndex (& ipamv1.IPAddress {}, index .IPAddressPoolRefCombinedField , index .IPAddressByCombinedPoolRef ).
175
+ Build ()
176
+
177
+ webhook := InClusterIPPool {
178
+ Client : fakeClient ,
179
+ }
180
+
181
+ oldNamespacedPool := namespacedPool .DeepCopyObject ()
182
+ oldGlobalPool := globalPool .DeepCopyObject ()
183
+ namespacedPool .Spec .Addresses = []string {"10.0.0.15-10.0.0.20" }
184
+ globalPool .Spec .Addresses = []string {"10.0.0.15-10.0.0.20" }
185
+
186
+ g .Expect (webhook .ValidateUpdate (ctx , oldNamespacedPool , namespacedPool )).NotTo (Succeed (), "should not allow removing in use IPs from addresses field in pool" )
187
+ g .Expect (webhook .ValidateUpdate (ctx , oldGlobalPool , globalPool )).NotTo (Succeed (), "should not allow removing in use IPs from addresses field in pool" )
101
188
}
102
189
103
190
func TestInClusterIPPoolDefaulting (t * testing.T ) {
0 commit comments