mirror of
https://github.com/kubernetes/client-go.git
synced 2025-07-08 04:28:56 +00:00
Fix linting issues in staging/src/k8s.io/client-go/discovery/*
Signed-off-by: Jay Lim <jay@imjching.com> Kubernetes-commit: 7a317e6262ac42dc20abd1596d505abda81b1ee3
This commit is contained in:
parent
982e3f5500
commit
c1a902d20d
@ -44,6 +44,7 @@ type memCacheClient struct {
|
|||||||
cacheValid bool
|
cacheValid bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error Constants
|
||||||
var (
|
var (
|
||||||
ErrCacheEmpty = errors.New("the cache has not been filled yet")
|
ErrCacheEmpty = errors.New("the cache has not been filled yet")
|
||||||
ErrCacheNotFound = errors.New("not found")
|
ErrCacheNotFound = errors.New("not found")
|
||||||
|
@ -94,6 +94,8 @@ func (d *CachedDiscoveryClient) ServerResources() ([]*metav1.APIResourceList, er
|
|||||||
return ServerResources(d)
|
return ServerResources(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerGroups returns the supported groups, with information like supported versions and the
|
||||||
|
// preferred version.
|
||||||
func (d *CachedDiscoveryClient) ServerGroups() (*metav1.APIGroupList, error) {
|
func (d *CachedDiscoveryClient) ServerGroups() (*metav1.APIGroupList, error) {
|
||||||
filename := filepath.Join(d.cacheDirectory, "servergroups.json")
|
filename := filepath.Join(d.cacheDirectory, "servergroups.json")
|
||||||
cachedBytes, err := d.getCachedFile(filename)
|
cachedBytes, err := d.getCachedFile(filename)
|
||||||
@ -202,26 +204,36 @@ func (d *CachedDiscoveryClient) writeCachedFile(filename string, obj runtime.Obj
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RESTClient returns a RESTClient that is used to communicate with API server
|
||||||
|
// by this client implementation.
|
||||||
func (d *CachedDiscoveryClient) RESTClient() restclient.Interface {
|
func (d *CachedDiscoveryClient) RESTClient() restclient.Interface {
|
||||||
return d.delegate.RESTClient()
|
return d.delegate.RESTClient()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerPreferredResources returns the supported resources with the version preferred by the
|
||||||
|
// server.
|
||||||
func (d *CachedDiscoveryClient) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
|
func (d *CachedDiscoveryClient) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
|
||||||
return ServerPreferredResources(d)
|
return ServerPreferredResources(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerPreferredNamespacedResources returns the supported namespaced resources with the
|
||||||
|
// version preferred by the server.
|
||||||
func (d *CachedDiscoveryClient) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
|
func (d *CachedDiscoveryClient) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
|
||||||
return ServerPreferredNamespacedResources(d)
|
return ServerPreferredNamespacedResources(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerVersion retrieves and parses the server's version (git version).
|
||||||
func (d *CachedDiscoveryClient) ServerVersion() (*version.Info, error) {
|
func (d *CachedDiscoveryClient) ServerVersion() (*version.Info, error) {
|
||||||
return d.delegate.ServerVersion()
|
return d.delegate.ServerVersion()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OpenAPISchema retrieves and parses the swagger API schema the server supports.
|
||||||
func (d *CachedDiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
|
func (d *CachedDiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
|
||||||
return d.delegate.OpenAPISchema()
|
return d.delegate.OpenAPISchema()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fresh is supposed to tell the caller whether or not to retry if the cache
|
||||||
|
// fails to find something (false = retry, true = no need to retry).
|
||||||
func (d *CachedDiscoveryClient) Fresh() bool {
|
func (d *CachedDiscoveryClient) Fresh() bool {
|
||||||
d.mutex.Lock()
|
d.mutex.Lock()
|
||||||
defer d.mutex.Unlock()
|
defer d.mutex.Unlock()
|
||||||
@ -229,6 +241,7 @@ func (d *CachedDiscoveryClient) Fresh() bool {
|
|||||||
return d.fresh
|
return d.fresh
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate enforces that no cached data is used in the future that is older than the current time.
|
||||||
func (d *CachedDiscoveryClient) Invalidate() {
|
func (d *CachedDiscoveryClient) Invalidate() {
|
||||||
d.mutex.Lock()
|
d.mutex.Lock()
|
||||||
defer d.mutex.Unlock()
|
defer d.mutex.Unlock()
|
||||||
|
@ -263,8 +263,8 @@ func ServerPreferredResources(d DiscoveryInterface) ([]*metav1.APIResourceList,
|
|||||||
|
|
||||||
result := []*metav1.APIResourceList{}
|
result := []*metav1.APIResourceList{}
|
||||||
grVersions := map[schema.GroupResource]string{} // selected version of a GroupResource
|
grVersions := map[schema.GroupResource]string{} // selected version of a GroupResource
|
||||||
grApiResources := map[schema.GroupResource]*metav1.APIResource{} // selected APIResource for a GroupResource
|
grAPIResources := map[schema.GroupResource]*metav1.APIResource{} // selected APIResource for a GroupResource
|
||||||
gvApiResourceLists := map[schema.GroupVersion]*metav1.APIResourceList{} // blueprint for a APIResourceList for later grouping
|
gvAPIResourceLists := map[schema.GroupVersion]*metav1.APIResourceList{} // blueprint for a APIResourceList for later grouping
|
||||||
|
|
||||||
for _, apiGroup := range serverGroupList.Groups {
|
for _, apiGroup := range serverGroupList.Groups {
|
||||||
for _, version := range apiGroup.Versions {
|
for _, version := range apiGroup.Versions {
|
||||||
@ -276,11 +276,11 @@ func ServerPreferredResources(d DiscoveryInterface) ([]*metav1.APIResourceList,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create empty list which is filled later in another loop
|
// create empty list which is filled later in another loop
|
||||||
emptyApiResourceList := metav1.APIResourceList{
|
emptyAPIResourceList := metav1.APIResourceList{
|
||||||
GroupVersion: version.GroupVersion,
|
GroupVersion: version.GroupVersion,
|
||||||
}
|
}
|
||||||
gvApiResourceLists[groupVersion] = &emptyApiResourceList
|
gvAPIResourceLists[groupVersion] = &emptyAPIResourceList
|
||||||
result = append(result, &emptyApiResourceList)
|
result = append(result, &emptyAPIResourceList)
|
||||||
|
|
||||||
for i := range apiResourceList.APIResources {
|
for i := range apiResourceList.APIResources {
|
||||||
apiResource := &apiResourceList.APIResources[i]
|
apiResource := &apiResourceList.APIResources[i]
|
||||||
@ -288,21 +288,21 @@ func ServerPreferredResources(d DiscoveryInterface) ([]*metav1.APIResourceList,
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
gv := schema.GroupResource{Group: apiGroup.Name, Resource: apiResource.Name}
|
gv := schema.GroupResource{Group: apiGroup.Name, Resource: apiResource.Name}
|
||||||
if _, ok := grApiResources[gv]; ok && version.Version != apiGroup.PreferredVersion.Version {
|
if _, ok := grAPIResources[gv]; ok && version.Version != apiGroup.PreferredVersion.Version {
|
||||||
// only override with preferred version
|
// only override with preferred version
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
grVersions[gv] = version.Version
|
grVersions[gv] = version.Version
|
||||||
grApiResources[gv] = apiResource
|
grAPIResources[gv] = apiResource
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// group selected APIResources according to GroupVersion into APIResourceLists
|
// group selected APIResources according to GroupVersion into APIResourceLists
|
||||||
for groupResource, apiResource := range grApiResources {
|
for groupResource, apiResource := range grAPIResources {
|
||||||
version := grVersions[groupResource]
|
version := grVersions[groupResource]
|
||||||
groupVersion := schema.GroupVersion{Group: groupResource.Group, Version: version}
|
groupVersion := schema.GroupVersion{Group: groupResource.Group, Version: version}
|
||||||
apiResourceList := gvApiResourceLists[groupVersion]
|
apiResourceList := gvAPIResourceLists[groupVersion]
|
||||||
apiResourceList.APIResources = append(apiResourceList.APIResources, *apiResource)
|
apiResourceList.APIResources = append(apiResourceList.APIResources, *apiResource)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -464,9 +464,9 @@ func NewDiscoveryClient(c restclient.Interface) *DiscoveryClient {
|
|||||||
|
|
||||||
// RESTClient returns a RESTClient that is used to communicate
|
// RESTClient returns a RESTClient that is used to communicate
|
||||||
// with API server by this client implementation.
|
// with API server by this client implementation.
|
||||||
func (c *DiscoveryClient) RESTClient() restclient.Interface {
|
func (d *DiscoveryClient) RESTClient() restclient.Interface {
|
||||||
if c == nil {
|
if d == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return c.restClient
|
return d.restClient
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,8 @@ type FakeDiscovery struct {
|
|||||||
FakedServerVersion *version.Info
|
FakedServerVersion *version.Info
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerResourcesForGroupVersion returns the supported resources for a group
|
||||||
|
// and version.
|
||||||
func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
|
func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
|
||||||
action := testing.ActionImpl{
|
action := testing.ActionImpl{
|
||||||
Verb: "get",
|
Verb: "get",
|
||||||
@ -50,6 +52,7 @@ func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*me
|
|||||||
return nil, fmt.Errorf("GroupVersion %q not found", groupVersion)
|
return nil, fmt.Errorf("GroupVersion %q not found", groupVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerResources returns the supported resources for all groups and versions.
|
||||||
func (c *FakeDiscovery) ServerResources() ([]*metav1.APIResourceList, error) {
|
func (c *FakeDiscovery) ServerResources() ([]*metav1.APIResourceList, error) {
|
||||||
action := testing.ActionImpl{
|
action := testing.ActionImpl{
|
||||||
Verb: "get",
|
Verb: "get",
|
||||||
@ -59,14 +62,20 @@ func (c *FakeDiscovery) ServerResources() ([]*metav1.APIResourceList, error) {
|
|||||||
return c.Resources, nil
|
return c.Resources, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerPreferredResources returns the supported resources with the version
|
||||||
|
// preferred by the server.
|
||||||
func (c *FakeDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
|
func (c *FakeDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerPreferredNamespacedResources returns the supported namespaced resources
|
||||||
|
// with the version preferred by the server.
|
||||||
func (c *FakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
|
func (c *FakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerGroups returns the supported groups, with information like supported
|
||||||
|
// versions and the preferred version.
|
||||||
func (c *FakeDiscovery) ServerGroups() (*metav1.APIGroupList, error) {
|
func (c *FakeDiscovery) ServerGroups() (*metav1.APIGroupList, error) {
|
||||||
action := testing.ActionImpl{
|
action := testing.ActionImpl{
|
||||||
Verb: "get",
|
Verb: "get",
|
||||||
@ -108,6 +117,7 @@ func (c *FakeDiscovery) ServerGroups() (*metav1.APIGroupList, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerVersion retrieves and parses the server's version.
|
||||||
func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
|
func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
|
||||||
action := testing.ActionImpl{}
|
action := testing.ActionImpl{}
|
||||||
action.Verb = "get"
|
action.Verb = "get"
|
||||||
@ -122,10 +132,13 @@ func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
|
|||||||
return &versionInfo, nil
|
return &versionInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OpenAPISchema retrieves and parses the swagger API schema the server supports.
|
||||||
func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
|
func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
|
||||||
return &openapi_v2.Document{}, nil
|
return &openapi_v2.Document{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RESTClient returns a RESTClient that is used to communicate with API server
|
||||||
|
// by this client implementation.
|
||||||
func (c *FakeDiscovery) RESTClient() restclient.Interface {
|
func (c *FakeDiscovery) RESTClient() restclient.Interface {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -31,11 +31,11 @@ import (
|
|||||||
func MatchesServerVersion(clientVersion apimachineryversion.Info, client DiscoveryInterface) error {
|
func MatchesServerVersion(clientVersion apimachineryversion.Info, client DiscoveryInterface) error {
|
||||||
sVer, err := client.ServerVersion()
|
sVer, err := client.ServerVersion()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("couldn't read version from server: %v\n", err)
|
return fmt.Errorf("couldn't read version from server: %v", err)
|
||||||
}
|
}
|
||||||
// GitVersion includes GitCommit and GitTreeState, but best to be safe?
|
// GitVersion includes GitCommit and GitTreeState, but best to be safe?
|
||||||
if clientVersion.GitVersion != sVer.GitVersion || clientVersion.GitCommit != sVer.GitCommit || clientVersion.GitTreeState != sVer.GitTreeState {
|
if clientVersion.GitVersion != sVer.GitVersion || clientVersion.GitCommit != sVer.GitCommit || clientVersion.GitTreeState != sVer.GitTreeState {
|
||||||
return fmt.Errorf("server version (%#v) differs from client version (%#v)!\n", sVer, clientVersion)
|
return fmt.Errorf("server version (%#v) differs from client version (%#v)", sVer, clientVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -101,12 +101,15 @@ func FilteredBy(pred ResourcePredicate, rls []*metav1.APIResourceList) []*metav1
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResourcePredicate has a method to check if a resource matches a given condition.
|
||||||
type ResourcePredicate interface {
|
type ResourcePredicate interface {
|
||||||
Match(groupVersion string, r *metav1.APIResource) bool
|
Match(groupVersion string, r *metav1.APIResource) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResourcePredicateFunc returns true if it matches a resource based on a custom condition.
|
||||||
type ResourcePredicateFunc func(groupVersion string, r *metav1.APIResource) bool
|
type ResourcePredicateFunc func(groupVersion string, r *metav1.APIResource) bool
|
||||||
|
|
||||||
|
// Match is a wrapper around ResourcePredicateFunc.
|
||||||
func (fn ResourcePredicateFunc) Match(groupVersion string, r *metav1.APIResource) bool {
|
func (fn ResourcePredicateFunc) Match(groupVersion string, r *metav1.APIResource) bool {
|
||||||
return fn(groupVersion, r)
|
return fn(groupVersion, r)
|
||||||
}
|
}
|
||||||
@ -116,6 +119,7 @@ type SupportsAllVerbs struct {
|
|||||||
Verbs []string
|
Verbs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Match checks if a resource contains all the given verbs.
|
||||||
func (p SupportsAllVerbs) Match(groupVersion string, r *metav1.APIResource) bool {
|
func (p SupportsAllVerbs) Match(groupVersion string, r *metav1.APIResource) bool {
|
||||||
return sets.NewString([]string(r.Verbs)...).HasAll(p.Verbs...)
|
return sets.NewString([]string(r.Verbs)...).HasAll(p.Verbs...)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user