forked from openshift/origin
/
describer.go
735 lines (640 loc) · 22.1 KB
/
describer.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
package describe
import (
"fmt"
"reflect"
"strconv"
"strings"
"text/tabwriter"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrs "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
kctl "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/docker/docker/pkg/parsers"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
buildapi "github.com/openshift/origin/pkg/build/api"
buildutil "github.com/openshift/origin/pkg/build/util"
"github.com/openshift/origin/pkg/client"
imageapi "github.com/openshift/origin/pkg/image/api"
templateapi "github.com/openshift/origin/pkg/template/api"
)
func DescriberFor(kind string, c *client.Client, kclient kclient.Interface, host string) (kctl.Describer, bool) {
switch kind {
case "Build":
return &BuildDescriber{c}, true
case "BuildConfig":
return &BuildConfigDescriber{c, host}, true
case "BuildLog":
return &BuildLogDescriber{c}, true
case "Deployment":
return &DeploymentDescriber{c}, true
case "DeploymentConfig":
return NewDeploymentConfigDescriber(c, kclient), true
case "Identity":
return &IdentityDescriber{c}, true
case "Image":
return &ImageDescriber{c}, true
case "ImageRepository":
return &ImageRepositoryDescriber{c}, true
case "ImageRepositoryTag":
return &ImageRepositoryTagDescriber{c}, true
case "ImageStreamImage":
return &ImageStreamImageDescriber{c}, true
case "Route":
return &RouteDescriber{c}, true
case "Project":
return &ProjectDescriber{c}, true
case "Template":
return &TemplateDescriber{c, meta.NewAccessor(), kapi.Scheme, nil}, true
case "Policy":
return &PolicyDescriber{c}, true
case "PolicyBinding":
return &PolicyBindingDescriber{c}, true
case "RoleBinding":
return &RoleBindingDescriber{c}, true
case "Role":
return &RoleDescriber{c}, true
case "User":
return &UserDescriber{c}, true
case "UserIdentityMapping":
return &UserIdentityMappingDescriber{c}, true
}
return nil, false
}
// BuildDescriber generates information about a build
type BuildDescriber struct {
client.Interface
}
func (d *BuildDescriber) DescribeUser(out *tabwriter.Writer, label string, u buildapi.SourceControlUser) {
if len(u.Name) > 0 && len(u.Email) > 0 {
formatString(out, label, fmt.Sprintf("%s <%s>", u.Name, u.Email))
return
}
if len(u.Name) > 0 {
formatString(out, label, u.Name)
return
}
if len(u.Email) > 0 {
formatString(out, label, u.Email)
}
}
func (d *BuildDescriber) Describe(namespace, name string) (string, error) {
c := d.Builds(namespace)
build, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, build.ObjectMeta)
formatString(out, "BuildConfig", build.Labels[buildapi.BuildConfigLabel])
formatString(out, "Status", bold(build.Status))
if build.StartTimestamp != nil {
formatString(out, "Started", build.StartTimestamp.Time)
}
if build.CompletionTimestamp != nil {
formatString(out, "Finished", build.CompletionTimestamp.Time)
}
// Create the time object with second-level precision so we don't get
// output like "duration: 1.2724395728934s"
t := util.Now().Rfc3339Copy()
if build.StartTimestamp != nil && build.CompletionTimestamp != nil {
// time a build ran from pod creation to build finish or cancel
formatString(out, "Duration", build.CompletionTimestamp.Sub(build.StartTimestamp.Rfc3339Copy().Time))
} else if build.CompletionTimestamp != nil && build.Status == buildapi.BuildStatusCancelled {
// time a build waited for its pod before ultimately being canceled before that pod was created
formatString(out, "Duration", fmt.Sprintf("waited for %s", build.CompletionTimestamp.Sub(build.CreationTimestamp.Rfc3339Copy().Time)))
} else if build.CompletionTimestamp != nil && build.Status != buildapi.BuildStatusCancelled {
// for some reason we never saw the pod enter the running state, so we don't know when it
// "started", so instead print out the time from creation to completion.
formatString(out, "Duration", build.CompletionTimestamp.Sub(build.CreationTimestamp.Rfc3339Copy().Time))
} else if build.StartTimestamp == nil && build.Status != buildapi.BuildStatusCancelled {
// time a new build has been waiting for its pod to be created so it can run
formatString(out, "Duration", fmt.Sprintf("waiting for %s", t.Sub(build.CreationTimestamp.Rfc3339Copy().Time)))
} else if build.CompletionTimestamp == nil {
// time a still running build has been running in a pod
formatString(out, "Duration", fmt.Sprintf("running for %s", t.Sub(build.StartTimestamp.Rfc3339Copy().Time)))
}
formatString(out, "Build Pod", buildutil.GetBuildPodName(build))
describeBuildParameters(build.Parameters, out)
return nil
})
}
// BuildConfigDescriber generates information about a buildConfig
type BuildConfigDescriber struct {
client.Interface
host string
}
func describeBuildParameters(p buildapi.BuildParameters, out *tabwriter.Writer) {
formatString(out, "Strategy", p.Strategy.Type)
switch p.Strategy.Type {
case buildapi.DockerBuildStrategyType:
if p.Strategy.DockerStrategy != nil && p.Strategy.DockerStrategy.NoCache {
formatString(out, "No Cache", "yes")
}
if p.Strategy.DockerStrategy != nil {
formatString(out, "Image", p.Strategy.DockerStrategy.Image)
}
case buildapi.STIBuildStrategyType:
describeSTIStrategy(p.Strategy.STIStrategy, out)
case buildapi.CustomBuildStrategyType:
formatString(out, "Image", p.Strategy.CustomStrategy.Image)
if p.Strategy.CustomStrategy.ExposeDockerSocket {
formatString(out, "Expose Docker Socket", "yes")
}
if len(p.Strategy.CustomStrategy.Env) != 0 {
formatString(out, "Environment", formatLabels(convertEnv(p.Strategy.CustomStrategy.Env)))
}
}
formatString(out, "Source Type", p.Source.Type)
if p.Source.Git != nil {
formatString(out, "URL", p.Source.Git.URI)
if len(p.Source.Git.Ref) > 0 {
formatString(out, "Ref", p.Source.Git.Ref)
}
if len(p.Source.ContextDir) > 0 {
formatString(out, "ContextDir", p.Source.ContextDir)
}
}
if p.Output.To != nil {
if p.Output.To.Namespace != "" {
formatString(out, "Output to", fmt.Sprintf("%s/%s", p.Output.To.Namespace, p.Output.To.Name))
} else {
formatString(out, "Output to", p.Output.To.Name)
}
}
formatString(out, "Output Spec", p.Output.DockerImageReference)
if len(p.Output.PushSecretName) > 0 {
formatString(out, "Push Secret", p.Output.PushSecretName)
}
if p.Revision != nil && p.Revision.Type == buildapi.BuildSourceGit && p.Revision.Git != nil {
buildDescriber := &BuildDescriber{}
formatString(out, "Git Commit", p.Revision.Git.Commit)
buildDescriber.DescribeUser(out, "Revision Author", p.Revision.Git.Author)
buildDescriber.DescribeUser(out, "Revision Committer", p.Revision.Git.Committer)
if len(p.Revision.Git.Message) > 0 {
formatString(out, "Revision Message", p.Revision.Git.Message)
}
}
}
func describeSTIStrategy(s *buildapi.STIBuildStrategy, out *tabwriter.Writer) {
if s.From != nil && s.From.Name != "" {
if s.From.Namespace != "" {
formatString(out, "Image Repository", fmt.Sprintf("%s/%s", s.From.Name, s.From.Namespace))
} else {
formatString(out, "Image Repository", s.From.Name)
}
if s.Tag != "" {
formatString(out, "Image Repository Tag", s.Tag)
}
} else {
formatString(out, "Builder Image", s.Image)
}
if s.Scripts != "" {
formatString(out, "Scripts", s.Scripts)
}
if s.Incremental {
formatString(out, "Incremental Build", "yes")
}
}
// DescribeTriggers generates information about the triggers associated with a buildconfig
func (d *BuildConfigDescriber) DescribeTriggers(bc *buildapi.BuildConfig, host string, out *tabwriter.Writer) {
webhooks := webhookURL(bc, host)
for whType, whURL := range webhooks {
t := strings.Title(whType)
formatString(out, "Webhook "+t, whURL)
}
for _, trigger := range bc.Triggers {
if trigger.Type != buildapi.ImageChangeBuildTriggerType {
continue
}
if trigger.ImageChange.From.Namespace != "" {
formatString(out, "Image Repository Trigger", fmt.Sprintf("%s/%s", trigger.ImageChange.From.Namespace, trigger.ImageChange.From.Name))
} else {
formatString(out, "Image Repository Trigger", trigger.ImageChange.From.Name)
}
formatString(out, "- Tag", trigger.ImageChange.Tag)
formatString(out, "- Image", trigger.ImageChange.Image)
formatString(out, "- LastTriggeredImageID", trigger.ImageChange.LastTriggeredImageID)
}
}
func (d *BuildConfigDescriber) Describe(namespace, name string) (string, error) {
c := d.BuildConfigs(namespace)
buildConfig, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, buildConfig.ObjectMeta)
if buildConfig.LastVersion == 0 {
formatString(out, "Latest Version", "Never built")
} else {
formatString(out, "Latest Version", strconv.Itoa(buildConfig.LastVersion))
}
describeBuildParameters(buildConfig.Parameters, out)
d.DescribeTriggers(buildConfig, d.host, out)
return nil
})
}
// BuildLogDescriber generates information about a BuildLog
type BuildLogDescriber struct {
client.Interface
}
func (d *BuildLogDescriber) Describe(namespace, name string) (string, error) {
return fmt.Sprintf("Name: %s/%s, Labels:", namespace, name), nil
}
// ImageDescriber generates information about a Image
type ImageDescriber struct {
client.Interface
}
func (d *ImageDescriber) Describe(namespace, name string) (string, error) {
c := d.Images()
image, err := c.Get(name)
if err != nil {
return "", err
}
return describeImage(image)
}
func describeImage(image *imageapi.Image) (string, error) {
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, image.ObjectMeta)
formatString(out, "Docker Image", image.DockerImageReference)
return nil
})
}
// ImageRepositoryTagDescriber generates information about a ImageRepositoryTag (Image).
type ImageRepositoryTagDescriber struct {
client.Interface
}
func (d *ImageRepositoryTagDescriber) Describe(namespace, name string) (string, error) {
c := d.ImageRepositoryTags(namespace)
repo, tag := parsers.ParseRepositoryTag(name)
if tag == "" {
// TODO use repo's preferred default, when that's coded
tag = "latest"
}
image, err := c.Get(repo, tag)
if err != nil {
return "", err
}
return describeImage(image)
}
// ImageStreamImageDescriber generates information about a ImageStreamImage (Image).
type ImageStreamImageDescriber struct {
client.Interface
}
func (d *ImageStreamImageDescriber) Describe(namespace, name string) (string, error) {
c := d.ImageStreamImages(namespace)
repo, id := parsers.ParseRepositoryTag(name)
image, err := c.Get(repo, id)
if err != nil {
return "", err
}
return describeImage(image)
}
// ImageRepositoryDescriber generates information about a ImageRepository
type ImageRepositoryDescriber struct {
client.Interface
}
func (d *ImageRepositoryDescriber) Describe(namespace, name string) (string, error) {
c := d.ImageRepositories(namespace)
imageRepository, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, imageRepository.ObjectMeta)
formatString(out, "Tags", formatLabels(imageRepository.Tags))
formatString(out, "Registry", imageRepository.Status.DockerImageRepository)
return nil
})
}
// RouteDescriber generates information about a Route
type RouteDescriber struct {
client.Interface
}
func (d *RouteDescriber) Describe(namespace, name string) (string, error) {
c := d.Routes(namespace)
route, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, route.ObjectMeta)
formatString(out, "Host", route.Host)
formatString(out, "Path", route.Path)
formatString(out, "Service", route.ServiceName)
return nil
})
}
// ProjectDescriber generates information about a Project
type ProjectDescriber struct {
client.Interface
}
func (d *ProjectDescriber) Describe(namespace, name string) (string, error) {
c := d.Projects()
project, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, project.ObjectMeta)
formatString(out, "Display Name", project.DisplayName)
formatString(out, "Status", project.Status.Phase)
return nil
})
}
// PolicyDescriber generates information about a Project
type PolicyDescriber struct {
client.Interface
}
// TODO make something a lot prettier
func (d *PolicyDescriber) Describe(namespace, name string) (string, error) {
c := d.Policies(namespace)
policy, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, policy.ObjectMeta)
formatString(out, "Last Modified", policy.LastModified)
// using .List() here because I always want the sorted order that it provides
for _, key := range util.KeySet(reflect.ValueOf(policy.Roles)).List() {
role := policy.Roles[key]
fmt.Fprint(out, key+"\t"+policyRuleHeadings+"\n")
for _, rule := range role.Rules {
describePolicyRule(out, rule, "\t")
}
}
return nil
})
}
const policyRuleHeadings = "Verbs\tResources\tResource Names\tExtension"
func describePolicyRule(out *tabwriter.Writer, rule authorizationapi.PolicyRule, indent string) {
extensionString := ""
if rule.AttributeRestrictions != (runtime.EmbeddedObject{}) {
extensionString = fmt.Sprintf("%v", rule.AttributeRestrictions)
}
fmt.Fprintf(out, indent+"%v\t%v\t%v\t%v\n",
rule.Verbs.List(),
rule.Resources.List(),
rule.ResourceNames.List(),
extensionString)
}
// RoleDescriber generates information about a Project
type RoleDescriber struct {
client.Interface
}
func (d *RoleDescriber) Describe(namespace, name string) (string, error) {
c := d.Roles(namespace)
role, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, role.ObjectMeta)
fmt.Fprint(out, policyRuleHeadings+"\n")
for _, rule := range role.Rules {
describePolicyRule(out, rule, "")
}
return nil
})
}
// PolicyBindingDescriber generates information about a Project
type PolicyBindingDescriber struct {
client.Interface
}
func (d *PolicyBindingDescriber) Describe(namespace, name string) (string, error) {
c := d.PolicyBindings(namespace)
policyBinding, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, policyBinding.ObjectMeta)
formatString(out, "Last Modified", policyBinding.LastModified)
formatString(out, "Policy", policyBinding.PolicyRef.Namespace)
// using .List() here because I always want the sorted order that it provides
for _, key := range util.KeySet(reflect.ValueOf(policyBinding.RoleBindings)).List() {
roleBinding := policyBinding.RoleBindings[key]
formatString(out, "RoleBinding["+key+"]", " ")
formatString(out, "\tRole", roleBinding.RoleRef.Name)
formatString(out, "\tUsers", roleBinding.Users.List())
formatString(out, "\tGroups", roleBinding.Groups.List())
}
return nil
})
}
// RoleBindingDescriber generates information about a Project
type RoleBindingDescriber struct {
client.Interface
}
func (d *RoleBindingDescriber) Describe(namespace, name string) (string, error) {
c := d.RoleBindings(namespace)
roleBinding, err := c.Get(name)
if err != nil {
return "", err
}
role, err := d.Roles(roleBinding.RoleRef.Namespace).Get(roleBinding.RoleRef.Name)
return DescribeRoleBinding(roleBinding, role, err)
}
// DescribeRoleBinding prints out information about a role binding and its associated role
func DescribeRoleBinding(roleBinding *authorizationapi.RoleBinding, role *authorizationapi.Role, err error) (string, error) {
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, roleBinding.ObjectMeta)
formatString(out, "Role", roleBinding.RoleRef.Namespace+"/"+roleBinding.RoleRef.Name)
formatString(out, "Users", roleBinding.Users.List())
formatString(out, "Groups", roleBinding.Groups.List())
switch {
case err != nil:
formatString(out, "Policy Rules", fmt.Sprintf("error: %v", err))
case role != nil:
fmt.Fprint(out, policyRuleHeadings+"\n")
for _, rule := range role.Rules {
describePolicyRule(out, rule, "")
}
default:
formatString(out, "Policy Rules", "<none>")
}
return nil
})
}
// DescribeRole prints out information about a role
func DescribeRole(role *authorizationapi.Role) (string, error) {
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, role.ObjectMeta)
fmt.Fprint(out, policyRuleHeadings+"\n")
for _, rule := range role.Rules {
describePolicyRule(out, rule, "")
}
return nil
})
}
// TemplateDescriber generates information about a template
type TemplateDescriber struct {
client.Interface
meta.MetadataAccessor
runtime.ObjectTyper
DescribeObject func(obj runtime.Object, out *tabwriter.Writer) (bool, error)
}
func (d *TemplateDescriber) DescribeParameters(params []templateapi.Parameter, out *tabwriter.Writer) {
formatString(out, "Parameters", " ")
indent := " "
for _, p := range params {
formatString(out, indent+"Name", p.Name)
formatString(out, indent+"Description", p.Description)
if len(p.Generate) == 0 {
formatString(out, indent+"Value", p.Value)
continue
}
if len(p.Value) > 0 {
formatString(out, indent+"Value", p.Value)
formatString(out, indent+"Generated (ignored)", p.Generate)
formatString(out, indent+"From", p.From)
} else {
formatString(out, indent+"Generated", p.Generate)
formatString(out, indent+"From", p.From)
}
out.Write([]byte("\n"))
}
}
func (d *TemplateDescriber) DescribeObjects(objects []runtime.Object, labels map[string]string, out *tabwriter.Writer) {
formatString(out, "Objects", " ")
indent := " "
for _, obj := range objects {
if d.DescribeObject != nil {
if ok, _ := d.DescribeObject(obj, out); ok {
out.Write([]byte("\n"))
continue
}
}
_, kind, _ := d.ObjectTyper.ObjectVersionAndKind(obj)
meta := kapi.ObjectMeta{}
meta.Name, _ = d.MetadataAccessor.Name(obj)
meta.Annotations, _ = d.MetadataAccessor.Annotations(obj)
meta.Labels, _ = d.MetadataAccessor.Labels(obj)
fmt.Fprintf(out, fmt.Sprintf("%s%s\t%s\n", indent, kind, meta.Name))
if len(meta.Labels) > 0 {
formatString(out, indent+"Labels", formatLabels(meta.Labels))
}
formatAnnotations(out, meta, indent)
}
if len(labels) > 0 {
out.Write([]byte("\n"))
formatString(out, indent+"Common Labels", formatLabels(labels))
}
}
func (d *TemplateDescriber) Describe(namespace, name string) (string, error) {
c := d.Templates(namespace)
template, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, template.ObjectMeta)
out.Write([]byte("\n"))
out.Flush()
d.DescribeParameters(template.Parameters, out)
out.Write([]byte("\n"))
d.DescribeObjects(template.Objects, template.ObjectLabels, out)
return nil
})
}
// IdentityDescriber generates information about a user
type IdentityDescriber struct {
client.Interface
}
func (d *IdentityDescriber) Describe(namespace, name string) (string, error) {
userClient := d.Users()
identityClient := d.Identities()
identity, err := identityClient.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, identity.ObjectMeta)
if len(identity.User.Name) == 0 {
formatString(out, "User Name", identity.User.Name)
formatString(out, "User UID", identity.User.UID)
} else {
resolvedUser, err := userClient.Get(identity.User.Name)
nameValue := identity.User.Name
uidValue := string(identity.User.UID)
if kerrs.IsNotFound(err) {
nameValue += fmt.Sprintf(" (Error: User does not exist)")
} else if err != nil {
nameValue += fmt.Sprintf(" (Error: User lookup failed)")
} else {
if !util.NewStringSet(resolvedUser.Identities...).Has(name) {
nameValue += fmt.Sprintf(" (Error: User identities do not include %s)", name)
}
if resolvedUser.UID != identity.User.UID {
uidValue += fmt.Sprintf(" (Error: Actual user UID is %s)", string(resolvedUser.UID))
}
}
formatString(out, "User Name", nameValue)
formatString(out, "User UID", uidValue)
}
return nil
})
}
// UserIdentityMappingDescriber generates information about a user
type UserIdentityMappingDescriber struct {
client.Interface
}
func (d *UserIdentityMappingDescriber) Describe(namespace, name string) (string, error) {
c := d.UserIdentityMappings()
mapping, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, mapping.ObjectMeta)
formatString(out, "Identity", mapping.Identity.Name)
formatString(out, "User Name", mapping.User.Name)
formatString(out, "User UID", mapping.User.UID)
return nil
})
}
// UserDescriber generates information about a user
type UserDescriber struct {
client.Interface
}
func (d *UserDescriber) Describe(namespace, name string) (string, error) {
userClient := d.Users()
identityClient := d.Identities()
user, err := userClient.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, user.ObjectMeta)
if len(user.FullName) > 0 {
formatString(out, "Full Name", user.FullName)
}
if len(user.Identities) == 0 {
formatString(out, "Identities", "<none>")
} else {
for i, identity := range user.Identities {
resolvedIdentity, err := identityClient.Get(identity)
value := identity
if kerrs.IsNotFound(err) {
value += fmt.Sprintf(" (Error: Identity does not exist)")
} else if err != nil {
value += fmt.Sprintf(" (Error: Identity lookup failed)")
} else if resolvedIdentity.User.Name != name {
value += fmt.Sprintf(" (Error: Identity maps to user name '%s')", resolvedIdentity.User.Name)
} else if resolvedIdentity.User.UID != user.UID {
value += fmt.Sprintf(" (Error: Identity maps to user UID '%s')", resolvedIdentity.User.UID)
}
if i == 0 {
formatString(out, "Identities", value)
} else {
fmt.Fprintf(out, " \t%s\n", value)
}
}
}
return nil
})
}