Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / k8s.io / client-go / discovery / fake / discovery.go
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         "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 func (c *FakeDiscovery) ServerResources() ([]*metav1.APIResourceList, error) {
57         action := testing.ActionImpl{
58                 Verb:     "get",
59                 Resource: schema.GroupVersionResource{Resource: "resource"},
60         }
61         c.Invokes(action, nil)
62         return c.Resources, nil
63 }
64
65 // ServerPreferredResources returns the supported resources with the version
66 // preferred by the server.
67 func (c *FakeDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
68         return nil, nil
69 }
70
71 // ServerPreferredNamespacedResources returns the supported namespaced resources
72 // with the version preferred by the server.
73 func (c *FakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
74         return nil, nil
75 }
76
77 // ServerGroups returns the supported groups, with information like supported
78 // versions and the preferred version.
79 func (c *FakeDiscovery) ServerGroups() (*metav1.APIGroupList, error) {
80         action := testing.ActionImpl{
81                 Verb:     "get",
82                 Resource: schema.GroupVersionResource{Resource: "group"},
83         }
84         c.Invokes(action, nil)
85
86         groups := map[string]*metav1.APIGroup{}
87
88         for _, res := range c.Resources {
89                 gv, err := schema.ParseGroupVersion(res.GroupVersion)
90                 if err != nil {
91                         return nil, err
92                 }
93                 group := groups[gv.Group]
94                 if group == nil {
95                         group = &metav1.APIGroup{
96                                 Name: gv.Group,
97                                 PreferredVersion: metav1.GroupVersionForDiscovery{
98                                         GroupVersion: res.GroupVersion,
99                                         Version:      gv.Version,
100                                 },
101                         }
102                         groups[gv.Group] = group
103                 }
104
105                 group.Versions = append(group.Versions, metav1.GroupVersionForDiscovery{
106                         GroupVersion: res.GroupVersion,
107                         Version:      gv.Version,
108                 })
109         }
110
111         list := &metav1.APIGroupList{}
112         for _, apiGroup := range groups {
113                 list.Groups = append(list.Groups, *apiGroup)
114         }
115
116         return list, nil
117
118 }
119
120 // ServerVersion retrieves and parses the server's version.
121 func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
122         action := testing.ActionImpl{}
123         action.Verb = "get"
124         action.Resource = schema.GroupVersionResource{Resource: "version"}
125         c.Invokes(action, nil)
126
127         if c.FakedServerVersion != nil {
128                 return c.FakedServerVersion, nil
129         }
130
131         versionInfo := kubeversion.Get()
132         return &versionInfo, nil
133 }
134
135 // OpenAPISchema retrieves and parses the swagger API schema the server supports.
136 func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
137         return &openapi_v2.Document{}, nil
138 }
139
140 // RESTClient returns a RESTClient that is used to communicate with API server
141 // by this client implementation.
142 func (c *FakeDiscovery) RESTClient() restclient.Interface {
143         return nil
144 }