Provisioning: check finalizers when validating Repository object (#110955)

This commit is contained in:
Daniele Stefano Ferru
2025-09-11 21:38:41 -05:00
committed by GitHub
parent 7805f6b62d
commit 6b2b949f8f
4 changed files with 26 additions and 14 deletions
@@ -0,0 +1,10 @@
package repository
// RemoveOrphanResourcesFinalizer removes everything this repo created
const RemoveOrphanResourcesFinalizer = "remove-orphan-resources"
// ReleaseOrphanResourcesFinalizer removes the metadata for anything this repo created
const ReleaseOrphanResourcesFinalizer = "release-orphan-resources"
// CleanFinalizer calls the "OnDelete" function for resource
const CleanFinalizer = "cleanup"
+11
View File
@@ -84,6 +84,17 @@ func ValidateRepository(repo Repository) field.ErrorList {
}
}
if slices.Contains(cfg.Finalizers, RemoveOrphanResourcesFinalizer) &&
slices.Contains(cfg.Finalizers, ReleaseOrphanResourcesFinalizer) {
list = append(list,
field.Invalid(
field.NewPath("medatada", "finalizers"),
cfg.Finalizers,
"cannot have both remove and release orphan resources finalizers",
),
)
}
return list
}
@@ -19,15 +19,6 @@ import (
"github.com/grafana/grafana/pkg/registry/apis/provisioning/resources"
)
// RemoveOrphanResourcesFinalizer removes everything this repo created
const RemoveOrphanResourcesFinalizer = "remove-orphan-resources"
// ReleaseOrphanResourcesFinalizer removes the metadata for anything this repo created
const ReleaseOrphanResourcesFinalizer = "release-orphan-resources"
// CleanFinalizer calls the "OnDelete" function for resource
const CleanFinalizer = "cleanup"
type finalizer struct {
lister resources.ResourceLister
clientFactory resources.ClientFactory
@@ -41,7 +32,7 @@ func (f *finalizer) process(ctx context.Context,
for _, finalizer := range finalizers {
switch finalizer {
case CleanFinalizer:
case repository.CleanFinalizer:
// NOTE: the controller loop will never get run unless a finalizer is set
hooks, ok := repo.(repository.Hooks)
if ok {
@@ -50,7 +41,7 @@ func (f *finalizer) process(ctx context.Context,
}
}
case ReleaseOrphanResourcesFinalizer:
case repository.ReleaseOrphanResourcesFinalizer:
err := f.processExistingItems(ctx, repo.Config(),
func(client dynamic.ResourceInterface, item *provisioning.ResourceListItem) error {
patchAnnotations, err := getPatchedAnnotations(item)
@@ -67,7 +58,7 @@ func (f *finalizer) process(ctx context.Context,
return err
}
case RemoveOrphanResourcesFinalizer:
case repository.RemoveOrphanResourcesFinalizer:
err := f.processExistingItems(ctx, repo.Config(),
func(client dynamic.ResourceInterface, item *provisioning.ResourceListItem) error {
return client.Delete(ctx, item.Name, v1.DeleteOptions{})
+2 -2
View File
@@ -485,8 +485,8 @@ func (b *APIBuilder) Mutate(ctx context.Context, a admission.Attributes, o admis
// This is called on every update, so be careful to only add the finalizer for create
if len(r.Finalizers) == 0 && a.GetOperation() == admission.Create {
r.Finalizers = []string{
controller.RemoveOrphanResourcesFinalizer,
controller.CleanFinalizer,
repository.RemoveOrphanResourcesFinalizer,
repository.CleanFinalizer,
}
}