github.com/iter8-tools/iter8@v1.1.2/controllers/k8sclient/fake/simple.go (about) 1 // Package fake provides fake Kuberntes clients for testing 2 package fake 3 4 import ( 5 "context" 6 7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 8 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 9 "k8s.io/apimachinery/pkg/runtime" 10 "k8s.io/apimachinery/pkg/runtime/schema" 11 "k8s.io/apimachinery/pkg/types" 12 fakedynamic "k8s.io/client-go/dynamic/fake" 13 fakek8s "k8s.io/client-go/kubernetes/fake" 14 ) 15 16 // Client provides structured and dynamic fake clients 17 type Client struct { 18 *fakek8s.Clientset 19 *fakedynamic.FakeDynamicClient 20 } 21 22 /* 23 Patch applies a patch for a resource. 24 Important: fake clients should not be used for server-side apply or strategic-merge patches. 25 https://github.com/kubernetes/kubernetes/pull/78630#issuecomment-500424163 26 27 Hence, we are mocking the Patch call in this fake client so that, 28 instead of server-side apply as in the real client, we perform of merge patch instead. 29 */ 30 func (cl *Client) Patch(gvr schema.GroupVersionResource, objNamespace string, objName string, jsonBytes []byte) (*unstructured.Unstructured, error) { 31 return cl.FakeDynamicClient.Resource(gvr).Namespace(objNamespace).Patch(context.TODO(), objName, types.MergePatchType, jsonBytes, metav1.PatchOptions{}) 32 } 33 34 // New returns a new fake Kubernetes client populated with runtime objects 35 func New(sObjs []runtime.Object, unsObjs []runtime.Object) *Client { 36 s := runtime.NewScheme() 37 return &Client{ 38 fakek8s.NewSimpleClientset(sObjs...), 39 fakedynamic.NewSimpleDynamicClientWithCustomListKinds(s, map[schema.GroupVersionResource]string{ 40 { 41 Group: "apps", 42 Version: "v1", 43 Resource: "deployments", 44 }: "DeploymentList", 45 { 46 Group: "", 47 Version: "v1", 48 Resource: "configmaps", 49 }: "ConfigMapList", 50 { 51 Group: "networking.istio.io", 52 Version: "v1beta1", 53 Resource: "virtualservices", 54 }: "VirtualServiceList", 55 { 56 Group: "", 57 Version: "v1", 58 Resource: "services", 59 }: "ServiceList", 60 { 61 Group: "serving.kserve.io", 62 Version: "v1beta1", 63 Resource: "inferenceservices", 64 }: "InferenceServiceList", 65 { 66 Group: "", 67 Version: "v1", 68 Resource: "secrets", 69 }: "SecretList", 70 }, unsObjs...), 71 } 72 }