@@ -18,51 +18,115 @@ 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 {
53+ ObjectMeta : metav1.ObjectMeta {
54+ Name : nodeName ,
55+ },
56+ }
57+ Expect (k8sClient .Create (ctx , node )).To (Succeed ())
58+ DeferCleanup (func (ctx SpecContext ) {
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 {
4665 ObjectMeta : metav1.ObjectMeta {
47- Name : nodeName ,
48- Labels : map [string ]string {labelEvictionRequired : "true" },
66+ Name : nodeName ,
67+ },
68+ Spec : kvmv1.HypervisorSpec {
69+ LifecycleEnabled : true ,
4970 },
5071 }
51- Expect (k8sClient .Create (ctx , resource )).To (Succeed ())
72+ Expect (k8sClient .Create (ctx , hypervisor )).To (Succeed ())
5273 DeferCleanup (func (ctx SpecContext ) {
53- Expect (client . IgnoreNotFound ( k8sClient .Delete (ctx , resource ) )).To (Succeed ())
74+ Expect (k8sClient .Delete (ctx , hypervisor )).To (Succeed ())
5475 })
5576 })
5677
5778 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- }
62-
63- By ("Reconciling the created resource" )
64- _ , err := controller .Reconcile (ctx , req )
79+ JustBeforeEach (func (ctx SpecContext ) {
80+ _ , err := controller .Reconcile (ctx , reconcileReq )
6581 Expect (err ).NotTo (HaveOccurred ())
6682 })
83+ It ("should create a poddisruptionbudget" , func (ctx SpecContext ) {
84+ pdb := & policyv1.PodDisruptionBudget {}
85+ Expect (k8sClient .Get (ctx , maintenanceName , pdb )).To (Succeed ())
86+ Expect (pdb .Spec .MinAvailable ).To (HaveField ("IntVal" , BeNumerically ("==" , 1 )))
87+ })
88+
89+ It ("should create a failing deployment to signal onboarding not being completed" , func (ctx SpecContext ) {
90+ dep := & appsv1.Deployment {}
91+ Expect (k8sClient .Get (ctx , maintenanceName , dep )).To (Succeed ())
92+ Expect (dep .Spec .Template .Spec .Containers ).To (HaveLen (1 ))
93+ Expect (dep .Spec .Template .Spec .Containers [0 ].StartupProbe .Exec .Command ).To (Equal ([]string {"/bin/false" }))
94+ })
95+
96+ When ("the node has been onboarded" , func () {
97+ BeforeEach (func (ctx SpecContext ) {
98+ hypervisor := & kvmv1.Hypervisor {}
99+ Expect (k8sClient .Get (ctx , name , hypervisor )).To (Succeed ())
100+ meta .SetStatusCondition (& hypervisor .Status .Conditions , metav1.Condition {
101+ Type : kvmv1 .ConditionTypeOnboarding ,
102+ Status : metav1 .ConditionFalse ,
103+ Reason : "dontcare" ,
104+ Message : "dontcare" ,
105+ })
106+ Expect (k8sClient .Status ().Update (ctx , hypervisor )).To (Succeed ())
107+ })
108+
109+ It ("should create a deployment with onboarding completed" , func (ctx SpecContext ) {
110+ dep := & appsv1.Deployment {}
111+ Expect (k8sClient .Get (ctx , maintenanceName , dep )).To (Succeed ())
112+ Expect (dep .Spec .Template .Spec .Containers ).To (HaveLen (1 ))
113+ Expect (dep .Spec .Template .Spec .Containers [0 ].StartupProbe .Exec .Command ).To (Equal ([]string {"/bin/true" }))
114+ })
115+ })
116+
117+ When ("the node has been evicted" , func () {
118+ BeforeEach (func (ctx SpecContext ) {
119+ hypervisor := & kvmv1.Hypervisor {}
120+ Expect (k8sClient .Get (ctx , name , hypervisor )).To (Succeed ())
121+ meta .SetStatusCondition (& hypervisor .Status .Conditions , metav1.Condition {
122+ Type : kvmv1 .ConditionTypeEvicting ,
123+ Status : metav1 .ConditionFalse ,
124+ Reason : "dontcare" ,
125+ Message : "dontcare" ,
126+ })
127+ Expect (k8sClient .Status ().Update (ctx , hypervisor )).To (Succeed ())
128+ })
129+ })
130+
67131 })
68132})
0 commit comments