forked from kyma-project/lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Split resource deletion (kyma-project#1336)
* split cleanup process to postpone crd deletion after all resources get deleted * split cleanup process to postpone operator related resource deletion * revert doPreDelete * rename doPreDelete to removeModuleCR * fix flaky test * increase eventually timeout * long timeout when waiting for manifest deletion * increase timeout * set state to Warning if deletion is not finished * update module template * update moduletemplate * remove declarative test * apply review suggestion * add unit test for IsOperatorRelatedResources * add unit test for SplitResources enhance e2e to test module deletion when managed CR get blocked * import template operator api in tests * enhance GetManifest * fix test * fix flaky tests * fix e2e test * debug e2e * add missing Namespace to operator related resources
- Loading branch information
Showing
44 changed files
with
740 additions
and
1,103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/cli-runtime/pkg/resource" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/kyma-project/lifecycle-manager/api/shared" | ||
"github.com/kyma-project/lifecycle-manager/api/v1beta2" | ||
"github.com/kyma-project/lifecycle-manager/pkg/common" | ||
"github.com/kyma-project/lifecycle-manager/pkg/util" | ||
) | ||
|
||
var ErrDeletionNotFinished = errors.New("deletion is not yet finished") | ||
|
||
type ConcurrentCleanup struct { | ||
clnt client.Client | ||
manifest *v1beta2.Manifest | ||
} | ||
|
||
func NewConcurrentCleanup(clnt client.Client, manifest *v1beta2.Manifest) *ConcurrentCleanup { | ||
return &ConcurrentCleanup{ | ||
clnt: clnt, | ||
manifest: manifest, | ||
} | ||
} | ||
|
||
func (c *ConcurrentCleanup) DeleteDiffResources(ctx context.Context, resources []*resource.Info, | ||
) error { | ||
status := c.manifest.GetStatus() | ||
operatorRelatedResources, operatorManagedResources, err := SplitResources(resources) | ||
if err != nil { | ||
return err | ||
} | ||
if err := c.cleanupResources(ctx, operatorManagedResources, status); err != nil { | ||
return err | ||
} | ||
return c.cleanupResources(ctx, operatorRelatedResources, status) | ||
} | ||
|
||
func (c *ConcurrentCleanup) cleanupResources( | ||
ctx context.Context, | ||
resources []*resource.Info, | ||
status shared.Status, | ||
) error { | ||
if err := c.Run(ctx, resources); errors.Is(err, ErrDeletionNotFinished) { | ||
c.manifest.SetStatus(status.WithState(shared.StateWarning).WithErr(err)) | ||
return err | ||
} else if err != nil { | ||
c.manifest.SetStatus(status.WithState(shared.StateError).WithErr(err)) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func SplitResources(resources []*resource.Info) ([]*resource.Info, []*resource.Info, error) { | ||
operatorRelatedResources := make([]*resource.Info, 0) | ||
operatorManagedResources := make([]*resource.Info, 0) | ||
|
||
for _, item := range resources { | ||
obj, ok := item.Object.(client.Object) | ||
if !ok { | ||
return nil, nil, common.ErrTypeAssert | ||
} | ||
if IsOperatorRelatedResources(obj.GetObjectKind().GroupVersionKind().Kind) { | ||
operatorRelatedResources = append(operatorRelatedResources, item) | ||
continue | ||
} | ||
operatorManagedResources = append(operatorManagedResources, item) | ||
} | ||
|
||
return operatorRelatedResources, operatorManagedResources, nil | ||
} | ||
|
||
func IsOperatorRelatedResources(kind string) bool { | ||
return kind == "CustomResourceDefinition" || | ||
kind == "Namespace" || | ||
kind == "ServiceAccount" || | ||
kind == "Role" || | ||
kind == "ClusterRole" || | ||
kind == "RoleBinding" || | ||
kind == "ClusterRoleBinding" || | ||
kind == "Service" || | ||
kind == "Deployment" | ||
} | ||
|
||
func (c *ConcurrentCleanup) Run(ctx context.Context, infos []*resource.Info) error { | ||
results := make(chan error, len(infos)) | ||
for i := range infos { | ||
i := i | ||
go c.cleanupResource(ctx, infos[i], results) | ||
} | ||
|
||
var errs []error | ||
present := len(infos) | ||
for i := 0; i < len(infos); i++ { | ||
err := <-results | ||
if util.IsNotFound(err) { | ||
present-- | ||
continue | ||
} | ||
if err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
if len(errs) > 0 { | ||
return errors.Join(errs...) | ||
} | ||
|
||
if present > 0 { | ||
return ErrDeletionNotFinished | ||
} | ||
return nil | ||
} | ||
|
||
func (c *ConcurrentCleanup) cleanupResource(ctx context.Context, info *resource.Info, results chan error) { | ||
obj, ok := info.Object.(client.Object) | ||
if !ok { | ||
return | ||
} | ||
results <- c.clnt.Delete(ctx, obj, client.PropagationPolicy(apimetav1.DeletePropagationBackground)) | ||
} |
Oops, something went wrong.