@@ -11,6 +11,7 @@ import (
11
11
corev1 "k8s.io/api/core/v1"
12
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
13
"k8s.io/apimachinery/pkg/runtime"
14
+ kbclient "sigs.k8s.io/controller-runtime/pkg/client"
14
15
"sigs.k8s.io/controller-runtime/pkg/client/fake"
15
16
)
16
17
@@ -99,3 +100,257 @@ func TestGenerateAddNodeCommand(t *testing.T) {
99
100
wantCommand = "sudo ./my-app join --airgap-bundle my-app.airgap 192.168.0.100:30000 token"
100
101
req .Equal (wantCommand , gotCommand )
101
102
}
103
+
104
+ func TestGetAllNodeIPAddresses (t * testing.T ) {
105
+ scheme := runtime .NewScheme ()
106
+ corev1 .AddToScheme (scheme )
107
+ embeddedclusterv1beta1 .AddToScheme (scheme )
108
+
109
+ tests := []struct {
110
+ name string
111
+ roles []string
112
+ kbClient kbclient.Client
113
+ expectedEndpoints []string
114
+ }{
115
+ {
116
+ name : "no nodes" ,
117
+ roles : []string {"some-role" },
118
+ kbClient : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (
119
+ & embeddedclusterv1beta1.Installation {
120
+ ObjectMeta : metav1.ObjectMeta {
121
+ Name : time .Now ().Format ("20060102150405" ),
122
+ },
123
+ Spec : embeddedclusterv1beta1.InstallationSpec {
124
+ BinaryName : "my-app" ,
125
+ Config : & embeddedclusterv1beta1.ConfigSpec {
126
+ Version : "v1.100.0" ,
127
+ Roles : embeddedclusterv1beta1.Roles {
128
+ Controller : embeddedclusterv1beta1.NodeRole {
129
+ Name : "controller-role" ,
130
+ },
131
+ },
132
+ },
133
+ },
134
+ },
135
+ ).Build (),
136
+ expectedEndpoints : []string {},
137
+ },
138
+ {
139
+ name : "worker node joining cluster with 1 controller and 1 worker" ,
140
+ roles : []string {"some-role" },
141
+ kbClient : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (
142
+ & embeddedclusterv1beta1.Installation {
143
+ ObjectMeta : metav1.ObjectMeta {
144
+ Name : time .Now ().Format ("20060102150405" ),
145
+ },
146
+ Spec : embeddedclusterv1beta1.InstallationSpec {
147
+ BinaryName : "my-app" ,
148
+ Config : & embeddedclusterv1beta1.ConfigSpec {
149
+ Version : "v1.100.0" ,
150
+ Roles : embeddedclusterv1beta1.Roles {
151
+ Controller : embeddedclusterv1beta1.NodeRole {
152
+ Name : "controller-role" ,
153
+ },
154
+ },
155
+ },
156
+ },
157
+ },
158
+ & corev1.Node {
159
+ ObjectMeta : metav1.ObjectMeta {
160
+ Name : "controller" ,
161
+ Labels : map [string ]string {
162
+ "node-role.kubernetes.io/control-plane" : "true" ,
163
+ },
164
+ },
165
+ Status : corev1.NodeStatus {
166
+ Conditions : []corev1.NodeCondition {
167
+ {
168
+ Type : corev1 .NodeReady ,
169
+ Status : corev1 .ConditionTrue ,
170
+ },
171
+ },
172
+ Addresses : []corev1.NodeAddress {
173
+ {
174
+ Type : corev1 .NodeInternalIP ,
175
+ Address : "192.168.0.100" ,
176
+ },
177
+ },
178
+ },
179
+ },
180
+ & corev1.Node {
181
+ ObjectMeta : metav1.ObjectMeta {
182
+ Name : "worker" ,
183
+ Labels : map [string ]string {
184
+ "node-role.kubernetes.io/control-plane" : "false" ,
185
+ },
186
+ },
187
+ Status : corev1.NodeStatus {
188
+ Conditions : []corev1.NodeCondition {
189
+ {
190
+ Type : corev1 .NodeReady ,
191
+ Status : corev1 .ConditionTrue ,
192
+ },
193
+ },
194
+ Addresses : []corev1.NodeAddress {
195
+ {
196
+ Type : corev1 .NodeInternalIP ,
197
+ Address : "192.168.0.101" ,
198
+ },
199
+ },
200
+ },
201
+ },
202
+ ).Build (),
203
+ expectedEndpoints : []string {"192.168.0.100:6443" , "192.168.0.100:9443" },
204
+ },
205
+ {
206
+ name : "controller node joining cluster with 2 controller ready, 1 controller not ready, 1 worker ready, 1 worker not ready" ,
207
+ roles : []string {"controller-role" },
208
+ kbClient : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (
209
+ & embeddedclusterv1beta1.Installation {
210
+ ObjectMeta : metav1.ObjectMeta {
211
+ Name : time .Now ().Format ("20060102150405" ),
212
+ },
213
+ Spec : embeddedclusterv1beta1.InstallationSpec {
214
+ BinaryName : "my-app" ,
215
+ Config : & embeddedclusterv1beta1.ConfigSpec {
216
+ Version : "v1.100.0" ,
217
+ Roles : embeddedclusterv1beta1.Roles {
218
+ Controller : embeddedclusterv1beta1.NodeRole {
219
+ Name : "controller-role" ,
220
+ },
221
+ },
222
+ },
223
+ },
224
+ },
225
+ & corev1.Node {
226
+ ObjectMeta : metav1.ObjectMeta {
227
+ Name : "controller 1" ,
228
+ Labels : map [string ]string {
229
+ "node-role.kubernetes.io/control-plane" : "true" ,
230
+ },
231
+ },
232
+ Status : corev1.NodeStatus {
233
+ Conditions : []corev1.NodeCondition {
234
+ {
235
+ Type : corev1 .NodeReady ,
236
+ Status : corev1 .ConditionTrue ,
237
+ },
238
+ },
239
+ Addresses : []corev1.NodeAddress {
240
+ {
241
+ Type : corev1 .NodeInternalIP ,
242
+ Address : "192.168.0.100" ,
243
+ },
244
+ },
245
+ },
246
+ },
247
+ & corev1.Node {
248
+ ObjectMeta : metav1.ObjectMeta {
249
+ Name : "controller 2" ,
250
+ Labels : map [string ]string {
251
+ "node-role.kubernetes.io/control-plane" : "true" ,
252
+ },
253
+ },
254
+ Status : corev1.NodeStatus {
255
+ Conditions : []corev1.NodeCondition {
256
+ {
257
+ Type : corev1 .NodeReady ,
258
+ Status : corev1 .ConditionFalse ,
259
+ },
260
+ },
261
+ Addresses : []corev1.NodeAddress {
262
+ {
263
+ Type : corev1 .NodeInternalIP ,
264
+ Address : "192.168.0.101" ,
265
+ },
266
+ },
267
+ },
268
+ },
269
+ & corev1.Node {
270
+ ObjectMeta : metav1.ObjectMeta {
271
+ Name : "controller 3" ,
272
+ Labels : map [string ]string {
273
+ "node-role.kubernetes.io/control-plane" : "true" ,
274
+ },
275
+ },
276
+ Status : corev1.NodeStatus {
277
+ Conditions : []corev1.NodeCondition {
278
+ {
279
+ Type : corev1 .NodeReady ,
280
+ Status : corev1 .ConditionTrue ,
281
+ },
282
+ },
283
+ Addresses : []corev1.NodeAddress {
284
+ {
285
+ Type : corev1 .NodeInternalIP ,
286
+ Address : "192.168.0.102" ,
287
+ },
288
+ },
289
+ },
290
+ },
291
+ & corev1.Node {
292
+ ObjectMeta : metav1.ObjectMeta {
293
+ Name : "worker 1" ,
294
+ Labels : map [string ]string {},
295
+ },
296
+ Status : corev1.NodeStatus {
297
+ Conditions : []corev1.NodeCondition {
298
+ {
299
+ Type : corev1 .NodeReady ,
300
+ Status : corev1 .ConditionTrue ,
301
+ },
302
+ },
303
+ Addresses : []corev1.NodeAddress {
304
+ {
305
+ Type : corev1 .NodeInternalIP ,
306
+ Address : "192.168.0.103" ,
307
+ },
308
+ },
309
+ },
310
+ },
311
+ & corev1.Node {
312
+ ObjectMeta : metav1.ObjectMeta {
313
+ Name : "worker 2" ,
314
+ Labels : map [string ]string {
315
+ "node-role.kubernetes.io/control-plane" : "false" ,
316
+ },
317
+ },
318
+ Status : corev1.NodeStatus {
319
+ Conditions : []corev1.NodeCondition {
320
+ {
321
+ Type : corev1 .NodeReady ,
322
+ Status : corev1 .ConditionFalse ,
323
+ },
324
+ },
325
+ Addresses : []corev1.NodeAddress {
326
+ {
327
+ Type : corev1 .NodeInternalIP ,
328
+ Address : "192.168.0.104" ,
329
+ },
330
+ },
331
+ },
332
+ },
333
+ ).Build (),
334
+ expectedEndpoints : []string {
335
+ "192.168.0.100:6443" ,
336
+ "192.168.0.100:9443" ,
337
+ "192.168.0.100:2380" ,
338
+ "192.168.0.100:10250" ,
339
+ "192.168.0.102:6443" ,
340
+ "192.168.0.102:9443" ,
341
+ "192.168.0.102:2380" ,
342
+ "192.168.0.102:10250" ,
343
+ "192.168.0.103:10250" ,
344
+ },
345
+ },
346
+ }
347
+
348
+ for _ , test := range tests {
349
+ t .Run (test .name , func (t * testing.T ) {
350
+ req := require .New (t )
351
+ endpoints , err := GetEndpointsToCheck (context .Background (), test .kbClient , test .roles )
352
+ req .NoError (err )
353
+ req .Equal (test .expectedEndpoints , endpoints )
354
+ })
355
+ }
356
+ }
0 commit comments