forked from fluid-cloudnative/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fluid-cloudnative#41 from TrafalgarZZZ/dataload-co…
…ntroller Implement DataLoad controller
- Loading branch information
Showing
14 changed files
with
880 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
pkg/controllers/v1alpha1/dataload/dataload_controller.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
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" | ||
datav1alpha1 "github.com/cloudnativefluid/fluid/api/v1alpha1" | ||
"github.com/cloudnativefluid/fluid/pkg/common" | ||
cdataload "github.com/cloudnativefluid/fluid/pkg/dataload" | ||
"github.com/cloudnativefluid/fluid/pkg/utils" | ||
"github.com/go-logr/logr" | ||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// DataLoadReconciler reconciles a AlluxioDataLoad object | ||
type DataLoadReconciler struct { | ||
Scheme *runtime.Scheme | ||
*ReconcilerImplement | ||
} | ||
|
||
// Return a new DataLoad Reconciler | ||
func NewDataLoadReconciler(client client.Client, | ||
log logr.Logger, | ||
scheme *runtime.Scheme, | ||
recorder record.EventRecorder) *DataLoadReconciler { | ||
r := &DataLoadReconciler{ | ||
Scheme: scheme, | ||
} | ||
r.ReconcilerImplement = NewReconcilerImplement(client, log, recorder) | ||
r.Setup() | ||
return r | ||
} | ||
|
||
// Reconcile reconciles the AlluxioDataLoad Object | ||
// +kubebuilder:rbac:groups=data.fluid.io,resources=alluxiodataloads,verbs=get;list;watch;create;update;patch;delete | ||
// +kubebuilder:rbac:groups=data.fluid.io,resources=alluxiodataloads/status,verbs=get;update;patch | ||
|
||
func (r *DataLoadReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { | ||
ctx := cdataload.ReconcileRequestContext{ | ||
Context: context.Background(), | ||
Log: r.Log.WithValues("alluxiodataload", req.NamespacedName), | ||
Recorder: r.Recorder, | ||
NamespacedName: req.NamespacedName, | ||
} | ||
|
||
ctx.Log.V(1).Info("Reconciling dataload request", "request", req) | ||
|
||
/* | ||
1. Load the DataLoad resource object | ||
*/ | ||
dataload, err := utils.GetDataLoad(r, req.Name, req.Namespace) | ||
if err != nil { | ||
if utils.IgnoreNotFound(err) == nil { | ||
ctx.Log.Info("Dataload not found", "dataload", ctx.NamespacedName) | ||
return ctrl.Result{}, nil | ||
} else { | ||
ctx.Log.Error(err, "Failed to get dataload info") | ||
return utils.RequeueIfError(errors.Wrap(err, "Failed to get dataload info")) | ||
} | ||
} | ||
ctx.DataLoad = *dataload | ||
ctx.Log.Info("Dataload found.", "dataload", ctx.DataLoad) | ||
|
||
/* | ||
2. delete dataload if necessary | ||
*/ | ||
if utils.HasDeletionTimestamp(ctx.DataLoad.ObjectMeta) { | ||
return r.ReconcileDataloadDeletion(ctx) | ||
} | ||
|
||
/* | ||
3. Add finalizer | ||
*/ | ||
if !utils.ContainsString(ctx.DataLoad.ObjectMeta.GetFinalizers(), common.DATALOAD_FINALIZER) { | ||
return r.addFinalizerAndRequeue(ctx) | ||
} | ||
|
||
/* | ||
4. Do dataload reconciling | ||
*/ | ||
return r.ReconcileDataload(ctx) | ||
} | ||
|
||
func (r *DataLoadReconciler) addFinalizerAndRequeue(ctx cdataload.ReconcileRequestContext) (ctrl.Result, error) { | ||
ctx.DataLoad.ObjectMeta.Finalizers = append(ctx.DataLoad.ObjectMeta.Finalizers, common.DATALOAD_FINALIZER) | ||
ctx.Log.Info("Add finalizer and Requeue", "finalizer", common.DATALOAD_FINALIZER) | ||
prevGeneration := ctx.DataLoad.ObjectMeta.GetGeneration() | ||
if err := r.Update(ctx, &ctx.DataLoad); err != nil { | ||
ctx.Log.Error(err, "Failed to add finalizer to dataload", "StatusUpdateError", err) | ||
return utils.RequeueIfError(err) | ||
} | ||
return utils.RequeueImmediatelyUnlessGenerationChanged(prevGeneration, ctx.DataLoad.ObjectMeta.GetGeneration()) | ||
} | ||
|
||
//SetupWithManager setups the manager with AlluxioDataLoad | ||
func (r *DataLoadReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&datav1alpha1.AlluxioDataLoad{}). | ||
Complete(r) | ||
} |
Oops, something went wrong.