/
kube_factory_proxy.go
86 lines (73 loc) · 2.52 KB
/
kube_factory_proxy.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
74
75
76
77
78
79
80
81
82
83
84
85
86
package v2
import (
"fmt"
apicorev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/rest"
"k8s.io/kubectl/pkg/util/openapi"
)
// OpenAPISchema returns metadata and structural information about
// Kubernetes object definitions.
func (s *SingletonClients) OpenAPISchema() (openapi.Resources, error) {
parsedMetadata, err := s.openAPIParser.Parse()
if err != nil {
return nil, fmt.Errorf("failed to parse schema metadata: %w", err)
}
return parsedMetadata, nil
}
// OpenAPIGetter returns a getter for the openapi schema document.
func (s *SingletonClients) OpenAPIGetter() *openapi.CachedOpenAPIGetter {
return s.openAPIGetter
}
// UnstructuredClientForMapping returns a RESTClient for working with Unstructured objects.
func (s *SingletonClients) UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) {
s.unstructuredSyncLock.Lock()
defer s.unstructuredSyncLock.Unlock()
key := s.clientCacheKeyForMapping(mapping)
client, found := s.unstructuredRESTClientCache[key]
if found {
return client, nil
}
cfg := rest.CopyConfig(s.config)
cfg.APIPath = apis
if mapping.GroupVersionKind.Group == apicorev1.GroupName {
cfg.APIPath = api
}
gv := mapping.GroupVersionKind.GroupVersion()
cfg.ContentConfig = resource.UnstructuredPlusDefaultContentConfig()
cfg.GroupVersion = &gv
client, err := rest.RESTClientForConfigAndClient(cfg, s.httpClient)
if err != nil {
return nil, fmt.Errorf("failed to create httpClient config: %w", err)
}
s.unstructuredRESTClientCache[key] = client
return client, nil
}
// ClientForMapping returns a RESTClient for working with the specified RESTMapping or an error. This is intended
// for working with arbitrary resources and is not guaranteed to point to a Kubernetes APIServer.
func (s *SingletonClients) ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) {
s.structuredSyncLock.Lock()
defer s.structuredSyncLock.Unlock()
key := s.clientCacheKeyForMapping(mapping)
client, found := s.structuredRESTClientCache[key]
if found {
return client, nil
}
cfg := rest.CopyConfig(s.config)
gvk := mapping.GroupVersionKind
switch gvk.Group {
case apicorev1.GroupName:
cfg.APIPath = api
default:
cfg.APIPath = apis
}
gv := gvk.GroupVersion()
cfg.GroupVersion = &gv
client, err := rest.RESTClientForConfigAndClient(cfg, s.httpClient)
if err != nil {
return nil, fmt.Errorf("failed to create httpClient config: %w", err)
}
s.structuredRESTClientCache[key] = client
return client, nil
}