-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add watchers for NodeDisruptionBudget
- Loading branch information
Showing
3 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
internal/controller/nodedisruptionbudget_controller_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
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 | ||
Unless required applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
// +kubebuilder:docs-gen:collapse=Apache License | ||
|
||
package controller | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
nodedisruptionv1alpha1 "github.com/criteo/node-disruption-controller/api/v1alpha1" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
// +kubebuilder:docs-gen:collapse=Imports | ||
var _ = Describe("NodeDisruptionBudget controller", func() { | ||
|
||
// Define utility constants for object names and testing timeouts/durations and intervals. | ||
const ( | ||
NDBname = "test-ndb" | ||
NDBNamespace = "test-ndb" | ||
|
||
timeout = time.Second * 10 | ||
duration = time.Second * 10 | ||
interval = time.Millisecond * 250 | ||
) | ||
|
||
var ( | ||
_, cancelFn = context.WithCancel(context.Background()) | ||
) | ||
|
||
Context("In a cluster with several nodes", func() { | ||
ctx := context.Background() | ||
|
||
Context("With reconciler with default config", Ordered, func() { | ||
BeforeAll(func() { | ||
cancelFn = startReconcilerWithConfig(NodeDisruptionReconcilerConfig{ | ||
RejectEmptyNodeDisruption: false, | ||
RetryInterval: time.Second * 1, | ||
}) | ||
}) | ||
|
||
AfterAll(func() { | ||
cancelFn() | ||
}) | ||
|
||
AfterEach(func() { | ||
clearAllNodeDisruptionRessources() | ||
}) | ||
|
||
When("there are no budgets in the cluster", func() { | ||
It("it updates the NDB", func() { | ||
By("creating a budget that accepts one disruption") | ||
ndb := &nodedisruptionv1alpha1.NodeDisruptionBudget{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "nodedisruption.criteo.com/v1alpha1", | ||
Kind: "NodeDisruptionBudget", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: NDBname, | ||
Namespace: NDBNamespace, | ||
}, | ||
Spec: nodedisruptionv1alpha1.NodeDisruptionBudgetSpec{ | ||
NodeSelector: metav1.LabelSelector{MatchLabels: nodeLabels1}, | ||
MaxDisruptedNodes: 1, | ||
}, | ||
} | ||
Expect(k8sClient.Create(ctx, ndb)).Should(Succeed()) | ||
|
||
By("checking the NodeDisruptionBudget in synchronized") | ||
NDBLookupKey := types.NamespacedName{Name: NDBname, Namespace: NDBNamespace} | ||
createdNDB := &nodedisruptionv1alpha1.NodeDisruptionBudget{} | ||
Eventually(func() []string { | ||
err := k8sClient.Get(ctx, NDBLookupKey, createdNDB) | ||
Expect(err).Should(Succeed()) | ||
return createdNDB.Status.WatchedNodes | ||
}, timeout, interval).Should(Equal([]string{"node1", "node2"})) | ||
|
||
By("Adding Node") | ||
labels := map[string]string{ | ||
"testselect": "test1", | ||
"kubernetes.io/hostname": "node4", | ||
} | ||
node4 := &corev1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node4", | ||
Labels: labels, | ||
}, | ||
} | ||
Expect(k8sClient.Create(ctx, node4)).Should(Succeed()) | ||
|
||
By("checking the NodeDisruptionBudget updated the status") | ||
Eventually(func() []string { | ||
err := k8sClient.Get(ctx, NDBLookupKey, createdNDB) | ||
Expect(err).Should(Succeed()) | ||
return createdNDB.Status.WatchedNodes | ||
}, timeout, interval).Should(Equal([]string{"node1", "node2", "node4"})) | ||
|
||
By("Removing Node") | ||
Expect(k8sClient.Delete(ctx, node4)).Should(Succeed()) | ||
|
||
By("checking the NodeDisruptionBudget updated the status") | ||
Eventually(func() []string { | ||
err := k8sClient.Get(ctx, NDBLookupKey, createdNDB) | ||
Expect(err).Should(Succeed()) | ||
return createdNDB.Status.WatchedNodes | ||
}, timeout, interval).Should(Equal([]string{"node1", "node2"})) | ||
}) | ||
}) | ||
|
||
}) | ||
|
||
}) | ||
}) |