Repository navigation
fix(e2e): handle Terminating namespace in CreateNamespace #2523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: oadp-dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,38 +33,30 @@ func CreateNamespace(clientset *kubernetes.Clientset, namespace string) error { | |
| ns := corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}} | ||
| _, err := clientset.CoreV1().Namespaces().Create(context.Background(), &ns, metav1.CreateOptions{}) | ||
| if apierrors.IsAlreadyExists(err) { | ||
| // The namespace already exists — but it may be Terminating, which | ||
| // would cause subsequent resource creation to fail with "unable to | ||
| // create new content in namespace ... because it is being terminated". | ||
| // Check its phase and, if Terminating, wait for full deletion then | ||
| // re-create. If the namespace disappears between the Create above | ||
| // and this Get (NotFound), just retry the Create directly. | ||
| existing, getErr := clientset.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{}) | ||
| existingNs, getErr := clientset.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{}) | ||
| if apierrors.IsNotFound(getErr) { | ||
| // Namespace was deleted between Create and Get; create it now. | ||
| // Namespace finished deleting between Create and Get; retry Create. | ||
| _, err = clientset.CoreV1().Namespaces().Create(context.Background(), &ns, metav1.CreateOptions{}) | ||
| return err | ||
| } | ||
| if getErr != nil { | ||
| return fmt.Errorf("failed to get existing namespace %s: %w", namespace, getErr) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| if existing.Status.Phase == corev1.NamespaceTerminating { | ||
| log.Printf("Namespace %s is Terminating, waiting for deletion before re-creating", namespace) | ||
| pollErr := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 3*time.Minute, true, func(ctx context.Context) (bool, error) { | ||
| _, err := clientset.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}) | ||
| if apierrors.IsNotFound(err) { | ||
| if existingNs.Status.Phase == corev1.NamespaceTerminating { | ||
| log.Printf("Namespace %s is terminating, waiting for deletion before re-creating", namespace) | ||
| waitErr := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 2*time.Minute, true, func(ctx context.Context) (bool, error) { | ||
| _, getErr := clientset.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if apierrors.IsNotFound(getErr) { | ||
| return true, nil | ||
| } | ||
| return false, err | ||
| return false, getErr | ||
| }) | ||
| if pollErr != nil { | ||
| return fmt.Errorf("timed out waiting for Terminating namespace %s to be deleted: %w", namespace, pollErr) | ||
| if waitErr != nil { | ||
| return fmt.Errorf("timed out waiting for terminating namespace %s to be deleted: %w", namespace, waitErr) | ||
| } | ||
| // Namespace is gone; create it fresh. | ||
| _, err = clientset.CoreV1().Namespaces().Create(context.Background(), &ns, metav1.CreateOptions{}) | ||
| return err | ||
| } | ||
| // Namespace exists and is Active — nothing to do. | ||
| return nil | ||
| } | ||
| return err | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bound the requests outside the poll.
If the client has no request timeout and the API server stops responding, these requests can block indefinitely. The poll’s two-minute timeout does not cover them. Use a bounded context for the GET and both retry creates. (raw.githubusercontent.com)
As per path instructions: “context.Context for cancellation and timeouts.”
Also applies to: 39-39, 57-57
Source: Path instructions