Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions internal/controller/aggregates_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
package controller

import (
"context"
"fmt"
"net/http"

Expand Down Expand Up @@ -105,7 +104,7 @@ var _ = Describe("AggregatesController", func() {
)
// Setup and teardown

BeforeEach(func(ctx context.Context) {
BeforeEach(func(ctx SpecContext) {
By("Setting up the OpenStack http mock server")
fakeServer = testhelper.SetupHTTP()
DeferCleanup(fakeServer.Teardown)
Expand Down Expand Up @@ -135,19 +134,19 @@ var _ = Describe("AggregatesController", func() {
computeClient: client.ServiceClient(fakeServer),
}

DeferCleanup(func(ctx context.Context) {
DeferCleanup(func(ctx SpecContext) {
Expect(tc.Client.Delete(ctx, hypervisor)).To(Succeed())
})
})

JustBeforeEach(func(ctx context.Context) {
JustBeforeEach(func(ctx SpecContext) {
_, err := tc.Reconcile(ctx, ctrl.Request{NamespacedName: hypervisorName})
Expect(err).NotTo(HaveOccurred())
})

// Tests
Context("Adding new Aggregate", func() {
BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
By("Setting a missing aggregate")
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
Expand Down Expand Up @@ -189,7 +188,7 @@ var _ = Describe("AggregatesController", func() {
})
})

It("should update Aggregates and set status condition as Aggregates differ", func() {
It("should update Aggregates and set status condition as Aggregates differ", func(ctx SpecContext) {
updated := &kvmv1.Hypervisor{}
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Aggregates).To(ContainElements("test-aggregate1"))
Expand All @@ -198,7 +197,7 @@ var _ = Describe("AggregatesController", func() {
})

Context("Removing Aggregate", func() {
BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
// update status to have existing aggregate
Expand Down Expand Up @@ -233,7 +232,7 @@ var _ = Describe("AggregatesController", func() {
fakeServer.Mux.HandleFunc("POST /os-aggregates/99/action", expectRemoveHostFromAggregate)
})

It("should update Aggregates and set status condition when Aggregates differ", func() {
It("should update Aggregates and set status condition when Aggregates differ", func(ctx SpecContext) {
updated := &kvmv1.Hypervisor{}
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Aggregates).To(BeEmpty())
Expand All @@ -242,15 +241,15 @@ var _ = Describe("AggregatesController", func() {
})

Context("before onboarding", func() {
BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
// Remove the onboarding condition
hypervisor.Status.Conditions = []v1.Condition{}
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})

It("should neither update Aggregates and nor set status condition", func() {
It("should neither update Aggregates and nor set status condition", func(ctx SpecContext) {
updated := &kvmv1.Hypervisor{}
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Aggregates).To(BeEmpty())
Expand All @@ -259,7 +258,7 @@ var _ = Describe("AggregatesController", func() {
})

Context("when terminating", func() {
BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
// Remove the onboarding condition
Expand All @@ -272,7 +271,7 @@ var _ = Describe("AggregatesController", func() {
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})

It("should neither update Aggregates and nor set status condition", func() {
It("should neither update Aggregates and nor set status condition", func(ctx SpecContext) {
updated := &kvmv1.Hypervisor{}
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Aggregates).To(BeEmpty())
Expand Down
6 changes: 2 additions & 4 deletions internal/controller/decomission_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ limitations under the License.
package controller

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -84,7 +82,7 @@ var _ = Describe("Decommission Controller", func() {
})
})

AfterEach(func(ctx context.Context) {
AfterEach(func(ctx SpecContext) {
node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName.Name}}
By("Cleanup the specific node and hypervisor resource")
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, node))).To(Succeed())
Expand All @@ -102,7 +100,7 @@ var _ = Describe("Decommission Controller", func() {
})

Context("When reconciling a node", func() {
It("should set the finalizer", func(ctx context.Context) {
It("should set the finalizer", func(ctx SpecContext) {
By("reconciling the created resource")
_, err := r.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
Expand Down
25 changes: 11 additions & 14 deletions internal/controller/eviction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
package controller

import (
"context"
"fmt"
"net/http"

Expand Down Expand Up @@ -97,14 +96,12 @@ var _ = Describe("Eviction Controller", func() {
fakeServer testhelper.FakeServer
)

ctx := context.Background() //nolint:govet

BeforeEach(func() {
By("Setting up the OpenStack http mock server")
fakeServer = testhelper.SetupHTTP()
})

AfterEach(func() {
AfterEach(func(ctx SpecContext) {
resource := &kvmv1.Eviction{}
err := k8sClient.Get(ctx, typeNamespacedName, resource)
if err != nil {
Expand Down Expand Up @@ -133,7 +130,7 @@ var _ = Describe("Eviction Controller", func() {

Describe("API validation", func() {
When("creating an eviction without hypervisor", func() {
It("it should fail creating the resource", func() {
It("it should fail creating the resource", func(ctx SpecContext) {
resource := &kvmv1.Eviction{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Expand All @@ -149,7 +146,7 @@ var _ = Describe("Eviction Controller", func() {
})

When("creating an eviction without reason", func() {
It("it should fail creating the resource", func() {
It("it should fail creating the resource", func(ctx SpecContext) {
resource := &kvmv1.Eviction{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Expand All @@ -165,7 +162,7 @@ var _ = Describe("Eviction Controller", func() {
})

When("creating an eviction with reason and hypervisor", func() {
BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
By("creating the hypervisor resource")
hypervisor := &kvmv1.Hypervisor{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -174,7 +171,7 @@ var _ = Describe("Eviction Controller", func() {
}
Expect(ctrlRuntimeClient.IgnoreAlreadyExists(k8sClient.Create(ctx, hypervisor))).To(Succeed())
})
It("should successfully create the resource", func() {
It("should successfully create the resource", func(ctx SpecContext) {
resource := &kvmv1.Eviction{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Expand All @@ -193,7 +190,7 @@ var _ = Describe("Eviction Controller", func() {

Describe("Reconciliation", func() {
Describe("an eviction for 'test-hypervisor'", func() {
BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
By("Creating the resource")
resource := &kvmv1.Eviction{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -224,7 +221,7 @@ var _ = Describe("Eviction Controller", func() {
})
})

It("should fail reconciliation", func() {
It("should fail reconciliation", func(ctx SpecContext) {
for range 3 {
_, err := controllerReconciler.Reconcile(ctx, reconcileRequest)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -247,7 +244,7 @@ var _ = Describe("Eviction Controller", func() {

})
When("enabled hypervisor has no servers", func() {
BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand All @@ -266,7 +263,7 @@ var _ = Describe("Eviction Controller", func() {
}
Expect(ctrlRuntimeClient.IgnoreAlreadyExists(k8sClient.Create(ctx, hypervisor))).To(Succeed())
})
It("should succeed the reconciliation", func() {
It("should succeed the reconciliation", func(ctx SpecContext) {
runningCond := &metav1.Condition{
Type: kvmv1.ConditionTypeEvicting,
Status: metav1.ConditionTrue,
Expand Down Expand Up @@ -344,7 +341,7 @@ var _ = Describe("Eviction Controller", func() {
})
})
When("disabled hypervisor has no servers", func() {
BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand All @@ -362,7 +359,7 @@ var _ = Describe("Eviction Controller", func() {
}
Expect(ctrlRuntimeClient.IgnoreAlreadyExists(k8sClient.Create(ctx, hypervisor))).To(Succeed())
})
It("should succeed the reconciliation", func() {
It("should succeed the reconciliation", func(ctx SpecContext) {
for range 3 {
_, err := controllerReconciler.Reconcile(ctx, reconcileRequest)
Expect(err).NotTo(HaveOccurred())
Expand Down
13 changes: 5 additions & 8 deletions internal/controller/gardener_node_lifecycle_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var _ = Describe("Gardener Maintenance Controller", func() {
const nodeName = "node-test"
var controller *GardenerNodeLifecycleController

BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
controller = &GardenerNodeLifecycleController{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Expand All @@ -49,16 +49,13 @@ var _ = Describe("Gardener Maintenance Controller", func() {
},
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
})

AfterEach(func() {
node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}}
By("Cleanup the specific node")
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, node))).To(Succeed())
DeferCleanup(func(ctx SpecContext) {
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, resource))).To(Succeed())
})
})

Context("When reconciling a node", func() {
It("should successfully reconcile the resource", func() {
It("should successfully reconcile the resource", func(ctx SpecContext) {
req := ctrl.Request{
NamespacedName: types.NamespacedName{Name: nodeName},
}
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/hypervisor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func (hv *HypervisorController) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, fmt.Errorf("failed setting controller reference: %w", err)
}

if IsNodeConditionPresentAndEqual(node.Status.Conditions, "Terminating", corev1.ConditionTrue) {
hypervisor.Spec.Maintenance = kvmv1.MaintenanceTermination
}

if err := hv.Create(ctx, hypervisor, k8sclient.FieldOwner(HypervisorControllerName)); err != nil {
return ctrl.Result{}, err
}
Expand Down
Loading