Skip to content

Commit

Permalink
test(integration): force delete fixed namespaces
Browse files Browse the repository at this point in the history
Namespace deletion can be slow in K8s due to finalizers. This can be a
problem for fixed namespaces which are the same for each test that uses
them. This change tries to improve this by explicitly disabling
finalizers before deleting the namespace.
  • Loading branch information
swiatekm committed Jun 3, 2024
1 parent 9aa0eec commit 3853395
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
8 changes: 4 additions & 4 deletions tests/integration/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func GetAllLogsFeature(waitFunction stepfuncs.WaitForLogs, generate bool) featur
feature = feature.
Teardown(removeLogsDeployment).
Teardown(removeLogsDaemonset).
Teardown(stepfuncs.KubectlDeleteNamespaceOpt(internal.LogsGeneratorNamespace))
Teardown(stepfuncs.KubectlDeleteNamespaceOpt(internal.LogsGeneratorNamespace, true))
}

return feature.Feature()
Expand Down Expand Up @@ -576,7 +576,7 @@ func GetPartialLogsFeature() features.Feature {
)).
Teardown(removeLogsDeployment).
Teardown(removeLogsDaemonset).
Teardown(stepfuncs.KubectlDeleteNamespaceOpt(internal.LogsGeneratorNamespace)).
Teardown(stepfuncs.KubectlDeleteNamespaceOpt(internal.LogsGeneratorNamespace, true)).
Feature()
}

Expand Down Expand Up @@ -847,7 +847,7 @@ func GetCurlAppFeature() features.Feature {
waitDuration,
tickDuration,
)).
Teardown(stepfuncs.KubectlDeleteNamespaceOpt(internal.InstrumentationAppsNamespace)).
Teardown(stepfuncs.KubectlDeleteNamespaceOpt(internal.InstrumentationAppsNamespace, true)).
Feature()
}

Expand Down Expand Up @@ -949,7 +949,7 @@ func GetTracesFeature() features.Feature {
terrak8s.RunKubectl(t, &opts, "delete", "deployment", internal.TracesGeneratorName)
return ctx
}).
Teardown(stepfuncs.KubectlDeleteNamespaceOpt(internal.TracesGeneratorNamespace)).
Teardown(stepfuncs.KubectlDeleteNamespaceOpt(internal.TracesGeneratorNamespace, true)).
Feature()
}

Expand Down
23 changes: 16 additions & 7 deletions tests/integration/internal/stepfuncs/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"

Expand All @@ -19,17 +20,25 @@ import (

// KubectlDeleteNamespaceTestOpt wraps KubectlDeleteNamespaceOpt by extracting the
// namespace saved in the context by KubectlCreateNamespaceTestOpt/KubectlCreateNamespaceOpt.
func KubectlDeleteNamespaceTestOpt() features.Func {
func KubectlDeleteNamespaceTestOpt(force bool) features.Func {
return func(ctx context.Context, t *testing.T, envConf *envconf.Config) context.Context {
namespace := ctxopts.Namespace(ctx)
return KubectlDeleteNamespaceOpt(namespace)(ctx, t, envConf)
return KubectlDeleteNamespaceOpt(namespace, force)(ctx, t, envConf)
}
}

// KubectlDeleteNamespaceOpt returns a features.Func that with delete the namespace
// that was saved in context using KubectlSetNamespaceOpt or KubectlSetTestNamespaceOpt.
func KubectlDeleteNamespaceOpt(namespace string) features.Func {
// KubectlDeleteNamespaceOpt returns a features.Func that with delete the provided namespace using kubectl options saved in the context.
// If force is set to true, finalizers are ignored during the deletion.
func KubectlDeleteNamespaceOpt(namespace string, force bool) features.Func {
return func(ctx context.Context, t *testing.T, envConf *envconf.Config) context.Context {
var err error
if force {
ns := k8s.GetNamespace(t, ctxopts.KubectlOptions(ctx), namespace)
client := envConf.Client()
ns.Spec.Finalizers = []corev1.FinalizerName{}
err = client.Resources().Update(ctx, ns)
require.NoError(t, err)
}
k8s.DeleteNamespace(t, ctxopts.KubectlOptions(ctx), namespace)
return ctx
}
Expand Down Expand Up @@ -83,7 +92,7 @@ func KubectlDeleteOperatorNamespacesOpt() features.Func {
if values.Operator.InstrumentationNamespaces != "" {
namespaces := stdstrings.Split(values.Operator.InstrumentationNamespaces, ",")
for _, namespace := range namespaces {
k8s.DeleteNamespace(t, ctxopts.KubectlOptions(ctx), namespace)
ctx = KubectlDeleteNamespaceOpt(namespace, true)(ctx, t, envConf)
}
}
return ctx
Expand Down Expand Up @@ -118,7 +127,7 @@ func KubectlDeleteOverrideNamespaceOpt() features.Func {
err := yaml.Unmarshal(valuesFileBytes, &values)
require.NoError(t, err)
if values.NamespaceOverride != "" {
k8s.DeleteNamespace(t, ctxopts.KubectlOptions(ctx), values.NamespaceOverride)
ctx = KubectlDeleteNamespaceOpt(values.NamespaceOverride, true)(ctx, t, envConf)
}
return ctx
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func ConfigureTestEnv(testenv env.Environment) {
stepfuncs.HelmDeleteTestOpt(),
stepfuncs.KubectlDeleteOverrideNamespaceOpt(),
stepfuncs.KubectlDeleteOperatorNamespacesOpt(),
stepfuncs.KubectlDeleteNamespaceTestOpt(),
stepfuncs.KubectlDeleteNamespaceTestOpt(false),
) {
testenv.AfterEachTest(f)
}
Expand Down

0 comments on commit 3853395

Please sign in to comment.