forked from kubeflow/training-operator
/
job.go
221 lines (195 loc) · 6.26 KB
/
job.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
package tensorflow
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1unstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes/scheme"
common "github.com/kubeflow/tf-operator/pkg/apis/common/v1"
tfv1 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1"
tflogger "github.com/kubeflow/tf-operator/pkg/logger"
"github.com/kubeflow/tf-operator/pkg/util/k8sutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"k8s.io/apimachinery/pkg/runtime"
)
const (
failedMarshalTFJobReason = "InvalidTFJobSpec"
)
var (
tfJobsCreatedCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "tf_operator_jobs_created",
Help: "Counts number of TF jobs created",
})
)
// When a pod is added, set the defaults and enqueue the current tfjob.
func (tc *TFController) addTFJob(obj interface{}) {
// Convert from unstructured object.
tfJob, err := tfJobFromUnstructured(obj)
if err != nil {
un, ok := obj.(*metav1unstructured.Unstructured)
logger := &log.Entry{}
if ok {
logger = tflogger.LoggerForUnstructured(un, tfv1.Kind)
}
logger.Errorf("Failed to convert the TFJob: %v", err)
// Log the failure to conditions.
if err == errFailedMarshal {
errMsg := fmt.Sprintf("Failed to marshal the object to TFJob; the spec is invalid: %v", err)
logger.Warn(errMsg)
// TODO(jlewi): v1 doesn't appear to define an error type.
tc.Recorder.Event(un, v1.EventTypeWarning, failedMarshalTFJobReason, errMsg)
status := common.JobStatus{
Conditions: []common.JobCondition{
common.JobCondition{
Type: common.JobFailed,
Status: v1.ConditionTrue,
LastUpdateTime: metav1.Now(),
LastTransitionTime: metav1.Now(),
Reason: failedMarshalTFJobReason,
Message: errMsg,
},
},
}
statusMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&status)
if err != nil {
logger.Errorf("Could not covert the TFJobStatus to unstructured; %v", err)
return
}
client, err := k8sutil.NewCRDRestClient(&tfv1.SchemeGroupVersion)
if err == nil {
if err1 := metav1unstructured.SetNestedField(un.Object, statusMap, "status"); err1 != nil {
logger.Errorf("Could not set nested field: %v", err1)
}
logger.Infof("Updating the job to: %+v", un.Object)
err = client.UpdateStatus(un, tfv1.Plural)
if err != nil {
logger.Errorf("Could not update the TFJob: %v", err)
}
} else {
logger.Errorf("Could not create a REST client to update the TFJob")
}
}
return
}
// Set default for the new tfjob.
scheme.Scheme.Default(tfJob)
msg := fmt.Sprintf("TFJob %s is created.", tfJob.Name)
logger := tflogger.LoggerForJob(tfJob)
logger.Info(msg)
// Add a created condition.
err = updateTFJobConditions(tfJob, common.JobCreated, tfJobCreatedReason, msg)
if err != nil {
logger.Errorf("Append tfJob condition error: %v", err)
return
}
// Convert from tfjob object
err = unstructuredFromTFJob(obj, tfJob)
if err != nil {
logger.Errorf("Failed to convert the obj: %v", err)
return
}
tc.enqueueTFJob(obj)
tfJobsCreatedCount.Inc()
}
// When a pod is updated, enqueue the current tfjob.
func (tc *TFController) updateTFJob(old, cur interface{}) {
oldTFJob, err := tfJobFromUnstructured(old)
if err != nil {
return
}
curTFJob, err := tfJobFromUnstructured(cur)
if err != nil {
return
}
// never return error
key, err := KeyFunc(curTFJob)
if err != nil {
return
}
log.Infof("Updating tfjob: %s", oldTFJob.Name)
tc.enqueueTFJob(cur)
// check if need to add a new rsync for ActiveDeadlineSeconds
if curTFJob.Status.StartTime != nil {
curTFJobADS := curTFJob.Spec.ActiveDeadlineSeconds
if curTFJobADS == nil {
return
}
oldTFJobADS := oldTFJob.Spec.ActiveDeadlineSeconds
if oldTFJobADS == nil || *oldTFJobADS != *curTFJobADS {
now := metav1.Now()
start := curTFJob.Status.StartTime.Time
passed := now.Time.Sub(start)
total := time.Duration(*curTFJobADS) * time.Second
// AddAfter will handle total < passed
tc.WorkQueue.AddAfter(key, total-passed)
log.Infof("job ActiveDeadlineSeconds updated, will rsync after %d seconds", total-passed)
}
}
}
func (tc *TFController) deletePodsAndServices(tfJob *tfv1.TFJob, pods []*v1.Pod) error {
if len(pods) == 0 {
return nil
}
// Delete nothing when the cleanPodPolicy is None.
if *tfJob.Spec.CleanPodPolicy == common.CleanPodPolicyNone {
return nil
}
for _, pod := range pods {
if *tfJob.Spec.CleanPodPolicy == common.CleanPodPolicyRunning && pod.Status.Phase != v1.PodRunning {
continue
}
if err := tc.PodControl.DeletePod(pod.Namespace, pod.Name, tfJob); err != nil {
return err
}
// Pod and service have the same name, thus the service could be deleted using pod's name.
if err := tc.ServiceControl.DeleteService(pod.Namespace, pod.Name, tfJob); err != nil {
return err
}
}
return nil
}
func (tc *TFController) cleanupTFJob(tfJob *tfv1.TFJob) error {
currentTime := time.Now()
ttl := tfJob.Spec.TTLSecondsAfterFinished
if ttl == nil {
// do nothing if the cleanup delay is not set
return nil
}
duration := time.Second * time.Duration(*ttl)
if currentTime.After(tfJob.Status.CompletionTime.Add(duration)) {
err := tc.deleteTFJobHandler(tfJob)
if err != nil {
tflogger.LoggerForJob(tfJob).Warnf("Cleanup TFJob error: %v.", err)
return err
}
return nil
}
key, err := KeyFunc(tfJob)
if err != nil {
tflogger.LoggerForJob(tfJob).Warnf("Couldn't get key for tfjob object: %v", err)
return err
}
tc.WorkQueue.AddRateLimited(key)
return nil
}
// deleteTFJob deletes the given TFJob.
func (tc *TFController) deleteTFJob(tfJob *tfv1.TFJob) error {
return tc.tfJobClientSet.KubeflowV1().TFJobs(tfJob.Namespace).Delete(tfJob.Name, &metav1.DeleteOptions{})
}
func getTotalReplicas(tfjob *tfv1.TFJob) int32 {
tfjobReplicas := int32(0)
for _, r := range tfjob.Spec.TFReplicaSpecs {
tfjobReplicas += *r.Replicas
}
return tfjobReplicas
}
func getTotalFailedReplicas(tfjob *tfv1.TFJob) int32 {
totalFailedReplicas := int32(0)
for rtype := range tfjob.Status.ReplicaStatuses {
totalFailedReplicas += tfjob.Status.ReplicaStatuses[rtype].Failed
}
return totalFailedReplicas
}