| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
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 |
|
| 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 |
|
| 89 |
|
| 90 |
func (c *FakeDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) { |
| 91 |
return nil, nil |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
func (c *FakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) { |
| 97 |
return nil, nil |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 173 |
|
| 174 |
func (c *FakeDiscovery) RESTClient() restclient.Interface { |
| 175 |
return nil |
| 176 |
} |
| 177 |
|
| 178 |
func (c *FakeDiscovery) WithLegacy() discovery.DiscoveryInterface { |
| 179 |
panic("unimplemented") |
| 180 |
} |