...

Source file src/k8s.io/client-go/discovery/fake/discovery.go

Documentation: k8s.io/client-go/discovery/fake

     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  	"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  // FakeDiscovery implements discovery.DiscoveryInterface and sometimes calls testing.Fake.Invoke with an action,
    37  // 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.
    38  type FakeDiscovery struct {
    39  	*testing.Fake
    40  	FakedServerVersion *version.Info
    41  }
    42  
    43  // ServerResourcesForGroupVersion returns the supported resources for a group
    44  // and version.
    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  	c.Invokes(action, nil)
    51  	for _, resourceList := range c.Resources {
    52  		if resourceList.GroupVersion == groupVersion {
    53  			return resourceList, nil
    54  		}
    55  	}
    56  	return nil, &errors.StatusError{
    57  		ErrStatus: metav1.Status{
    58  			Status:  metav1.StatusFailure,
    59  			Code:    http.StatusNotFound,
    60  			Reason:  metav1.StatusReasonNotFound,
    61  			Message: fmt.Sprintf("the server could not find the requested resource, GroupVersion %q not found", groupVersion),
    62  		}}
    63  }
    64  
    65  // ServerGroupsAndResources returns the supported groups and resources for all groups and versions.
    66  func (c *FakeDiscovery) ServerGroupsAndResources() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
    67  	sgs, err := c.ServerGroups()
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  	resultGroups := []*metav1.APIGroup{}
    72  	for i := range sgs.Groups {
    73  		resultGroups = append(resultGroups, &sgs.Groups[i])
    74  	}
    75  
    76  	action := testing.ActionImpl{
    77  		Verb:     "get",
    78  		Resource: schema.GroupVersionResource{Resource: "resource"},
    79  	}
    80  	c.Invokes(action, nil)
    81  	return resultGroups, c.Resources, nil
    82  }
    83  
    84  // ServerPreferredResources returns the supported resources with the version
    85  // preferred by the server.
    86  func (c *FakeDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
    87  	return nil, nil
    88  }
    89  
    90  // ServerPreferredNamespacedResources returns the supported namespaced resources
    91  // with the version preferred by the server.
    92  func (c *FakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
    93  	return nil, nil
    94  }
    95  
    96  // ServerGroups returns the supported groups, with information like supported
    97  // versions and the preferred version.
    98  func (c *FakeDiscovery) ServerGroups() (*metav1.APIGroupList, error) {
    99  	action := testing.ActionImpl{
   100  		Verb:     "get",
   101  		Resource: schema.GroupVersionResource{Resource: "group"},
   102  	}
   103  	c.Invokes(action, nil)
   104  
   105  	groups := map[string]*metav1.APIGroup{}
   106  
   107  	for _, res := range c.Resources {
   108  		gv, err := schema.ParseGroupVersion(res.GroupVersion)
   109  		if err != nil {
   110  			return nil, err
   111  		}
   112  		group := groups[gv.Group]
   113  		if group == nil {
   114  			group = &metav1.APIGroup{
   115  				Name: gv.Group,
   116  				PreferredVersion: metav1.GroupVersionForDiscovery{
   117  					GroupVersion: res.GroupVersion,
   118  					Version:      gv.Version,
   119  				},
   120  			}
   121  			groups[gv.Group] = group
   122  		}
   123  
   124  		group.Versions = append(group.Versions, metav1.GroupVersionForDiscovery{
   125  			GroupVersion: res.GroupVersion,
   126  			Version:      gv.Version,
   127  		})
   128  	}
   129  
   130  	list := &metav1.APIGroupList{}
   131  	for _, apiGroup := range groups {
   132  		list.Groups = append(list.Groups, *apiGroup)
   133  	}
   134  
   135  	return list, nil
   136  
   137  }
   138  
   139  // ServerVersion retrieves and parses the server's version.
   140  func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
   141  	action := testing.ActionImpl{}
   142  	action.Verb = "get"
   143  	action.Resource = schema.GroupVersionResource{Resource: "version"}
   144  	_, err := c.Invokes(action, nil)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	if c.FakedServerVersion != nil {
   150  		return c.FakedServerVersion, nil
   151  	}
   152  
   153  	versionInfo := kubeversion.Get()
   154  	return &versionInfo, nil
   155  }
   156  
   157  // OpenAPISchema retrieves and parses the swagger API schema the server supports.
   158  func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
   159  	return &openapi_v2.Document{}, nil
   160  }
   161  
   162  func (c *FakeDiscovery) OpenAPIV3() openapi.Client {
   163  	panic("unimplemented")
   164  }
   165  
   166  // RESTClient returns a RESTClient that is used to communicate with API server
   167  // by this client implementation.
   168  func (c *FakeDiscovery) RESTClient() restclient.Interface {
   169  	return nil
   170  }
   171  
   172  func (c *FakeDiscovery) WithLegacy() discovery.DiscoveryInterface {
   173  	panic("unimplemented")
   174  }
   175  

View as plain text