Provisioning finalisers fix 2 (#111679)

* adding some logs to better understand what might be happening

* only focus this PR on improve logging in finalizer handling

* debug log before calling finalizers

* working on finalizers

* removing last todos, adding unit tests

* better use SupportedFinalizers name

* addressing comments

* wip: fix tests and add delete error in status

* chore: codegen

* chore: codegen openapi

* Merge remote-tracking branch 'origin/main' into provisioning-finalisers-fix-2

* update frontend client

* fix: errors in testing

* fix: breaking test

---------

Co-authored-by: Daniele Ferru <daniele.ferru@grafana.com>
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
Costa Alexoglou
2025-09-29 15:21:12 +02:00
committed by GitHub
co-authored by Daniele Ferru Ryan McKinley
parent 893523dd7c
commit 1b766b9c9f
22 changed files with 1296 additions and 484 deletions
@@ -41,6 +41,11 @@ type queueItem struct {
attempts int
}
//go:generate mockery --name finalizerProcessor --structname MockFinalizerProcessor --inpackage --filename finalizer_mock.go --with-expecter
type finalizerProcessor interface {
process(ctx context.Context, repo repository.Repository, finalizers []string) error
}
// RepositoryController controls how and when CRD is established.
type RepositoryController struct {
client client.ProvisioningV0alpha1Interface
@@ -50,7 +55,7 @@ type RepositoryController struct {
dualwrite dualwrite.Service
jobs jobs.Queue
finalizer *finalizer
finalizer finalizerProcessor
statusPatcher StatusPatcher
repoFactory repository.Factory
@@ -223,12 +228,15 @@ func (rc *RepositoryController) handleDelete(ctx context.Context, obj *provision
if len(obj.Finalizers) > 0 {
repo, err := rc.repoFactory.Build(ctx, obj)
if err != nil {
logger.Warn("unable to get repository for cleanup")
} else {
err := rc.finalizer.process(ctx, repo, obj.Finalizers)
if err != nil {
logger.Warn("error running finalizer", "err", err)
return fmt.Errorf("create repository from configuration: %w", err)
}
err = rc.finalizer.process(ctx, repo, obj.Finalizers)
if err != nil {
if statusErr := rc.updateDeleteStatus(ctx, obj, fmt.Errorf("remove finalizers: %w", err)); statusErr != nil {
logger.Error("failed to update repository status after finalizer removal error", "error", statusErr)
}
return fmt.Errorf("process finalizers: %w", err)
}
// remove the finalizers
@@ -238,12 +246,27 @@ func (rc *RepositoryController) handleDelete(ctx context.Context, obj *provision
]`), v1.PatchOptions{
FieldManager: "provisioning-controller",
})
return err // delete will be called again
if err != nil {
return fmt.Errorf("remove finalizers: %w", err)
}
return nil
} else {
logger.Info("no finalizers to process")
}
return nil
}
func (rc *RepositoryController) updateDeleteStatus(ctx context.Context, obj *provisioning.Repository, err error) error {
logger := logging.FromContext(ctx)
logger.Info("updating repository status with deletion error", "error", err.Error())
return rc.statusPatcher.Patch(ctx, obj, map[string]interface{}{
"op": "replace",
"path": "/status/deleteError",
"value": err.Error(),
})
}
func (rc *RepositoryController) shouldResync(obj *provisioning.Repository) bool {
// don't trigger resync if a sync was never started
if obj.Status.Sync.Finished == 0 && obj.Status.Sync.State == "" {