forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer.go
1308 lines (1160 loc) · 44.8 KB
/
printer.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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package describe
import (
"fmt"
"io"
"regexp"
"sort"
"strings"
"text/tabwriter"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/apis/core"
kprinters "k8s.io/kubernetes/pkg/printers"
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
oapi "github.com/openshift/origin/pkg/api"
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
networkapi "github.com/openshift/origin/pkg/network/apis/network"
oauthapi "github.com/openshift/origin/pkg/oauth/apis/oauth"
projectapi "github.com/openshift/origin/pkg/project/apis/project"
quotaapi "github.com/openshift/origin/pkg/quota/apis/quota"
routeapi "github.com/openshift/origin/pkg/route/apis/route"
securityapi "github.com/openshift/origin/pkg/security/apis/security"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
userapi "github.com/openshift/origin/pkg/user/apis/user"
)
var (
buildColumns = []string{"NAME", "TYPE", "FROM", "STATUS", "STARTED", "DURATION"}
buildConfigColumns = []string{"NAME", "TYPE", "FROM", "LATEST"}
imageColumns = []string{"NAME", "DOCKER REF"}
imageStreamTagColumns = []string{"NAME", "DOCKER REF", "UPDATED", "IMAGENAME"}
imageStreamImageColumns = []string{"NAME", "DOCKER REF", "UPDATED", "IMAGENAME"}
imageStreamColumns = []string{"NAME", "DOCKER REPO", "TAGS", "UPDATED"}
projectColumns = []string{"NAME", "DISPLAY NAME", "STATUS"}
routeColumns = []string{"NAME", "HOST/PORT", "PATH", "SERVICES", "PORT", "TERMINATION", "WILDCARD"}
deploymentConfigColumns = []string{"NAME", "REVISION", "DESIRED", "CURRENT", "TRIGGERED BY"}
templateColumns = []string{"NAME", "DESCRIPTION", "PARAMETERS", "OBJECTS"}
policyColumns = []string{"NAME", "ROLES", "LAST MODIFIED"}
policyBindingColumns = []string{"NAME", "ROLE BINDINGS", "LAST MODIFIED"}
roleBindingColumns = []string{"NAME", "ROLE", "USERS", "GROUPS", "SERVICE ACCOUNTS", "SUBJECTS"}
roleColumns = []string{"NAME"}
oauthClientColumns = []string{"NAME", "SECRET", "WWW-CHALLENGE", "TOKEN-MAX-AGE", "REDIRECT URIS"}
oauthClientAuthorizationColumns = []string{"NAME", "USER NAME", "CLIENT NAME", "SCOPES"}
oauthAccessTokenColumns = []string{"NAME", "USER NAME", "CLIENT NAME", "CREATED", "EXPIRES", "REDIRECT URI", "SCOPES"}
oauthAuthorizeTokenColumns = []string{"NAME", "USER NAME", "CLIENT NAME", "CREATED", "EXPIRES", "REDIRECT URI", "SCOPES"}
userColumns = []string{"NAME", "UID", "FULL NAME", "IDENTITIES"}
identityColumns = []string{"NAME", "IDP NAME", "IDP USER NAME", "USER NAME", "USER UID"}
userIdentityMappingColumns = []string{"NAME", "IDENTITY", "USER NAME", "USER UID"}
groupColumns = []string{"NAME", "USERS"}
// IsPersonalSubjectAccessReviewColumns contains known custom role extensions
IsPersonalSubjectAccessReviewColumns = []string{"NAME"}
hostSubnetColumns = []string{"NAME", "HOST", "HOST IP", "SUBNET", "EGRESS IPS"}
netNamespaceColumns = []string{"NAME", "NETID", "EGRESS IPS"}
clusterNetworkColumns = []string{"NAME", "CLUSTER NETWORKS", "SERVICE NETWORK", "PLUGIN NAME"}
egressNetworkPolicyColumns = []string{"NAME"}
clusterResourceQuotaColumns = []string{"NAME", "LABEL SELECTOR", "ANNOTATION SELECTOR"}
roleBindingRestrictionColumns = []string{"NAME", "SUBJECT TYPE", "SUBJECTS"}
templateInstanceColumns = []string{"NAME", "TEMPLATE"}
brokerTemplateInstanceColumns = []string{"NAME", "TEMPLATEINSTANCE"}
policyRuleColumns = []string{"VERBS", "NON-RESOURCE URLS", "RESOURCE NAMES", "API GROUPS", "RESOURCES"}
securityContextConstraintsColumns = []string{"NAME", "PRIV", "CAPS", "SELINUX", "RUNASUSER", "FSGROUP", "SUPGROUP", "PRIORITY", "READONLYROOTFS", "VOLUMES"}
)
func init() {
// TODO this should be eliminated
printersinternal.AddHandlers = addPrintHandlers
}
func addPrintHandlers(p kprinters.PrintHandler) {
printersinternal.AddKubeHandlers(p)
p.Handler(buildColumns, nil, printBuild)
p.Handler(buildColumns, nil, printBuildList)
p.Handler(buildConfigColumns, nil, printBuildConfig)
p.Handler(buildConfigColumns, nil, printBuildConfigList)
p.Handler(policyRuleColumns, nil, printSubjectRulesReview)
p.Handler(policyRuleColumns, nil, printSelfSubjectRulesReview)
p.Handler(imageColumns, nil, printImage)
p.Handler(imageStreamTagColumns, nil, printImageStreamTag)
p.Handler(imageStreamTagColumns, nil, printImageStreamTagList)
p.Handler(imageStreamImageColumns, nil, printImageStreamImage)
p.Handler(imageColumns, nil, printImageList)
p.Handler(imageStreamColumns, nil, printImageStream)
p.Handler(imageStreamColumns, nil, printImageStreamList)
p.Handler(projectColumns, nil, printProject)
p.Handler(projectColumns, nil, printProjectList)
p.Handler(routeColumns, nil, printRoute)
p.Handler(routeColumns, nil, printRouteList)
p.Handler(deploymentConfigColumns, nil, printDeploymentConfig)
p.Handler(deploymentConfigColumns, nil, printDeploymentConfigList)
p.Handler(templateColumns, nil, printTemplate)
p.Handler(templateColumns, nil, printTemplateList)
p.Handler(policyColumns, nil, printPolicy)
p.Handler(policyColumns, nil, printPolicyList)
p.Handler(policyBindingColumns, nil, printPolicyBinding)
p.Handler(policyBindingColumns, nil, printPolicyBindingList)
p.Handler(roleBindingColumns, nil, printRoleBinding)
p.Handler(roleBindingColumns, nil, printRoleBindingList)
p.Handler(roleColumns, nil, printRole)
p.Handler(roleColumns, nil, printRoleList)
p.Handler(policyColumns, nil, printClusterPolicy)
p.Handler(policyColumns, nil, printClusterPolicyList)
p.Handler(policyBindingColumns, nil, printClusterPolicyBinding)
p.Handler(policyBindingColumns, nil, printClusterPolicyBindingList)
p.Handler(roleColumns, nil, printClusterRole)
p.Handler(roleColumns, nil, printClusterRoleList)
p.Handler(roleBindingColumns, nil, printClusterRoleBinding)
p.Handler(roleBindingColumns, nil, printClusterRoleBindingList)
p.Handler(oauthClientColumns, nil, printOAuthClient)
p.Handler(oauthClientColumns, nil, printOAuthClientList)
p.Handler(oauthClientAuthorizationColumns, nil, printOAuthClientAuthorization)
p.Handler(oauthClientAuthorizationColumns, nil, printOAuthClientAuthorizationList)
p.Handler(oauthAccessTokenColumns, nil, printOAuthAccessToken)
p.Handler(oauthAccessTokenColumns, nil, printOAuthAccessTokenList)
p.Handler(oauthAuthorizeTokenColumns, nil, printOAuthAuthorizeToken)
p.Handler(oauthAuthorizeTokenColumns, nil, printOAuthAuthorizeTokenList)
p.Handler(userColumns, nil, printUser)
p.Handler(userColumns, nil, printUserList)
p.Handler(identityColumns, nil, printIdentity)
p.Handler(identityColumns, nil, printIdentityList)
p.Handler(userIdentityMappingColumns, nil, printUserIdentityMapping)
p.Handler(groupColumns, nil, printGroup)
p.Handler(groupColumns, nil, printGroupList)
p.Handler(IsPersonalSubjectAccessReviewColumns, nil, printIsPersonalSubjectAccessReview)
p.Handler(hostSubnetColumns, nil, printHostSubnet)
p.Handler(hostSubnetColumns, nil, printHostSubnetList)
p.Handler(netNamespaceColumns, nil, printNetNamespaceList)
p.Handler(netNamespaceColumns, nil, printNetNamespace)
p.Handler(clusterNetworkColumns, nil, printClusterNetwork)
p.Handler(clusterNetworkColumns, nil, printClusterNetworkList)
p.Handler(egressNetworkPolicyColumns, nil, printEgressNetworkPolicy)
p.Handler(egressNetworkPolicyColumns, nil, printEgressNetworkPolicyList)
p.Handler(clusterResourceQuotaColumns, nil, printClusterResourceQuota)
p.Handler(clusterResourceQuotaColumns, nil, printClusterResourceQuotaList)
p.Handler(clusterResourceQuotaColumns, nil, printAppliedClusterResourceQuota)
p.Handler(clusterResourceQuotaColumns, nil, printAppliedClusterResourceQuotaList)
p.Handler(roleBindingRestrictionColumns, nil, printRoleBindingRestriction)
p.Handler(roleBindingRestrictionColumns, nil, printRoleBindingRestrictionList)
p.Handler(templateInstanceColumns, nil, printTemplateInstance)
p.Handler(templateInstanceColumns, nil, printTemplateInstanceList)
p.Handler(brokerTemplateInstanceColumns, nil, printBrokerTemplateInstance)
p.Handler(brokerTemplateInstanceColumns, nil, printBrokerTemplateInstanceList)
p.Handler(securityContextConstraintsColumns, nil, printSecurityContextConstraints)
p.Handler(securityContextConstraintsColumns, nil, printSecurityContextConstraintsList)
}
const templateDescriptionLen = 80
// PrintTemplateParameters the Template parameters with their default values
func PrintTemplateParameters(params []templateapi.Parameter, output io.Writer) error {
w := tabwriter.NewWriter(output, 20, 5, 3, ' ', 0)
defer w.Flush()
parameterColumns := []string{"NAME", "DESCRIPTION", "GENERATOR", "VALUE"}
fmt.Fprintf(w, "%s\n", strings.Join(parameterColumns, "\t"))
for _, p := range params {
value := p.Value
if len(p.Generate) != 0 {
value = p.From
}
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", p.Name, p.Description, p.Generate, value)
if err != nil {
return err
}
}
return nil
}
// formatResourceName receives a resource kind, name, and boolean specifying
// whether or not to update the current name to "kind/name"
func formatResourceName(kind schema.GroupKind, name string, withKind bool) string {
if !withKind || kind.Empty() {
return name
}
return strings.ToLower(kind.String()) + "/" + name
}
func printTemplate(t *templateapi.Template, w io.Writer, opts kprinters.PrintOptions) error {
description := ""
if t.Annotations != nil {
description = t.Annotations["description"]
}
// Only print the first line of description
if lines := strings.SplitN(description, "\n", 2); len(lines) > 1 {
description = lines[0] + "..."
}
if len(description) > templateDescriptionLen {
description = strings.TrimSpace(description[:templateDescriptionLen-3]) + "..."
}
empty, generated, total := 0, 0, len(t.Parameters)
for _, p := range t.Parameters {
if len(p.Value) > 0 {
continue
}
if len(p.Generate) > 0 {
generated++
continue
}
empty++
}
params := ""
switch {
case empty > 0:
params = fmt.Sprintf("%d (%d blank)", total, empty)
case generated > 0:
params = fmt.Sprintf("%d (%d generated)", total, generated)
default:
params = fmt.Sprintf("%d (all set)", total)
}
name := formatResourceName(opts.Kind, t.Name, opts.WithKind)
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", t.Namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%d", name, description, params, len(t.Objects)); err != nil {
return err
}
if err := appendItemLabels(t.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printTemplateList(list *templateapi.TemplateList, w io.Writer, opts kprinters.PrintOptions) error {
for _, t := range list.Items {
if err := printTemplate(&t, w, opts); err != nil {
return err
}
}
return nil
}
func printBuild(build *buildapi.Build, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, build.Name, opts.WithKind)
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", build.Namespace); err != nil {
return err
}
}
var created string
if build.Status.StartTimestamp != nil {
created = fmt.Sprintf("%s ago", formatRelativeTime(build.Status.StartTimestamp.Time))
}
var duration string
if build.Status.Duration > 0 {
duration = build.Status.Duration.String()
}
from := describeSourceShort(build.Spec.CommonSpec)
status := string(build.Status.Phase)
if len(build.Status.Reason) > 0 {
status = fmt.Sprintf("%s (%s)", status, build.Status.Reason)
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s", name, buildapi.StrategyType(build.Spec.Strategy), from, status, created, duration); err != nil {
return err
}
if err := appendItemLabels(build.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func describeSourceShort(spec buildapi.CommonSpec) string {
var from string
switch source := spec.Source; {
case source.Binary != nil:
from = "Binary"
if rev := describeSourceGitRevision(spec); len(rev) != 0 {
from = fmt.Sprintf("%s@%s", from, rev)
}
case source.Dockerfile != nil && source.Git != nil:
from = "Dockerfile,Git"
if rev := describeSourceGitRevision(spec); len(rev) != 0 {
from = fmt.Sprintf("%s@%s", from, rev)
}
case source.Dockerfile != nil:
from = "Dockerfile"
case source.Git != nil:
from = "Git"
if rev := describeSourceGitRevision(spec); len(rev) != 0 {
from = fmt.Sprintf("%s@%s", from, rev)
}
default:
from = buildapi.SourceType(source)
}
return from
}
var nonCommitRev = regexp.MustCompile("[^a-fA-F0-9]")
func describeSourceGitRevision(spec buildapi.CommonSpec) string {
var rev string
if spec.Revision != nil && spec.Revision.Git != nil {
rev = spec.Revision.Git.Commit
}
if len(rev) == 0 && spec.Source.Git != nil {
rev = spec.Source.Git.Ref
}
// if this appears to be a full Git commit hash, shorten it to 7 characters for brevity
if !nonCommitRev.MatchString(rev) && len(rev) > 20 {
rev = rev[:7]
}
return rev
}
func printBuildList(buildList *buildapi.BuildList, w io.Writer, opts kprinters.PrintOptions) error {
builds := buildList.Items
sort.Sort(buildapi.BuildSliceByCreationTimestamp(builds))
for _, build := range builds {
if err := printBuild(&build, w, opts); err != nil {
return err
}
}
return nil
}
func printBuildConfig(bc *buildapi.BuildConfig, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, bc.Name, opts.WithKind)
from := describeSourceShort(bc.Spec.CommonSpec)
if bc.Spec.Strategy.CustomStrategy != nil {
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", bc.Namespace); err != nil {
return err
}
}
_, err := fmt.Fprintf(w, "%s\t%v\t%s\t%d\n", name, buildapi.StrategyType(bc.Spec.Strategy), bc.Spec.Strategy.CustomStrategy.From.Name, bc.Status.LastVersion)
return err
}
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", bc.Namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%v\t%s\t%d", name, buildapi.StrategyType(bc.Spec.Strategy), from, bc.Status.LastVersion); err != nil {
return err
}
if err := appendItemLabels(bc.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printSubjectRulesReview(rulesReview *authorizationapi.SubjectRulesReview, w io.Writer, opts kprinters.PrintOptions) error {
printPolicyRule(rulesReview.Status.Rules, w)
return nil
}
func printSelfSubjectRulesReview(selfSubjectRulesReview *authorizationapi.SelfSubjectRulesReview, w io.Writer, opts kprinters.PrintOptions) error {
printPolicyRule(selfSubjectRulesReview.Status.Rules, w)
return nil
}
func printPolicyRule(policyRules []authorizationapi.PolicyRule, w io.Writer) error {
for _, rule := range policyRules {
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\n",
rule.Verbs.List(),
rule.NonResourceURLs.List(),
rule.ResourceNames.List(),
rule.APIGroups,
rule.Resources.List(),
)
}
return nil
}
func printBuildConfigList(buildList *buildapi.BuildConfigList, w io.Writer, opts kprinters.PrintOptions) error {
for _, buildConfig := range buildList.Items {
if err := printBuildConfig(&buildConfig, w, opts); err != nil {
return err
}
}
return nil
}
func printImage(image *imageapi.Image, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, image.Name, opts.WithKind)
if _, err := fmt.Fprintf(w, "%s\t%s", name, image.DockerImageReference); err != nil {
return err
}
if err := appendItemLabels(image.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printImageStreamTag(ist *imageapi.ImageStreamTag, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, ist.Name, opts.WithKind)
created := fmt.Sprintf("%s ago", formatRelativeTime(ist.CreationTimestamp.Time))
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", ist.Namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", name, ist.Image.DockerImageReference, created, ist.Image.Name); err != nil {
return err
}
if err := appendItemLabels(ist.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printImageStreamTagList(list *imageapi.ImageStreamTagList, w io.Writer, opts kprinters.PrintOptions) error {
for _, ist := range list.Items {
if err := printImageStreamTag(&ist, w, opts); err != nil {
return err
}
}
return nil
}
func printImageStreamImage(isi *imageapi.ImageStreamImage, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, isi.Name, opts.WithKind)
created := fmt.Sprintf("%s ago", formatRelativeTime(isi.CreationTimestamp.Time))
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", isi.Namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", name, isi.Image.DockerImageReference, created, isi.Image.Name); err != nil {
return err
}
if err := appendItemLabels(isi.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printImageList(images *imageapi.ImageList, w io.Writer, opts kprinters.PrintOptions) error {
for _, image := range images.Items {
if err := printImage(&image, w, opts); err != nil {
return err
}
}
return nil
}
func printImageStream(stream *imageapi.ImageStream, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, stream.Name, opts.WithKind)
tags := ""
const numOfTagsShown = 3
var latest metav1.Time
for _, list := range stream.Status.Tags {
if len(list.Items) > 0 {
if list.Items[0].Created.After(latest.Time) {
latest = list.Items[0].Created
}
}
}
latestTime := ""
if !latest.IsZero() {
latestTime = fmt.Sprintf("%s ago", formatRelativeTime(latest.Time))
}
list := imageapi.SortStatusTags(stream.Status.Tags)
more := false
if len(list) > numOfTagsShown {
list = list[:numOfTagsShown]
more = true
}
tags = strings.Join(list, ",")
if more {
tags = fmt.Sprintf("%s + %d more...", tags, len(stream.Status.Tags)-numOfTagsShown)
}
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", stream.Namespace); err != nil {
return err
}
}
repo := stream.Spec.DockerImageRepository
if len(repo) == 0 {
repo = stream.Status.DockerImageRepository
}
if len(stream.Status.PublicDockerImageRepository) > 0 {
repo = stream.Status.PublicDockerImageRepository
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", name, repo, tags, latestTime); err != nil {
return err
}
if err := appendItemLabels(stream.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printImageStreamList(streams *imageapi.ImageStreamList, w io.Writer, opts kprinters.PrintOptions) error {
for _, stream := range streams.Items {
if err := printImageStream(&stream, w, opts); err != nil {
return err
}
}
return nil
}
func printProject(project *projectapi.Project, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, project.Name, opts.WithKind)
_, err := fmt.Fprintf(w, "%s\t%s\t%s", name, project.Annotations[oapi.OpenShiftDisplayName], project.Status.Phase)
if err := appendItemLabels(project.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return err
}
// SortableProjects is a list of projects that can be sorted
type SortableProjects []projectapi.Project
func (list SortableProjects) Len() int {
return len(list)
}
func (list SortableProjects) Swap(i, j int) {
list[i], list[j] = list[j], list[i]
}
func (list SortableProjects) Less(i, j int) bool {
return list[i].ObjectMeta.Name < list[j].ObjectMeta.Name
}
func printProjectList(projects *projectapi.ProjectList, w io.Writer, opts kprinters.PrintOptions) error {
sort.Sort(SortableProjects(projects.Items))
for _, project := range projects.Items {
if err := printProject(&project, w, opts); err != nil {
return err
}
}
return nil
}
func printRoute(route *routeapi.Route, w io.Writer, opts kprinters.PrintOptions) error {
tlsTerm := ""
insecurePolicy := ""
if route.Spec.TLS != nil {
tlsTerm = string(route.Spec.TLS.Termination)
insecurePolicy = string(route.Spec.TLS.InsecureEdgeTerminationPolicy)
}
name := formatResourceName(opts.Kind, route.Name, opts.WithKind)
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", route.Namespace); err != nil {
return err
}
}
var (
matchedHost bool
reason string
host = route.Spec.Host
admitted, errors = 0, 0
)
for _, ingress := range route.Status.Ingress {
switch status, condition := routeapi.IngressConditionStatus(&ingress, routeapi.RouteAdmitted); status {
case kapi.ConditionTrue:
admitted++
if !matchedHost {
matchedHost = ingress.Host == route.Spec.Host
host = ingress.Host
}
case kapi.ConditionFalse:
reason = condition.Reason
errors++
}
}
switch {
case route.Status.Ingress == nil:
// this is the legacy case, we should continue to show the host when talking to servers
// that have not set status ingress, since we can't distinguish this condition from there
// being no routers.
case admitted == 0 && errors > 0:
host = reason
case errors > 0:
host = fmt.Sprintf("%s ... %d rejected", host, errors)
case admitted == 0:
host = "Pending"
case admitted > 1:
host = fmt.Sprintf("%s ... %d more", host, admitted-1)
}
var policy string
switch {
case len(tlsTerm) != 0 && len(insecurePolicy) != 0:
policy = fmt.Sprintf("%s/%s", tlsTerm, insecurePolicy)
case len(tlsTerm) != 0:
policy = tlsTerm
case len(insecurePolicy) != 0:
policy = fmt.Sprintf("default/%s", insecurePolicy)
default:
policy = ""
}
backends := append([]routeapi.RouteTargetReference{route.Spec.To}, route.Spec.AlternateBackends...)
totalWeight := int32(0)
for _, backend := range backends {
if backend.Weight != nil {
totalWeight += *backend.Weight
}
}
var backendInfo []string
for _, backend := range backends {
switch {
case backend.Weight == nil, len(backends) == 1 && totalWeight != 0:
backendInfo = append(backendInfo, backend.Name)
case totalWeight == 0:
backendInfo = append(backendInfo, fmt.Sprintf("%s(0%%)", backend.Name))
default:
backendInfo = append(backendInfo, fmt.Sprintf("%s(%d%%)", backend.Name, *backend.Weight*100/totalWeight))
}
}
var port string
if route.Spec.Port != nil {
port = route.Spec.Port.TargetPort.String()
} else {
port = "<all>"
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s", name, host, route.Spec.Path, strings.Join(backendInfo, ","), port, policy, route.Spec.WildcardPolicy); err != nil {
return err
}
err := appendItemLabels(route.Labels, w, opts.ColumnLabels, opts.ShowLabels)
return err
}
func printRouteList(routeList *routeapi.RouteList, w io.Writer, opts kprinters.PrintOptions) error {
for _, route := range routeList.Items {
if err := printRoute(&route, w, opts); err != nil {
return err
}
}
return nil
}
func printDeploymentConfig(dc *appsapi.DeploymentConfig, w io.Writer, opts kprinters.PrintOptions) error {
var desired string
if dc.Spec.Test {
desired = fmt.Sprintf("%d (during test)", dc.Spec.Replicas)
} else {
desired = fmt.Sprintf("%d", dc.Spec.Replicas)
}
containers := sets.NewString()
if dc.Spec.Template != nil {
for _, c := range dc.Spec.Template.Spec.Containers {
containers.Insert(c.Name)
}
}
//names := containers.List()
referencedContainers := sets.NewString()
triggers := sets.String{}
for _, trigger := range dc.Spec.Triggers {
switch t := trigger.Type; t {
case appsapi.DeploymentTriggerOnConfigChange:
triggers.Insert("config")
case appsapi.DeploymentTriggerOnImageChange:
if p := trigger.ImageChangeParams; p != nil && p.Automatic {
var prefix string
if len(containers) != 1 && !containers.HasAll(p.ContainerNames...) {
sort.Sort(sort.StringSlice(p.ContainerNames))
prefix = strings.Join(p.ContainerNames, ",") + ":"
}
referencedContainers.Insert(p.ContainerNames...)
switch p.From.Kind {
case "ImageStreamTag":
triggers.Insert(fmt.Sprintf("image(%s%s)", prefix, p.From.Name))
default:
triggers.Insert(fmt.Sprintf("%s(%s%s)", p.From.Kind, prefix, p.From.Name))
}
}
default:
triggers.Insert(string(t))
}
}
name := formatResourceName(opts.Kind, dc.Name, opts.WithKind)
trigger := strings.Join(triggers.List(), ",")
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", dc.Namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%d\t%s\t%d\t%s", name, dc.Status.LatestVersion, desired, dc.Status.UpdatedReplicas, trigger); err != nil {
return err
}
err := appendItemLabels(dc.Labels, w, opts.ColumnLabels, opts.ShowLabels)
return err
}
func printDeploymentConfigList(list *appsapi.DeploymentConfigList, w io.Writer, opts kprinters.PrintOptions) error {
for _, dc := range list.Items {
if err := printDeploymentConfig(&dc, w, opts); err != nil {
return err
}
}
return nil
}
func printPolicy(policy *authorizationapi.Policy, w io.Writer, opts kprinters.PrintOptions) error {
roleNames := sets.String{}
for key := range policy.Roles {
roleNames.Insert(key)
}
name := formatResourceName(opts.Kind, policy.Name, opts.WithKind)
rolesString := strings.Join(roleNames.List(), ", ")
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", policy.Namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%v", name, rolesString, policy.LastModified); err != nil {
return err
}
if err := appendItemLabels(policy.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printPolicyList(list *authorizationapi.PolicyList, w io.Writer, opts kprinters.PrintOptions) error {
for _, policy := range list.Items {
if err := printPolicy(&policy, w, opts); err != nil {
return err
}
}
return nil
}
func printPolicyBinding(policyBinding *authorizationapi.PolicyBinding, w io.Writer, opts kprinters.PrintOptions) error {
roleBindingNames := sets.String{}
for key := range policyBinding.RoleBindings {
roleBindingNames.Insert(key)
}
name := formatResourceName(opts.Kind, policyBinding.Name, opts.WithKind)
roleBindingsString := strings.Join(roleBindingNames.List(), ", ")
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", policyBinding.Namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%v", name, roleBindingsString, policyBinding.LastModified); err != nil {
return err
}
if err := appendItemLabels(policyBinding.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printPolicyBindingList(list *authorizationapi.PolicyBindingList, w io.Writer, opts kprinters.PrintOptions) error {
for _, policyBinding := range list.Items {
if err := printPolicyBinding(&policyBinding, w, opts); err != nil {
return err
}
}
return nil
}
func printClusterPolicy(policy *authorizationapi.ClusterPolicy, w io.Writer, opts kprinters.PrintOptions) error {
return printPolicy(authorizationapi.ToPolicy(policy), w, opts)
}
func printClusterPolicyList(list *authorizationapi.ClusterPolicyList, w io.Writer, opts kprinters.PrintOptions) error {
return printPolicyList(authorizationapi.ToPolicyList(list), w, opts)
}
func printClusterPolicyBinding(policyBinding *authorizationapi.ClusterPolicyBinding, w io.Writer, opts kprinters.PrintOptions) error {
return printPolicyBinding(authorizationapi.ToPolicyBinding(policyBinding), w, opts)
}
func printClusterPolicyBindingList(list *authorizationapi.ClusterPolicyBindingList, w io.Writer, opts kprinters.PrintOptions) error {
return printPolicyBindingList(authorizationapi.ToPolicyBindingList(list), w, opts)
}
func printClusterRole(role *authorizationapi.ClusterRole, w io.Writer, opts kprinters.PrintOptions) error {
return printRole(authorizationapi.ToRole(role), w, opts)
}
func printClusterRoleList(list *authorizationapi.ClusterRoleList, w io.Writer, opts kprinters.PrintOptions) error {
return printRoleList(authorizationapi.ToRoleList(list), w, opts)
}
func printClusterRoleBinding(roleBinding *authorizationapi.ClusterRoleBinding, w io.Writer, opts kprinters.PrintOptions) error {
return printRoleBinding(authorizationapi.ToRoleBinding(roleBinding), w, opts)
}
func printClusterRoleBindingList(list *authorizationapi.ClusterRoleBindingList, w io.Writer, opts kprinters.PrintOptions) error {
return printRoleBindingList(authorizationapi.ToRoleBindingList(list), w, opts)
}
func printIsPersonalSubjectAccessReview(a *authorizationapi.IsPersonalSubjectAccessReview, w io.Writer, opts kprinters.PrintOptions) error {
_, err := fmt.Fprintf(w, "IsPersonalSubjectAccessReview\n")
return err
}
func printRole(role *authorizationapi.Role, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, role.Name, opts.WithKind)
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", role.Namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s", name); err != nil {
return err
}
if err := appendItemLabels(role.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printRoleList(list *authorizationapi.RoleList, w io.Writer, opts kprinters.PrintOptions) error {
for _, role := range list.Items {
if err := printRole(&role, w, opts); err != nil {
return err
}
}
return nil
}
func truncatedList(list []string, maxLength int) string {
if len(list) > maxLength {
return fmt.Sprintf("%s (%d more)", strings.Join(list[0:maxLength], ", "), len(list)-maxLength)
}
return strings.Join(list, ", ")
}
func printRoleBinding(roleBinding *authorizationapi.RoleBinding, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, roleBinding.Name, opts.WithKind)
if opts.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", roleBinding.Namespace); err != nil {
return err
}
}
users, groups, sas, others := authorizationapi.SubjectsStrings(roleBinding.Namespace, roleBinding.Subjects)
if _, err := fmt.Fprintf(w, "%s\t%s\t%v\t%v\t%v\t%v", name,
roleBinding.RoleRef.Namespace+"/"+roleBinding.RoleRef.Name, truncatedList(users, 5),
truncatedList(groups, 5), strings.Join(sas, ", "), strings.Join(others, ", ")); err != nil {
return err
}
if err := appendItemLabels(roleBinding.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printRoleBindingList(list *authorizationapi.RoleBindingList, w io.Writer, opts kprinters.PrintOptions) error {
for _, roleBinding := range list.Items {
if err := printRoleBinding(&roleBinding, w, opts); err != nil {
return err
}
}
return nil
}
func printOAuthClient(client *oauthapi.OAuthClient, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, client.Name, opts.WithKind)
challenge := "FALSE"
if client.RespondWithChallenges {
challenge = "TRUE"
}
var maxAge string
switch {
case client.AccessTokenMaxAgeSeconds == nil:
maxAge = "default"
case *client.AccessTokenMaxAgeSeconds == 0:
maxAge = "unexpiring"
default:
duration := time.Duration(*client.AccessTokenMaxAgeSeconds) * time.Second
maxAge = duration.String()
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%v", name, client.Secret, challenge, maxAge, strings.Join(client.RedirectURIs, ",")); err != nil {
return err
}
if err := appendItemLabels(client.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printOAuthClientList(list *oauthapi.OAuthClientList, w io.Writer, opts kprinters.PrintOptions) error {
for _, item := range list.Items {
if err := printOAuthClient(&item, w, opts); err != nil {
return err
}
}
return nil
}
func printOAuthClientAuthorization(auth *oauthapi.OAuthClientAuthorization, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, auth.Name, opts.WithKind)
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%v\n", name, auth.UserName, auth.ClientName, strings.Join(auth.Scopes, ","))
return err
}
func printOAuthClientAuthorizationList(list *oauthapi.OAuthClientAuthorizationList, w io.Writer, opts kprinters.PrintOptions) error {
for _, item := range list.Items {
if err := printOAuthClientAuthorization(&item, w, opts); err != nil {
return err
}
}
return nil
}
func printOAuthAccessToken(token *oauthapi.OAuthAccessToken, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, token.Name, opts.WithKind)
created := token.CreationTimestamp
expires := "never"
if token.ExpiresIn > 0 {
expires = created.Add(time.Duration(token.ExpiresIn) * time.Second).String()
}
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", name, token.UserName, token.ClientName, created, expires, token.RedirectURI, strings.Join(token.Scopes, ","))
return err
}
func printOAuthAccessTokenList(list *oauthapi.OAuthAccessTokenList, w io.Writer, opts kprinters.PrintOptions) error {
for _, item := range list.Items {
if err := printOAuthAccessToken(&item, w, opts); err != nil {
return err
}
}
return nil
}
func printOAuthAuthorizeToken(token *oauthapi.OAuthAuthorizeToken, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, token.Name, opts.WithKind)
created := token.CreationTimestamp
expires := created.Add(time.Duration(token.ExpiresIn) * time.Second)
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", name, token.UserName, token.ClientName, created, expires, token.RedirectURI, strings.Join(token.Scopes, ","))
return err
}
func printOAuthAuthorizeTokenList(list *oauthapi.OAuthAuthorizeTokenList, w io.Writer, opts kprinters.PrintOptions) error {
for _, item := range list.Items {
if err := printOAuthAuthorizeToken(&item, w, opts); err != nil {
return err
}
}
return nil
}
func printUser(user *userapi.User, w io.Writer, opts kprinters.PrintOptions) error {
name := formatResourceName(opts.Kind, user.Name, opts.WithKind)
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", name, user.UID, user.FullName, strings.Join(user.Identities, ", ")); err != nil {
return err
}
if err := appendItemLabels(user.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil {
return err
}
return nil
}
func printUserList(list *userapi.UserList, w io.Writer, opts kprinters.PrintOptions) error {
for _, item := range list.Items {
if err := printUser(&item, w, opts); err != nil {
return err
}
}