Skip to content

Commit

Permalink
Update k8s deps
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Buil <[email protected]>
  • Loading branch information
manuelbuil committed Mar 15, 2024
1 parent 209cfb0 commit 1feacac
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 438 deletions.
8 changes: 4 additions & 4 deletions e2e/client/ippool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
)

func isIPPoolAllocationsEmpty(k8sIPAM *kubeClient.KubernetesIPAM, ipPoolCIDR string) wait.ConditionFunc {
func isIPPoolAllocationsEmpty(ctx context.Context, k8sIPAM *kubeClient.KubernetesIPAM, ipPoolCIDR string) wait.ConditionFunc {
return func() (bool, error) {
ipPool, err := k8sIPAM.GetIPPool(context.Background(), kubeClient.PoolIdentifier{IpRange: ipPoolCIDR, NetworkName: kubeClient.UnnamedNetwork})
ipPool, err := k8sIPAM.GetIPPool(ctx, kubeClient.PoolIdentifier{IpRange: ipPoolCIDR, NetworkName: kubeClient.UnnamedNetwork})
noPoolError := fmt.Errorf("k8s pool initialized")
if errors.Is(err, noPoolError) {
return true, nil
Expand All @@ -33,6 +33,6 @@ func isIPPoolAllocationsEmpty(k8sIPAM *kubeClient.KubernetesIPAM, ipPoolCIDR str

// WaitForZeroIPPoolAllocations polls up to timeout seconds for IP pool allocations to be gone from the Kubernetes cluster.
// Returns an error if any IP pool allocations remain after time limit, or if GETing IP pools causes an error.
func WaitForZeroIPPoolAllocations(k8sIPAM *kubeClient.KubernetesIPAM, ipPoolCIDR string, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, isIPPoolAllocationsEmpty(k8sIPAM, ipPoolCIDR))
func WaitForZeroIPPoolAllocations(ctx context.Context, k8sIPAM *kubeClient.KubernetesIPAM, ipPoolCIDR string, timeout time.Duration) error {
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, isIPPoolAllocationsEmpty(ctx, k8sIPAM, ipPoolCIDR))
}
26 changes: 13 additions & 13 deletions e2e/client/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ import (

// WaitForPodReady polls up to timeout seconds for pod to enter steady state (running or succeeded state).
// Returns an error if the pod never enters a steady state.
func WaitForPodReady(cs *kubernetes.Clientset, namespace, podName string, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, isPodRunning(cs, podName, namespace))
func WaitForPodReady(ctx context.Context, cs *kubernetes.Clientset, namespace, podName string, timeout time.Duration) error {
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, isPodRunning(ctx, cs, podName, namespace))
}

// WaitForPodToDisappear polls up to timeout seconds for pod to be gone from the Kubernetes cluster.
// Returns an error if the pod is never deleted, or if GETing it returns an error other than `NotFound`.
func WaitForPodToDisappear(cs *kubernetes.Clientset, namespace, podName string, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, isPodGone(cs, podName, namespace))
func WaitForPodToDisappear(ctx context.Context, cs *kubernetes.Clientset, namespace, podName string, timeout time.Duration) error {
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, isPodGone(ctx, cs, podName, namespace))
}

// WaitForPodBySelector waits up to timeout seconds for all pods in 'namespace' with given 'selector' to enter provided state
// If no pods are found, return nil.
func WaitForPodBySelector(cs *kubernetes.Clientset, namespace, selector string, timeout time.Duration) error {
podList, err := ListPods(cs, namespace, selector)
func WaitForPodBySelector(ctx context.Context, cs *kubernetes.Clientset, namespace, selector string, timeout time.Duration) error {
podList, err := ListPods(ctx, cs, namespace, selector)
if err != nil {
return err
}
Expand All @@ -38,27 +38,27 @@ func WaitForPodBySelector(cs *kubernetes.Clientset, namespace, selector string,
}

for _, pod := range podList.Items {
if err := WaitForPodReady(cs, namespace, pod.Name, timeout); err != nil {
if err := WaitForPodReady(ctx, cs, namespace, pod.Name, timeout); err != nil {
return err
}
}
return nil
}

// ListPods returns the list of currently scheduled or running pods in `namespace` with the given selector
func ListPods(cs *kubernetes.Clientset, namespace, selector string) (*corev1.PodList, error) {
func ListPods(ctx context.Context, cs *kubernetes.Clientset, namespace, selector string) (*corev1.PodList, error) {
listOptions := metav1.ListOptions{LabelSelector: selector}
podList, err := cs.CoreV1().Pods(namespace).List(context.Background(), listOptions)
podList, err := cs.CoreV1().Pods(namespace).List(ctx, listOptions)

if err != nil {
return nil, err
}
return podList, nil
}

func isPodRunning(cs *kubernetes.Clientset, podName, namespace string) wait.ConditionFunc {
func isPodRunning(ctx context.Context, cs *kubernetes.Clientset, podName, namespace string) wait.ConditionFunc {
return func() (bool, error) {
pod, err := cs.CoreV1().Pods(namespace).Get(context.Background(), podName, metav1.GetOptions{})
pod, err := cs.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return false, err
}
Expand All @@ -76,9 +76,9 @@ func isPodRunning(cs *kubernetes.Clientset, podName, namespace string) wait.Cond
}
}

func isPodGone(cs *kubernetes.Clientset, podName, namespace string) wait.ConditionFunc {
func isPodGone(ctx context.Context, cs *kubernetes.Clientset, podName, namespace string) wait.ConditionFunc {
return func() (bool, error) {
pod, err := cs.CoreV1().Pods(namespace).Get(context.Background(), podName, metav1.GetOptions{})
pod, err := cs.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{})
if err != nil && k8serrors.IsNotFound(err) {
return true, nil
} else if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions e2e/client/replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ import (

// WaitForReplicaSetSteadyState only plays nice with the replicaSet it's being used with.
// Any pods that might be up still from a previous test may cause unexpected results.
func WaitForReplicaSetSteadyState(cs *kubernetes.Clientset, namespace, label string, replicaSet *appsv1.ReplicaSet, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, isReplicaSetSteady(cs, replicaSet.Name, namespace, label))
func WaitForReplicaSetSteadyState(ctx context.Context, cs *kubernetes.Clientset, namespace, label string, replicaSet *appsv1.ReplicaSet, timeout time.Duration) error {
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, isReplicaSetSteady(ctx, cs, replicaSet.Name, namespace, label))
}

// WaitForReplicaSetToDisappear polls up to timeout seconds for replicaset to be gone from the Kubernetes cluster.
// Returns an error if the replicaset is never deleted, or if GETing it returns an error other than `NotFound`.
func WaitForReplicaSetToDisappear(cs *kubernetes.Clientset, namespace, rsName string, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, isReplicaSetGone(cs, rsName, namespace))
func WaitForReplicaSetToDisappear(ctx context.Context, cs *kubernetes.Clientset, namespace, rsName string, timeout time.Duration) error {
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, isReplicaSetGone(ctx, cs, rsName, namespace))
}

func isReplicaSetSteady(cs *kubernetes.Clientset, replicaSetName, namespace, label string) wait.ConditionFunc {
func isReplicaSetSteady(ctx context.Context, cs *kubernetes.Clientset, replicaSetName, namespace, label string) wait.ConditionFunc {
return func() (bool, error) {
podList, err := ListPods(cs, namespace, label)
podList, err := ListPods(ctx, cs, namespace, label)
if err != nil {
return false, err
}

replicaSet, err := cs.AppsV1().ReplicaSets(namespace).Get(context.Background(), replicaSetName, metav1.GetOptions{})
replicaSet, err := cs.AppsV1().ReplicaSets(namespace).Get(ctx, replicaSetName, metav1.GetOptions{})
if err != nil {
return false, err
}
Expand All @@ -53,9 +53,9 @@ func isReplicaSetSynchronized(replicaSet *appsv1.ReplicaSet, podList *corev1.Pod
return replicaSet.Status.ReadyReplicas == (*replicaSet.Spec.Replicas) && int32(len(podList.Items)) == (*replicaSet.Spec.Replicas)
}

func isReplicaSetGone(cs *kubernetes.Clientset, rsName, namespace string) wait.ConditionFunc {
func isReplicaSetGone(ctx context.Context, cs *kubernetes.Clientset, rsName, namespace string) wait.ConditionFunc {
return func() (bool, error) {
replicaSet, err := cs.AppsV1().ReplicaSets(namespace).Get(context.Background(), rsName, metav1.GetOptions{})
replicaSet, err := cs.AppsV1().ReplicaSets(namespace).Get(ctx, rsName, metav1.GetOptions{})
if err != nil && k8serrors.IsNotFound(err) {
return true, nil
} else if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions e2e/client/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ import (
)

// WaitForStatefulSetGone ...
func WaitForStatefulSetGone(cs *kubernetes.Clientset, namespace, serviceName string, labelSelector string, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, isStatefulSetGone(cs, serviceName, namespace, labelSelector))
func WaitForStatefulSetGone(ctx context.Context, cs *kubernetes.Clientset, namespace, serviceName string, labelSelector string, timeout time.Duration) error {
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, isStatefulSetGone(ctx, cs, serviceName, namespace, labelSelector))
}

func isStatefulSetGone(cs *kubernetes.Clientset, serviceName string, namespace string, labelSelector string) wait.ConditionFunc {
func isStatefulSetGone(ctx context.Context, cs *kubernetes.Clientset, serviceName string, namespace string, labelSelector string) wait.ConditionFunc {
return func() (done bool, err error) {
statefulSet, err := cs.AppsV1().StatefulSets(namespace).Get(context.Background(), serviceName, metav1.GetOptions{})
statefulSet, err := cs.AppsV1().StatefulSets(namespace).Get(ctx, serviceName, metav1.GetOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return false, fmt.Errorf("something weird happened with the stateful set whose status is: [%s]. Errors: %w", statefulSet.Status.String(), err)
}

associatedPods, err := cs.CoreV1().Pods(namespace).List(context.TODO(), selectViaLabels(labelSelector))
associatedPods, err := cs.CoreV1().Pods(namespace).List(ctx, selectViaLabels(labelSelector))
if err != nil {
return false, err
}
Expand All @@ -46,13 +46,13 @@ func areAssociatedPodsGone(pods *corev1.PodList) bool {
return len(pods.Items) == 0
}

func WaitForStatefulSetCondition(cs *kubernetes.Clientset, namespace, serviceName string, expectedReplicas int, timeout time.Duration, predicate statefulSetPredicate) error {
return wait.PollImmediate(time.Second, timeout, doesStatefulsetComplyWithCondition(cs, serviceName, namespace, expectedReplicas, predicate))
func WaitForStatefulSetCondition(ctx context.Context, cs *kubernetes.Clientset, namespace, serviceName string, expectedReplicas int, timeout time.Duration, predicate statefulSetPredicate) error {
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, doesStatefulsetComplyWithCondition(ctx, cs, serviceName, namespace, expectedReplicas, predicate))
}

func doesStatefulsetComplyWithCondition(cs *kubernetes.Clientset, serviceName string, namespace string, expectedReplicas int, predicate statefulSetPredicate) wait.ConditionFunc {
func doesStatefulsetComplyWithCondition(ctx context.Context, cs *kubernetes.Clientset, serviceName string, namespace string, expectedReplicas int, predicate statefulSetPredicate) wait.ConditionFunc {
return func() (bool, error) {
statefulSet, err := cs.AppsV1().StatefulSets(namespace).Get(context.Background(), serviceName, metav1.GetOptions{})
statefulSet, err := cs.AppsV1().StatefulSets(namespace).Get(ctx, serviceName, metav1.GetOptions{})
if err != nil {
return false, err
}
Expand Down
22 changes: 13 additions & 9 deletions e2e/client/whereabouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ func (c *ClientInfo) DelNetAttachDef(netattach *nettypes.NetworkAttachmentDefini
}

func (c *ClientInfo) ProvisionPod(podName string, namespace string, label, annotations map[string]string) (*corev1.Pod, error) {
ctx := context.Background()
pod := entities.PodObject(podName, namespace, label, annotations)
pod, err := c.Client.CoreV1().Pods(pod.Namespace).Create(context.Background(), pod, metav1.CreateOptions{})
pod, err := c.Client.CoreV1().Pods(pod.Namespace).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
return nil, err
}

const podCreateTimeout = 10 * time.Second
if err := WaitForPodReady(c.Client, pod.Namespace, pod.Name, podCreateTimeout); err != nil {
if err := WaitForPodReady(ctx, c.Client, pod.Namespace, pod.Name, podCreateTimeout); err != nil {
return nil, err
}

Expand All @@ -94,20 +95,21 @@ func (c *ClientInfo) DeletePod(pod *corev1.Pod) error {
}

func (c *ClientInfo) ProvisionReplicaSet(rsName string, namespace string, replicaCount int32, labels, annotations map[string]string) (*appsv1.ReplicaSet, error) {
ctx := context.Background()
replicaSet, err := c.Client.AppsV1().ReplicaSets(namespace).Create(
context.Background(),
ctx,
entities.ReplicaSetObject(replicaCount, rsName, namespace, labels, annotations),
metav1.CreateOptions{})
if err != nil {
return nil, err
}

const rsCreateTimeout = 600 * time.Second
if err := WaitForPodBySelector(c.Client, namespace, entities.ReplicaSetQuery(rsName), rsCreateTimeout); err != nil {
if err := WaitForPodBySelector(ctx, c.Client, namespace, entities.ReplicaSetQuery(rsName), rsCreateTimeout); err != nil {
return nil, err
}

replicaSet, err = c.Client.AppsV1().ReplicaSets(namespace).Get(context.Background(), replicaSet.Name, metav1.GetOptions{})
replicaSet, err = c.Client.AppsV1().ReplicaSets(namespace).Get(ctx, replicaSet.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
Expand All @@ -124,12 +126,13 @@ func (c *ClientInfo) UpdateReplicaSet(replicaSet *appsv1.ReplicaSet) (*appsv1.Re
}

func (c *ClientInfo) DeleteReplicaSet(replicaSet *appsv1.ReplicaSet) error {
ctx := context.Background()
const rsDeleteTimeout = 2 * rsCreateTimeout
if err := c.Client.AppsV1().ReplicaSets(replicaSet.GetNamespace()).Delete(context.Background(), replicaSet.Name, metav1.DeleteOptions{}); err != nil {
if err := c.Client.AppsV1().ReplicaSets(replicaSet.GetNamespace()).Delete(ctx, replicaSet.Name, metav1.DeleteOptions{}); err != nil {
return err
}

if err := WaitForReplicaSetToDisappear(c.Client, replicaSet.GetNamespace(), replicaSet.GetName(), rsDeleteTimeout); err != nil {
if err := WaitForReplicaSetToDisappear(ctx, c.Client, replicaSet.GetNamespace(), replicaSet.GetName(), rsDeleteTimeout); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -159,13 +162,14 @@ func (c *ClientInfo) ProvisionStatefulSet(statefulSetName string, namespace stri

func (c *ClientInfo) DeleteStatefulSet(namespace string, serviceName string, labelSelector string) error {
const statefulSetDeleteTimeout = 6 * deleteTimeout
ctx := context.Background()

if err := c.Client.AppsV1().StatefulSets(namespace).Delete(
context.TODO(), serviceName, deleteRightNowAndBlockUntilAssociatedPodsAreGone()); err != nil {
ctx, serviceName, deleteRightNowAndBlockUntilAssociatedPodsAreGone()); err != nil {
return err
}

return WaitForStatefulSetGone(c.Client, namespace, serviceName, labelSelector, statefulSetDeleteTimeout)
return WaitForStatefulSetGone(ctx, c.Client, namespace, serviceName, labelSelector, statefulSetDeleteTimeout)
}

func (c *ClientInfo) ScaleStatefulSet(statefulSetName string, namespace string, deltaInstance int) error {
Expand Down
10 changes: 6 additions & 4 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,11 @@ var _ = Describe("Whereabouts functionality", func() {

It("allocates each IP pool entry with a unique pod IP", func() {
By("creating max number of pods and checking IP Pool validity")
ctx := context.Backgroud()
for i := 0; i < testConfig.NumberOfIterations; i++ {
Expect(
checkZeroIPPoolAllocationsAndReplicas(
clientInfo, k8sIPAM, rsName, testNamespace, ipPoolCIDR, testNetworkName)).To(Succeed())
ctx, clientInfo, k8sIPAM, rsName, testNamespace, ipPoolCIDR, testNetworkName)).To(Succeed())

allPods, err := clientInfo.Client.CoreV1().Pods(core.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -273,6 +274,7 @@ var _ = Describe("Whereabouts functionality", func() {
Expect(err).NotTo(HaveOccurred())
Expect(
wbtestclient.WaitForReplicaSetSteadyState(
ctx,
clientInfo.Client,
testNamespace,
entities.ReplicaSetQuery(rsName),
Expand Down Expand Up @@ -699,7 +701,7 @@ func podTierLabel(podTier string) map[string]string {
}

// Waits for all replicas to be fully removed from replicaset, and checks that there are 0 ip pool allocations
func checkZeroIPPoolAllocationsAndReplicas(clientInfo *wbtestclient.ClientInfo, k8sIPAM *wbstorage.KubernetesIPAM, rsName, namespace string, ipPoolCIDR string, networkNames ...string) error {
func checkZeroIPPoolAllocationsAndReplicas(ctx context.Context, clientInfo *wbtestclient.ClientInfo, k8sIPAM *wbstorage.KubernetesIPAM, rsName, namespace string, ipPoolCIDR string, networkNames ...string) error {
const (
emptyReplicaSet = 0
rsSteadyTimeout = 1200 * time.Second
Expand All @@ -720,11 +722,11 @@ func checkZeroIPPoolAllocationsAndReplicas(clientInfo *wbtestclient.ClientInfo,
}

matchingLabel := entities.ReplicaSetQuery(rsName)
if err = wbtestclient.WaitForReplicaSetSteadyState(clientInfo.Client, namespace, matchingLabel, replicaSet, rsSteadyTimeout); err != nil {
if err = wbtestclient.WaitForReplicaSetSteadyState(ctx, clientInfo.Client, namespace, matchingLabel, replicaSet, rsSteadyTimeout); err != nil {
return err
}

if err = wbtestclient.WaitForZeroIPPoolAllocations(k8sIPAM, ipPoolCIDR, zeroIPPoolTimeout); err != nil {
if err = wbtestclient.WaitForZeroIPPoolAllocations(ctx, k8sIPAM, ipPoolCIDR, zeroIPPoolTimeout); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 1feacac

Please sign in to comment.