Provisioning: check finalizers when validating Repository object (#110955)
This commit is contained in:
@@ -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"
|
||||
@@ -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{})
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user