forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
643 lines (576 loc) · 18.6 KB
/
cli.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package util
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"strings"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
authorizationapiv1 "k8s.io/api/authorization/v1"
kapiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/storage/names"
kclientset "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
clientcmd "k8s.io/client-go/tools/clientcmd"
kinternalclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
e2e "k8s.io/kubernetes/test/e2e/framework"
_ "github.com/openshift/origin/pkg/api/install"
appsclientset "github.com/openshift/origin/pkg/apps/generated/internalclientset"
authorizationclientset "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
buildclientset "github.com/openshift/origin/pkg/build/generated/internalclientset"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
imageclientset "github.com/openshift/origin/pkg/image/generated/internalclientset"
"github.com/openshift/origin/pkg/oc/cli/config"
projectapi "github.com/openshift/origin/pkg/project/apis/project"
projectclientset "github.com/openshift/origin/pkg/project/generated/internalclientset"
routeclientset "github.com/openshift/origin/pkg/route/generated/internalclientset"
securityclientset "github.com/openshift/origin/pkg/security/generated/internalclientset"
templateclientset "github.com/openshift/origin/pkg/template/generated/internalclientset"
userclientset "github.com/openshift/origin/pkg/user/generated/internalclientset"
testutil "github.com/openshift/origin/test/util"
)
// CLI provides function to call the OpenShift CLI and Kubernetes and OpenShift
// clients.
type CLI struct {
execPath string
verb string
configPath string
adminConfigPath string
username string
globalArgs []string
commandArgs []string
finalArgs []string
namespacesToDelete []string
stdin *bytes.Buffer
stdout io.Writer
stderr io.Writer
verbose bool
withoutNamespace bool
kubeFramework *e2e.Framework
}
// NewCLI initialize the upstream E2E framework and set the namespace to match
// with the project name. Note that this function does not initialize the project
// role bindings for the namespace.
func NewCLI(project, adminConfigPath string) *CLI {
client := &CLI{}
// has to run before the default framework nukes the client
g.AfterEach(client.TeardownProject)
client.kubeFramework = e2e.NewDefaultFramework(project)
client.kubeFramework.SkipNamespaceCreation = true
client.username = "admin"
client.execPath = "oc"
if len(adminConfigPath) == 0 {
FatalErr(fmt.Errorf("you must set the KUBECONFIG variable to admin kubeconfig"))
}
client.adminConfigPath = adminConfigPath
g.BeforeEach(client.SetupProject)
return client
}
// KubeFramework returns Kubernetes framework which contains helper functions
// specific for Kubernetes resources
func (c *CLI) KubeFramework() *e2e.Framework {
return c.kubeFramework
}
// Username returns the name of currently logged user. If there is no user assigned
// for the current session, it returns 'admin'.
func (c *CLI) Username() string {
return c.username
}
// AsAdmin changes current config file path to the admin config.
func (c *CLI) AsAdmin() *CLI {
nc := *c
nc.configPath = c.adminConfigPath
return &nc
}
// ChangeUser changes the user used by the current CLI session.
func (c *CLI) ChangeUser(name string) *CLI {
adminClientConfig, err := testutil.GetClusterAdminClientConfig(c.adminConfigPath)
if err != nil {
FatalErr(err)
}
_, clientConfig, err := testutil.GetClientForUser(adminClientConfig, name)
if err != nil {
FatalErr(err)
}
kubeConfig, err := config.CreateConfig(c.Namespace(), clientConfig)
if err != nil {
FatalErr(err)
}
f, err := ioutil.TempFile("", "configfile")
if err != nil {
FatalErr(err)
}
c.configPath = f.Name()
err = clientcmd.WriteToFile(*kubeConfig, c.configPath)
if err != nil {
FatalErr(err)
}
c.username = name
e2e.Logf("configPath is now %q", c.configPath)
return c
}
// SetNamespace sets a new namespace
func (c *CLI) SetNamespace(ns string) *CLI {
c.kubeFramework.Namespace = &kapiv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns,
},
}
return c
}
// WithoutNamespace instructs the command should be invoked without adding --namespace parameter
func (c CLI) WithoutNamespace() *CLI {
c.withoutNamespace = true
return &c
}
// SetupProject creates a new project and assign a random user to the project.
// All resources will be then created within this project.
func (c *CLI) SetupProject() {
newNamespace := names.SimpleNameGenerator.GenerateName(fmt.Sprintf("e2e-test-%s-", c.kubeFramework.BaseName))
c.SetNamespace(newNamespace).ChangeUser(fmt.Sprintf("%s-user", newNamespace))
e2e.Logf("The user is now %q", c.Username())
e2e.Logf("Creating project %q", newNamespace)
_, err := c.ProjectClient().Project().ProjectRequests().Create(&projectapi.ProjectRequest{
ObjectMeta: metav1.ObjectMeta{Name: newNamespace},
})
o.Expect(err).NotTo(o.HaveOccurred())
// TODO: remove when https://github.com/kubernetes/kubernetes/pull/62606 merges and is in origin
c.namespacesToDelete = append(c.namespacesToDelete, newNamespace)
e2e.Logf("Waiting on permissions in project %q ...", newNamespace)
err = WaitForSelfSAR(1*time.Second, 60*time.Second, c.KubeClient(), authorizationapiv1.SelfSubjectAccessReviewSpec{
ResourceAttributes: &authorizationapiv1.ResourceAttributes{
Namespace: newNamespace,
Verb: "create",
Group: "",
Resource: "pods",
},
})
o.Expect(err).NotTo(o.HaveOccurred())
}
// TeardownProject removes projects created by this test.
func (c *CLI) TeardownProject() {
if len(c.configPath) > 0 {
os.Remove(c.configPath)
}
if len(c.namespacesToDelete) > 0 {
timeout := e2e.DefaultNamespaceDeletionTimeout
if c.kubeFramework.NamespaceDeletionTimeout != 0 {
timeout = c.kubeFramework.NamespaceDeletionTimeout
}
e2e.DeleteNamespaces(c.kubeFramework.ClientSet, c.namespacesToDelete, nil)
e2e.WaitForNamespacesDeleted(c.kubeFramework.ClientSet, c.namespacesToDelete, timeout)
}
}
// Verbose turns on printing verbose messages when executing OpenShift commands
func (c *CLI) Verbose() *CLI {
c.verbose = true
return c
}
func (c *CLI) AppsClient() appsclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.configPath, nil)
if err != nil {
FatalErr(err)
}
client, err := appsclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) AuthorizationClient() authorizationclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.configPath, nil)
if err != nil {
FatalErr(err)
}
client, err := authorizationclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) BuildClient() buildclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.configPath, nil)
if err != nil {
FatalErr(err)
}
client, err := buildclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) ImageClient() imageclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.configPath, nil)
if err != nil {
FatalErr(err)
}
client, err := imageclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) ProjectClient() projectclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.configPath, nil)
if err != nil {
FatalErr(err)
}
client, err := projectclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) RouteClient() routeclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.configPath, nil)
if err != nil {
FatalErr(err)
}
client, err := routeclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
// Client provides an OpenShift client for the current user. If the user is not
// set, then it provides client for the cluster admin user
func (c *CLI) TemplateClient() templateclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.configPath, nil)
if err != nil {
FatalErr(err)
}
client, err := templateclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) UserClient() userclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.configPath, nil)
if err != nil {
FatalErr(err)
}
client, err := userclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) AdminAppsClient() appsclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.adminConfigPath, nil)
if err != nil {
FatalErr(err)
}
client, err := appsclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) AdminAuthorizationClient() authorizationclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.adminConfigPath, nil)
if err != nil {
FatalErr(err)
}
client, err := authorizationclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) AdminBuildClient() buildclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.adminConfigPath, nil)
if err != nil {
FatalErr(err)
}
client, err := buildclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) AdminImageClient() imageclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.adminConfigPath, nil)
if err != nil {
FatalErr(err)
}
client, err := imageclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) AdminProjectClient() projectclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.adminConfigPath, nil)
if err != nil {
FatalErr(err)
}
client, err := projectclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) AdminRouteClient() routeclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.adminConfigPath, nil)
if err != nil {
FatalErr(err)
}
client, err := routeclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
// AdminClient provides an OpenShift client for the cluster admin user.
func (c *CLI) AdminTemplateClient() templateclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.adminConfigPath, nil)
if err != nil {
FatalErr(err)
}
client, err := templateclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) AdminUserClient() userclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.adminConfigPath, nil)
if err != nil {
FatalErr(err)
}
client, err := userclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
func (c *CLI) AdminSecurityClient() securityclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.adminConfigPath, nil)
if err != nil {
FatalErr(err)
}
client, err := securityclientset.NewForConfig(clientConfig)
if err != nil {
FatalErr(err)
}
return client
}
// KubeClient provides a Kubernetes client for the current namespace
func (c *CLI) KubeClient() kclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.configPath, nil)
if err != nil {
FatalErr(err)
}
return kclientset.NewForConfigOrDie(clientConfig)
}
// KubeClient provides a Kubernetes client for the current namespace
func (c *CLI) InternalKubeClient() kinternalclientset.Interface {
clientConfig, err := configapi.GetClientConfig(c.configPath, nil)
if err != nil {
FatalErr(err)
}
return kinternalclientset.NewForConfigOrDie(clientConfig)
}
// AdminKubeClient provides a Kubernetes client for the cluster admin user.
func (c *CLI) AdminKubeClient() kclientset.Interface {
return kclientset.NewForConfigOrDie(c.AdminConfig())
}
// AdminKubeClient provides a Kubernetes client for the cluster admin user.
func (c *CLI) InternalAdminKubeClient() kinternalclientset.Interface {
return kinternalclientset.NewForConfigOrDie(c.AdminConfig())
}
func (c *CLI) AdminConfig() *restclient.Config {
clientConfig, err := configapi.GetClientConfig(c.adminConfigPath, nil)
if err != nil {
FatalErr(err)
}
return clientConfig
}
// Namespace returns the name of the namespace used in the current test case.
// If the namespace is not set, an empty string is returned.
func (c *CLI) Namespace() string {
if c.kubeFramework.Namespace == nil {
return ""
}
return c.kubeFramework.Namespace.Name
}
// setOutput allows to override the default command output
func (c *CLI) setOutput(out io.Writer) *CLI {
c.stdout = out
return c
}
// Run executes given OpenShift CLI command verb (iow. "oc <verb>").
// This function also override the default 'stdout' to redirect all output
// to a buffer and prepare the global flags such as namespace and config path.
func (c *CLI) Run(commands ...string) *CLI {
in, out, errout := &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}
nc := &CLI{
execPath: c.execPath,
verb: commands[0],
kubeFramework: c.KubeFramework(),
adminConfigPath: c.adminConfigPath,
configPath: c.configPath,
username: c.username,
globalArgs: append(commands, []string{
fmt.Sprintf("--config=%s", c.configPath),
}...),
}
if !c.withoutNamespace {
nc.globalArgs = append(nc.globalArgs, fmt.Sprintf("--namespace=%s", c.Namespace()))
}
nc.stdin, nc.stdout, nc.stderr = in, out, errout
return nc.setOutput(c.stdout)
}
// Template sets a Go template for the OpenShift CLI command.
// This is equivalent of running "oc get foo -o template --template='{{ .spec }}'"
func (c *CLI) Template(t string) *CLI {
if c.verb != "get" {
FatalErr("Cannot use Template() for non-get verbs.")
}
templateArgs := []string{"--output=template", fmt.Sprintf("--template=%s", t)}
commandArgs := append(c.commandArgs, templateArgs...)
c.finalArgs = append(c.globalArgs, commandArgs...)
return c
}
// InputString adds expected input to the command
func (c *CLI) InputString(input string) *CLI {
c.stdin.WriteString(input)
return c
}
// Args sets the additional arguments for the OpenShift CLI command
func (c *CLI) Args(args ...string) *CLI {
c.commandArgs = args
c.finalArgs = append(c.globalArgs, c.commandArgs...)
return c
}
func (c *CLI) printCmd() string {
return strings.Join(c.finalArgs, " ")
}
type ExitError struct {
Cmd string
StdErr string
*exec.ExitError
}
// Output executes the command and returns stdout/stderr combined into one string
func (c *CLI) Output() (string, error) {
if c.verbose {
fmt.Printf("DEBUG: oc %s\n", c.printCmd())
}
cmd := exec.Command(c.execPath, c.finalArgs...)
cmd.Stdin = c.stdin
e2e.Logf("Running '%s %s'", c.execPath, strings.Join(c.finalArgs, " "))
out, err := cmd.CombinedOutput()
trimmed := strings.TrimSpace(string(out))
switch err.(type) {
case nil:
c.stdout = bytes.NewBuffer(out)
return trimmed, nil
case *exec.ExitError:
e2e.Logf("Error running %v:\n%s", cmd, trimmed)
return trimmed, &ExitError{ExitError: err.(*exec.ExitError), Cmd: c.execPath + " " + strings.Join(c.finalArgs, " "), StdErr: trimmed}
default:
FatalErr(fmt.Errorf("unable to execute %q: %v", c.execPath, err))
// unreachable code
return "", nil
}
}
// Outputs executes the command and returns the stdout/stderr output as separate strings
func (c *CLI) Outputs() (string, string, error) {
if c.verbose {
fmt.Printf("DEBUG: oc %s\n", c.printCmd())
}
cmd := exec.Command(c.execPath, c.finalArgs...)
cmd.Stdin = c.stdin
e2e.Logf("Running '%s %s'", c.execPath, strings.Join(c.finalArgs, " "))
//out, err := cmd.CombinedOutput()
var stdErrBuff, stdOutBuff bytes.Buffer
cmd.Stdout = &stdOutBuff
cmd.Stderr = &stdErrBuff
err := cmd.Run()
stdOutBytes := stdOutBuff.Bytes()
stdErrBytes := stdErrBuff.Bytes()
stdOut := strings.TrimSpace(string(stdOutBytes))
stdErr := strings.TrimSpace(string(stdErrBytes))
switch err.(type) {
case nil:
c.stdout = bytes.NewBuffer(stdOutBytes)
c.stderr = bytes.NewBuffer(stdErrBytes)
return stdOut, stdErr, nil
case *exec.ExitError:
e2e.Logf("Error running %v:\nStdOut>\n%s\nStdErr>\n%s\n", cmd, stdOut, stdErr)
return stdOut, stdErr, err
default:
FatalErr(fmt.Errorf("unable to execute %q: %v", c.execPath, err))
// unreachable code
return "", "", nil
}
}
// Background executes the command in the background and returns the Cmd object
// which may be killed later via cmd.Process.Kill(). It also returns buffers
// holding the stdout & stderr of the command, which may be read from only after
// calling cmd.Wait().
func (c *CLI) Background() (*exec.Cmd, *bytes.Buffer, *bytes.Buffer, error) {
if c.verbose {
fmt.Printf("DEBUG: oc %s\n", c.printCmd())
}
cmd := exec.Command(c.execPath, c.finalArgs...)
cmd.Stdin = c.stdin
var stdout, stderr bytes.Buffer
cmd.Stdout = bufio.NewWriter(&stdout)
cmd.Stderr = bufio.NewWriter(&stderr)
e2e.Logf("Running '%s %s'", c.execPath, strings.Join(c.finalArgs, " "))
err := cmd.Start()
return cmd, &stdout, &stderr, err
}
// BackgroundRC executes the command in the background and returns the Cmd
// object which may be killed later via cmd.Process.Kill(). It returns a
// ReadCloser for stdout. If in doubt, use Background(). Consult the os/exec
// documentation.
func (c *CLI) BackgroundRC() (*exec.Cmd, io.ReadCloser, error) {
if c.verbose {
fmt.Printf("DEBUG: oc %s\n", c.printCmd())
}
cmd := exec.Command(c.execPath, c.finalArgs...)
cmd.Stdin = c.stdin
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
e2e.Logf("Running '%s %s'", c.execPath, strings.Join(c.finalArgs, " "))
err = cmd.Start()
return cmd, stdout, err
}
// OutputToFile executes the command and store output to a file
func (c *CLI) OutputToFile(filename string) (string, error) {
content, err := c.Output()
if err != nil {
return "", err
}
path := filepath.Join(e2e.TestContext.OutputDir, c.Namespace()+"-"+filename)
return path, ioutil.WriteFile(path, []byte(content), 0644)
}
// Execute executes the current command and return error if the execution failed
// This function will set the default output to Ginkgo writer.
func (c *CLI) Execute() error {
out, err := c.Output()
if _, err := io.Copy(g.GinkgoWriter, strings.NewReader(out+"\n")); err != nil {
fmt.Fprintln(os.Stderr, "ERROR: Unable to copy the output to ginkgo writer")
}
os.Stdout.Sync()
return err
}
// FatalErr exits the test in case a fatal error has occurred.
func FatalErr(msg interface{}) {
// the path that leads to this being called isn't always clear...
fmt.Fprintln(g.GinkgoWriter, string(debug.Stack()))
e2e.Failf("%v", msg)
}