forked from openshift/origin
/
printer.go
387 lines (341 loc) · 12.4 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
package describe
import (
"fmt"
"io"
"strings"
kctl "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
buildapi "github.com/openshift/origin/pkg/build/api"
deployapi "github.com/openshift/origin/pkg/deploy/api"
imageapi "github.com/openshift/origin/pkg/image/api"
oauthapi "github.com/openshift/origin/pkg/oauth/api"
projectapi "github.com/openshift/origin/pkg/project/api"
routeapi "github.com/openshift/origin/pkg/route/api"
templateapi "github.com/openshift/origin/pkg/template/api"
userapi "github.com/openshift/origin/pkg/user/api"
)
var (
buildColumns = []string{"NAME", "TYPE", "STATUS", "POD"}
buildConfigColumns = []string{"NAME", "TYPE", "SOURCE"}
imageColumns = []string{"NAME", "DOCKER REF"}
imageRepositoryColumns = []string{"NAME", "DOCKER REPO", "TAGS"}
projectColumns = []string{"NAME", "DISPLAY NAME"}
routeColumns = []string{"NAME", "HOST/PORT", "PATH", "SERVICE", "LABELS"}
deploymentColumns = []string{"NAME", "STATUS", "CAUSE"}
deploymentConfigColumns = []string{"NAME", "TRIGGERS", "LATEST VERSION"}
templateColumns = []string{"NAME", "DESCRIPTION", "PARAMETERS", "OBJECTS"}
parameterColumns = []string{"NAME", "DESCRIPTION", "GENERATOR", "VALUE"}
policyColumns = []string{"NAME", "ROLES", "LAST MODIFIED"}
policyBindingColumns = []string{"NAME", "ROLE BINDINGS", "LAST MODIFIED"}
oauthClientColumns = []string{"NAME", "SECRET", "WWW-CHALLENGE", "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"}
userIdentityMappingColumns = []string{"NAME", "IDENTITY PROVIDER", "IDENTITY USERNAME", "USER NAME"}
)
func NewHumanReadablePrinter(noHeaders bool) *kctl.HumanReadablePrinter {
p := kctl.NewHumanReadablePrinter(noHeaders)
p.Handler(buildColumns, printBuild)
p.Handler(buildColumns, printBuildList)
p.Handler(buildConfigColumns, printBuildConfig)
p.Handler(buildConfigColumns, printBuildConfigList)
p.Handler(imageColumns, printImage)
p.Handler(imageColumns, printImageList)
p.Handler(imageRepositoryColumns, printImageRepository)
p.Handler(imageRepositoryColumns, printImageRepositoryList)
p.Handler(projectColumns, printProject)
p.Handler(projectColumns, printProjectList)
p.Handler(routeColumns, printRoute)
p.Handler(routeColumns, printRouteList)
p.Handler(deploymentColumns, printDeployment)
p.Handler(deploymentColumns, printDeploymentList)
p.Handler(deploymentConfigColumns, printDeploymentConfig)
p.Handler(deploymentConfigColumns, printDeploymentConfigList)
p.Handler(templateColumns, printTemplate)
p.Handler(templateColumns, printTemplateList)
p.Handler(policyColumns, printPolicy)
p.Handler(policyColumns, printPolicyList)
p.Handler(policyBindingColumns, printPolicyBinding)
p.Handler(policyBindingColumns, printPolicyBindingList)
p.Handler(oauthClientColumns, printOAuthClient)
p.Handler(oauthClientColumns, printOAuthClientList)
p.Handler(oauthClientAuthorizationColumns, printOAuthClientAuthorization)
p.Handler(oauthClientAuthorizationColumns, printOAuthClientAuthorizationList)
p.Handler(oauthAccessTokenColumns, printOAuthAccessToken)
p.Handler(oauthAccessTokenColumns, printOAuthAccessTokenList)
p.Handler(oauthAuthorizeTokenColumns, printOAuthAuthorizeToken)
p.Handler(oauthAuthorizeTokenColumns, printOAuthAuthorizeTokenList)
p.Handler(userColumns, printUser)
p.Handler(userIdentityMappingColumns, printUserIdentityMapping)
return p
}
const templateDescriptionLen = 80
func printTemplate(t *templateapi.Template, w io.Writer) error {
description := ""
if t.Annotations != nil {
description = t.Annotations["description"]
}
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)
}
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%d\n", t.Name, description, params, len(t.Objects))
return err
}
func printTemplateParameters(t *templateapi.Template, w io.Writer) error {
for _, p := range t.Parameters {
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
}
func printTemplateList(list *templateapi.TemplateList, w io.Writer) error {
for _, t := range list.Items {
if err := printTemplate(&t, w); err != nil {
return err
}
}
return nil
}
func printBuild(build *buildapi.Build, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", build.Name, build.Parameters.Strategy.Type, build.Status, build.PodName)
return err
}
func printBuildList(buildList *buildapi.BuildList, w io.Writer) error {
for _, build := range buildList.Items {
if err := printBuild(&build, w); err != nil {
return err
}
}
return nil
}
func printBuildConfig(bc *buildapi.BuildConfig, w io.Writer) error {
if bc.Parameters.Strategy.Type == buildapi.CustomBuildStrategyType {
_, err := fmt.Fprintf(w, "%s\t%v\t%s\n", bc.Name, bc.Parameters.Strategy.Type, bc.Parameters.Strategy.CustomStrategy.Image)
return err
}
_, err := fmt.Fprintf(w, "%s\t%v\t%s\n", bc.Name, bc.Parameters.Strategy.Type, bc.Parameters.Source.Git.URI)
return err
}
func printBuildConfigList(buildList *buildapi.BuildConfigList, w io.Writer) error {
for _, buildConfig := range buildList.Items {
if err := printBuildConfig(&buildConfig, w); err != nil {
return err
}
}
return nil
}
func printImage(image *imageapi.Image, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\n", image.Name, image.DockerImageReference)
return err
}
func printImageList(images *imageapi.ImageList, w io.Writer) error {
for _, image := range images.Items {
if err := printImage(&image, w); err != nil {
return err
}
}
return nil
}
func printImageRepository(repo *imageapi.ImageRepository, w io.Writer) error {
tags := ""
if len(repo.Tags) > 0 {
var t []string
for tag := range repo.Tags {
t = append(t, tag)
}
tags = strings.Join(t, ",")
}
_, err := fmt.Fprintf(w, "%s\t%s\t%s\n", repo.Name, repo.Status.DockerImageRepository, tags)
return err
}
func printImageRepositoryList(repos *imageapi.ImageRepositoryList, w io.Writer) error {
for _, repo := range repos.Items {
if err := printImageRepository(&repo, w); err != nil {
return err
}
}
return nil
}
func printProject(project *projectapi.Project, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\n", project.Name, project.DisplayName)
return err
}
func printProjectList(projects *projectapi.ProjectList, w io.Writer) error {
for _, project := range projects.Items {
if err := printProject(&project, w); err != nil {
return err
}
}
return nil
}
func printRoute(route *routeapi.Route, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", route.Name, route.Host, route.Path, route.ServiceName, labels.Set(route.Labels))
return err
}
func printRouteList(routeList *routeapi.RouteList, w io.Writer) error {
for _, route := range routeList.Items {
if err := printRoute(&route, w); err != nil {
return err
}
}
return nil
}
func printDeployment(d *deployapi.Deployment, w io.Writer) error {
causes := util.StringSet{}
if d.Details != nil {
for _, cause := range d.Details.Causes {
causes.Insert(string(cause.Type))
}
}
cStr := strings.Join(causes.List(), ", ")
_, err := fmt.Fprintf(w, "%s\t%s\t%s\n", d.Name, d.Status, cStr)
return err
}
func printDeploymentList(list *deployapi.DeploymentList, w io.Writer) error {
for _, d := range list.Items {
if err := printDeployment(&d, w); err != nil {
return err
}
}
return nil
}
func printDeploymentConfig(dc *deployapi.DeploymentConfig, w io.Writer) error {
triggers := util.StringSet{}
for _, trigger := range dc.Triggers {
triggers.Insert(string(trigger.Type))
}
tStr := strings.Join(triggers.List(), ", ")
_, err := fmt.Fprintf(w, "%s\t%s\t%v\n", dc.Name, tStr, dc.LatestVersion)
return err
}
func printDeploymentConfigList(list *deployapi.DeploymentConfigList, w io.Writer) error {
for _, dc := range list.Items {
if err := printDeploymentConfig(&dc, w); err != nil {
return err
}
}
return nil
}
func printPolicy(policy *authorizationapi.Policy, w io.Writer) error {
roleNames := util.StringSet{}
for key := range policy.Roles {
roleNames.Insert(key)
}
rolesString := strings.Join(roleNames.List(), ", ")
_, err := fmt.Fprintf(w, "%s\t%s\t%v\n", policy.Name, rolesString, policy.LastModified)
return err
}
func printPolicyList(list *authorizationapi.PolicyList, w io.Writer) error {
for _, policy := range list.Items {
if err := printPolicy(&policy, w); err != nil {
return err
}
}
return nil
}
func printPolicyBinding(policyBinding *authorizationapi.PolicyBinding, w io.Writer) error {
roleBindingNames := util.StringSet{}
for key := range policyBinding.RoleBindings {
roleBindingNames.Insert(key)
}
roleBindingsString := strings.Join(roleBindingNames.List(), ", ")
_, err := fmt.Fprintf(w, "%s\t%s\t%v\n", policyBinding.Name, roleBindingsString, policyBinding.LastModified)
return err
}
func printPolicyBindingList(list *authorizationapi.PolicyBindingList, w io.Writer) error {
for _, policyBinding := range list.Items {
if err := printPolicyBinding(&policyBinding, w); err != nil {
return err
}
}
return nil
}
func printOAuthClient(client *oauthapi.OAuthClient, w io.Writer) error {
challenge := "FALSE"
if client.RespondWithChallenges {
challenge = "TRUE"
}
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%v\n", client.Name, client.Secret, challenge, strings.Join(client.RedirectURIs, ","))
return err
}
func printOAuthClientList(list *oauthapi.OAuthClientList, w io.Writer) error {
for _, item := range list.Items {
if err := printOAuthClient(&item, w); err != nil {
return err
}
}
return nil
}
func printOAuthClientAuthorization(auth *oauthapi.OAuthClientAuthorization, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%v\n", auth.Name, auth.UserName, auth.ClientName, strings.Join(auth.Scopes, ","))
return err
}
func printOAuthClientAuthorizationList(list *oauthapi.OAuthClientAuthorizationList, w io.Writer) error {
for _, item := range list.Items {
if err := printOAuthClientAuthorization(&item, w); err != nil {
return err
}
}
return nil
}
func printOAuthAccessToken(token *oauthapi.OAuthAccessToken, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\t%s\t%s\n", token.Name, token.UserName, token.ClientName, token.CreationTimestamp, token.ExpiresIn, token.RedirectURI, strings.Join(token.Scopes, ","))
return err
}
func printOAuthAccessTokenList(list *oauthapi.OAuthAccessTokenList, w io.Writer) error {
for _, item := range list.Items {
if err := printOAuthAccessToken(&item, w); err != nil {
return err
}
}
return nil
}
func printOAuthAuthorizeToken(token *oauthapi.OAuthAuthorizeToken, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\t%s\t%s\n", token.Name, token.UserName, token.ClientName, token.CreationTimestamp, token.ExpiresIn, token.RedirectURI, strings.Join(token.Scopes, ","))
return err
}
func printOAuthAuthorizeTokenList(list *oauthapi.OAuthAuthorizeTokenList, w io.Writer) error {
for _, item := range list.Items {
if err := printOAuthAuthorizeToken(&item, w); err != nil {
return err
}
}
return nil
}
func printUser(user *userapi.User, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%s\n", user.Name, user.UID, user.FullName)
return err
}
func printUserIdentityMapping(mapping *userapi.UserIdentityMapping, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", mapping.Name, mapping.Identity.Provider, mapping.Identity.UserName, mapping.User.Name)
return err
}