forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resourcemeta.go
73 lines (64 loc) · 1.95 KB
/
resourcemeta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package extractor
import (
"github.com/kyma-project/kyma/components/console-backend-service/internal/apierror"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/k8s/pretty"
"github.com/pkg/errors"
"k8s.io/client-go/discovery"
)
type ResourceMeta struct {
Name string
Namespace string
Kind string
APIVersion string
}
func ExtractResourceMeta(in map[string]interface{}) (ResourceMeta, error) {
var errs apierror.ErrorFieldAggregate
apiVersion, ok := in["apiVersion"].(string)
if !ok {
errs = append(errs, apierror.NewMissingField("apiVersion"))
}
kind, ok := in["kind"].(string)
if !ok {
errs = append(errs, apierror.NewMissingField("kind"))
}
metadata, ok := in["metadata"].(map[string]interface{})
var name, namespace string
if ok {
name, ok = metadata["name"].(string)
if !ok {
errs = append(errs, apierror.NewMissingField("metadata.name"))
}
namespace, ok = metadata["namespace"].(string)
if !ok {
errs = append(errs, apierror.NewMissingField("metadata.namespace"))
}
} else {
errs = append(errs, apierror.NewMissingField("metadata"))
}
if len(errs) > 0 {
return ResourceMeta{}, apierror.NewInvalid(pretty.Resource, errs)
}
return ResourceMeta{
Name: name,
Namespace: namespace,
Kind: kind,
APIVersion: apiVersion,
}, nil
}
func GetPluralNameFromKind(kind, apiVersion string, client discovery.DiscoveryInterface) (string, error) {
resources, err := client.ServerResourcesForGroupVersion(apiVersion)
if err != nil {
return "", errors.Wrapf(err, "while fetching resources for group version %s", apiVersion)
}
var plural string
for _, resource := range resources.APIResources {
if resource.Kind == kind {
plural = resource.Name
break
}
}
if plural == "" {
return "", apierror.NewInvalid(pretty.Resource, apierror.ErrorFieldAggregate{apierror.NewInvalidField("kind", kind, "resource plural name for specified kind not found")})
}
return plural, nil
}