Provisioning: Run validation on config updates (#103265)

* Run validation on config updates

* Refactor code

* Add debug lines

* Remove test check on admission

* Organize imports

* Delegate events to the API client

* Extend error notification

* Deep copy default data

---------

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
Co-authored-by: Clarity-89 <homes89@ukr.net>
This commit is contained in:
Roberto Jiménez Sánchez
2025-04-07 14:48:28 +03:00
committed by GitHub
co-authored by Ryan McKinley Clarity-89
parent e1ec9bddbd
commit fc099e9f0d
5 changed files with 60 additions and 40 deletions
+13 -23
View File
@@ -476,36 +476,26 @@ func (b *APIBuilder) Validate(ctx context.Context, a admission.Attributes, o adm
}
}
// Early exit to avoid more expensive checks if we have already found errors
if len(list) > 0 {
return invalidRepositoryError(a.GetName(), list)
}
// Exit early if we have already found errors
targetError := b.verifyAgaintsExistingRepositories(cfg)
if targetError != nil {
list = append(list, targetError)
return invalidRepositoryError(a.GetName(), field.ErrorList{targetError})
}
// For *create* we do a synchronous test... this can be expensive!
// it is the same as a full healthcheck, so should not be run on every update
if len(list) == 0 && a.GetOperation() == admission.Create {
testResults, err := repository.TestRepository(ctx, repo)
if err != nil {
list = append(list, field.Invalid(field.NewPath("spec"),
"Repository test failed", "Unable to verify repository: "+err.Error()))
}
if !testResults.Success {
for _, err := range testResults.Errors {
list = append(list, field.Invalid(field.NewPath("spec"),
"Repository test failed", err))
}
}
}
if len(list) > 0 {
return apierrors.NewInvalid(
provisioning.RepositoryResourceInfo.GroupVersionKind().GroupKind(),
a.GetName(), list)
}
return nil
}
func invalidRepositoryError(name string, list field.ErrorList) error {
return apierrors.NewInvalid(
provisioning.RepositoryResourceInfo.GroupVersionKind().GroupKind(),
name, list)
}
// TODO: move this to a more appropriate place. Probably controller/validation.go
func (b *APIBuilder) verifyAgaintsExistingRepositories(cfg *provisioning.Repository) *field.Error {
all, err := b.repositoryLister.Repositories(cfg.Namespace).List(labels.Everything())