-
Notifications
You must be signed in to change notification settings - Fork 716
/
Copy pathcontroller_status.go
314 lines (284 loc) · 11.2 KB
/
controller_status.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
// Copyright 2018 The Kubeflow Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package controller provides a Kubernetes controller for a TFJob resource.
package controller
import (
"fmt"
"strconv"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
tfv1alpha2 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1alpha2"
)
const (
// tfJobCreatedReason is added in a tfjob when it is created.
tfJobCreatedReason = "TFJobCreated"
// tfJobSucceededReason is added in a tfjob when it is succeeded.
tfJobSucceededReason = "TFJobSucceeded"
// tfJobSucceededReason is added in a tfjob when it is running.
tfJobRunningReason = "TFJobRunning"
// tfJobSucceededReason is added in a tfjob when it is failed.
tfJobFailedReason = "TFJobFailed"
// tfJobRestartingReason is added in a tfjob when it is restarting.
tfJobRestartingReason = "TFJobRestarting"
//workerCreatedReason is added in a status when it is created.
workerCreatedReason = "WorkerCreated"
//workerSucceededReason is added in a status when it is succeeded.
workerSucceededReason = "WorkerSucceeded"
//workerSucceededReason is added in a status when it is running.
workerRunningReason = "WorkerRunning"
//workerFailedReason is added in a status when it is failed.
workerFailedReason = "WorkerFailed"
//workerPendingReason is added in a status when it is pending.
workerPendingReason = "WorkerPending"
//psCreatedReason is added in a status when it is created.
psCreatedreason = "PSCreated"
//psSucceededReason is added in a status when it is succeeded.
psSucceededReason = "PSSucceeded"
//psRunningReason is added in a status when it is running.
psRunningReason = "PSRunning"
//psFailedReason is added in a status when it is failed.
psFailedReason = "PSFailed"
//psPendingReason is added in a status when it is pending.
psPendingReason = "PSPending"
)
// updateStatus updates the status of the tfjob.
func (tc *TFJobController) updateStatus(tfjob *tfv1alpha2.TFJob, rtype tfv1alpha2.TFReplicaType, replicas int) error {
// Expect to have `replicas - succeeded` pods alive.
expected := replicas - int(tfjob.Status.TFReplicaStatuses[rtype].Succeeded)
running := int(tfjob.Status.TFReplicaStatuses[rtype].Active)
failed := int(tfjob.Status.TFReplicaStatuses[rtype].Failed)
if rtype == tfv1alpha2.TFReplicaTypeWorker {
// All workers are running, set StartTime.
if running == replicas {
now := metav1.Now()
tfjob.Status.StartTime = &now
}
// Some workers are still running, leave a running condition.
if running > 0 {
msg := fmt.Sprintf("TFJob %s is running.", tfjob.Name)
err := tc.updateTFJobConditions(tfjob, tfv1alpha2.TFJobRunning, tfJobRunningReason, msg)
if err != nil {
loggerForTFJob(tfjob).Infof("Append tfjob condition error: %v", err)
return err
}
}
// All workers are succeeded, leave a succeeded condition.
if expected == 0 {
msg := fmt.Sprintf("TFJob %s is successfully completed.", tfjob.Name)
now := metav1.Now()
tfjob.Status.CompletionTime = &now
err := tc.updateTFJobConditions(tfjob, tfv1alpha2.TFJobSucceeded, tfJobSucceededReason, msg)
if err != nil {
loggerForTFJob(tfjob).Infof("Append tfjob condition error: %v", err)
return err
}
}
}
// Some workers or pss are failed , leave a failed condition.
if failed > 0 {
msg := fmt.Sprintf("TFJob %s is failed.", tfjob.Name)
err := tc.updateTFJobConditions(tfjob, tfv1alpha2.TFJobFailed, tfJobFailedReason, msg)
if err != nil {
loggerForTFJob(tfjob).Infof("Append tfjob condition error: %v", err)
return err
}
}
return nil
}
func (tc *TFJobController) updateStatusNew(tfjob *tfv1alpha2.TFJob, rstatus map[string]v1.PodPhase, wreplicas int, preplicas int) error {
if preplicas == 0 {
err := tc.updateStatus(tfjob, tfv1alpha2.TFReplicaTypeWorker, wreplicas)
if err != nil {
return err
}
} else {
status := make(map[string]int)
initReplicasStatusesNum(status)
workerKey := string(tfv1alpha2.TFReplicaTypeWorker) + "-0"
switch rstatus[workerKey] {
case v1.PodRunning:
status[workerRunningReason] = 1
case v1.PodSucceeded:
status[workerSucceededReason] = 1
case v1.PodPending:
status[workerPendingReason] = 1
case v1.PodFailed:
status[workerFailedReason] = 1
}
for i := 0; i < preplicas; i++ {
psKey := string(tfv1alpha2.TFReplicaTypePS) + "-" + strconv.Itoa(i)
switch rstatus[psKey] {
case v1.PodRunning:
status[psRunningReason]++
case v1.PodFailed:
status[psFailedReason]++
case v1.PodPending:
status[psPendingReason]++
}
}
if status[psRunningReason] == preplicas && status[workerRunningReason] == 1 {
//Running
msg := fmt.Sprintf("TFJob %s is running.", tfjob.Name)
now := metav1.Now()
tfjob.Status.CompletionTime = &now
err := tc.updateTFJobConditions(tfjob, tfv1alpha2.TFJobRunning, tfJobRunningReason, msg)
if err != nil {
loggerForTFJob(tfjob).Infof("Append tfjob condition error: %v", err)
return err
}
}
if status[psRunningReason] == preplicas && status[workerSucceededReason] == 1 {
//Succeeded
msg := fmt.Sprintf("TFJob %s is successfully completed.", tfjob.Name)
now := metav1.Now()
tfjob.Status.CompletionTime = &now
err := tc.updateTFJobConditions(tfjob, tfv1alpha2.TFJobSucceeded, tfJobSucceededReason, msg)
if err != nil {
loggerForTFJob(tfjob).Infof("Append tfjob condition error: %v", err)
return err
}
}
if status[psFailedReason] != 0 || status[workerFailedReason] != 0 {
//Failed
msg := fmt.Sprintf("TFJob %s is failed.", tfjob.Name)
now := metav1.Now()
tfjob.Status.CompletionTime = &now
err := tc.updateTFJobConditions(tfjob, tfv1alpha2.TFJobFailed, tfJobFailedReason, msg)
if err != nil {
loggerForTFJob(tfjob).Infof("Append tfjob condition error: %v", err)
return err
}
}
if status[psRunningReason] == preplicas && status[workerPendingReason] == 1 && tfjob.Status.Conditions[len(tfjob.Status.Conditions)-1].Type == tfv1alpha2.TFJobFailed {
//Restarting
msg := fmt.Sprintf("TFJob %s is restarting ", tfjob.Name)
now := metav1.Now()
tfjob.Status.CompletionTime = &now
err := tc.updateTFJobConditions(tfjob, tfv1alpha2.TFJobRestarting, tfJobRestartingReason, msg)
if err != nil {
loggerForTFJob(tfjob).Infof("Append tfjob condition error: %v", err)
return err
}
}
}
return nil
}
// initReplicasStatusesNum initializes the PS or Worker for status.
func initReplicasStatusesNum(status map[string]int) {
status[workerPendingReason] = 0
status[workerRunningReason] = 0
status[workerFailedReason] = 0
status[workerSucceededReason] = 0
status[workerCreatedReason] = 0
status[psCreatedreason] = 0
status[psPendingReason] = 0
status[psRunningReason] = 0
status[psFailedReason] = 0
status[psSucceededReason] = 0
}
// updateTFJobStatus updates the status of the given TFJob.
func (tc *TFJobController) updateTFJobStatus(tfjob *tfv1alpha2.TFJob) error {
_, err := tc.tfJobClientSet.KubeflowV1alpha2().TFJobs(tfjob.Namespace).Update(tfjob)
return err
}
// updateTFJobConditions updates the conditions of the given tfjob.
func (tc *TFJobController) updateTFJobConditions(tfjob *tfv1alpha2.TFJob, conditionType tfv1alpha2.TFJobConditionType, reason, message string) error {
condition := newCondition(conditionType, reason, message)
setCondition(&tfjob.Status, condition)
return nil
}
// initializeTFReplicaStatuses initializes the TFReplicaStatuses for replica.
func initializeTFReplicaStatuses(tfjob *tfv1alpha2.TFJob, rtype tfv1alpha2.TFReplicaType) {
if tfjob.Status.TFReplicaStatuses == nil {
tfjob.Status.TFReplicaStatuses = make(map[tfv1alpha2.TFReplicaType]*tfv1alpha2.TFReplicaStatus)
}
tfjob.Status.TFReplicaStatuses[rtype] = &tfv1alpha2.TFReplicaStatus{}
}
// updateTFJobReplicaStatuses updates the TFJobReplicaStatuses according to the pod.
func updateTFJobReplicaStatuses(tfjob *tfv1alpha2.TFJob, rtype tfv1alpha2.TFReplicaType, pod *v1.Pod) {
switch pod.Status.Phase {
case v1.PodRunning:
tfjob.Status.TFReplicaStatuses[rtype].Active++
case v1.PodSucceeded:
tfjob.Status.TFReplicaStatuses[rtype].Succeeded++
case v1.PodFailed:
tfjob.Status.TFReplicaStatuses[rtype].Failed++
}
}
func addTFJobReplicaStatuses(rtype tfv1alpha2.TFReplicaType, index int, pod *v1.Pod, rstatus map[string]v1.PodPhase) {
key := string(rtype) + "-" + strconv.Itoa(index)
for _, val := range pod.Status.ContainerStatuses {
if val.State.Waiting != nil {
rstatus[key] = v1.PodFailed
return
}
}
rstatus[key] = pod.Status.Phase
}
// newCondition creates a new tfjob condition.
func newCondition(conditionType tfv1alpha2.TFJobConditionType, reason, message string) tfv1alpha2.TFJobCondition {
return tfv1alpha2.TFJobCondition{
Type: conditionType,
Status: v1.ConditionTrue,
LastUpdateTime: metav1.Now(),
LastTransitionTime: metav1.Now(),
Reason: reason,
Message: message,
}
}
// getCondition returns the condition with the provided type.
func getCondition(status tfv1alpha2.TFJobStatus, condType tfv1alpha2.TFJobConditionType) *tfv1alpha2.TFJobCondition {
/*for i := range status.Conditions {
c := status.Conditions[i]
if c.Type == condType {
return &c
}
}*/
if len(status.Conditions) > 0 {
return &status.Conditions[len(status.Conditions)-1]
}
return nil
}
// setCondition updates the tfjob to include the provided condition.
// If the condition that we are about to add already exists
// and has the same status and reason then we are not going to update.
func setCondition(status *tfv1alpha2.TFJobStatus, condition tfv1alpha2.TFJobCondition) {
currentCond := getCondition(*status, condition.Type)
// Do nothing if condition doesn't change
if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason {
return
}
// Do not update lastTransitionTime if the status of the condition doesn't change.
if currentCond != nil && currentCond.Status == condition.Status {
condition.LastTransitionTime = currentCond.LastTransitionTime
}
// Append the updated condition to the
newConditions := filterOutCondition(status.Conditions, condition.Type)
status.Conditions = append(newConditions, condition)
}
// removeCondition removes the tfjob condition with the provided type.
func removementCondition(status *tfv1alpha2.TFJobStatus, condType tfv1alpha2.TFJobConditionType) {
status.Conditions = filterOutCondition(status.Conditions, condType)
}
// filterOutCondition returns a new slice of tfjob conditions without conditions with the provided type.
func filterOutCondition(conditions []tfv1alpha2.TFJobCondition, condType tfv1alpha2.TFJobConditionType) []tfv1alpha2.TFJobCondition {
var newConditions []tfv1alpha2.TFJobCondition
for _, c := range conditions {
if c.Type == condType {
continue
}
newConditions = append(newConditions, c)
}
return newConditions
}