Skip to content

Commit

Permalink
Kubernetes benchmarks: Wait for namespace cleanup before proceeding.
Browse files Browse the repository at this point in the history
Prior to this change, the namespace cleanup process was "best-effort" and
its result was ignored. But deletion is asynchronous and can fail. So the
new implementation checks whether the `benchmark` namespace exists and, if
it does, deletes it and waits for its full deletion to be complete.

PiperOrigin-RevId: 708113377
  • Loading branch information
EtiennePerot authored and gvisor-bot committed Dec 20, 2024
1 parent e6e1ecb commit 0fb4cc7
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions test/kubernetes/testcluster/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"reflect"
"strings"
"time"

cspb "google.golang.org/genproto/googleapis/container/v1"
Expand Down Expand Up @@ -56,7 +57,9 @@ func (t *TestCluster) Namespace(namespace string) *Namespace {
// This should be used in the beginning of tests, such that the namespace
// is empty and ready to be used.
func (n *Namespace) Reset(ctx context.Context) error {
n.Cleanup(ctx)
if err := n.Cleanup(ctx); err != nil {
return fmt.Errorf("failed to clean up namespace %q: %w", n.Namespace, err)
}
_, err := n.testCluster.createNamespace(ctx, &v13.Namespace{
TypeMeta: v1.TypeMeta{
Kind: "namespace",
Expand All @@ -71,7 +74,42 @@ func (n *Namespace) Reset(ctx context.Context) error {

// Cleanup deletes this namespace if it exists.
func (n *Namespace) Cleanup(ctx context.Context) error {
return n.testCluster.deleteNamespace(ctx, n.Namespace)
namespaceExists := func() (*v13.Namespace, bool, error) {
ns, err := n.testCluster.getNamespace(ctx, n.Namespace)
switch {
case err == nil:
return ns, true, nil
case strings.Contains(err.Error(), "not found"):
return nil, false, nil
default:
return nil, false, err
}
}
_, exists, err := namespaceExists()
if err != nil {
return fmt.Errorf("failed to check if namespace %q exists: %w", n.Namespace, err)
}
if !exists {
return nil
}
if err := n.testCluster.deleteNamespace(ctx, n.Namespace); err != nil && !strings.Contains(err.Error(), "object is being deleted") {
return fmt.Errorf("failed to delete namespace %q: %w", n.Namespace, err)
}
var ns *v13.Namespace
for ctx.Err() == nil {
ns, exists, err = namespaceExists()
if err != nil {
return fmt.Errorf("failed to check if namespace %q exists: %w", n.Namespace, err)
}
if !exists {
return nil
}
select {
case <-ctx.Done():
case <-time.After(pollInterval):
}
}
return fmt.Errorf("failed to delete namespace %q (context: %w); last namespace status: %v", n.Namespace, ctx.Err(), ns)
}

// NewAlpinePod returns an alpine pod template.
Expand Down

0 comments on commit 0fb4cc7

Please sign in to comment.