github.com/kiali/kiali@v1.84.0/kubernetes/kubetest/fake.go (about) 1 package kubetest 2 3 import ( 4 "context" 5 6 osappsfake "github.com/openshift/client-go/apps/clientset/versioned/fake" 7 osappsscheme "github.com/openshift/client-go/apps/clientset/versioned/scheme" 8 oauthfake "github.com/openshift/client-go/oauth/clientset/versioned/fake" 9 oauthscheme "github.com/openshift/client-go/oauth/clientset/versioned/scheme" 10 projectfake "github.com/openshift/client-go/project/clientset/versioned/fake" 11 projectscheme "github.com/openshift/client-go/project/clientset/versioned/scheme" 12 routefake "github.com/openshift/client-go/route/clientset/versioned/fake" 13 routescheme "github.com/openshift/client-go/route/clientset/versioned/scheme" 14 userfake "github.com/openshift/client-go/user/clientset/versioned/fake" 15 userscheme "github.com/openshift/client-go/user/clientset/versioned/scheme" 16 networking_v1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1" 17 istio "istio.io/client-go/pkg/clientset/versioned" 18 istiofake "istio.io/client-go/pkg/clientset/versioned/fake" 19 istioscheme "istio.io/client-go/pkg/clientset/versioned/scheme" 20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 "k8s.io/apimachinery/pkg/runtime" 22 "k8s.io/client-go/kubernetes" 23 kubefake "k8s.io/client-go/kubernetes/fake" 24 kubescheme "k8s.io/client-go/kubernetes/scheme" 25 gatewayapi "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned" 26 gatewayapifake "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/fake" 27 gatewayapischeme "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/scheme" 28 29 kialikube "github.com/kiali/kiali/kubernetes" 30 ) 31 32 func isIstioResource(obj runtime.Object) bool { 33 _, _, err := istioscheme.Scheme.ObjectKinds(obj) 34 return err == nil 35 } 36 37 func isKubeResource(obj runtime.Object) bool { 38 _, _, err := kubescheme.Scheme.ObjectKinds(obj) 39 return err == nil 40 } 41 42 func isGatewayAPIResource(obj runtime.Object) bool { 43 _, _, err := gatewayapischeme.Scheme.ObjectKinds(obj) 44 return err == nil 45 } 46 47 func isProjectResource(obj runtime.Object) bool { 48 _, _, err := projectscheme.Scheme.ObjectKinds(obj) 49 return err == nil 50 } 51 52 func isRouteResource(obj runtime.Object) bool { 53 _, _, err := routescheme.Scheme.ObjectKinds(obj) 54 return err == nil 55 } 56 57 func isOSAppsResource(obj runtime.Object) bool { 58 _, _, err := osappsscheme.Scheme.ObjectKinds(obj) 59 return err == nil 60 } 61 62 func isUserResource(obj runtime.Object) bool { 63 _, _, err := userscheme.Scheme.ObjectKinds(obj) 64 return err == nil 65 } 66 67 func isOAuthResource(obj runtime.Object) bool { 68 _, _, err := oauthscheme.Scheme.ObjectKinds(obj) 69 return err == nil 70 } 71 72 // NewFakeK8sClient creates a new fake kubernetes client for testing purposes. 73 func NewFakeK8sClient(objects ...runtime.Object) *FakeK8sClient { 74 // NOTE: The kube fake client object tracker guesses the resource name based on the Kind. 75 // For a plural resource, it will convert the kind to lowercase and add an "ies" to the end. 76 // In the case of objects like "Gateway" where the plural is actually "Gateways", the conversion 77 // is wrong. The guessing of the resource name only happens with tracker.Add inside of NewSimpleClientset. 78 // If we create the object after creating the clientset, then the tracker will use the correct resource name. 79 var ( 80 kubeObjects []runtime.Object 81 istioObjects []runtime.Object 82 gatewayapiObjects []runtime.Object 83 osAppsObjects []runtime.Object 84 routeObjects []runtime.Object 85 projectObjects []runtime.Object 86 userObjects []runtime.Object 87 oAuthObjects []runtime.Object 88 istioGateways []*networking_v1beta1.Gateway 89 ) 90 91 for _, obj := range objects { 92 o := obj 93 switch { 94 case isKubeResource(o): 95 kubeObjects = append(kubeObjects, o) 96 case isIstioResource(o): 97 if gw, ok := o.(*networking_v1beta1.Gateway); ok { 98 istioGateways = append(istioGateways, gw) 99 } else { 100 istioObjects = append(istioObjects, o) 101 } 102 case isGatewayAPIResource(o): 103 gatewayapiObjects = append(gatewayapiObjects, o) 104 case isOSAppsResource(o): 105 osAppsObjects = append(osAppsObjects, o) 106 case isRouteResource(o): 107 routeObjects = append(routeObjects, o) 108 case isProjectResource(o): 109 projectObjects = append(projectObjects, o) 110 case isUserResource(o): 111 userObjects = append(userObjects, o) 112 case isOAuthResource(o): 113 oAuthObjects = append(oAuthObjects, o) 114 } 115 } 116 117 kubeClient := kubefake.NewSimpleClientset(kubeObjects...) 118 istioClient := istiofake.NewSimpleClientset(istioObjects...) 119 gatewayAPIClient := gatewayapifake.NewSimpleClientset(gatewayapiObjects...) 120 osAppsClient := osappsfake.NewSimpleClientset(osAppsObjects...) 121 projectClient := projectfake.NewSimpleClientset(projectObjects...) 122 routeClient := routefake.NewSimpleClientset(routeObjects...) 123 userClient := userfake.NewSimpleClientset(userObjects...) 124 oAuthClient := oauthfake.NewSimpleClientset(oAuthObjects...) 125 126 // These are created separately because the fake clientset guesses the resource name based on the Kind. 127 for _, gw := range istioGateways { 128 if _, err := istioClient.NetworkingV1beta1().Gateways(gw.Namespace).Create(context.TODO(), gw, metav1.CreateOptions{}); err != nil { 129 panic(err) 130 } 131 } 132 133 return &FakeK8sClient{ 134 ClientInterface: kialikube.NewClient(kubeClient, istioClient, gatewayAPIClient, osAppsClient, projectClient, routeClient, userClient, oAuthClient), 135 KubeClientset: kubeClient, 136 IstioClientset: istioClient, 137 ProjectFake: projectClient, 138 IstioAPIEnabled: true, 139 } 140 } 141 142 // FakeK8sClient is an implementation of the kiali Kubernetes client interface used for tests. 143 type FakeK8sClient struct { 144 OpenShift bool 145 GatewayAPIEnabled bool 146 IstioAPIEnabled bool 147 kialikube.ClientInterface 148 // Underlying kubernetes clientset. 149 KubeClientset kubernetes.Interface 150 // Underlying istio clientset. 151 IstioClientset istio.Interface 152 // Underlying gateway api clientset. 153 GatewayAPIClientset gatewayapi.Interface 154 // Token is the kiali token this client uses. 155 Token string 156 KubeClusterInfo kialikube.ClusterInfo 157 ProjectFake *projectfake.Clientset 158 } 159 160 func (c *FakeK8sClient) IsOpenShift() bool { return c.OpenShift } 161 func (c *FakeK8sClient) IsExpGatewayAPI() bool { return c.GatewayAPIEnabled } 162 func (c *FakeK8sClient) IsGatewayAPI() bool { return c.GatewayAPIEnabled } 163 func (c *FakeK8sClient) IsIstioAPI() bool { return c.IstioAPIEnabled } 164 func (c *FakeK8sClient) GetToken() string { return c.Token } 165 func (c *FakeK8sClient) ClusterInfo() kialikube.ClusterInfo { return c.KubeClusterInfo } 166 167 var _ kialikube.ClientInterface = &FakeK8sClient{}