1
0
mirror of https://github.com/rancher/steve.git synced 2025-08-22 16:16:49 +00:00

Updating ByNames to not return nil, nil

ByNames could previously return a nil value and a nil error. This caused
issues when other parts of the application
(pkg/stores/partition/parallel.go) tried to use the result. Now this
will return an empty list on the error condition, instead of nil
This commit is contained in:
Michael Bolot 2023-10-13 14:21:14 -05:00
parent 811e0b3572
commit 0b23400f9f
2 changed files with 16 additions and 3 deletions

View File

@ -205,9 +205,10 @@ func tableToObjects(obj map[string]interface{}) []unstructured.Unstructured {
// be returned in the list.
func (s *Store) ByNames(apiOp *types.APIRequest, schema *types.APISchema, names sets.String) (*unstructured.UnstructuredList, []types.Warning, error) {
if apiOp.Namespace == "*" {
// This happens when you grant namespaced objects with "get" by name in a clusterrolebinding. We will treat
// this as an invalid situation instead of listing all objects in the cluster and filtering by name.
return nil, nil, nil
// This happens when you grant namespaced objects with "get" or "list "by name in a clusterrolebinding.
// We will treat this as an invalid situation instead of listing all objects in the cluster
// and filtering by name.
return &unstructured.UnstructuredList{}, nil, nil
}
buffer := WarningBuffer{}
adminClient, err := s.clientGetter.TableAdminClient(apiOp, schema, apiOp.Namespace, &buffer)

View File

@ -68,6 +68,18 @@ func TestWatchNamesErrReceive(t *testing.T) {
assert.Equal(t, 0, len(c.ResultChan()), "Expected all secrets to have been received")
}
func TestByNames(t *testing.T) {
s := Store{}
apiSchema := &types.APISchema{Schema: &schemas.Schema{}}
apiOp := &types.APIRequest{Namespace: "*", Schema: apiSchema, Request: &http.Request{}}
names := sets.NewString("some-resource", "some-other-resource")
result, warn, err := s.ByNames(apiOp, apiSchema, names)
assert.NotNil(t, result)
assert.Len(t, result.Items, 0)
assert.Nil(t, err)
assert.Nil(t, warn)
}
func (t *testFactory) TableAdminClientForWatch(ctx *types.APIRequest, schema *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) {
return t.fakeClient.Resource(schema2.GroupVersionResource{}), nil
}