@@ -18,51 +18,137 @@ limitations under the License.
1818package controller
1919
2020import (
21+ "fmt"
22+
2123 . "github.com/onsi/ginkgo/v2"
2224 . "github.com/onsi/gomega"
25+ appsv1 "k8s.io/api/apps/v1"
2326 corev1 "k8s.io/api/core/v1"
27+ policyv1 "k8s.io/api/policy/v1"
28+ "k8s.io/apimachinery/pkg/api/meta"
2429 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2530 "k8s.io/apimachinery/pkg/types"
2631 ctrl "sigs.k8s.io/controller-runtime"
27- "sigs.k8s.io/controller-runtime/pkg/client"
32+
33+ kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
2834)
2935
3036var _ = Describe ("Gardener Maintenance Controller" , func () {
3137 const nodeName = "node-test"
32- var controller * GardenerNodeLifecycleController
38+ var (
39+ controller * GardenerNodeLifecycleController
40+ name = types.NamespacedName {Name : nodeName }
41+ reconcileReq = ctrl.Request {NamespacedName : name }
42+ maintenanceName = types.NamespacedName {Name : fmt .Sprintf ("maint-%v" , nodeName ), Namespace : "kube-system" }
43+ )
3344
3445 BeforeEach (func (ctx SpecContext ) {
3546 controller = & GardenerNodeLifecycleController {
3647 Client : k8sClient ,
3748 Scheme : k8sClient .Scheme (),
3849 }
3950
40- By ("creating the namespace for the reconciler" )
41- ns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "monsoon3" }}
42- Expect (client .IgnoreAlreadyExists (k8sClient .Create (ctx , ns ))).To (Succeed ())
43-
4451 By ("creating the core resource for the Kind Node" )
45- resource := & corev1.Node {
52+ node := & corev1.Node {
4653 ObjectMeta : metav1.ObjectMeta {
47- Name : nodeName ,
48- Labels : map [string ]string {labelEvictionRequired : "true" },
54+ Name : nodeName ,
4955 },
5056 }
51- Expect (k8sClient .Create (ctx , resource )).To (Succeed ())
57+ Expect (k8sClient .Create (ctx , node )).To (Succeed ())
5258 DeferCleanup (func (ctx SpecContext ) {
53- Expect (client .IgnoreNotFound (k8sClient .Delete (ctx , resource ))).To (Succeed ())
59+ By ("Cleanup the specific node" )
60+ Expect (k8sClient .Delete (ctx , node )).To (Succeed ())
61+ })
62+
63+ By ("creating the core resource for the Kind hypervisor" )
64+ hypervisor := & kvmv1.Hypervisor {
65+ ObjectMeta : metav1.ObjectMeta {
66+ Name : nodeName ,
67+ },
68+ Spec : kvmv1.HypervisorSpec {
69+ LifecycleEnabled : true ,
70+ },
71+ }
72+ Expect (k8sClient .Create (ctx , hypervisor )).To (Succeed ())
73+ DeferCleanup (func (ctx SpecContext ) {
74+ Expect (k8sClient .Delete (ctx , hypervisor )).To (Succeed ())
5475 })
5576 })
5677
57- Context ("When reconciling a node" , func () {
58- It ("should successfully reconcile the resource" , func (ctx SpecContext ) {
59- req := ctrl.Request {
60- NamespacedName : types.NamespacedName {Name : nodeName },
61- }
78+ Context ("When reconciling a terminating node" , func () {
79+ BeforeEach (func (ctx SpecContext ) {
80+ By ("Marking the node as terminating" )
81+ node := & corev1.Node {}
82+ Expect (k8sClient .Get (ctx , name , node )).To (Succeed ())
83+ node .Status .Conditions = append (node .Status .Conditions , corev1.NodeCondition {
84+ Type : "Terminating" ,
85+ })
86+ Expect (k8sClient .Status ().Update (ctx , node )).To (Succeed ())
87+ })
6288
89+ It ("should successfully reconcile the resource" , func (ctx SpecContext ) {
6390 By ("Reconciling the created resource" )
64- _ , err := controller .Reconcile (ctx , req )
91+ _ , err := controller .Reconcile (ctx , reconcileReq )
6592 Expect (err ).NotTo (HaveOccurred ())
93+
94+ hypervisor := & kvmv1.Hypervisor {}
95+ Expect (k8sClient .Get (ctx , name , hypervisor )).To (Succeed ())
96+ Expect (hypervisor .Spec .Maintenance ).To (Equal (kvmv1 .MaintenanceTermination ))
6697 })
6798 })
99+
100+ Context ("When reconciling a node" , func () {
101+ JustBeforeEach (func (ctx SpecContext ) {
102+ _ , err := controller .Reconcile (ctx , reconcileReq )
103+ Expect (err ).NotTo (HaveOccurred ())
104+ })
105+ It ("should create a poddisruptionbudget" , func (ctx SpecContext ) {
106+ pdb := & policyv1.PodDisruptionBudget {}
107+ Expect (k8sClient .Get (ctx , maintenanceName , pdb )).To (Succeed ())
108+ Expect (pdb .Spec .MinAvailable ).To (HaveField ("IntVal" , BeNumerically ("==" , 1 )))
109+ })
110+
111+ It ("should create a failing deployment to signal onboarding not being completed" , func (ctx SpecContext ) {
112+ dep := & appsv1.Deployment {}
113+ Expect (k8sClient .Get (ctx , maintenanceName , dep )).To (Succeed ())
114+ Expect (dep .Spec .Template .Spec .Containers ).To (HaveLen (1 ))
115+ Expect (dep .Spec .Template .Spec .Containers [0 ].StartupProbe .Exec .Command ).To (Equal ([]string {"/bin/false" }))
116+ })
117+
118+ When ("the node has been onboarded" , func () {
119+ BeforeEach (func (ctx SpecContext ) {
120+ hypervisor := & kvmv1.Hypervisor {}
121+ Expect (k8sClient .Get (ctx , name , hypervisor )).To (Succeed ())
122+ meta .SetStatusCondition (& hypervisor .Status .Conditions , metav1.Condition {
123+ Type : kvmv1 .ConditionTypeOnboarding ,
124+ Status : metav1 .ConditionFalse ,
125+ Reason : "dontcare" ,
126+ Message : "dontcare" ,
127+ })
128+ Expect (k8sClient .Status ().Update (ctx , hypervisor )).To (Succeed ())
129+ })
130+
131+ It ("should create a deployment with onboarding completed" , func (ctx SpecContext ) {
132+ dep := & appsv1.Deployment {}
133+ Expect (k8sClient .Get (ctx , maintenanceName , dep )).To (Succeed ())
134+ Expect (dep .Spec .Template .Spec .Containers ).To (HaveLen (1 ))
135+ Expect (dep .Spec .Template .Spec .Containers [0 ].StartupProbe .Exec .Command ).To (Equal ([]string {"/bin/true" }))
136+ })
137+ })
138+
139+ When ("the node has been evicted" , func () {
140+ BeforeEach (func (ctx SpecContext ) {
141+ hypervisor := & kvmv1.Hypervisor {}
142+ Expect (k8sClient .Get (ctx , name , hypervisor )).To (Succeed ())
143+ meta .SetStatusCondition (& hypervisor .Status .Conditions , metav1.Condition {
144+ Type : kvmv1 .ConditionTypeEvicting ,
145+ Status : metav1 .ConditionFalse ,
146+ Reason : "dontcare" ,
147+ Message : "dontcare" ,
148+ })
149+ Expect (k8sClient .Status ().Update (ctx , hypervisor )).To (Succeed ())
150+ })
151+ })
152+
153+ })
68154})
0 commit comments