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 11, 2023
1 parent 84b11d5 commit 04bf0fe
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions e2e/operator_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ 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 {
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 @@ -471,7 +471,7 @@ func (tctx *OperatorContext) subtest(f func(context.Context, *OperatorContext))
childCtx.T = t
childCtx.kClient = NewLabelWriterClient(tctx.kClient.Base(), childCtx.getSubTestLabels())
t.Cleanup(func() {
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 @@ -583,16 +583,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 @@ -615,6 +618,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 04bf0fe

Please sign in to comment.