Skip to content

Commit

Permalink
fix: wait namespace is actually deleted (#413) (#422)
Browse files Browse the repository at this point in the history
* fix: wait namespace is actually deleted (#413)
* Adding Skip Delete for Namespace in Tests

Co-authored-by: Ken Sipe <kensipe@gmail.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Co-authored-by: Erik Godding Boye <egboye@gmail.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Ken Sipe <kensipe@gmail.com>
  • Loading branch information
3 people authored Dec 20, 2022
1 parent b6c593f commit 049344a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
9 changes: 9 additions & 0 deletions pkg/kuttlctl/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ For more detailed documentation, visit: https://kuttl.dev`,
return errors.New("only use --attach-control-plane-output with --start-control-plane")
}

// if we are working with a control plane we can not wait to delete ns (there is no ns controller)
// this is added before flags potentially override. control plane should skip ns and cluster delete but
// perhaps there are cases where that is part of the test. In general, there is no cluster to delete and
// there is no namespace controller.
if options.StartControlPlane {
options.SkipDelete = true
options.SkipClusterDelete = true
}

if isSet(flags, "skip-delete") {
options.SkipDelete = skipDelete
}
Expand Down
20 changes: 17 additions & 3 deletions pkg/test/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
corev1 "k8s.io/api/core/v1"
eventsv1 "k8s.io/api/events/v1"
eventsbeta1 "k8s.io/api/events/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -69,13 +70,26 @@ func (t *Case) DeleteNamespace(cl client.Client, ns *namespace) error {
defer cancel()
}

return cl.Delete(ctx, &corev1.Namespace{
nsObj := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns.Name,
},
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
},
}

if err := cl.Delete(ctx, nsObj); err != nil {
return err
}

return wait.PollImmediateUntilWithContext(ctx, 100*time.Millisecond, func(ctx context.Context) (done bool, err error) {
actual := &corev1.Namespace{}
err = cl.Get(ctx, client.ObjectKey{Name: ns.Name}, actual)
if k8serrors.IsNotFound(err) {
return true, nil
}
return false, err
})
}

Expand Down Expand Up @@ -120,7 +134,7 @@ func (t *Case) NamespaceExists(namespace string) (bool, error) {
}
ns := &corev1.Namespace{}
err = cl.Get(context.TODO(), client.ObjectKey{Name: namespace}, ns)
if err != nil && !errors.IsNotFound(err) {
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
return ns.Name == namespace, nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/test/case_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func TestMultiClusterCase(t *testing.T) {
}

c := Case{
Logger: testutils.NewTestLogger(t, ""),
Logger: testutils.NewTestLogger(t, ""),
SkipDelete: true,
Steps: []*Step{
{
Name: "initialize-testenv",
Expand Down
2 changes: 2 additions & 0 deletions pkg/test/harness_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestHarnessRunIntegration(t *testing.T) {
"./test_data/",
},
StartControlPlane: true,
SkipDelete: true,
CRDDir: "./test_crds/",
},
T: t,
Expand All @@ -39,6 +40,7 @@ func TestHarnessRunIntegrationWithConfig(t *testing.T) {
},
// set as true to skip service account check
StartControlPlane: true,
SkipDelete: true,
Config: config,
CRDDir: "./test_crds/",
},
Expand Down

0 comments on commit 049344a

Please sign in to comment.