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