discovery.go 5.3 KB

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