generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 960
/
Copy pathstatus_handler.go
193 lines (167 loc) · 7.25 KB
/
status_handler.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
/*
Copyright 2023 The Fluid 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 dataload
import (
"github.com/fluid-cloudnative/fluid/pkg/dataflow"
"github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient"
"github.com/pkg/errors"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/dataoperation"
cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/fluid-cloudnative/fluid/pkg/utils/helm"
)
type OnceStatusHandler struct {
client.Client
dataLoad *datav1alpha1.DataLoad
}
var _ dataoperation.StatusHandler = &OnceStatusHandler{}
type CronStatusHandler struct {
client.Client
dataLoad *datav1alpha1.DataLoad
}
var _ dataoperation.StatusHandler = &CronStatusHandler{}
type OnEventStatusHandler struct {
client.Client
dataLoad *datav1alpha1.DataLoad
}
func (r *OnceStatusHandler) GetOperationStatus(ctx cruntime.ReconcileRequestContext, opStatus *datav1alpha1.OperationStatus) (result *datav1alpha1.OperationStatus, err error) {
result = opStatus.DeepCopy()
// 2. Check running status of the DataLoad job
releaseName := utils.GetDataLoadReleaseName(r.dataLoad.GetName())
jobName := utils.GetDataLoadJobName(releaseName)
ctx.Log.V(1).Info("DataLoad chart already existed, check its running status")
job, err := kubeclient.GetJob(r.Client, jobName, ctx.Namespace)
if err != nil {
// helm release found but job missing, delete the helm release and requeue
if utils.IgnoreNotFound(err) == nil {
ctx.Log.Info("Related Job missing, will delete helm chart and retry", "namespace", ctx.Namespace, "jobName", jobName)
if err = helm.DeleteReleaseIfExists(releaseName, ctx.Namespace); err != nil {
ctx.Log.Error(err, "can't delete dataload release", "namespace", ctx.Namespace, "releaseName", releaseName)
return
}
}
// other error
ctx.Log.Error(err, "can't get dataload job", "namespace", ctx.Namespace, "jobName", jobName)
return
}
finishedJobCondition := kubeclient.GetFinishedJobCondition(job)
if finishedJobCondition == nil {
ctx.Log.V(1).Info("DataLoad job still running", "namespace", ctx.Namespace, "jobName", jobName)
return
}
isJobSucceed := finishedJobCondition.Type == batchv1.JobComplete
// set the node labels in status when job succeed
if result.NodeAffinity == nil && isJobSucceed {
// generate the node labels
result.NodeAffinity, err = dataflow.GenerateNodeAffinity(job)
if err != nil {
return nil, errors.Wrap(err, "error to generate the node labels")
}
}
// job either failed or complete, update DataLoad's phase status
result.Conditions = []datav1alpha1.Condition{
{
Type: common.ConditionType(finishedJobCondition.Type),
Status: finishedJobCondition.Status,
Reason: finishedJobCondition.Reason,
Message: finishedJobCondition.Message,
LastProbeTime: finishedJobCondition.LastProbeTime,
LastTransitionTime: finishedJobCondition.LastTransitionTime,
},
}
if isJobSucceed {
result.Phase = common.PhaseComplete
} else {
result.Phase = common.PhaseFailed
}
result.Duration = utils.CalculateDuration(job.CreationTimestamp.Time, finishedJobCondition.LastTransitionTime.Time)
return
}
func (c *CronStatusHandler) GetOperationStatus(ctx cruntime.ReconcileRequestContext, opStatus *datav1alpha1.OperationStatus) (result *datav1alpha1.OperationStatus, err error) {
result = opStatus.DeepCopy()
// 1. Check running status of the DataLoad job
releaseName := utils.GetDataLoadReleaseName(c.dataLoad.GetName())
cronjobName := utils.GetDataLoadJobName(releaseName)
cronjobStatus, err := kubeclient.GetCronJobStatus(c.Client, types.NamespacedName{Namespace: c.dataLoad.GetNamespace(), Name: cronjobName})
if err != nil {
// helm release found but cronjob missing, delete the helm release and requeue
if utils.IgnoreNotFound(err) == nil {
ctx.Log.Info("Related Cronjob missing, will delete helm chart and retry", "namespace", ctx.Namespace, "cronjobName", cronjobName)
if err = helm.DeleteReleaseIfExists(releaseName, ctx.Namespace); err != nil {
ctx.Log.Error(err, "can't delete DataLoad release", "namespace", ctx.Namespace, "releaseName", releaseName)
return
}
return
}
// other error
ctx.Log.Error(err, "can't get DataLoad cronjob", "namespace", ctx.Namespace, "cronjobName", cronjobName)
return
}
// update LastScheduleTime and LastSuccessfulTime
result.LastScheduleTime = cronjobStatus.LastScheduleTime
result.LastSuccessfulTime = cronjobStatus.LastSuccessfulTime
jobs, err := utils.ListDataOperationJobByCronjob(c.Client, types.NamespacedName{Namespace: c.dataLoad.GetNamespace(), Name: cronjobName})
if err != nil {
ctx.Log.Error(err, "can't list DataLoad job by cronjob", "namespace", ctx.Namespace, "cronjobName", cronjobName)
return
}
var currentJob *batchv1.Job
for _, job := range jobs {
if job.CreationTimestamp == *cronjobStatus.LastScheduleTime || job.CreationTimestamp.After(cronjobStatus.LastScheduleTime.Time) {
currentJob = &job
break
}
}
if currentJob == nil {
ctx.Log.Info("can't get newest job by cronjob, skip", "namespace", ctx.Namespace, "cronjobName", cronjobName)
return
}
finishedJobCondition := kubeclient.GetFinishedJobCondition(currentJob)
if finishedJobCondition == nil {
ctx.Log.V(1).Info("DataLoad job still running", "namespace", ctx.Namespace, "cronjobName", cronjobName)
if opStatus.Phase == common.PhaseComplete || opStatus.Phase == common.PhaseFailed {
// if dataload was complete or failed, but now job is running, set dataload pending first
// dataset will be locked only when dataload pending
result.Phase = common.PhasePending
result.Duration = "-"
}
return
}
// job either failed or complete, update dataload's phase status
result.Conditions = []datav1alpha1.Condition{
{
Type: common.ConditionType(finishedJobCondition.Type),
Status: finishedJobCondition.Status,
Reason: finishedJobCondition.Reason,
Message: finishedJobCondition.Message,
LastProbeTime: finishedJobCondition.LastProbeTime,
LastTransitionTime: finishedJobCondition.LastTransitionTime,
},
}
if finishedJobCondition.Type == batchv1.JobFailed {
result.Phase = common.PhaseFailed
} else {
result.Phase = common.PhaseComplete
}
result.Duration = utils.CalculateDuration(currentJob.CreationTimestamp.Time, finishedJobCondition.LastTransitionTime.Time)
return
}
func (o *OnEventStatusHandler) GetOperationStatus(ctx cruntime.ReconcileRequestContext, opStatus *datav1alpha1.OperationStatus) (result *datav1alpha1.OperationStatus, err error) {
//TODO implement me
return nil, nil
}