Skip to content

Commit 0c86b20

Browse files
committed
fix: Aggregate and return all invalid target resource errors during SearchQuery validation instead of the first.
1 parent 0e089eb commit 0c86b20

File tree

1 file changed

+18
-2
lines changed
  • internal/registry/searchquery

1 file changed

+18
-2
lines changed

internal/registry/searchquery/rest.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
apierrors "k8s.io/apimachinery/pkg/api/errors"
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414
"k8s.io/apimachinery/pkg/runtime"
15+
"k8s.io/apimachinery/pkg/runtime/schema"
16+
"k8s.io/apimachinery/pkg/util/validation/field"
1517
"k8s.io/apiserver/pkg/registry/rest"
1618
"k8s.io/klog/v2"
1719

@@ -192,7 +194,10 @@ func (r *REST) resolveIndexUIDs(query *searchv1alpha1.SearchQuery) ([]string, er
192194
policies := r.policyCache.GetPolicies()
193195

194196
if len(query.Spec.TargetResources) > 0 {
195-
for _, tr := range query.Spec.TargetResources {
197+
var errs field.ErrorList
198+
targetResourcesPath := field.NewPath("spec", "targetResources")
199+
200+
for i, tr := range query.Spec.TargetResources {
196201
found := false
197202
for _, cp := range policies {
198203
p := cp.Policy
@@ -206,9 +211,20 @@ func (r *REST) resolveIndexUIDs(query *searchv1alpha1.SearchQuery) ([]string, er
206211
}
207212
}
208213
if !found {
209-
return nil, apierrors.NewBadRequest(fmt.Sprintf("target resource %s/%s %s is not currently indexed or policy is not ready", tr.Group, tr.Version, tr.Kind))
214+
errs = append(errs, field.NotFound(
215+
targetResourcesPath.Index(i),
216+
fmt.Sprintf("%s/%s %s", tr.Group, tr.Version, tr.Kind),
217+
))
210218
}
211219
}
220+
221+
if len(errs) > 0 {
222+
return nil, apierrors.NewInvalid(
223+
schema.GroupKind{Group: searchv1alpha1.SchemeGroupVersion.Group, Kind: "SearchQuery"},
224+
query.Name,
225+
errs,
226+
)
227+
}
212228
} else {
213229
for _, cp := range policies {
214230
if cp.Policy.Status.IndexName != "" {

0 commit comments

Comments
 (0)