Repository navigation
Expand file tree
/
Copy pathstackitcluster_controller_test.go
More file actions
1074 lines (893 loc) · 44.2 KB
/
Copy pathstackitcluster_controller_test.go
File metadata and controls
1074 lines (893 loc) · 44.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2026.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
*/
package controller
import (
"context"
"fmt"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1"
"github.com/stackitcloud/cluster-api-provider-stackit/cloud"
cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake"
bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion"
)
var _ = Describe("StackitCluster Controller", func() {
const namespace = "default"
var (
ctx context.Context
clusterName string
credentials string
fakeCloud *cloudfake.Client
reconciler *StackitClusterReconciler
request reconcile.Request
stackitKey types.NamespacedName
stackitClust *infrav1.StackitCluster
)
BeforeEach(func() {
ctx = context.Background()
clusterName = fmt.Sprintf("cluster-%d", time.Now().UnixNano())
credentials = "stackit-credentials-" + clusterName
stackitKey = types.NamespacedName{Namespace: namespace, Name: clusterName}
request = reconcile.Request{NamespacedName: stackitKey}
fakeCloud = cloudfake.New(cloud.Network{ID: testNetworkID, Name: "network"})
reconciler = &StackitClusterReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
CloudClientFactory: func(context.Context, cloud.Credentials) (cloud.Client, error) {
return fakeCloud, nil
},
}
createCredentialsSecret(ctx, credentials, namespace, testProjectID)
createOwnerCluster(ctx, clusterName)
stackitClust = newStackitCluster(clusterName, namespace, true)
stackitClust.Spec.CredentialsSecretRef.Name = credentials
Expect(k8sClient.Create(ctx, stackitClust)).To(Succeed())
})
AfterEach(func() {
deleteIfExists(ctx, stackitClust)
deleteIfExists(ctx, &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: clusterName, Namespace: namespace}})
deleteIfExists(ctx, &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: credentials, Namespace: namespace}})
})
It("creates the API server load balancer and publishes the control plane endpoint", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{}
Expect(k8sClient.Update(ctx, got)).To(Succeed())
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeTrue())
Expect(got.Spec.ControlPlaneEndpoint).To(Equal(clusterv1.APIEndpoint{Host: "203.0.113.10", Port: 6443}))
Expect(got.Status.APIServerEndpoint).To(Equal(got.Spec.ControlPlaneEndpoint))
Expect(got.Status.APIServerLoadBalancerID).NotTo(BeEmpty())
Expect(got.Status.FailureDomains).To(ConsistOf(
clusterv1.FailureDomain{Name: "eu01-1", ControlPlane: new(true), Attributes: map[string]string{"region": "eu01"}},
clusterv1.FailureDomain{Name: "eu01-2", ControlPlane: new(true), Attributes: map[string]string{"region": "eu01"}},
clusterv1.FailureDomain{Name: "eu01-3", ControlPlane: new(true), Attributes: map[string]string{"region": "eu01"}},
))
Expect(fakeCloud.LoadBalancerCount()).To(Equal(1))
expectCondition(got.Status.Conditions, infrav1.ClusterReadyCondition, metav1.ConditionTrue, "Available")
expectCondition(got.Status.Conditions, infrav1.ClusterNetworkReadyCondition, metav1.ConditionTrue, "Available")
expectCondition(got.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition, metav1.ConditionTrue, "Available")
})
It("uses an externally provided control plane endpoint when load balancer creation is disabled", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.APIServerLoadBalancer.Enabled = false
got.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{Host: "198.51.100.10", Port: 6443}
Expect(k8sClient.Update(ctx, got)).To(Succeed())
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeTrue())
Expect(got.Status.APIServerEndpoint.Host).To(Equal("198.51.100.10"))
Expect(fakeCloud.LoadBalancerCount()).To(Equal(0))
expectCondition(got.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition, metav1.ConditionTrue, "Skipped")
})
It("creates the bastion and publishes its public IP when enabled", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion = validBastionSpec()
Expect(k8sClient.Update(ctx, got)).To(Succeed())
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeTrue())
Expect(got.Status.Bastion.ServerID).NotTo(BeEmpty())
Expect(got.Status.Bastion.PublicIPID).NotTo(BeEmpty())
Expect(got.Status.Bastion.PublicIP).To(Equal("203.0.113.22"))
Expect(got.Status.Bastion.SecurityGroupID).NotTo(BeEmpty())
Expect(fakeCloud.ServerCount()).To(Equal(1))
Expect(fakeCloud.PublicIPCount()).To(Equal(1))
Expect(fakeCloud.SecurityGroupCount()).To(Equal(1))
expectCondition(got.Status.Conditions, infrav1.ClusterBastionReadyCondition, metav1.ConditionTrue, "Available")
})
It("reports a failed bastion server with its own reason", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion = validBastionSpec()
Expect(k8sClient.Update(ctx, got)).To(Succeed())
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
fakeCloud.SetServerState(got.Status.Bastion.ServerID, "ERROR", "")
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result.RequeueAfter).To(Equal(15 * time.Second))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
expectCondition(got.Status.Conditions, infrav1.ClusterBastionReadyCondition, metav1.ConditionFalse, "InstanceFailed")
condition := meta.FindStatusCondition(got.Status.Conditions, infrav1.ClusterBastionReadyCondition)
Expect(condition.Message).To(Equal("bastion server failed (state ERROR)"))
})
It("deletes existing bastion resources when bastion is disabled", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion = validBastionSpec()
Expect(k8sClient.Update(ctx, got)).To(Succeed())
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
_, err = fakeCloud.EnsureNodeSSHAccess(ctx, cloud.NodeSSHAccessInput{
Name: got.Name + "-node-ssh",
ServerID: got.Status.Bastion.ServerID,
BastionSecurityGroupID: got.Status.Bastion.SecurityGroupID,
Tags: bastionservice.NodeSSHAccessTags(got),
})
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.SecurityGroupCount()).To(Equal(2))
got.Spec.Bastion.Enabled = false
Expect(k8sClient.Update(ctx, got)).To(Succeed())
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Bastion).To(Equal(infrav1.StackitBastionStatus{}))
Expect(fakeCloud.ServerCount()).To(Equal(0))
Expect(fakeCloud.PublicIPCount()).To(Equal(0))
Expect(fakeCloud.SecurityGroupCount()).To(Equal(0))
expectCondition(got.Status.Conditions, infrav1.ClusterBastionReadyCondition, metav1.ConditionTrue, "Skipped")
})
It("deletes bastion resources during cluster deletion", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion = validBastionSpec()
Expect(k8sClient.Update(ctx, got)).To(Succeed())
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Bastion.PublicIP).NotTo(BeEmpty())
_, err = fakeCloud.EnsureNodeSSHAccess(ctx, cloud.NodeSSHAccessInput{
Name: got.Name + "-node-ssh",
ServerID: got.Status.Bastion.ServerID,
BastionSecurityGroupID: got.Status.Bastion.SecurityGroupID,
Tags: bastionservice.NodeSSHAccessTags(got),
})
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.SecurityGroupCount()).To(Equal(2))
Expect(k8sClient.Delete(ctx, got)).To(Succeed())
_, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.ServerCount()).To(Equal(0))
Expect(fakeCloud.PublicIPCount()).To(Equal(0))
Expect(fakeCloud.SecurityGroupCount()).To(Equal(0))
Eventually(func() bool {
err := k8sClient.Get(ctx, stackitKey, &infrav1.StackitCluster{})
return apierrors.IsNotFound(err)
}).Should(BeTrue())
})
It("cleans up bastion resources during deletion even when bastion status was never persisted", func() {
// A bastion whose status patch never landed must still be cleaned up, so
// cleanup follows the spec flag rather than persisted status.
createOwnerCluster(ctx, clusterName+"-nolb")
defer deleteIfExists(ctx, &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: clusterName + "-nolb", Namespace: namespace},
})
lbDisabled := newStackitCluster(clusterName+"-nolb", namespace, false)
lbDisabled.Spec.CredentialsSecretRef.Name = credentials
lbDisabled.Spec.Bastion = validBastionSpec()
Expect(k8sClient.Create(ctx, lbDisabled)).To(Succeed())
defer deleteIfExists(ctx, lbDisabled)
key := types.NamespacedName{Namespace: namespace, Name: lbDisabled.Name}
req := reconcile.Request{NamespacedName: key}
_, err := reconciler.Reconcile(ctx, req)
Expect(err).NotTo(HaveOccurred())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, key, got)).To(Succeed())
Expect(got.Status.Bastion.ServerID).NotTo(BeEmpty())
Expect(fakeCloud.ServerCount()).To(Equal(1))
By("losing the persisted bastion status, as if the patch never landed")
got.Status.Bastion = infrav1.StackitBastionStatus{}
Expect(k8sClient.Status().Update(ctx, got)).To(Succeed())
Expect(k8sClient.Get(ctx, key, got)).To(Succeed())
Expect(hasBastionStatus(got.Status.Bastion)).To(BeFalse())
Expect(got.Status.APIServerLoadBalancerID).To(BeEmpty())
By("deleting the cluster")
Expect(k8sClient.Delete(ctx, got)).To(Succeed())
_, err = reconciler.Reconcile(ctx, req)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.ServerCount()).To(Equal(0),
"bastion server leaked because cleanup was gated on status alone")
Expect(fakeCloud.PublicIPCount()).To(Equal(0))
Expect(fakeCloud.SecurityGroupCount()).To(Equal(0))
})
It("finalizes deletion when the credentials Secret is already gone", func() {
// The Secret commonly disappears first during namespace teardown, and
// without it no cloud client can be built at all.
createOwnerCluster(ctx, clusterName+"-nocreds")
defer deleteIfExists(ctx, &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: clusterName + "-nocreds", Namespace: namespace},
})
orphaned := newStackitCluster(clusterName+"-nocreds", namespace, false)
orphaned.Spec.CredentialsSecretRef.Name = credentials
orphaned.Spec.Bastion = validBastionSpec()
Expect(k8sClient.Create(ctx, orphaned)).To(Succeed())
defer deleteIfExists(ctx, orphaned)
key := types.NamespacedName{Namespace: namespace, Name: orphaned.Name}
req := reconcile.Request{NamespacedName: key}
_, err := reconciler.Reconcile(ctx, req)
Expect(err).NotTo(HaveOccurred())
By("removing the credentials Secret, as namespace teardown would")
Expect(k8sClient.Delete(ctx, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: credentials, Namespace: namespace},
})).To(Succeed())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, key, got)).To(Succeed())
Expect(k8sClient.Delete(ctx, got)).To(Succeed())
_, err = reconciler.Reconcile(ctx, req)
Expect(err).NotTo(HaveOccurred(), "deletion must not block on a Secret that can never come back")
Eventually(func() bool {
return apierrors.IsNotFound(k8sClient.Get(ctx, key, &infrav1.StackitCluster{}))
}).Should(BeTrue(), "cluster stayed in Terminating because the finalizer was never removed")
})
It("tears the bastion down when disabled even if its status was never persisted", func() {
// With the status lost, a status-gated teardown would report the bastion
// as disabled while leaving port 22 open.
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion = validBastionSpec()
Expect(k8sClient.Update(ctx, got)).To(Succeed())
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.ServerCount()).To(Equal(1))
By("losing the persisted bastion status and its condition")
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Status.Bastion = infrav1.StackitBastionStatus{}
got.Status.Conditions = nil
Expect(k8sClient.Status().Update(ctx, got)).To(Succeed())
By("disabling the bastion")
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion.Enabled = false
Expect(k8sClient.Update(ctx, got)).To(Succeed())
_, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.ServerCount()).To(Equal(0),
"bastion kept running with port 22 open while reporting itself disabled")
Expect(fakeCloud.PublicIPCount()).To(Equal(0))
})
It("tears the bastion down when disabled even if only its status was lost", func() {
// Narrower than the case above: the condition survives and still reports
// the bastion as available, only the status fields are gone.
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion = validBastionSpec()
Expect(k8sClient.Update(ctx, got)).To(Succeed())
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.ServerCount()).To(Equal(1))
By("losing the persisted bastion status while keeping the conditions")
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Status.Bastion = infrav1.StackitBastionStatus{}
Expect(k8sClient.Status().Update(ctx, got)).To(Succeed())
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
expectCondition(got.Status.Conditions, infrav1.ClusterBastionReadyCondition, metav1.ConditionTrue, "Available")
By("disabling the bastion")
got.Spec.Bastion.Enabled = false
Expect(k8sClient.Update(ctx, got)).To(Succeed())
_, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.ServerCount()).To(Equal(0),
"bastion kept running because cleanup was gated on the bastion status")
Expect(fakeCloud.PublicIPCount()).To(Equal(0))
})
It("cleans up the load balancer during deletion when it was disabled and its status was lost", func() {
// Flipping apiServerLoadBalancer.enabled off neither deletes the load
// balancer nor clears its ID, so deletion cannot rely on either.
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.LoadBalancerCount()).To(Equal(1))
By("losing the persisted load balancer ID")
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Status.APIServerLoadBalancerID = ""
Expect(k8sClient.Status().Update(ctx, got)).To(Succeed())
By("disabling the load balancer")
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.APIServerLoadBalancer.Enabled = false
Expect(k8sClient.Update(ctx, got)).To(Succeed())
By("deleting the cluster")
Expect(k8sClient.Delete(ctx, got)).To(Succeed())
_, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.LoadBalancerCount()).To(Equal(0),
"load balancer leaked because cleanup was gated on spec and status")
})
It("validates bastion specs", func() {
spec := validBastionSpec()
Expect(validateBastionSpec(spec)).To(Succeed())
spec.AllowedCIDRs = []string{"not-a-cidr"}
Expect(validateBastionSpec(spec)).To(MatchError(ContainSubstring("invalid CIDR")))
})
It("creates the bastion with cloud-init user data from a ConfigMap", func() {
expectBastionCloudInit(
ctx, reconciler, request, stackitKey, fakeCloud,
"ConfigMap", "bastion-cloud-init-"+clusterName,
"#cloud-config\npackages:\n- htop\n", createCloudInitConfigMap,
)
})
It("creates the bastion with cloud-init user data from a Secret", func() {
expectBastionCloudInit(
ctx, reconciler, request, stackitKey, fakeCloud,
"Secret", "bastion-cloud-init-secret-"+clusterName,
"#cloud-config\npackages:\n- jq\n", createCloudInitSecret,
)
})
It("marks the bastion not ready when cloud-init ref is missing", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion = validBastionSpec()
got.Spec.Bastion.CloudInitRef = &infrav1.StackitBastionCloudInitRef{
Kind: "ConfigMap",
Name: "missing-" + clusterName,
Key: "userData",
}
Expect(k8sClient.Update(ctx, got)).To(Succeed())
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeFalse())
Expect(fakeCloud.ServerCount()).To(Equal(0))
expectCondition(got.Status.Conditions, infrav1.ClusterBastionReadyCondition, metav1.ConditionFalse, "CloudInitRefError")
})
It("recreates the bastion and node SSH access when referenced cloud-init changes", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
cloudInitName := "bastion-cloud-init-" + clusterName
createCloudInitConfigMap(ctx, cloudInitName, namespace, "userData", "#cloud-config\npackages:\n- htop\n")
got.Spec.Bastion = validBastionSpec()
got.Spec.Bastion.CloudInitRef = &infrav1.StackitBastionCloudInitRef{
Kind: "ConfigMap",
Name: cloudInitName,
Key: "userData",
}
Expect(k8sClient.Update(ctx, got)).To(Succeed())
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
oldServerID := got.Status.Bastion.ServerID
Expect(oldServerID).NotTo(BeEmpty())
_, err = fakeCloud.EnsureNodeSSHAccess(ctx, cloud.NodeSSHAccessInput{
Name: got.Name + "-node-ssh",
ServerID: got.Status.Bastion.ServerID,
BastionSecurityGroupID: got.Status.Bastion.SecurityGroupID,
Tags: bastionservice.NodeSSHAccessTags(got),
})
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.SecurityGroupCount()).To(Equal(2))
configMap := &corev1.ConfigMap{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: cloudInitName, Namespace: namespace}, configMap)).To(Succeed())
configMap.Data["userData"] = "#cloud-config\npackages:\n- jq\n"
Expect(k8sClient.Update(ctx, configMap)).To(Succeed())
result, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result.RequeueAfter).To(BeNumerically(">", 0))
Expect(fakeCloud.ServerCount()).To(Equal(0))
Expect(fakeCloud.PublicIPCount()).To(Equal(0))
Expect(fakeCloud.SecurityGroupCount()).To(Equal(0))
result, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Bastion.ServerID).NotTo(BeEmpty())
Expect(got.Status.Bastion.ServerID).NotTo(Equal(oldServerID))
Expect(got.Status.Bastion.CloudInitHash).To(Equal(bastionCloudInitHash([]byte(configMap.Data["userData"]))))
Expect(string(fakeCloud.ServerUserData(got.Status.Bastion.ServerID))).To(Equal(configMap.Data["userData"]))
})
It("marks the network not ready when the configured network does not exist", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Network.ID = "99999999-9999-9999-9999-999999999999"
Expect(k8sClient.Update(ctx, got)).To(Succeed())
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeFalse())
expectCondition(got.Status.Conditions, infrav1.ClusterNetworkReadyCondition, metav1.ConditionFalse, "NetworkNotFound")
expectCondition(got.Status.Conditions, infrav1.ClusterReadyCondition, metav1.ConditionFalse, "NetworkNotFound")
})
It("marks conditions not ready when API server load balancer creation fails", func() {
fakeCloud.FailNextEnsureLB = fmt.Errorf("load balancer creation rejected: %w", cloud.ErrInvalidInput)
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeFalse())
expectCondition(got.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition, metav1.ConditionFalse, "LoadBalancerError")
})
It("marks conditions not ready when bastion creation fails", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion = validBastionSpec()
Expect(k8sClient.Update(ctx, got)).To(Succeed())
fakeCloud.FailNextEnsureBastion = fmt.Errorf("bastion creation rejected: %w", cloud.ErrInvalidInput)
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeFalse())
expectCondition(got.Status.Conditions, infrav1.ClusterBastionReadyCondition, metav1.ConditionFalse, "BastionError")
})
It("requeues when network lookup returns a transient error", func() {
fakeCloud.FailNextGetNetwork = fmt.Errorf("temporary network lookup failure: %w", cloud.ErrTransient)
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result.RequeueAfter).To(BeNumerically(">", 0))
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
expectCondition(got.Status.Conditions, infrav1.ClusterNetworkReadyCondition, metav1.ConditionFalse, "NetworkNotFound")
expectCondition(got.Status.Conditions, infrav1.ClusterReadyCondition, metav1.ConditionFalse, "NetworkNotFound")
})
// The requeue is what lets a corrected Secret take effect: nothing else
// enqueues the cluster once the credentials are rejected.
It("requeues on unauthorized credentials and recovers once they are corrected", func() {
reconciler.CloudClientFactory = func(context.Context, cloud.Credentials) (cloud.Client, error) {
return nil, fmt.Errorf("authenticate: %w", cloud.ErrUnauthorized)
}
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result.RequeueAfter).To(Equal(credentialsRetryRequeueAfter))
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeFalse())
expectCondition(got.Status.Conditions, infrav1.ClusterCredentialsReadyCondition, metav1.ConditionFalse, "CredentialsInvalid")
expectCondition(got.Status.Conditions, infrav1.ClusterReadyCondition, metav1.ConditionFalse, "CredentialsInvalid")
By("correcting the credentials")
reconciler.CloudClientFactory = func(context.Context, cloud.Credentials) (cloud.Client, error) {
return fakeCloud, nil
}
_, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeTrue())
expectCondition(got.Status.Conditions, infrav1.ClusterCredentialsReadyCondition, metav1.ConditionTrue, "Available")
})
//nolint:dupl // Pause behavior must be verified independently for the cluster reconciler.
It("does not call the cloud API when the owning Cluster is paused", func() {
cluster := &clusterv1.Cluster{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: clusterName, Namespace: namespace}, cluster)).To(Succeed())
cluster.Spec.Paused = new(true)
Expect(k8sClient.Update(ctx, cluster)).To(Succeed())
cloudClientFactoryCalls := 0
reconciler.CloudClientFactory = func(context.Context, cloud.Credentials) (cloud.Client, error) {
cloudClientFactoryCalls++
return fakeCloud, nil
}
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(cloudClientFactoryCalls).To(Equal(0))
Expect(fakeCloud.LoadBalancerCount()).To(Equal(0))
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
expectCondition(got.Status.Conditions, clusterv1.PausedCondition, metav1.ConditionTrue, clusterv1.PausedReason)
})
It("does not call the cloud API when the StackitCluster has the paused annotation", func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Annotations = map[string]string{clusterv1.PausedAnnotation: ""}
Expect(k8sClient.Update(ctx, got)).To(Succeed())
cloudClientFactoryCalls := 0
reconciler.CloudClientFactory = func(context.Context, cloud.Credentials) (cloud.Client, error) {
cloudClientFactoryCalls++
return fakeCloud, nil
}
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
Expect(cloudClientFactoryCalls).To(Equal(0))
Expect(fakeCloud.LoadBalancerCount()).To(Equal(0))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
expectCondition(got.Status.Conditions, clusterv1.PausedCondition, metav1.ConditionTrue, clusterv1.PausedReason)
})
It("deletes the provider-managed load balancer and removes the finalizer", func() {
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.APIServerLoadBalancerID).NotTo(BeEmpty())
Expect(fakeCloud.LoadBalancerCount()).To(Equal(1))
Expect(k8sClient.Delete(ctx, got)).To(Succeed())
_, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.LoadBalancerCount()).To(Equal(0))
Eventually(func() bool {
err := k8sClient.Get(ctx, stackitKey, &infrav1.StackitCluster{})
return apierrors.IsNotFound(err)
}).Should(BeTrue())
})
It("returns an error when API server load balancer deletion fails", func() {
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.APIServerLoadBalancerID).NotTo(BeEmpty())
Expect(k8sClient.Delete(ctx, got)).To(Succeed())
fakeCloud.FailNextDeleteLB = fmt.Errorf("delete load balancer timeout: %w", cloud.ErrTransient)
_, err = reconciler.Reconcile(ctx, request)
Expect(err).To(HaveOccurred())
})
// GetNetwork is the first cloud call of the reconcile, so a hook there proves
// the ordering rather than only the end result.
It("persists the finalizer before the first cloud call", func() {
var finalizersAtFirstCall []string
fakeCloud.BeforeGetNetwork = func() {
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
finalizersAtFirstCall = got.Finalizers
}
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.LoadBalancerCount()).To(Equal(1))
Expect(finalizersAtFirstCall).To(ContainElement(infrav1.ClusterFinalizer),
"cloud resources were created while the API server had no finalizer to clean them up")
})
// Machine controllers reach credentials and project context through this
// StackitCluster, so it has to outlive them. Cluster API orders this
// correctly when deletion starts at the Cluster, but a namespace teardown or
// a direct delete of this object bypasses that ordering.
It("keeps the finalizer while Machines still exist for the cluster", func() {
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
machineName := "machine-" + clusterName
createOwnerMachine(ctx, machineName, clusterName, "stackit-"+machineName)
DeferCleanup(func() {
deleteIfExists(ctx, &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: machineName, Namespace: namespace}})
})
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(fakeCloud.LoadBalancerCount()).To(Equal(1))
Expect(k8sClient.Delete(ctx, got)).To(Succeed())
By("requeueing instead of tearing down shared infrastructure")
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result.RequeueAfter).To(Equal(deleteRequeueAfter))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Finalizers).To(ContainElement(infrav1.ClusterFinalizer),
"the finalizer must survive while a Machine still needs the StackitCluster")
Expect(fakeCloud.LoadBalancerCount()).To(Equal(1),
"the load balancer must not be deleted while Machines still exist")
})
It("removes the finalizer once the last Machine is gone", func() {
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
machineName := "machine-" + clusterName
createOwnerMachine(ctx, machineName, clusterName, "stackit-"+machineName)
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(k8sClient.Delete(ctx, got)).To(Succeed())
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result.RequeueAfter).To(Equal(deleteRequeueAfter))
By("deleting the last Machine")
deleteIfExists(ctx, &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: machineName, Namespace: namespace}})
_, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.LoadBalancerCount()).To(Equal(0))
Eventually(func() bool {
err := k8sClient.Get(ctx, stackitKey, &infrav1.StackitCluster{})
return apierrors.IsNotFound(err)
}).Should(BeTrue())
})
It("keeps the finalizer when load balancer deletion returns a transient error", func() {
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.APIServerLoadBalancerID).NotTo(BeEmpty())
Expect(k8sClient.Delete(ctx, got)).To(Succeed())
fakeCloud.FailNextDeleteLB = fmt.Errorf("delete load balancer: %w", cloud.ErrTransient)
_, err = reconciler.Reconcile(ctx, request)
Expect(err).To(HaveOccurred())
Expect(fakeCloud.LoadBalancerCount()).To(Equal(1))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Finalizers).To(ContainElement(infrav1.ClusterFinalizer))
})
It("fills the target pool with the control plane machine addresses", func() {
createControlPlaneMachine(ctx, "cp-0-"+clusterName, clusterName, "10.0.0.11")
createControlPlaneMachine(ctx, "cp-1-"+clusterName, clusterName, "10.0.0.12")
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(fakeCloud.LoadBalancerTargetIPs(got.Status.APIServerLoadBalancerID)).To(Equal(map[string]string{
"cp-0-" + clusterName: "10.0.0.11",
"cp-1-" + clusterName: "10.0.0.12",
}))
expectCondition(got.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition, metav1.ConditionTrue, "Available")
})
It("keeps the bootstrap placeholder while no control plane machine has an address", func() {
// STACKIT rejects an empty target pool.
createControlPlaneMachine(ctx, "cp-0-"+clusterName, clusterName, "")
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(fakeCloud.LoadBalancerTargetIPs(got.Status.APIServerLoadBalancerID)).To(Equal(map[string]string{
"capi-bootstrap-placeholder": "10.0.0.1",
}))
})
It("keeps worker machines out of the target pool", func() {
machineName := "worker-" + clusterName
createOwnerMachine(ctx, machineName, clusterName, "stackit-"+machineName)
DeferCleanup(func() {
deleteIfExists(ctx, &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: machineName, Namespace: namespace}})
})
setMachineInternalIP(ctx, machineName, "10.0.0.11")
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(fakeCloud.LoadBalancerTargetIPs(got.Status.APIServerLoadBalancerID)).To(Equal(map[string]string{
"capi-bootstrap-placeholder": "10.0.0.1",
}))
})
It("drops a control plane machine that is being deleted from the target pool", func() {
deleting := "cp-0-" + clusterName
createControlPlaneMachine(ctx, deleting, clusterName, "10.0.0.11")
createControlPlaneMachine(ctx, "cp-1-"+clusterName, clusterName, "10.0.0.12")
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
loadBalancerID := got.Status.APIServerLoadBalancerID
Expect(fakeCloud.LoadBalancerTargetCount(loadBalancerID)).To(Equal(2))
markMachineDeleting(ctx, deleting)
_, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
// The target goes before the node is drained, not after.
Expect(fakeCloud.LoadBalancerTargetIPs(loadBalancerID)).To(Equal(map[string]string{
"cp-1-" + clusterName: "10.0.0.12",
}))
})
It("keeps the target pool when every control plane machine is being deleted", func() {
// Cluster deletion removes the control plane before the infrastructure.
first := "cp-0-" + clusterName
second := "cp-1-" + clusterName
createControlPlaneMachine(ctx, first, clusterName, "10.0.0.11")
createControlPlaneMachine(ctx, second, clusterName, "10.0.0.12")
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
loadBalancerID := got.Status.APIServerLoadBalancerID
markMachineDeleting(ctx, first)
markMachineDeleting(ctx, second)
_, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(fakeCloud.LoadBalancerTargetIPs(loadBalancerID)).To(Equal(map[string]string{
first: "10.0.0.11",
second: "10.0.0.12",
}), "the pool must not collapse onto the bootstrap placeholder")
})
It("requeues without failing the cluster when the target pool update fails", func() {
createControlPlaneMachine(ctx, "cp-0-"+clusterName, clusterName, "10.0.0.11")
_, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeTrue())
fakeCloud.FailNextSetTargets = fmt.Errorf("update target pool timeout: %w", cloud.ErrTransient)
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result.RequeueAfter).To(BeNumerically(">", 0))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
expectCondition(got.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition, metav1.ConditionFalse, "LoadBalancerTargetError")
// The machine reconciler stops while the cluster is not ready, so a
// broken target pool must not take the cluster down with it.
Expect(got.Status.Ready).To(BeTrue())
expectCondition(got.Status.Conditions, infrav1.ClusterReadyCondition, metav1.ConditionTrue, "Available")
})
It("requeues when the first target pool update fails permanently", func() {
// Without the requeue nothing would drive another reconcile.
createControlPlaneMachine(ctx, "cp-0-"+clusterName, clusterName, "10.0.0.11")
fakeCloud.FailNextSetTargets = fmt.Errorf("target pool rejected: %w", cloud.ErrInvalidInput)
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result.RequeueAfter).To(BeNumerically(">", 0))
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
expectCondition(got.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition, metav1.ConditionFalse, "LoadBalancerTargetError")
By("recovering on the next attempt")
_, err = reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeTrue())
Expect(fakeCloud.LoadBalancerTargetIPs(got.Status.APIServerLoadBalancerID)).To(Equal(map[string]string{
"cp-0-" + clusterName: "10.0.0.11",
}))
})
It("maps control plane Machine events to StackitCluster reconcile requests", func() {
machineName := "cp-0-" + clusterName
createControlPlaneMachine(ctx, machineName, clusterName, "10.0.0.11")
machine := &clusterv1.Machine{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: machineName, Namespace: namespace}, machine)).To(Succeed())
Expect(reconciler.loadBalancerTargetsForControlPlanes(ctx, machine)).To(Equal([]reconcile.Request{request}))
})
It("ignores worker Machine events", func() {
machineName := "worker-" + clusterName
createOwnerMachine(ctx, machineName, clusterName, "stackit-"+machineName)
DeferCleanup(func() {
deleteIfExists(ctx, &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: machineName, Namespace: namespace}})
})
machine := &clusterv1.Machine{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: machineName, Namespace: namespace}, machine)).To(Succeed())
Expect(reconciler.loadBalancerTargetsForControlPlanes(ctx, machine)).To(BeEmpty())
})
It("maps owning Cluster events to StackitCluster reconcile requests", func() {
cluster := &clusterv1.Cluster{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: clusterName, Namespace: namespace}, cluster)).To(Succeed())
requests := reconciler.filterForStackitClusterResources(ctx, cluster)
Expect(requests).To(Equal([]reconcile.Request{request}))
})
It("maps bastion cloud-init ConfigMap and Secret events to StackitCluster reconcile requests", func() {
configMapName := "bastion-cloud-init-" + clusterName
secretName := "bastion-cloud-init-secret-" + clusterName
createCloudInitConfigMap(ctx, configMapName, namespace, "userData", "#cloud-config\n")
createCloudInitSecret(ctx, secretName, namespace, "userData", "#cloud-config\n")
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion = validBastionSpec()
got.Spec.Bastion.CloudInitRef = &infrav1.StackitBastionCloudInitRef{
Kind: "ConfigMap",
Name: configMapName,
Key: "userData",
}
Expect(k8sClient.Update(ctx, got)).To(Succeed())
configMap := &corev1.ConfigMap{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: configMapName, Namespace: namespace}, configMap)).To(Succeed())
requests := reconciler.stackitClusterRequestsForCloudInitRef(ctx, configMap)
Expect(requests).To(Equal([]reconcile.Request{request}))
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
got.Spec.Bastion.CloudInitRef.Kind = "Secret"
got.Spec.Bastion.CloudInitRef.Name = secretName
Expect(k8sClient.Update(ctx, got)).To(Succeed())
secret := &corev1.Secret{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: secretName, Namespace: namespace}, secret)).To(Succeed())
requests = reconciler.stackitClusterRequestsForCloudInitRef(ctx, secret)
Expect(requests).To(Equal([]reconcile.Request{request}))
})
It("ignores Cluster events for other infrastructure providers", func() {
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: "other", Namespace: namespace},
Spec: clusterv1.ClusterSpec{
InfrastructureRef: clusterv1.ContractVersionedObjectReference{
APIGroup: "other.infrastructure.example.com",
Kind: "OtherCluster",
Name: "other",
},
},
}
requests := reconciler.filterForStackitClusterResources(ctx, cluster)
Expect(requests).To(BeEmpty())
})
It("discovers failure domains dynamically and filters out metro (-m) zones", func() {
fakeCloud.AvailabilityZones = []string{"eu01-2", "eu01-m", "eu01-1", "eu01-3"}
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(reconcile.Result{}))
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.FailureDomains).To(Equal([]clusterv1.FailureDomain{
{Name: "eu01-1", ControlPlane: new(true), Attributes: map[string]string{"region": "eu01"}},
{Name: "eu01-2", ControlPlane: new(true), Attributes: map[string]string{"region": "eu01"}},
{Name: "eu01-3", ControlPlane: new(true), Attributes: map[string]string{"region": "eu01"}},
}))
})
It("reports an error condition when listing availability zones fails", func() {
fakeCloud.FailNextListAZ = fmt.Errorf("cloud failure: %w", cloud.ErrTransient)
result, err := reconciler.Reconcile(ctx, request)
Expect(err).NotTo(HaveOccurred())
Expect(result.RequeueAfter).To(BeNumerically(">", 0))
got := &infrav1.StackitCluster{}
Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed())
Expect(got.Status.Ready).To(BeFalse())
expectCondition(got.Status.Conditions, infrav1.ClusterReadyCondition, metav1.ConditionFalse, "AvailabilityZonesError")
})