Skip to content

Bulk delete namespaces in reconfiguration nfr test #3402

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions tests/framework/resourcemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"io"
"net/http"
"reflect"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -221,6 +222,47 @@ func (rm *ResourceManager) DeleteNamespace(name string) error {
})
}

func (rm *ResourceManager) DeleteNamespaces(names []string) error {
ctx, cancel := context.WithTimeout(context.Background(), rm.TimeoutConfig.DeleteNamespaceTimeout*2)
defer cancel()

var combinedErrors error
ns := &core.Namespace{}
for _, name := range names {
if err := rm.K8sClient.Get(ctx, types.NamespacedName{Name: name}, ns); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the Get call first? Can we just call delete and check for the same IsNotFound error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me see, i know that Get is used to populate ns so that Delete works because Delete needs the populated ns object, however if all it needs is just the name, yea we can remove the Get call and manually make the ns object for delete.

if apierrors.IsNotFound(err) {
continue
}
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("error getting namespace: %w", err))
}

if err := rm.K8sClient.Delete(ctx, ns); err != nil {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("error deleting namespace: %w", err))
}
}

err := wait.PollUntilContextCancel(
ctx,
500*time.Millisecond,
true, /* poll immediately */
func(ctx context.Context) (bool, error) {
nsList := &core.NamespaceList{}
if err := rm.K8sClient.List(ctx, nsList); err != nil {
return false, nil //nolint:nilerr // retry on error
}

for _, namespace := range nsList.Items {
if slices.Contains(names, namespace.Name) {
return false, nil
}
}

return true, nil
})

return errors.Join(combinedErrors, err)
}

// DeleteFromFiles deletes Kubernetes resources defined within the provided YAML files.
func (rm *ResourceManager) DeleteFromFiles(files []string, namespace string) error {
handlerFunc := func(obj unstructured.Unstructured) error {
Expand Down
12 changes: 4 additions & 8 deletions tests/suite/reconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,12 @@ var _ = Describe("Reconfiguration Performance Testing", Ordered, Label("nfr", "r
cleanupResources := func() error {
var err error

// FIXME (bjee19): https://github.com/nginx/nginx-gateway-fabric/issues/2376
// Find a way to bulk delete these namespaces.
for i := 1; i <= maxResourceCount; i++ {
nsName := "namespace" + strconv.Itoa(i)
resultError := resourceManager.DeleteNamespace(nsName)
if resultError != nil {
err = resultError
}
namespaces := make([]string, maxResourceCount)
for i := range maxResourceCount {
namespaces[i] = "namespace" + strconv.Itoa(i+1)
}

err = resourceManager.DeleteNamespaces(namespaces)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
err = resourceManager.DeleteNamespaces(namespaces)
Expect(resourceManager.DeleteNamespaces(namespaces)).To(Succeed())

Or do you want to return this error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a better way to do this, but we do have an Expect clause in the AfterEach on cleanupResources, but if we add it here, and there is any error, it will stop and thus won't run the reconfig delete namespace.

Do you know if there is another way around this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see...I guess let's just leave as-is for now.

Expect(resourceManager.DeleteNamespace(reconfigNamespace.Name)).To(Succeed())

return err
Expand Down
Loading