...

Source file src/github.com/redhat-developer/odo/pkg/kclient/fake/discovery.go

Documentation: github.com/redhat-developer/odo/pkg/kclient/fake

     1  package fake
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"strings"
     7  	"sync"
     8  
     9  	kerrors "k8s.io/apimachinery/pkg/api/errors"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"k8s.io/apimachinery/pkg/runtime/schema"
    12  	"k8s.io/apimachinery/pkg/version"
    13  	"k8s.io/client-go/discovery/fake"
    14  )
    15  
    16  type ResourceMapEntry struct {
    17  	list *metav1.APIResourceList
    18  }
    19  
    20  type FakeDiscovery struct {
    21  	*fake.FakeDiscovery
    22  
    23  	lock        sync.Mutex
    24  	resourceMap map[string]*ResourceMapEntry
    25  }
    26  
    27  // NewFakeDiscovery returns a FakeDiscovery instance
    28  func NewFakeDiscovery() *FakeDiscovery {
    29  	return &FakeDiscovery{resourceMap: make(map[string]*ResourceMapEntry, 7)}
    30  }
    31  
    32  // AddResourceList appends a resource to the APIResourceList
    33  func (c *FakeDiscovery) AddResourceList(key string, are *metav1.APIResourceList) {
    34  	c.resourceMap[key] = &ResourceMapEntry{list: are}
    35  }
    36  
    37  // ServerResourcesForGroupVersion returns the supported resources for a group and version.
    38  func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
    39  	c.lock.Lock()
    40  	defer c.lock.Unlock()
    41  	found := false
    42  	arl := metav1.APIResourceList{
    43  		GroupVersion: groupVersion,
    44  		APIResources: nil,
    45  	}
    46  	for k, v := range c.resourceMap {
    47  		if strings.Contains(k, groupVersion) {
    48  			found = true
    49  			arl.APIResources = append(arl.APIResources, v.list.APIResources...)
    50  		}
    51  	}
    52  	if found {
    53  		return &arl, nil
    54  	}
    55  	return nil, kerrors.NewNotFound(schema.GroupResource{}, "")
    56  }
    57  
    58  // ServerVersion returns the server version information of the FakeDiscovery client instance
    59  func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
    60  	versionInfo := version.Info{
    61  		Major:        "1",
    62  		Minor:        "16",
    63  		GitVersion:   "v1.16.0+0000000",
    64  		GitCommit:    "",
    65  		GitTreeState: "",
    66  		BuildDate:    "",
    67  		GoVersion:    runtime.Version(),
    68  		Compiler:     runtime.Compiler,
    69  		Platform:     fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
    70  	}
    71  
    72  	return &versionInfo, nil
    73  }
    74  

View as plain text