Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #98 from FabianKramm/master
Browse files Browse the repository at this point in the history
fix: use background context for critical tasks
  • Loading branch information
FabianKramm authored Jan 27, 2021
2 parents 3994bf1 + 1eabd4c commit 3886dad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
5 changes: 4 additions & 1 deletion pkg/apiserver/registry/account/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ func (r *accountREST) Delete(ctx context.Context, name string, deleteValidation
return nil, false, err
}

err = r.client.Delete(ctx, configAccount, &client.DeleteOptions{
// we have to use a background context here, because it might
// be possible that the user is cancelling the request and we want
// to fully delete the account and its children
err = r.client.Delete(context.Background(), configAccount, &client.DeleteOptions{
Raw: options,
})
if err != nil {
Expand Down
23 changes: 14 additions & 9 deletions pkg/apiserver/registry/space/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/loft-sh/kiosk/pkg/apiserver/registry/util"
"github.com/loft-sh/kiosk/pkg/authorization"
"github.com/loft-sh/kiosk/pkg/constants"
"github.com/loft-sh/kiosk/pkg/util/loghelper"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -40,7 +41,6 @@ import (
"k8s.io/apiserver/pkg/endpoints/filters"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/client-go/util/retry"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
"time"
Expand Down Expand Up @@ -279,7 +279,13 @@ func (r *spaceStorage) Create(ctx context.Context, obj runtime.Object, createVal
// Create the default space templates and role binding
err = r.initializeSpace(ctx, namespace, account)
if err != nil {
_ = r.client.Delete(ctx, namespace)
// we have to use a background context here, because it might
// be possible that the user is cancelling the request
spaceErr := r.client.Delete(context.Background(), namespace)
if spaceErr != nil {
loghelper.Infof("error deleting namespace %s after creation: %v", namespace.Name, spaceErr)
}

return nil, err
}
} else {
Expand Down Expand Up @@ -313,9 +319,7 @@ func (r *spaceStorage) waitForAccess(ctx context.Context, user user.Info, namesp
}

// here we wait until the authorizer tells us that the account can get the space
backoff := retry.DefaultBackoff
backoff.Steps = 8
return wait.ExponentialBackoff(backoff, func() (bool, error) {
return wait.PollImmediate(time.Second, time.Minute, func() (bool, error) {
decision, _, err := r.authorizer.Authorize(ctx, a)
if err != nil {
return false, err
Expand Down Expand Up @@ -352,9 +356,7 @@ func (r *spaceStorage) initializeSpace(ctx context.Context, namespace *corev1.Na
}

// Wait for template instances to be deployed
backoff := retry.DefaultBackoff
backoff.Steps = 8
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
err := wait.PollImmediate(time.Second, time.Minute, func() (bool, error) {
// Get the template instances
instanceList := &configv1alpha1.TemplateInstanceList{}
err := r.client.List(ctx, instanceList, client.InNamespace(namespace.Name))
Expand Down Expand Up @@ -463,7 +465,10 @@ func (r *spaceStorage) Delete(ctx context.Context, name string, deleteValidation
return nil, false, err
}

err = r.client.Delete(ctx, namespace, &client.DeleteOptions{
// we have to use a background context here, because it might
// be possible that the user is cancelling the request and we want
// to fully delete the namespace or otherwise there might be left overs
err = r.client.Delete(context.Background(), namespace, &client.DeleteOptions{
Raw: options,
})
if err != nil {
Expand Down

0 comments on commit 3886dad

Please sign in to comment.