github.com/argoproj/argo-cd/v3@v3.2.1/test/e2e/fixture/cluster/actions.go (about) 1 package cluster 2 3 import ( 4 "context" 5 "errors" 6 "log" 7 "strings" 8 "time" 9 10 "k8s.io/client-go/kubernetes" 11 "k8s.io/client-go/tools/clientcmd" 12 13 "github.com/argoproj/argo-cd/v3/common" 14 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" 15 "github.com/argoproj/argo-cd/v3/util/clusterauth" 16 17 clusterpkg "github.com/argoproj/argo-cd/v3/pkg/apiclient/cluster" 18 "github.com/argoproj/argo-cd/v3/test/e2e/fixture" 19 ) 20 21 // this implements the "when" part of given/when/then 22 // 23 // none of the func implement error checks, and that is complete intended, you should check for errors 24 // using the Then() 25 type Actions struct { 26 context *Context 27 lastOutput string 28 lastError error 29 ignoreErrors bool 30 } 31 32 func (a *Actions) IgnoreErrors() *Actions { 33 a.ignoreErrors = true 34 return a 35 } 36 37 func (a *Actions) DoNotIgnoreErrors() *Actions { 38 a.ignoreErrors = false 39 return a 40 } 41 42 func (a *Actions) Create() *Actions { 43 _, clusterClient, _ := fixture.ArgoCDClientset.NewClusterClient() 44 45 _, err := clusterClient.Create(context.Background(), &clusterpkg.ClusterCreateRequest{ 46 Cluster: &v1alpha1.Cluster{ 47 Server: a.context.server, 48 Name: a.context.name, 49 Config: v1alpha1.ClusterConfig{BearerToken: a.context.bearerToken}, 50 ConnectionState: v1alpha1.ConnectionState{}, 51 ServerVersion: "", 52 Namespaces: a.context.namespaces, 53 RefreshRequestedAt: nil, 54 Info: v1alpha1.ClusterInfo{}, 55 Shard: nil, 56 ClusterResources: false, 57 Project: a.context.project, 58 }, 59 Upsert: a.context.upsert, 60 }) 61 if err != nil { 62 if !a.ignoreErrors { 63 log.Fatalf("Failed to upsert cluster %v", err.Error()) 64 } 65 a.lastError = errors.New(err.Error()) 66 } 67 68 return a 69 } 70 71 func (a *Actions) CreateWithRBAC() *Actions { 72 pathOpts := clientcmd.NewDefaultPathOptions() 73 config, err := pathOpts.GetStartingConfig() 74 if err != nil { 75 a.lastError = err 76 return a 77 } 78 clientConfig := clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{}) 79 conf, err := clientConfig.ClientConfig() 80 if err != nil { 81 a.lastError = err 82 return a 83 } 84 client := kubernetes.NewForConfigOrDie(conf) 85 86 _, err = clusterauth.InstallClusterManagerRBAC(client, "kube-system", []string{}, common.BearerTokenTimeout) 87 if err != nil { 88 a.lastError = err 89 return a 90 } 91 92 return a.Create() 93 } 94 95 func (a *Actions) List() *Actions { 96 a.context.t.Helper() 97 a.runCli("cluster", "list") 98 return a 99 } 100 101 func (a *Actions) Get() *Actions { 102 a.context.t.Helper() 103 a.runCli("cluster", "get", a.context.server) 104 return a 105 } 106 107 func (a *Actions) GetByName(name string) *Actions { 108 a.context.t.Helper() 109 a.runCli("cluster", "get", name) 110 return a 111 } 112 113 func (a *Actions) SetNamespaces() *Actions { 114 a.context.t.Helper() 115 a.runCli("cluster", "set", a.context.name, "--namespace", strings.Join(a.context.namespaces, ",")) 116 return a 117 } 118 119 func (a *Actions) DeleteByName() *Actions { 120 a.context.t.Helper() 121 122 a.runCli("cluster", "rm", a.context.name, "--yes") 123 return a 124 } 125 126 func (a *Actions) DeleteByServer() *Actions { 127 a.context.t.Helper() 128 129 a.runCli("cluster", "rm", a.context.server, "--yes") 130 return a 131 } 132 133 func (a *Actions) Then() *Consequences { 134 a.context.t.Helper() 135 time.Sleep(fixture.WhenThenSleepInterval) 136 return &Consequences{a.context, a} 137 } 138 139 func (a *Actions) runCli(args ...string) { 140 a.context.t.Helper() 141 a.lastOutput, a.lastError = fixture.RunCli(args...) 142 }