-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_deployment.go
297 lines (254 loc) · 7.65 KB
/
component_deployment.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
package models
import (
"strings"
"github.com/equinor/radix-api/api/utils"
corev1 "k8s.io/api/core/v1"
)
// Component describe an component part of an deployment
// swagger:model Component
type Component struct {
// Name the component
//
// required: true
// example: server
Name string `json:"name"`
// Type of component
//
// required: true
// example: component
Type string `json:"type"`
// Status of the component
// required: false
// - Stopped = Component is stopped (no replica)
// - Consistent = Component is consistent with config
// - Restarting = User has triggered restart, but this is not reconciled
//
// example: Consistent
Status string `json:"status"`
// Image name
//
// required: true
// example: radixdev.azurecr.io/radix-api-server:cdgkg
Image string `json:"image"`
// Ports defines the port number and protocol that a component is exposed for internally in environment
//
// required: false
// type: "array"
// items:
// "$ref": "#/definitions/Port"
Ports []Port `json:"ports"`
// SchedulerPort defines the port number that a Job Scheduler is exposed internally in environment
//
// required: false
// example: 8080
SchedulerPort *int32 `json:"schedulerPort,omitempty"`
// ScheduledJobPayloadPath defines the payload path, where payload for Job Scheduler will be mapped as a file. From radixconfig.yaml
//
// required: false
// example: "/tmp/payload"
ScheduledJobPayloadPath string `json:"scheduledJobPayloadPath,omitempty"`
// Component secret names. From radixconfig.yaml
//
// required: false
// example: DB_CON,A_SECRET
Secrets []string `json:"secrets"`
// Variable names map to values. From radixconfig.yaml
//
// required: false
Variables map[string]string `json:"variables"`
// Array of pod names
//
// required: false
// deprecated: true
// example: server-78fc8857c4-hm76l,server-78fc8857c4-asfa2
// Deprecated: Use ReplicaList instead.
Replicas []string `json:"replicas"`
// Array of ReplicaSummary
//
// required: false
ReplicaList []ReplicaSummary `json:"replicaList"`
// HorizontalScaling defines horizontal scaling summary for this component
//
// required: false
HorizontalScalingSummary *HorizontalScalingSummary `json:"horizontalScalingSummary"`
// Array of ScheduledJobList
//
// required: false
ScheduledJobList []ScheduledJobSummary `json:"scheduledJobList"`
}
// Port describe an component part of an deployment
// swagger:model Port
type Port struct {
// Component port name. From radixconfig.yaml
//
// required: true
// example: http
Name string `json:"name"`
// Component port number. From radixconfig.yaml
//
// required: false
// example: 8080
Port int32 `json:"port"`
}
// ComponentSummary describe an component part of an deployment
// swagger:model ComponentSummary
type ComponentSummary struct {
// Name the component
//
// required: true
// example: server
Name string `json:"name"`
// Type of component
//
// required: true
// example: component
Type string `json:"type"`
// Image name
//
// required: true
// example: radixdev.azurecr.io/radix-api-server:cdgkg
Image string `json:"image"`
}
// ReplicaSummary describes condition of a pod
// swagger:model ReplicaSummary
type ReplicaSummary struct {
// Pod name
//
// required: true
// example: server-78fc8857c4-hm76l
Name string `json:"name"`
// Created timestamp
//
// required: false
// example: 2006-01-02T15:04:05Z
Created string `json:"created"`
// Status describes the component container status
//
// required: false
Status ReplicaStatus `json:"replicaStatus"`
// StatusMessage provides message describing the status of a component container inside a pod
//
// required: false
StatusMessage string `json:"statusMessage"`
// RestartCount count of restarts of a component container inside a pod
//
// required: false
RestartCount int32 `json:"restartCount"`
}
// ReplicaStatus describes the status of a component container inside a pod
// swagger:model ReplicaStatus
type ReplicaStatus struct {
// Status of the container
// - Pending = Container in Waiting state and the reason is ContainerCreating
// - Failing = Container in Waiting state and the reason is anything else but ContainerCreating
// - Running = Container in Running state
// - Terminated = Container in Terminated state
//
// Enum: Pending,Failing,Running,Terminated
// required: true
// example: Pending, Failing, Running, Terminated, Starting
Status string `json:"status"`
}
// HorizontalScalingSummary describe the summary of horizontal scaling of a component
// swagger:model HorizontalScalingSummary
type HorizontalScalingSummary struct {
// Component minimum replicas. From radixconfig.yaml
//
// required: false
// example: 2
MinReplicas int32 `json:"minReplicas"`
// Component maximum replicas. From radixconfig.yaml
//
// required: false
// example: 5
MaxReplicas int32 `json:"maxReplicas"`
// Component current average CPU utilization over all pods, represented as a percentage of requested CPU
//
// required: false
// example: 70
CurrentCPUUtilizationPercentage int32 `json:"currentCPUUtilizationPercentage"`
// Component target average CPU utilization over all pods
//
// required: false
// example: 80
TargetCPUUtilizationPercentage int32 `json:"targetCPUUtilizationPercentage"`
}
// ScheduledJobSummary holds general information about scheduled job
// swagger:model ScheduledJobSummary
type ScheduledJobSummary struct {
// Name of the scheduled job
//
// required: false
// example: job-component-20181029135644-algpv-6hznh
Name string `json:"name"`
// Created timestamp
//
// required: false
// example: 2006-01-02T15:04:05Z
Created string `json:"created"`
// Started timestamp
//
// required: false
// example: 2006-01-02T15:04:05Z
Started string `json:"started"`
// Ended timestamp
//
// required: false
// example: 2006-01-02T15:04:05Z
Ended string `json:"ended"`
// Status of the job
//
// required: false
// Enum: Waiting,Running,Succeeded,Stopping,Stopped,Failed
// example: Waiting
Status string `json:"status"`
// Array of ReplicaSummary
//
// required: false
ReplicaList []ReplicaSummary `json:"replicaList"`
}
func GetReplicaSummary(pod corev1.Pod) ReplicaSummary {
replicaSummary := ReplicaSummary{}
replicaSummary.Name = pod.GetName()
creationTimestamp := pod.GetCreationTimestamp()
replicaSummary.Created = utils.FormatTimestamp(creationTimestamp.Time)
if len(pod.Status.ContainerStatuses) <= 0 {
return replicaSummary
}
// We assume one component container per component pod
containerStatus := pod.Status.ContainerStatuses[0]
containerState := containerStatus.State
// Set default Pending status
replicaSummary.Status = ReplicaStatus{Status: Pending.String()}
if containerState.Waiting != nil {
replicaSummary.StatusMessage = containerState.Waiting.Message
if !strings.EqualFold(containerState.Waiting.Reason, "ContainerCreating") {
replicaSummary.Status = ReplicaStatus{Status: Failing.String()}
}
}
if containerState.Running != nil {
if containerStatus.Ready {
replicaSummary.Status = ReplicaStatus{Status: Running.String()}
} else {
replicaSummary.Status = ReplicaStatus{Status: Starting.String()}
}
}
if containerState.Terminated != nil {
replicaSummary.Status = ReplicaStatus{Status: Terminated.String()}
replicaSummary.StatusMessage = containerState.Terminated.Message
}
replicaSummary.RestartCount = containerStatus.RestartCount
return replicaSummary
}
func (job *ScheduledJobSummary) GetCreated() string {
return job.Created
}
func (job *ScheduledJobSummary) GetStarted() string {
return job.Started
}
func (job *ScheduledJobSummary) GetEnded() string {
return job.Ended
}
func (job *ScheduledJobSummary) GetStatus() string {
return job.Status
}