github.com/oam-dev/cluster-gateway@v1.9.0/e2e/roundtrip/clustergateway.go (about) 1 package roundtrip 2 3 import ( 4 "context" 5 6 . "github.com/onsi/ginkgo/v2" 7 . "github.com/onsi/gomega" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 "k8s.io/apimachinery/pkg/types" 10 "k8s.io/client-go/kubernetes" 11 clusterv1 "open-cluster-management.io/api/cluster/v1" 12 13 "github.com/oam-dev/cluster-gateway/e2e/framework" 14 multicluster "github.com/oam-dev/cluster-gateway/pkg/apis/cluster/transport" 15 clusterv1alpha1 "github.com/oam-dev/cluster-gateway/pkg/apis/cluster/v1alpha1" 16 ) 17 18 const ( 19 roundtripTestBasename = "roundtrip" 20 ) 21 22 var _ = Describe("Basic RoundTrip Test", func() { 23 f := framework.NewE2EFramework(roundtripTestBasename) 24 25 It("ClusterGateway in the API discovery", 26 func() { 27 By("Discovering ClusterGateway") 28 nativeClient := f.HubNativeClient() 29 resources, err := nativeClient.Discovery(). 30 ServerResourcesForGroupVersion("cluster.core.oam.dev/v1alpha1") 31 Expect(err).NotTo(HaveOccurred()) 32 apiFound := false 33 for _, resource := range resources.APIResources { 34 if resource.Kind == "ClusterGateway" { 35 apiFound = true 36 } 37 } 38 if !apiFound { 39 Fail(`Api ClusterGateway not found`) 40 } 41 }) 42 43 It("ManagedCluster present", 44 func() { 45 By("Getting ManagedCluster") 46 if f.IsOCMInstalled() { 47 runtimeClient := f.HubRuntimeClient() 48 cluster := &clusterv1.ManagedCluster{} 49 err := runtimeClient.Get(context.TODO(), types.NamespacedName{ 50 Name: f.TestClusterName(), 51 }, cluster) 52 Expect(err).NotTo(HaveOccurred()) 53 } 54 }) 55 56 It("ClusterGateway can be read via GET", 57 func() { 58 By("Getting ClusterGateway") 59 runtimeClient := f.HubRuntimeClient() 60 clusterGateway := &clusterv1alpha1.ClusterGateway{} 61 err := runtimeClient.Get(context.TODO(), types.NamespacedName{ 62 Name: f.TestClusterName(), 63 }, clusterGateway) 64 Expect(err).NotTo(HaveOccurred()) 65 }) 66 67 It("ClusterGateway can be read via LIST", 68 func() { 69 By("Getting ClusterGateway") 70 runtimeClient := f.HubRuntimeClient() 71 clusterGatewayList := &clusterv1alpha1.ClusterGatewayList{} 72 err := runtimeClient.List(context.TODO(), clusterGatewayList) 73 Expect(err).NotTo(HaveOccurred()) 74 clusterFound := false 75 for _, clusterGateway := range clusterGatewayList.Items { 76 if clusterGateway.Name == f.TestClusterName() { 77 clusterFound = true 78 } 79 } 80 if !clusterFound { 81 Fail(`ClusterGateway not found`) 82 } 83 }) 84 85 It("ClusterGateway healthiness can be manipulated", 86 func() { 87 By("get healthiness") 88 gw, err := f.HubGatewayClient(). 89 ClusterV1alpha1(). 90 ClusterGateways(). 91 GetHealthiness(context.TODO(), f.TestClusterName(), metav1.GetOptions{}) 92 Expect(err).NotTo(HaveOccurred()) 93 Expect(gw).ShouldNot(BeNil()) 94 Expect(gw.Status.Healthy).To(BeFalse()) 95 By("update healthiness") 96 gw.Status.Healthy = true 97 gw.Status.HealthyReason = clusterv1alpha1.HealthyReasonTypeConnectionTimeout 98 updated, err := f.HubGatewayClient(). 99 ClusterV1alpha1(). 100 ClusterGateways(). 101 UpdateHealthiness(context.TODO(), gw, metav1.UpdateOptions{}) 102 Expect(err).NotTo(HaveOccurred()) 103 Expect(updated).NotTo(BeNil()) 104 Expect(updated.Status.Healthy).To(BeTrue()) 105 Expect(updated.Status.HealthyReason).To(Equal(clusterv1alpha1.HealthyReasonTypeConnectionTimeout)) 106 }) 107 108 It("Probing cluster health (raw)", 109 func() { 110 resp, err := f.HubNativeClient().Discovery(). 111 RESTClient(). 112 Get(). 113 AbsPath( 114 "apis/cluster.core.oam.dev/v1alpha1/clustergateways", 115 f.TestClusterName(), 116 "proxy", 117 "healthz", 118 ).DoRaw(context.TODO()) 119 Expect(err).NotTo(HaveOccurred()) 120 Expect(string(resp)).To(Equal("ok")) 121 }) 122 123 It("Probing cluster health (context)", 124 func() { 125 cfg := f.HubRESTConfig() 126 cfg.WrapTransport = multicluster.NewClusterGatewayRoundTripper 127 multiClusterClient, err := kubernetes.NewForConfig(cfg) 128 Expect(err).NotTo(HaveOccurred()) 129 resp, err := multiClusterClient.RESTClient(). 130 Get(). 131 AbsPath("healthz"). 132 DoRaw(multicluster.WithMultiClusterContext(context.TODO(), f.TestClusterName())) 133 Expect(err).NotTo(HaveOccurred()) 134 Expect(string(resp)).To(Equal("ok")) 135 }) 136 137 })