...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package metadata
16
17 import (
18 "fmt"
19 "strings"
20
21 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
22
23 dclunstruct "github.com/GoogleCloudPlatform/declarative-resource-client-library/unstructured"
24 k8sschema "k8s.io/apimachinery/pkg/runtime/schema"
25 )
26
27
28 func ToServiceTypeVersion(gvk k8sschema.GroupVersionKind, loader ServiceMetadataLoader) (dclunstruct.ServiceTypeVersion, error) {
29 service := groupToService(gvk.Group)
30 sm, found := loader.GetServiceMetadata(service)
31 if !found {
32 return dclunstruct.ServiceTypeVersion{}, fmt.Errorf("ServiceMetadata for service %v is not found", service)
33 }
34 r, found := sm.GetResourceWithKind(gvk.Kind)
35 if !found {
36 return dclunstruct.ServiceTypeVersion{}, fmt.Errorf("resource with kind %v not supported in service %v", gvk.Kind, service)
37 }
38 return dclunstruct.ServiceTypeVersion{
39 Service: sm.ServiceNameUsedByDCL,
40 Type: r.DCLType,
41 Version: r.DCLVersion,
42 }, nil
43 }
44
45
46 func ToGroupVersionKind(stv dclunstruct.ServiceTypeVersion, loader ServiceMetadataLoader) (k8sschema.GroupVersionKind, error) {
47 sm, found := loader.GetServiceMetadata(stv.Service)
48 if !found {
49 return k8sschema.GroupVersionKind{}, fmt.Errorf("ServiceMetadata for service %v is not found", stv.Service)
50 }
51 r, found := sm.GetResourceWithType(stv.Type)
52 if !found {
53 return k8sschema.GroupVersionKind{}, fmt.Errorf("resource with DCL type %v not supported in service %v", stv.Type, stv.Service)
54 }
55 return GVKForResource(sm, r), nil
56 }
57
58 func IsDCLBasedResourceKind(gvk k8sschema.GroupVersionKind, loader ServiceMetadataLoader) bool {
59 r, found := loader.GetResourceWithGVK(gvk)
60 if !found {
61 return false
62 }
63 return r.Releasable
64 }
65
66 func groupToService(group string) string {
67 return strings.TrimSuffix(group, k8s.ApiDomainSuffix)
68 }
69
View as plain text