1
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
37
38 type FakeDiscovery struct {
39 *testing.Fake
40 FakedServerVersion *version.Info
41 }
42
43
44
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 c.Invokes(action, nil)
51 for _, resourceList := range c.Resources {
52 if resourceList.GroupVersion == groupVersion {
53 return resourceList, nil
54 }
55 }
56 return nil, &errors.StatusError{
57 ErrStatus: metav1.Status{
58 Status: metav1.StatusFailure,
59 Code: http.StatusNotFound,
60 Reason: metav1.StatusReasonNotFound,
61 Message: fmt.Sprintf("the server could not find the requested resource, GroupVersion %q not found", groupVersion),
62 }}
63 }
64
65
66 func (c *FakeDiscovery) ServerGroupsAndResources() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
67 sgs, err := c.ServerGroups()
68 if err != nil {
69 return nil, nil, err
70 }
71 resultGroups := []*metav1.APIGroup{}
72 for i := range sgs.Groups {
73 resultGroups = append(resultGroups, &sgs.Groups[i])
74 }
75
76 action := testing.ActionImpl{
77 Verb: "get",
78 Resource: schema.GroupVersionResource{Resource: "resource"},
79 }
80 c.Invokes(action, nil)
81 return resultGroups, c.Resources, nil
82 }
83
84
85
86 func (c *FakeDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
87 return nil, nil
88 }
89
90
91
92 func (c *FakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
93 return nil, nil
94 }
95
96
97
98 func (c *FakeDiscovery) ServerGroups() (*metav1.APIGroupList, error) {
99 action := testing.ActionImpl{
100 Verb: "get",
101 Resource: schema.GroupVersionResource{Resource: "group"},
102 }
103 c.Invokes(action, nil)
104
105 groups := map[string]*metav1.APIGroup{}
106
107 for _, res := range c.Resources {
108 gv, err := schema.ParseGroupVersion(res.GroupVersion)
109 if err != nil {
110 return nil, err
111 }
112 group := groups[gv.Group]
113 if group == nil {
114 group = &metav1.APIGroup{
115 Name: gv.Group,
116 PreferredVersion: metav1.GroupVersionForDiscovery{
117 GroupVersion: res.GroupVersion,
118 Version: gv.Version,
119 },
120 }
121 groups[gv.Group] = group
122 }
123
124 group.Versions = append(group.Versions, metav1.GroupVersionForDiscovery{
125 GroupVersion: res.GroupVersion,
126 Version: gv.Version,
127 })
128 }
129
130 list := &metav1.APIGroupList{}
131 for _, apiGroup := range groups {
132 list.Groups = append(list.Groups, *apiGroup)
133 }
134
135 return list, nil
136
137 }
138
139
140 func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
141 action := testing.ActionImpl{}
142 action.Verb = "get"
143 action.Resource = schema.GroupVersionResource{Resource: "version"}
144 _, err := c.Invokes(action, nil)
145 if err != nil {
146 return nil, err
147 }
148
149 if c.FakedServerVersion != nil {
150 return c.FakedServerVersion, nil
151 }
152
153 versionInfo := kubeversion.Get()
154 return &versionInfo, nil
155 }
156
157
158 func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
159 return &openapi_v2.Document{}, nil
160 }
161
162 func (c *FakeDiscovery) OpenAPIV3() openapi.Client {
163 panic("unimplemented")
164 }
165
166
167
168 func (c *FakeDiscovery) RESTClient() restclient.Interface {
169 return nil
170 }
171
172 func (c *FakeDiscovery) WithLegacy() discovery.DiscoveryInterface {
173 panic("unimplemented")
174 }
175
View as plain text