generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 956
/
dataload_controller.go
234 lines (206 loc) · 8.4 KB
/
dataload_controller.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
/*
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 (
"context"
"fmt"
"sync"
"time"
"github.com/fluid-cloudnative/fluid/pkg/common"
cdataload "github.com/fluid-cloudnative/fluid/pkg/dataload"
"github.com/fluid-cloudnative/fluid/pkg/ddc"
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/fluid-cloudnative/fluid/pkg/utils/jindo"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
)
// DataLoadReconciler reconciles a DataLoad object
type DataLoadReconciler struct {
Scheme *runtime.Scheme
engines map[string]base.Engine
mutex *sync.Mutex
*DataLoadReconcilerImplement
}
// NewDataLoadReconciler returns a DataLoadReconciler
func NewDataLoadReconciler(client client.Client,
log logr.Logger,
scheme *runtime.Scheme,
recorder record.EventRecorder) *DataLoadReconciler {
r := &DataLoadReconciler{
Scheme: scheme,
mutex: &sync.Mutex{},
engines: map[string]base.Engine{},
}
r.DataLoadReconcilerImplement = NewDataLoadReconcilerImplement(client, log, recorder)
return r
}
// +kubebuilder:rbac:groups=data.fluid.io,resources=dataloads,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=data.fluid.io,resources=dataloads/status,verbs=get;update;patch
// Reconcile reconciles the DataLoad object
func (r *DataLoadReconciler) Reconcile(context context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx := cruntime.ReconcileRequestContext{
Context: context,
Log: r.Log.WithValues("dataload", req.NamespacedName),
Recorder: r.Recorder,
Client: r.Client,
Category: common.AccelerateCategory,
}
// 1. Get DataLoad object
dataload, err := utils.GetDataLoad(r.Client, req.Name, req.Namespace)
if err != nil {
if utils.IgnoreNotFound(err) == nil {
ctx.Log.Info("DataLoad not found")
return utils.NoRequeue()
} else {
ctx.Log.Error(err, "failed to get DataLoad")
return utils.RequeueIfError(errors.Wrap(err, "failed to get DataLoad info"))
}
}
targetDataload := *dataload
ctx.Log.V(1).Info("DataLoad found", "detail", dataload)
// 2. Reconcile deletion of the object if necessary
if utils.HasDeletionTimestamp(dataload.ObjectMeta) {
return r.ReconcileDataLoadDeletion(ctx, targetDataload, r.engines, r.mutex)
}
// 3. get the dataset
targetDataset, err := utils.GetDataset(r.Client, targetDataload.Spec.Dataset.Name, req.Namespace)
if err != nil {
if utils.IgnoreNotFound(err) == nil {
ctx.Log.Info("The dataset is not found", "dataset", req.Namespace+"/"+targetDataload.Spec.Dataset.Name)
// no datset means no metadata, not necessary to ReconcileDataLoad
return utils.RequeueAfterInterval(20 * time.Second)
} else {
ctx.Log.Error(err, "Failed to get the ddc dataset")
return utils.RequeueIfError(errors.Wrap(err, "Unable to get dataset"))
}
}
ctx.Dataset = targetDataset
ctx.NamespacedName = types.NamespacedName{
Name: targetDataset.Name,
Namespace: targetDataset.Namespace,
}
// 4. get the runtime
index, boundedRuntime := utils.GetRuntimeByCategory(targetDataset.Status.Runtimes, common.AccelerateCategory)
if index == -1 {
ctx.Log.Info("bounded runtime with Accelerate Category is not found on the target dataset", "targetDataset", targetDataset)
r.Recorder.Eventf(&targetDataload,
v1.EventTypeNormal,
common.RuntimeNotReady,
"Bounded accelerate runtime not ready")
return utils.RequeueAfterInterval(20 * time.Second)
}
ctx.RuntimeType = boundedRuntime.Type
var fluidRuntime client.Object
switch ctx.RuntimeType {
case common.AlluxioRuntime:
fluidRuntime, err = utils.GetAlluxioRuntime(ctx.Client, boundedRuntime.Name, boundedRuntime.Namespace)
case common.JindoRuntime:
fluidRuntime, err = utils.GetJindoRuntime(ctx.Client, boundedRuntime.Name, boundedRuntime.Namespace)
ctx.RuntimeType = jindo.GetRuntimeType()
case common.GooseFSRuntime:
fluidRuntime, err = utils.GetGooseFSRuntime(ctx.Client, boundedRuntime.Name, boundedRuntime.Namespace)
case common.JuiceFSRuntime:
fluidRuntime, err = utils.GetJuiceFSRuntime(ctx.Client, boundedRuntime.Name, boundedRuntime.Namespace)
default:
ctx.Log.Error(fmt.Errorf("RuntimeNotSupported"), "The runtime is not supported yet", "runtime", boundedRuntime)
r.Recorder.Eventf(&targetDataload,
v1.EventTypeNormal,
common.RuntimeNotReady,
"Bounded accelerate runtime not supported")
}
if err != nil {
if utils.IgnoreNotFound(err) == nil {
ctx.Log.V(1).Info("The runtime is not found", "runtime", ctx.NamespacedName)
return ctrl.Result{}, nil
} else {
ctx.Log.Error(err, "Failed to get the ddc runtime")
return utils.RequeueIfError(errors.Wrap(err, "Unable to get ddc runtime"))
}
}
ctx.Runtime = fluidRuntime
ctx.Log.V(1).Info("get the runtime", "runtime", ctx.Runtime)
// 5. create or get engine
engine, err := r.GetOrCreateEngine(ctx)
if err != nil {
r.Recorder.Eventf(&targetDataload, v1.EventTypeWarning, common.ErrorProcessDatasetReason, "Process DataLoad error %v", err)
return utils.RequeueIfError(errors.Wrap(err, "Failed to create or get engine"))
}
// 6. add finalizer and requeue
if !utils.ContainsString(targetDataload.ObjectMeta.GetFinalizers(), cdataload.DATALOAD_FINALIZER) {
return r.addFinalizerAndRequeue(ctx, targetDataload)
}
// 7. add owner and requeue
if !utils.ContainsOwners(targetDataload.GetOwnerReferences(), targetDataset) {
return r.AddOwnerAndRequeue(ctx, targetDataload, targetDataset)
}
return r.ReconcileDataLoad(ctx, targetDataload, engine)
}
// AddOwnerAndRequeue adds Owner and requeue
func (r *DataLoadReconciler) AddOwnerAndRequeue(ctx cruntime.ReconcileRequestContext, targetDataLoad datav1alpha1.DataLoad, targetDataset *datav1alpha1.Dataset) (ctrl.Result, error) {
targetDataLoad.ObjectMeta.OwnerReferences = append(targetDataLoad.GetOwnerReferences(), metav1.OwnerReference{
APIVersion: targetDataset.APIVersion,
Kind: targetDataset.Kind,
Name: targetDataset.Name,
UID: targetDataset.UID,
})
if err := r.Update(ctx, &targetDataLoad); err != nil {
ctx.Log.Error(err, "Failed to add ownerreference", "StatusUpdateError", ctx)
return utils.RequeueIfError(err)
}
return utils.RequeueImmediately()
}
func (r *DataLoadReconciler) addFinalizerAndRequeue(ctx cruntime.ReconcileRequestContext, targetDataload datav1alpha1.DataLoad) (ctrl.Result, error) {
targetDataload.ObjectMeta.Finalizers = append(targetDataload.ObjectMeta.Finalizers, cdataload.DATALOAD_FINALIZER)
ctx.Log.Info("Add finalizer and requeue", "finalizer", cdataload.DATALOAD_FINALIZER)
prevGeneration := targetDataload.ObjectMeta.GetGeneration()
if err := r.Update(ctx, &targetDataload); err != nil {
ctx.Log.Error(err, "failed to add finalizer to dataload", "StatusUpdateError", err)
return utils.RequeueIfError(err)
}
return utils.RequeueImmediatelyUnlessGenerationChanged(prevGeneration, targetDataload.ObjectMeta.GetGeneration())
}
// SetupWithManager sets up the controller with the given controller manager
func (r *DataLoadReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&datav1alpha1.DataLoad{}).
Complete(r)
}
// GetOrCreateEngine gets the Engine
func (r *DataLoadReconciler) GetOrCreateEngine(
ctx cruntime.ReconcileRequestContext) (engine base.Engine, err error) {
found := false
id := ddc.GenerateEngineID(ctx.NamespacedName)
r.mutex.Lock()
defer r.mutex.Unlock()
if engine, found = r.engines[id]; !found {
engine, err = ddc.CreateEngine(id,
ctx)
if err != nil {
return nil, err
}
r.engines[id] = engine
r.Log.V(1).Info("Put Engine to engine map")
} else {
r.Log.V(1).Info("Get Engine from engine map")
}
return engine, err
}