Skip to content

Commit

Permalink
Merge pull request #441 from manuelbuil/updateK8sdeps
Browse files Browse the repository at this point in the history
Update k8s deps
  • Loading branch information
dougbtv authored Apr 11, 2024
2 parents 209cfb0 + cd53175 commit 5c9fc45
Show file tree
Hide file tree
Showing 1,263 changed files with 122,383 additions and 49,997 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:

- name: Generate code
if: steps.changed-files.outputs.any_changed == 'true'
run: ./hack/generate-code.sh && hack/verify-codegen.sh
run: chmod +x vendor/k8s.io/code-generator/generate-internal-groups.sh && ./hack/generate-code.sh && hack/verify-codegen.sh

- name: Run go fmt
if: steps.changed-files.outputs.any_changed == 'true'
Expand Down
10 changes: 5 additions & 5 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 {
return func() (bool, error) {
ipPool, err := k8sIPAM.GetIPPool(context.Background(), kubeClient.PoolIdentifier{IpRange: ipPoolCIDR, NetworkName: kubeClient.UnnamedNetwork})
func isIPPoolAllocationsEmpty(ctx context.Context, k8sIPAM *kubeClient.KubernetesIPAM, ipPoolCIDR string) wait.ConditionWithContextFunc {
return func(context.Context) (bool, error) {
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, true, isIPPoolAllocationsEmpty(ctx, k8sIPAM, ipPoolCIDR))
}
30 changes: 15 additions & 15 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, true, 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, true, 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 {
return func() (bool, error) {
pod, err := cs.CoreV1().Pods(namespace).Get(context.Background(), podName, metav1.GetOptions{})
func isPodRunning(ctx context.Context, cs *kubernetes.Clientset, podName, namespace string) wait.ConditionWithContextFunc {
return func(context.Context) (bool, error) {
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 {
return func() (bool, error) {
pod, err := cs.CoreV1().Pods(namespace).Get(context.Background(), podName, metav1.GetOptions{})
func isPodGone(ctx context.Context, cs *kubernetes.Clientset, podName, namespace string) wait.ConditionWithContextFunc {
return func(context.Context) (bool, error) {
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
22 changes: 11 additions & 11 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, true, 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, true, isReplicaSetGone(ctx, cs, rsName, namespace))
}

func isReplicaSetSteady(cs *kubernetes.Clientset, replicaSetName, namespace, label string) wait.ConditionFunc {
return func() (bool, error) {
podList, err := ListPods(cs, namespace, label)
func isReplicaSetSteady(ctx context.Context, cs *kubernetes.Clientset, replicaSetName, namespace, label string) wait.ConditionWithContextFunc {
return func(context.Context) (bool, error) {
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 {
return func() (bool, error) {
replicaSet, err := cs.AppsV1().ReplicaSets(namespace).Get(context.Background(), rsName, metav1.GetOptions{})
func isReplicaSetGone(ctx context.Context, cs *kubernetes.Clientset, rsName, namespace string) wait.ConditionWithContextFunc {
return func(context.Context) (bool, error) {
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
22 changes: 11 additions & 11 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, true, isStatefulSetGone(ctx, cs, serviceName, namespace, labelSelector))
}

func isStatefulSetGone(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{})
func isStatefulSetGone(ctx context.Context, cs *kubernetes.Clientset, serviceName string, namespace string, labelSelector string) wait.ConditionWithContextFunc {
return func(context.Context) (done bool, err error) {
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, true, doesStatefulsetComplyWithCondition(ctx, cs, serviceName, namespace, expectedReplicas, predicate))
}

func doesStatefulsetComplyWithCondition(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{})
func doesStatefulsetComplyWithCondition(ctx context.Context, cs *kubernetes.Clientset, serviceName string, namespace string, expectedReplicas int, predicate statefulSetPredicate) wait.ConditionWithContextFunc {
return func(context.Context) (bool, error) {
statefulSet, err := cs.AppsV1().StatefulSets(namespace).Get(ctx, serviceName, metav1.GetOptions{})
if err != nil {
return false, err
}
Expand Down
33 changes: 20 additions & 13 deletions e2e/client/whereabouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,19 @@ 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
}

pod, err = c.Client.CoreV1().Pods(pod.Namespace).Get(context.Background(), pod.Name, metav1.GetOptions{})
pod, err = c.Client.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
Expand All @@ -82,32 +83,34 @@ func (c *ClientInfo) ProvisionPod(podName string, namespace string, label, annot
}

func (c *ClientInfo) DeletePod(pod *corev1.Pod) error {
if err := c.Client.CoreV1().Pods(pod.Namespace).Delete(context.TODO(), pod.Name, metav1.DeleteOptions{}); err != nil {
ctx := context.Background()
if err := c.Client.CoreV1().Pods(pod.Namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{}); err != nil {
return err
}

const podDeleteTimeout = 20 * time.Second
if err := WaitForPodToDisappear(c.Client, pod.GetNamespace(), pod.GetName(), podDeleteTimeout); err != nil {
if err := WaitForPodToDisappear(ctx, c.Client, pod.GetNamespace(), pod.GetName(), podDeleteTimeout); err != nil {
return err
}
return nil
}

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,28 +127,31 @@ 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
}

func (c *ClientInfo) ProvisionStatefulSet(statefulSetName string, namespace string, serviceName string, replicas int, networkNames ...string) (*appsv1.StatefulSet, error) {
const statefulSetCreateTimeout = 60 * createTimeout
ctx := context.Background()
statefulSet, err := c.Client.AppsV1().StatefulSets(namespace).Create(
context.TODO(),
ctx,
entities.StatefulSetSpec(statefulSetName, namespace, serviceName, replicas, entities.PodNetworkSelectionElements(networkNames...)),
metav1.CreateOptions{})
if err != nil {
return nil, err
}

if err := WaitForStatefulSetCondition(
ctx,
c.Client,
namespace,
serviceName,
Expand All @@ -159,13 +165,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
Loading

0 comments on commit 5c9fc45

Please sign in to comment.