github.com/verrazzano/verrazzano@v1.7.1/pkg/k8sutil/fake/clientset.go (about)

     1  // Copyright (c) 2021, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package fake
     5  
     6  import (
     7  	"k8s.io/apimachinery/pkg/runtime"
     8  	"k8s.io/apimachinery/pkg/runtime/schema"
     9  	"k8s.io/client-go/kubernetes"
    10  	fakeclientset "k8s.io/client-go/kubernetes/fake"
    11  	corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
    12  	fakecorev1 "k8s.io/client-go/kubernetes/typed/core/v1/fake"
    13  	restclient "k8s.io/client-go/rest"
    14  	fakeRESTClient "k8s.io/client-go/rest/fake"
    15  )
    16  
    17  func NewClientsetConfig(objects ...runtime.Object) (*restclient.Config, kubernetes.Interface) {
    18  	cfg := &restclient.Config{}
    19  	client := fakeclientset.NewSimpleClientset(objects...)
    20  	wrappedClient := &WrappedClientset{Clientset: client}
    21  	return cfg, wrappedClient
    22  }
    23  
    24  /*
    25  There is a deficiency in the FakeCoreV1 implementation that returns a nil RESTClient
    26  To get around this limitation, we wrap the fakes in our own types, which supply a valid
    27  RESTClient for unit testing.
    28  */
    29  type (
    30  	WrappedClientset struct {
    31  		*fakeclientset.Clientset
    32  	}
    33  	WrappedCoreV1 struct {
    34  		fakecorev1.FakeCoreV1
    35  		RestClient restclient.Interface
    36  	}
    37  )
    38  
    39  func (w *WrappedClientset) CoreV1() corev1.CoreV1Interface {
    40  	return &WrappedCoreV1{
    41  		FakeCoreV1: fakecorev1.FakeCoreV1{
    42  			Fake: &w.Fake,
    43  		},
    44  		RestClient: &fakeRESTClient.RESTClient{GroupVersion: schema.GroupVersion{Version: "v1"}},
    45  	}
    46  }
    47  
    48  func (w *WrappedCoreV1) RESTClient() restclient.Interface {
    49  	return w.RestClient
    50  }