k8s.io/client-go@v0.22.2/discovery/fake/discovery.go (about) 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 22 openapi_v2 "github.com/googleapis/gnostic/openapiv2" 23 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime/schema" 26 "k8s.io/apimachinery/pkg/version" 27 kubeversion "k8s.io/client-go/pkg/version" 28 restclient "k8s.io/client-go/rest" 29 "k8s.io/client-go/testing" 30 ) 31 32 // FakeDiscovery implements discovery.DiscoveryInterface and sometimes calls testing.Fake.Invoke with an action, 33 // 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. 34 type FakeDiscovery struct { 35 *testing.Fake 36 FakedServerVersion *version.Info 37 } 38 39 // ServerResourcesForGroupVersion returns the supported resources for a group 40 // and version. 41 func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) { 42 action := testing.ActionImpl{ 43 Verb: "get", 44 Resource: schema.GroupVersionResource{Resource: "resource"}, 45 } 46 c.Invokes(action, nil) 47 for _, resourceList := range c.Resources { 48 if resourceList.GroupVersion == groupVersion { 49 return resourceList, nil 50 } 51 } 52 return nil, fmt.Errorf("GroupVersion %q not found", groupVersion) 53 } 54 55 // ServerResources returns the supported resources for all groups and versions. 56 // Deprecated: use ServerGroupsAndResources instead. 57 func (c *FakeDiscovery) ServerResources() ([]*metav1.APIResourceList, error) { 58 _, rs, err := c.ServerGroupsAndResources() 59 return rs, err 60 } 61 62 // ServerGroupsAndResources returns the supported groups and resources for all groups and versions. 63 func (c *FakeDiscovery) ServerGroupsAndResources() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) { 64 sgs, err := c.ServerGroups() 65 if err != nil { 66 return nil, nil, err 67 } 68 resultGroups := []*metav1.APIGroup{} 69 for i := range sgs.Groups { 70 resultGroups = append(resultGroups, &sgs.Groups[i]) 71 } 72 73 action := testing.ActionImpl{ 74 Verb: "get", 75 Resource: schema.GroupVersionResource{Resource: "resource"}, 76 } 77 c.Invokes(action, nil) 78 return resultGroups, c.Resources, nil 79 } 80 81 // ServerPreferredResources returns the supported resources with the version 82 // preferred by the server. 83 func (c *FakeDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) { 84 return nil, nil 85 } 86 87 // ServerPreferredNamespacedResources returns the supported namespaced resources 88 // with the version preferred by the server. 89 func (c *FakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) { 90 return nil, nil 91 } 92 93 // ServerGroups returns the supported groups, with information like supported 94 // versions and the preferred version. 95 func (c *FakeDiscovery) ServerGroups() (*metav1.APIGroupList, error) { 96 action := testing.ActionImpl{ 97 Verb: "get", 98 Resource: schema.GroupVersionResource{Resource: "group"}, 99 } 100 c.Invokes(action, nil) 101 102 groups := map[string]*metav1.APIGroup{} 103 104 for _, res := range c.Resources { 105 gv, err := schema.ParseGroupVersion(res.GroupVersion) 106 if err != nil { 107 return nil, err 108 } 109 group := groups[gv.Group] 110 if group == nil { 111 group = &metav1.APIGroup{ 112 Name: gv.Group, 113 PreferredVersion: metav1.GroupVersionForDiscovery{ 114 GroupVersion: res.GroupVersion, 115 Version: gv.Version, 116 }, 117 } 118 groups[gv.Group] = group 119 } 120 121 group.Versions = append(group.Versions, metav1.GroupVersionForDiscovery{ 122 GroupVersion: res.GroupVersion, 123 Version: gv.Version, 124 }) 125 } 126 127 list := &metav1.APIGroupList{} 128 for _, apiGroup := range groups { 129 list.Groups = append(list.Groups, *apiGroup) 130 } 131 132 return list, nil 133 134 } 135 136 // ServerVersion retrieves and parses the server's version. 137 func (c *FakeDiscovery) ServerVersion() (*version.Info, error) { 138 action := testing.ActionImpl{} 139 action.Verb = "get" 140 action.Resource = schema.GroupVersionResource{Resource: "version"} 141 c.Invokes(action, nil) 142 143 if c.FakedServerVersion != nil { 144 return c.FakedServerVersion, nil 145 } 146 147 versionInfo := kubeversion.Get() 148 return &versionInfo, nil 149 } 150 151 // OpenAPISchema retrieves and parses the swagger API schema the server supports. 152 func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) { 153 return &openapi_v2.Document{}, nil 154 } 155 156 // RESTClient returns a RESTClient that is used to communicate with API server 157 // by this client implementation. 158 func (c *FakeDiscovery) RESTClient() restclient.Interface { 159 return nil 160 }