forked from openshift/origin
/
deployments.go
245 lines (213 loc) · 8.19 KB
/
deployments.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
package describe
import (
"fmt"
"io"
"strconv"
"strings"
"text/tabwriter"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
labels "github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/openshift/origin/pkg/client"
deployapi "github.com/openshift/origin/pkg/deploy/api"
deployutil "github.com/openshift/origin/pkg/deploy/util"
)
// DeploymentConfigDescriber generates information about a DeploymentConfig
type DeploymentConfigDescriber struct {
client deploymentDescriberClient
}
type deploymentDescriberClient interface {
getDeploymentConfig(namespace, name string) (*deployapi.DeploymentConfig, error)
getDeployment(namespace, name string) (*kapi.ReplicationController, error)
listPods(namespace string, selector labels.Selector) (*kapi.PodList, error)
}
type genericDeploymentDescriberClient struct {
getDeploymentConfigFunc func(namespace, name string) (*deployapi.DeploymentConfig, error)
getDeploymentFunc func(namespace, name string) (*kapi.ReplicationController, error)
listPodsFunc func(namespace string, selector labels.Selector) (*kapi.PodList, error)
}
func (c *genericDeploymentDescriberClient) getDeploymentConfig(namespace, name string) (*deployapi.DeploymentConfig, error) {
return c.getDeploymentConfigFunc(namespace, name)
}
func (c *genericDeploymentDescriberClient) getDeployment(namespace, name string) (*kapi.ReplicationController, error) {
return c.getDeploymentFunc(namespace, name)
}
func (c *genericDeploymentDescriberClient) listPods(namespace string, selector labels.Selector) (*kapi.PodList, error) {
return c.listPodsFunc(namespace, selector)
}
func NewDeploymentConfigDescriberForConfig(config *deployapi.DeploymentConfig) *DeploymentConfigDescriber {
return &DeploymentConfigDescriber{
client: &genericDeploymentDescriberClient{
getDeploymentConfigFunc: func(namespace, name string) (*deployapi.DeploymentConfig, error) {
return config, nil
},
getDeploymentFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
return nil, kerrors.NewNotFound("ReplicatonController", name)
},
listPodsFunc: func(namespace string, selector labels.Selector) (*kapi.PodList, error) {
return nil, kerrors.NewNotFound("PodList", fmt.Sprintf("%v", selector))
},
},
}
}
func NewDeploymentConfigDescriber(client client.Interface, kclient kclient.Interface) *DeploymentConfigDescriber {
return &DeploymentConfigDescriber{
client: &genericDeploymentDescriberClient{
getDeploymentConfigFunc: func(namespace, name string) (*deployapi.DeploymentConfig, error) {
return client.DeploymentConfigs(namespace).Get(name)
},
getDeploymentFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
return kclient.ReplicationControllers(namespace).Get(name)
},
listPodsFunc: func(namespace string, selector labels.Selector) (*kapi.PodList, error) {
return kclient.Pods(namespace).List(selector)
},
},
}
}
func (d *DeploymentConfigDescriber) Describe(namespace, name string) (string, error) {
deploymentConfig, err := d.client.getDeploymentConfig(namespace, name)
if err != nil {
return "", err
}
return tabbedString(func(out *tabwriter.Writer) error {
formatMeta(out, deploymentConfig.ObjectMeta)
if deploymentConfig.LatestVersion == 0 {
formatString(out, "Latest Version", "Not deployed")
} else {
formatString(out, "Latest Version", strconv.Itoa(deploymentConfig.LatestVersion))
}
printStrategy(deploymentConfig.Template.Strategy, out)
printTriggers(deploymentConfig.Triggers, out)
printReplicationControllerSpec(deploymentConfig.Template.ControllerTemplate, out)
deploymentName := deployutil.LatestDeploymentNameForConfig(deploymentConfig)
deployment, err := d.client.getDeployment(namespace, deploymentName)
if err != nil {
if kerrors.IsNotFound(err) {
formatString(out, "Latest Deployment", "<none>")
} else {
formatString(out, "Latest Deployment", fmt.Sprintf("error: %v", err))
}
} else {
printDeploymentRc(deployment, d.client, out)
}
return nil
})
}
func printStrategy(strategy deployapi.DeploymentStrategy, w io.Writer) {
fmt.Fprintf(w, "Strategy:\t%s\n", strategy.Type)
switch strategy.Type {
case deployapi.DeploymentStrategyTypeRecreate:
case deployapi.DeploymentStrategyTypeCustom:
fmt.Fprintf(w, "\t- Image:\t%s\n", strategy.CustomParams.Image)
if len(strategy.CustomParams.Environment) > 0 {
fmt.Fprintf(w, "\t- Environment:\t%s\n", formatLabels(convertEnv(strategy.CustomParams.Environment)))
}
if len(strategy.CustomParams.Command) > 0 {
fmt.Fprintf(w, "\t- Command:\t%v\n", strings.Join(strategy.CustomParams.Command, " "))
}
}
}
func printTriggers(triggers []deployapi.DeploymentTriggerPolicy, w io.Writer) {
if len(triggers) == 0 {
fmt.Fprint(w, "Triggers:\t<none>\n")
return
}
fmt.Fprint(w, "Triggers:\n")
for _, t := range triggers {
fmt.Fprintf(w, "\t- %s\n", t.Type)
switch t.Type {
case deployapi.DeploymentTriggerOnConfigChange:
fmt.Fprintf(w, "\t\t<no options>\n")
case deployapi.DeploymentTriggerOnImageChange:
if len(t.ImageChangeParams.RepositoryName) > 0 {
fmt.Fprintf(w, "\t\tAutomatic:\t%v\n\t\tRepository:\t%s\n\t\tTag:\t%s\n",
t.ImageChangeParams.Automatic,
t.ImageChangeParams.RepositoryName,
t.ImageChangeParams.Tag,
)
} else if len(t.ImageChangeParams.From.Name) > 0 {
fmt.Fprintf(w, "\t\tAutomatic:\t%v\n\t\tImage Repository:\t%s\n\t\tTag:\t%s\n",
t.ImageChangeParams.Automatic,
t.ImageChangeParams.From.Name,
t.ImageChangeParams.Tag,
)
}
default:
fmt.Fprint(w, "unknown\n")
}
}
}
func printReplicationControllerSpec(spec kapi.ReplicationControllerSpec, w io.Writer) error {
fmt.Fprint(w, "Template:\n")
fmt.Fprintf(w, "\tSelector:\t%s\n\tReplicas:\t%d\n",
formatLabels(spec.Selector),
spec.Replicas)
fmt.Fprintf(w, "\tContainers:\n\t\tNAME\tIMAGE\tENV\n")
for _, container := range spec.Template.Spec.Containers {
fmt.Fprintf(w, "\t\t%s\t%s\t%s\n",
container.Name,
container.Image,
formatLabels(convertEnv(container.Env)))
}
return nil
}
func printDeploymentRc(deployment *kapi.ReplicationController, client deploymentDescriberClient, w io.Writer) error {
running, waiting, succeeded, failed, err := getPodStatusForDeployment(deployment, client)
if err != nil {
return err
}
fmt.Fprint(w, "Latest Deployment:\n")
fmt.Fprintf(w, "\tName:\t%s\n", deployment.Name)
fmt.Fprintf(w, "\tStatus:\t%s\n", deployment.Annotations[deployapi.DeploymentStatusAnnotation])
fmt.Fprintf(w, "\tSelector:\t%s\n", formatLabels(deployment.Spec.Selector))
fmt.Fprintf(w, "\tLabels:\t%s\n", formatLabels(deployment.Labels))
fmt.Fprintf(w, "\tReplicas:\t%d current / %d desired\n", deployment.Status.Replicas, deployment.Spec.Replicas)
fmt.Fprintf(w, "\tPods Status:\t%d Running / %d Waiting / %d Succeeded / %d Failed\n", running, waiting, succeeded, failed)
return nil
}
func getPodStatusForDeployment(deployment *kapi.ReplicationController, client deploymentDescriberClient) (running, waiting, succeeded, failed int, err error) {
rcPods, err := client.listPods(deployment.Namespace, labels.SelectorFromSet(deployment.Spec.Selector))
if err != nil {
return
}
for _, pod := range rcPods.Items {
switch pod.Status.Phase {
case kapi.PodRunning:
running++
case kapi.PodPending:
waiting++
case kapi.PodSucceeded:
succeeded++
case kapi.PodFailed:
failed++
}
}
return
}
// DeploymentDescriber generates information about a deployment
// DEPRECATED.
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
})
}