forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
describer.go
288 lines (256 loc) · 8.32 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
package describe
import (
"fmt"
"strings"
"text/tabwriter"
kctl "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
buildapi "github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/client"
)
func DescriberFor(kind string, c *client.Client, host string) (kctl.Describer, bool) {
switch kind {
case "Build":
return &BuildDescriber{c}, true
case "BuildConfig":
return &BuildConfigDescriber{c, host}, true
case "Deployment":
return &DeploymentDescriber{c}, true
case "DeploymentConfig":
return &DeploymentConfigDescriber{c}, true
case "Image":
return &ImageDescriber{c}, true
case "ImageRepository":
return &ImageRepositoryDescriber{c}, true
case "Route":
return &RouteDescriber{c}, true
case "Project":
return &ProjectDescriber{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) DescribeParameters(p buildapi.BuildParameters, out *tabwriter.Writer) {
formatString(out, "Strategy", p.Strategy.Type)
switch p.Strategy.Type {
case buildapi.DockerBuildStrategyType:
if p.Strategy.DockerStrategy != nil && len(p.Strategy.DockerStrategy.ContextDir) == 0 {
formatString(out, "Context Directory", p.Strategy.DockerStrategy.ContextDir)
}
if p.Strategy.DockerStrategy != nil && p.Strategy.DockerStrategy.NoCache {
formatString(out, "No Cache", "yes")
}
if p.Strategy.DockerStrategy != nil {
formatString(out, "BaseImage", p.Strategy.DockerStrategy.BaseImage)
}
case buildapi.STIBuildStrategyType:
formatString(out, "Builder Image", p.Strategy.STIStrategy.Image)
if p.Strategy.STIStrategy.Clean {
formatString(out, "Clean Build", "yes")
}
case buildapi.CustomBuildStrategyType:
formatString(out, "Builder 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)
}
}
formatString(out, "Output To", p.Output.To)
formatString(out, "Output Spec", p.Output.DockerImageReference)
if p.Revision != nil && p.Revision.Type == buildapi.BuildSourceGit && p.Revision.Git != nil {
formatString(out, "Git Commit", p.Revision.Git.Commit)
d.DescribeUser(out, "Revision Author", p.Revision.Git.Author)
d.DescribeUser(out, "Revision Committer", p.Revision.Git.Committer)
if len(p.Revision.Git.Message) > 0 {
formatString(out, "Revision Message", p.Revision.Git.Message)
}
}
}
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, "Status", bold(build.Status))
formatString(out, "Build Pod", build.PodName)
d.DescribeParameters(build.Parameters, out)
return nil
})
}
// BuildConfigDescriber generates information about a buildConfig
type BuildConfigDescriber struct {
client.Interface
// TODO: this is broken, webhook URL generation should be done by client interface using
// the string value
host string
}
// 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
}
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
}
buildDescriber := &BuildDescriber{}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, buildConfig.ObjectMeta)
buildDescriber.DescribeParameters(buildConfig.Parameters, out)
d.DescribeTriggers(buildConfig, d.host, out)
return nil
})
}
// DeploymentDescriber generates information about a deployment
type DeploymentDescriber struct {
client.Interface
}
func (d *DeploymentDescriber) Describe(namespace, name string) (string, error) {
c := d.Deployments(namespace)
deployment, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, deployment.ObjectMeta)
formatString(out, "Status", bold(deployment.Status))
formatString(out, "Strategy", deployment.Strategy.Type)
causes := []string{}
if deployment.Details != nil {
for _, c := range deployment.Details.Causes {
causes = append(causes, string(c.Type))
}
}
formatString(out, "Causes", strings.Join(causes, ","))
return nil
})
}
// DeploymentConfigDescriber generates information about a DeploymentConfig
type DeploymentConfigDescriber struct {
client.Interface
}
func (d *DeploymentConfigDescriber) Describe(namespace, name string) (string, error) {
c := d.DeploymentConfigs(namespace)
deploymentConfig, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, deploymentConfig.ObjectMeta)
formatString(out, "Latest Version", deploymentConfig.LatestVersion)
triggers := []string{}
for _, t := range deploymentConfig.Triggers {
triggers = append(triggers, string(t.Type))
}
formatString(out, "Triggers", strings.Join(triggers, ","))
return nil
})
}
// ImageDescriber generates information about a Image
type ImageDescriber struct {
client.Interface
}
func (d *ImageDescriber) Describe(namespace, name string) (string, error) {
c := d.Images(namespace)
image, err := c.Get(name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, image.ObjectMeta)
formatString(out, "Docker Image", image.DockerImageReference)
return nil
})
}
// 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)
return nil
})
}