Skip to content

Commit

Permalink
Faster e2e cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSpiritXIII committed Oct 18, 2023
1 parent d8983cf commit befa4fc
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions e2e/operator_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestMain(m *testing.M) {
}

if cleanup {
fmt.Fprintln(os.Stderr, "cleaning resources before tests...", err)
fmt.Fprintln(os.Stdout, "cleaning resources before tests...")
if err := cleanupResources(context.Background(), kubeconfig, c, ""); err != nil {
fmt.Fprintln(os.Stderr, "cleaning up failed:", err)
os.Exit(1)
Expand Down Expand Up @@ -235,8 +235,10 @@ func newOperatorContext(t *testing.T) *OperatorContext {
}
tctx.kClient = NewLabelWriterClient(c, tctx.getSubTestLabels())
t.Cleanup(func() {
if err := cleanupResources(ctx, kubeconfig, tctx.Client(), tctx.getSubTestLabelValue()); err != nil {
t.Fatalf("unable to cleanup resources: %s", err)
if !leakResources {
if err := cleanupResourcesInNamespaces(ctx, kubeconfig, tctx.Client(), []string{namespace, pubNamespace}, tctx.getSubTestLabelValue()); err != nil {
t.Fatalf("unable to cleanup resources: %s", err)
}
}
cancel()
})
Expand Down Expand Up @@ -500,7 +502,7 @@ func (tctx *OperatorContext) subtest(f func(context.Context, *OperatorContext))
return
}
t.Log("cleaning up resources...")
if err := cleanupResources(ctx, kubeconfig, childCtx.Client(), childCtx.getSubTestLabelValue()); err != nil {
if err := cleanupResourcesInNamespaces(ctx, kubeconfig, childCtx.Client(), []string{tctx.namespace, tctx.pubNamespace}, childCtx.getSubTestLabelValue()); err != nil {
t.Fatalf("unable to cleanup resources: %s", err)
}
})
Expand Down Expand Up @@ -612,16 +614,19 @@ func cleanupResource(ctx context.Context, kubeClient client.Client, gvk schema.G
// cleanupResources cleans all resources created by tests. If no label value is provided, then all
// resources with the label are removed.
func cleanupResources(ctx context.Context, restConfig *rest.Config, kubeClient client.Client, labelValue string) error {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(restConfig)
namespaces, err := getNamespaces(ctx, kubeClient)
if err != nil {
return err
}
gvks, err := getGroupVersionKinds(discoveryClient)
return cleanupResourcesInNamespaces(ctx, restConfig, kubeClient, namespaces, labelValue)
}

func cleanupResourcesInNamespaces(ctx context.Context, restConfig *rest.Config, kubeClient client.Client, namespaces []string, labelValue string) error {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(restConfig)
if err != nil {
return err
}

namespaces, err := getNamespaces(ctx, kubeClient)
gvks, err := getGroupVersionKinds(discoveryClient)
if err != nil {
return err
}
Expand All @@ -644,6 +649,10 @@ func cleanupResources(ctx context.Context, restConfig *rest.Config, kubeClient c
return err
}
if namespaced {
if labelValue == "" {
// Skip because deleting the namespace will delete the resource.
continue
}
for _, namespace := range namespaces {
if err := cleanupResource(ctx, kubeClient, gvk, labelValue, namespace); err != nil {
errs = append(errs, err)
Expand Down

0 comments on commit befa4fc

Please sign in to comment.