12
"github.com/cosi-project/runtime/pkg/resource"
13
"github.com/cosi-project/runtime/pkg/resource/meta"
14
"github.com/cosi-project/runtime/pkg/safe"
15
"github.com/siderolabs/gen/xslices"
16
"google.golang.org/grpc/codes"
17
"google.golang.org/grpc/status"
21
func (c *Client) ResolveResourceKind(ctx context.Context, resourceNamespace *resource.Namespace, resourceType resource.Type) (*meta.ResourceDefinition, error) {
22
registeredResources, err := safe.StateListAll[*meta.ResourceDefinition](ctx, c.COSI)
27
matched := []*meta.ResourceDefinition{}
29
for it := registeredResources.Iterator(); it.Next(); {
32
if strings.EqualFold(rd.Metadata().ID(), resourceType) {
33
matched = append(matched, rd)
38
spec := rd.TypedSpec()
40
for _, alias := range spec.AllAliases {
41
if strings.EqualFold(alias, resourceType) {
42
matched = append(matched, rd)
50
case len(matched) == 1:
51
if *resourceNamespace == "" {
52
*resourceNamespace = matched[0].TypedSpec().DefaultNamespace
55
return matched[0], nil
56
case len(matched) > 1:
57
matchedTypes := xslices.Map(matched, func(rd *meta.ResourceDefinition) string { return rd.Metadata().ID() })
59
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("resource type %q is ambiguous: %v", resourceType, matchedTypes))
61
return nil, status.Error(codes.NotFound, fmt.Sprintf("resource %q is not registered", resourceType))