talos

Форк
0
/
resources.go 
63 строки · 1.8 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
package client
6

7
import (
8
	"context"
9
	"fmt"
10
	"strings"
11

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"
18
)
19

20
// ResolveResourceKind resolves potentially aliased 'resourceType' and replaces empty 'resourceNamespace' with the default namespace for the resource.
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)
23
	if err != nil {
24
		return nil, err
25
	}
26

27
	matched := []*meta.ResourceDefinition{}
28

29
	for it := registeredResources.Iterator(); it.Next(); {
30
		rd := it.Value()
31

32
		if strings.EqualFold(rd.Metadata().ID(), resourceType) {
33
			matched = append(matched, rd)
34

35
			continue
36
		}
37

38
		spec := rd.TypedSpec()
39

40
		for _, alias := range spec.AllAliases {
41
			if strings.EqualFold(alias, resourceType) {
42
				matched = append(matched, rd)
43

44
				break
45
			}
46
		}
47
	}
48

49
	switch {
50
	case len(matched) == 1:
51
		if *resourceNamespace == "" {
52
			*resourceNamespace = matched[0].TypedSpec().DefaultNamespace
53
		}
54

55
		return matched[0], nil
56
	case len(matched) > 1:
57
		matchedTypes := xslices.Map(matched, func(rd *meta.ResourceDefinition) string { return rd.Metadata().ID() })
58

59
		return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("resource type %q is ambiguous: %v", resourceType, matchedTypes))
60
	default:
61
		return nil, status.Error(codes.NotFound, fmt.Sprintf("resource %q is not registered", resourceType))
62
	}
63
}
64

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.