Debian Codegraph @

discovery/fake/discovery.go

raw symbols SCIP
1 /*
2 Copyright 2016 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package fake
18
19 import (
20 "fmt"
21 "net/http"
22
23 openapi_v2 "github.com/google/gnostic-models/openapiv2"
24
25 "k8s.io/apimachinery/pkg/api/errors"
26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27 "k8s.io/apimachinery/pkg/runtime/schema"
28 "k8s.io/apimachinery/pkg/version"
29 "k8s.io/client-go/discovery"
30 "k8s.io/client-go/openapi"
31 kubeversion "k8s.io/client-go/pkg/version"
32 restclient "k8s.io/client-go/rest"
33 "k8s.io/client-go/testing"
34 )
35
36 // FakeDiscovery implements discovery.DiscoveryInterface and sometimes calls testing.Fake.Invoke with an action,
37 // but doesn't respect the return value if any. There is a way to fake static values like ServerVersion by using the Faked... fields on the struct.
38 type FakeDiscovery struct {
39 *testing.Fake
40 FakedServerVersion *version.Info
41 }
42
43 // ServerResourcesForGroupVersion returns the supported resources for a group
44 // and version.
45 func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
46 action := testing.ActionImpl{
47 Verb: "get",
48 Resource: schema.GroupVersionResource{Resource: "resource"},
49 }
50 if _, err := c.Invokes(action, nil); err != nil {
51 return nil, err
52 }
53 for _, resourceList := range c.Resources {
54 if resourceList.GroupVersion == groupVersion {
55 return resourceList, nil
56 }
57 }
58 return nil, &errors.StatusError{
59 ErrStatus: metav1.Status{
60 Status: metav1.StatusFailure,
61 Code: http.StatusNotFound,
62 Reason: metav1.StatusReasonNotFound,
63 Message: fmt.Sprintf("the server could not find the requested resource, GroupVersion %q not found", groupVersion),
64 }}
65 }
66
67 // ServerGroupsAndResources returns the supported groups and resources for all groups and versions.
68 func (c *FakeDiscovery) ServerGroupsAndResources() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
69 sgs, err := c.ServerGroups()
70 if err != nil {
71 return nil, nil, err
72 }
73 resultGroups := []*metav1.APIGroup{}
74 for i := range sgs.Groups {
75 resultGroups = append(resultGroups, &sgs.Groups[i])
76 }
77
78 action := testing.ActionImpl{
79 Verb: "get",
80 Resource: schema.GroupVersionResource{Resource: "resource"},
81 }
82 if _, err = c.Invokes(action, nil); err != nil {
83 return resultGroups, c.Resources, err
84 }
85 return resultGroups, c.Resources, nil
86 }
87
88 // ServerPreferredResources returns the supported resources with the version
89 // preferred by the server.
90 func (c *FakeDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
91 return nil, nil
92 }
93
94 // ServerPreferredNamespacedResources returns the supported namespaced resources
95 // with the version preferred by the server.
96 func (c *FakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
97 return nil, nil
98 }
99
100 // ServerGroups returns the supported groups, with information like supported
101 // versions and the preferred version.
102 func (c *FakeDiscovery) ServerGroups() (*metav1.APIGroupList, error) {
103 action := testing.ActionImpl{
104 Verb: "get",
105 Resource: schema.GroupVersionResource{Resource: "group"},
106 }
107 if _, err := c.Invokes(action, nil); err != nil {
108 return nil, err
109 }
110
111 groups := map[string]*metav1.APIGroup{}
112
113 for _, res := range c.Resources {
114 gv, err := schema.ParseGroupVersion(res.GroupVersion)
115 if err != nil {
116 return nil, err
117 }
118 group := groups[gv.Group]
119 if group == nil {
120 group = &metav1.APIGroup{
121 Name: gv.Group,
122 PreferredVersion: metav1.GroupVersionForDiscovery{
123 GroupVersion: res.GroupVersion,
124 Version: gv.Version,
125 },
126 }
127 groups[gv.Group] = group
128 }
129
130 group.Versions = append(group.Versions, metav1.GroupVersionForDiscovery{
131 GroupVersion: res.GroupVersion,
132 Version: gv.Version,
133 })
134 }
135
136 list := &metav1.APIGroupList{}
137 for _, apiGroup := range groups {
138 list.Groups = append(list.Groups, *apiGroup)
139 }
140
141 return list, nil
142
143 }
144
145 // ServerVersion retrieves and parses the server's version.
146 func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
147 action := testing.ActionImpl{}
148 action.Verb = "get"
149 action.Resource = schema.GroupVersionResource{Resource: "version"}
150 _, err := c.Invokes(action, nil)
151 if err != nil {
152 return nil, err
153 }
154
155 if c.FakedServerVersion != nil {
156 return c.FakedServerVersion, nil
157 }
158
159 versionInfo := kubeversion.Get()
160 return &versionInfo, nil
161 }
162
163 // OpenAPISchema retrieves and parses the swagger API schema the server supports.
164 func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
165 return &openapi_v2.Document{}, nil
166 }
167
168 func (c *FakeDiscovery) OpenAPIV3() openapi.Client {
169 panic("unimplemented")
170 }
171
172 // RESTClient returns a RESTClient that is used to communicate with API server
173 // by this client implementation.
174 func (c *FakeDiscovery) RESTClient() restclient.Interface {
175 return nil
176 }
177
178 func (c *FakeDiscovery) WithLegacy() discovery.DiscoveryInterface {
179 panic("unimplemented")
180 }