@@ -3,6 +3,7 @@ package e2e
33import (
44 "fmt"
55 "net/http"
6+ "os"
67
78 "github.com/onsi/ginkgo/v2"
89 "github.com/onsi/gomega"
@@ -11,8 +12,11 @@ import (
1112 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213 "k8s.io/apimachinery/pkg/util/rand"
1314 "k8s.io/client-go/kubernetes"
15+ "k8s.io/client-go/tools/clientcmd"
1416 "k8s.io/klog/v2"
1517
18+ "github.com/karmada-io/karmada/pkg/karmadactl"
19+ "github.com/karmada-io/karmada/pkg/karmadactl/options"
1620 "github.com/karmada-io/karmada/test/e2e/framework"
1721 "github.com/karmada-io/karmada/test/helper"
1822)
@@ -21,7 +25,7 @@ const (
2125 clusterProxy = "/apis/cluster.karmada.io/v1alpha1/clusters/%s/proxy/"
2226)
2327
24- var _ = ginkgo . Describe ("Aggregated Kubernetes API Endpoint testing" , func () {
28+ var _ = framework . SerialDescribe ("Aggregated Kubernetes API Endpoint testing" , func () {
2529 var member1 , member2 string
2630 var saName , saNamespace string
2731 var tomServiceAccount * corev1.ServiceAccount
@@ -31,6 +35,78 @@ var _ = ginkgo.Describe("Aggregated Kubernetes API Endpoint testing", func() {
3135 var tomClusterRoleOnMember * rbacv1.ClusterRole
3236 var tomClusterRoleBindingOnMember * rbacv1.ClusterRoleBinding
3337
38+ var (
39+ clusterName string
40+ homeDir string
41+ kubeConfigPath string
42+ clusterContext string
43+ controlPlane string
44+ karmadaConfig karmadactl.KarmadaConfig
45+
46+ secretStoreNamespace string
47+ )
48+
49+ ginkgo .BeforeEach (func () {
50+ clusterName = "member-e2e-" + rand .String (RandomStrLength )
51+ homeDir = os .Getenv ("HOME" )
52+ kubeConfigPath = fmt .Sprintf ("%s/.kube/%s.config" , homeDir , clusterName )
53+ clusterContext = fmt .Sprintf ("kind-%s" , clusterName )
54+ controlPlane = fmt .Sprintf ("%s-control-plane" , clusterName )
55+ karmadaConfig = karmadactl .NewKarmadaConfig (clientcmd .NewDefaultPathOptions ())
56+
57+ secretStoreNamespace = "test-" + rand .String (RandomStrLength )
58+ })
59+
60+ ginkgo .BeforeEach (func () {
61+ ginkgo .By (fmt .Sprintf ("Create cluster: %s" , clusterName ), func () {
62+ err := createCluster (clusterName , kubeConfigPath , controlPlane , clusterContext )
63+ gomega .Expect (err ).ShouldNot (gomega .HaveOccurred ())
64+ })
65+ ginkgo .DeferCleanup (func () {
66+ ginkgo .By (fmt .Sprintf ("Deleting clusters: %s" , clusterName ), func () {
67+ err := deleteCluster (clusterName , kubeConfigPath )
68+ gomega .Expect (err ).ShouldNot (gomega .HaveOccurred ())
69+ _ = os .Remove (kubeConfigPath )
70+ })
71+ })
72+ })
73+
74+ ginkgo .BeforeEach (func () {
75+ ginkgo .By (fmt .Sprintf ("Joinning cluster: %s" , clusterName ), func () {
76+ opts := karmadactl.CommandJoinOption {
77+ GlobalCommandOptions : options.GlobalCommandOptions {
78+ KarmadaContext : karmadaContext ,
79+ },
80+ DryRun : false ,
81+ ClusterNamespace : secretStoreNamespace ,
82+ ClusterName : clusterName ,
83+ ClusterContext : clusterContext ,
84+ ClusterKubeConfig : kubeConfigPath ,
85+ }
86+ err := karmadactl .RunJoin (karmadaConfig , opts )
87+ gomega .Expect (err ).ShouldNot (gomega .HaveOccurred ())
88+ })
89+ })
90+
91+ ginkgo .AfterEach (func () {
92+ ginkgo .By (fmt .Sprintf ("Unjoinning cluster: %s" , clusterName ), func () {
93+ karmadaConfig := karmadactl .NewKarmadaConfig (clientcmd .NewDefaultPathOptions ())
94+ opts := karmadactl.CommandUnjoinOption {
95+ GlobalCommandOptions : options.GlobalCommandOptions {
96+ KarmadaContext : karmadaContext ,
97+ },
98+ DryRun : false ,
99+ ClusterNamespace : secretStoreNamespace ,
100+ ClusterName : clusterName ,
101+ ClusterContext : clusterContext ,
102+ ClusterKubeConfig : kubeConfigPath ,
103+ Wait : 5 * options .DefaultKarmadactlCommandDuration ,
104+ }
105+ err := karmadactl .RunUnjoin (karmadaConfig , opts )
106+ gomega .Expect (err ).ShouldNot (gomega .HaveOccurred ())
107+ })
108+ })
109+
34110 ginkgo .BeforeEach (func () {
35111 member1 , member2 = "member1" , "member2"
36112
@@ -52,7 +128,7 @@ var _ = ginkgo.Describe("Aggregated Kubernetes API Endpoint testing", func() {
52128 APIGroups : []string {"cluster.karmada.io" },
53129 Verbs : []string {"*" },
54130 Resources : []string {"clusters/proxy" },
55- ResourceNames : []string {member1 },
131+ ResourceNames : []string {member1 , clusterName },
56132 },
57133 })
58134 tomClusterRoleBinding = helper .NewClusterRoleBinding (tomServiceAccount .Name , tomClusterRole .Name , []rbacv1.Subject {
@@ -156,5 +232,61 @@ var _ = ginkgo.Describe("Aggregated Kubernetes API Endpoint testing", func() {
156232 })
157233 })
158234 })
235+
236+ ginkgo .When (fmt .Sprintf ("Serviceaccount(tom) access the %s cluster" , clusterName ), func () {
237+ var clusterClient kubernetes.Interface
238+
239+ ginkgo .BeforeEach (func () {
240+ clusterConfig , err := clientcmd .BuildConfigFromFlags ("" , kubeConfigPath )
241+ gomega .Expect (err ).ShouldNot (gomega .HaveOccurred ())
242+ clusterClient = kubernetes .NewForConfigOrDie (clusterConfig )
243+ })
244+
245+ ginkgo .BeforeEach (func () {
246+ klog .Infof ("Waiting for namespace present on cluster(%s)" , clusterName )
247+ framework .WaitNamespacePresentOnClusterByClient (clusterClient , tomServiceAccount .Namespace )
248+
249+ klog .Infof ("Create ServiceAccount(%s) in the cluster(%s)" , klog .KObj (tomServiceAccount ).String (), clusterName )
250+ framework .CreateServiceAccount (clusterClient , tomServiceAccount )
251+ ginkgo .DeferCleanup (func () {
252+ klog .Infof ("Delete ServiceAccount(%s) in the cluster(%s)" , klog .KObj (tomServiceAccount ).String (), clusterName )
253+ framework .RemoveServiceAccount (clusterClient , tomServiceAccount .Namespace , tomServiceAccount .Name )
254+ })
255+ })
256+
257+ ginkgo .AfterEach (func () {
258+ framework .RemoveClusterRole (clusterClient , tomClusterRoleOnMember .Name )
259+ framework .RemoveClusterRoleBinding (clusterClient , tomClusterRoleBindingOnMember .Name )
260+ })
261+
262+ ginkgo .It ("tom access the member cluster" , func () {
263+ ginkgo .By ("access the cluster `/api` path with right" , func () {
264+ gomega .Eventually (func (g gomega.Gomega ) (int , error ) {
265+ code , err := helper .DoRequest (fmt .Sprintf (karmadaHost + clusterProxy + "api" , clusterName ), tomToken )
266+ g .Expect (err ).ShouldNot (gomega .HaveOccurred ())
267+ return code , nil
268+ }, pollTimeout , pollInterval ).Should (gomega .Equal (http .StatusOK ))
269+ })
270+
271+ ginkgo .By ("access the cluster /api/v1/nodes path without right" , func () {
272+ code , err := helper .DoRequest (fmt .Sprintf (karmadaHost + clusterProxy + "api/v1/nodes" , clusterName ), tomToken )
273+ gomega .Expect (err ).ShouldNot (gomega .HaveOccurred ())
274+ gomega .Expect (code ).Should (gomega .Equal (http .StatusForbidden ))
275+ })
276+
277+ ginkgo .By (fmt .Sprintf ("create rbac in the %s cluster" , clusterName ), func () {
278+ framework .CreateClusterRole (clusterClient , tomClusterRoleOnMember )
279+ framework .CreateClusterRoleBinding (clusterClient , tomClusterRoleBindingOnMember )
280+ })
281+
282+ ginkgo .By (fmt .Sprintf ("access the %s /api/v1/nodes path with right" , clusterName ), func () {
283+ gomega .Eventually (func (g gomega.Gomega ) (int , error ) {
284+ code , err := helper .DoRequest (fmt .Sprintf (karmadaHost + clusterProxy + "api/v1/nodes" , clusterName ), tomToken )
285+ g .Expect (err ).ShouldNot (gomega .HaveOccurred ())
286+ return code , nil
287+ }, pollTimeout , pollInterval ).Should (gomega .Equal (http .StatusOK ))
288+ })
289+ })
290+ })
159291 })
160292})
0 commit comments