From a58bc6235daa5dfbbb55c87821393a7830f5dbd8 Mon Sep 17 00:00:00 2001 From: jiuyu Date: Mon, 13 Jan 2025 14:41:16 +0800 Subject: [PATCH] Enhancement: add hashCache for namespacedName related labels Signed-off-by: jiuyu --- pkg/common/label.go | 2 + pkg/controllers/runtime_controller.go | 9 ++- pkg/csi/plugins/nodeserver.go | 8 ++- pkg/ctrl/fuse.go | 4 +- pkg/ddc/alluxio/deprecated_label.go | 2 +- pkg/ddc/alluxio/deprecated_label_test.go | 2 +- pkg/ddc/alluxio/engine.go | 1 + pkg/ddc/alluxio/master_internal_test.go | 18 ++++- pkg/ddc/alluxio/runtime_info.go | 36 +++++----- pkg/ddc/alluxio/shutdown.go | 2 +- pkg/ddc/alluxio/shutdown_test.go | 2 + pkg/ddc/alluxio/transform.go | 2 +- pkg/ddc/alluxio/transform_fuse.go | 2 +- pkg/ddc/alluxio/transform_fuse_test.go | 42 ++++++++--- pkg/ddc/alluxio/transform_test.go | 22 +++++- pkg/ddc/base/label.go | 24 +++++-- pkg/ddc/base/label_test.go | 2 +- pkg/ddc/base/runtime.go | 27 ++++++- pkg/ddc/efc/master_internal_test.go | 37 ++++++---- pkg/ddc/efc/metadata_test.go | 17 +++-- pkg/ddc/efc/runtime_info.go | 4 ++ pkg/ddc/efc/shutdown.go | 2 +- pkg/ddc/efc/transform.go | 4 +- pkg/ddc/efc/transform_test.go | 18 +++-- pkg/ddc/goosefs/deprecated_label.go | 2 +- pkg/ddc/goosefs/deprecated_label_test.go | 2 +- pkg/ddc/goosefs/engine.go | 1 + pkg/ddc/goosefs/master_internal_test.go | 17 ++++- pkg/ddc/goosefs/runtime_info.go | 35 +++++---- pkg/ddc/goosefs/shutdown.go | 2 +- pkg/ddc/goosefs/transform.go | 2 +- pkg/ddc/goosefs/transform_fuse.go | 2 +- pkg/ddc/goosefs/transform_fuse_test.go | 25 +++++-- pkg/ddc/goosefs/transform_test.go | 13 +++- pkg/ddc/jindo/deprecated_label.go | 2 +- pkg/ddc/jindo/deprecated_label_test.go | 2 +- pkg/ddc/jindo/master_internal_test.go | 16 ++++- pkg/ddc/jindo/runtime_info.go | 36 +++++----- pkg/ddc/jindo/shutdown.go | 2 +- pkg/ddc/jindo/transform.go | 4 +- pkg/ddc/jindo/worker.go | 2 +- pkg/ddc/jindo/worker_test.go | 11 ++- pkg/ddc/jindocache/deprecated_label.go | 2 +- pkg/ddc/jindocache/deprecated_label_test.go | 2 +- pkg/ddc/jindocache/master_internal_test.go | 16 ++++- pkg/ddc/jindocache/runtime_info.go | 35 +++++---- pkg/ddc/jindocache/shutdown.go | 2 +- pkg/ddc/jindocache/transform.go | 5 +- pkg/ddc/jindocache/transform_test.go | 54 +++++++++----- pkg/ddc/jindofsx/deprecated_label.go | 2 +- pkg/ddc/jindofsx/deprecated_label_test.go | 2 +- pkg/ddc/jindofsx/master_internal_test.go | 18 ++++- pkg/ddc/jindofsx/runtime_info.go | 36 +++++----- pkg/ddc/jindofsx/shutdown.go | 2 +- pkg/ddc/jindofsx/transform.go | 4 +- pkg/ddc/jindofsx/transform_test.go | 18 +++-- pkg/ddc/juicefs/data_migrate.go | 2 +- pkg/ddc/juicefs/data_migrate_test.go | 14 ++-- pkg/ddc/juicefs/deprecated_label.go | 2 +- pkg/ddc/juicefs/deprecated_label_test.go | 2 +- pkg/ddc/juicefs/master_internal_test.go | 17 ++++- pkg/ddc/juicefs/runtime_info.go | 36 +++++----- pkg/ddc/juicefs/shutdown.go | 2 +- pkg/ddc/juicefs/transform.go | 2 +- pkg/ddc/juicefs/transform_fuse.go | 2 +- pkg/ddc/juicefs/transform_fuse_test.go | 6 ++ pkg/ddc/juicefs/transform_test.go | 6 ++ pkg/ddc/thin/referencedataset/runtime.go | 1 + pkg/ddc/thin/referencedataset/volume.go | 2 +- pkg/ddc/thin/runtime_info.go | 4 ++ pkg/ddc/thin/shutdown.go | 2 +- pkg/ddc/thin/transform.go | 2 +- pkg/ddc/thin/transform_fuse.go | 2 +- pkg/ddc/thin/transform_fuse_test.go | 28 +++++++- pkg/ddc/vineyard/master_internal_test.go | 18 ++++- pkg/ddc/vineyard/runtime_info.go | 18 ++++- pkg/ddc/vineyard/shut_down.go | 2 +- pkg/ddc/vineyard/transform.go | 4 +- pkg/ddc/vineyard/transform_test.go | 17 ++++- pkg/utils/dataset/lifecycle/node.go | 4 +- pkg/utils/dataset/volume/get.go | 4 +- pkg/utils/excluisve.go | 40 ----------- pkg/utils/excluisve_test.go | 78 -------------------- pkg/utils/hash.go | 80 --------------------- pkg/utils/hash_test.go | 66 ----------------- pkg/utils/kubeclient/volume.go | 28 +++----- pkg/utils/kubeclient/volume_claim.go | 2 +- pkg/utils/kubeclient/volume_test.go | 74 ------------------- pkg/utils/label.go | 59 +++++++++++---- pkg/utils/label_test.go | 14 ++-- 90 files changed, 672 insertions(+), 631 deletions(-) delete mode 100644 pkg/utils/excluisve.go delete mode 100644 pkg/utils/excluisve_test.go delete mode 100644 pkg/utils/hash.go delete mode 100644 pkg/utils/hash_test.go diff --git a/pkg/common/label.go b/pkg/common/label.go index 7d2b86f2c1b..d7929c2c991 100644 --- a/pkg/common/label.go +++ b/pkg/common/label.go @@ -56,6 +56,8 @@ const ( // i.e. fluid.io/dataset.referring-namespace LabelAnnotationDatasetReferringNameSpace = LabelAnnotationDataset + ".referring-namespace" + LabelAnnotationOwnerDatasetUID = LabelAnnotationPrefix + "owner-dataset-uid" + // LabelNodePublishMethod is a pv label that indicates the method nodePuhlishVolume use // i.e. fluid.io/node-publish-method LabelNodePublishMethod = LabelAnnotationPrefix + "node-publish-method" diff --git a/pkg/controllers/runtime_controller.go b/pkg/controllers/runtime_controller.go index 55624b9807b..8afcd42a375 100644 --- a/pkg/controllers/runtime_controller.go +++ b/pkg/controllers/runtime_controller.go @@ -76,7 +76,6 @@ func NewRuntimeReconciler(reconciler RuntimeReconcilerInterface, client client.C // ReconcileInternal handles the logic of reconcile runtime func (r *RuntimeReconciler) ReconcileInternal(ctx cruntime.ReconcileRequestContext) (ctrl.Result, error) { - // 0. Set context time limit ctxWithTimeout, cancel := context.WithTimeout(ctx.Context, reconcileTimeout) defer cancel() @@ -139,6 +138,7 @@ func (r *RuntimeReconciler) ReconcileInternal(ctx cruntime.ReconcileRequestConte if !utils.ContainsOwners(objectMeta.GetOwnerReferences(), dataset) { return r.AddOwnerAndRequeue(ctx, dataset) } + if !dataset.CanbeBound(ctx.Name, ctx.Namespace, ctx.Category) { ctx.Log.Info("the dataset can't be bound to the runtime, because it's already bound to another runtime ", "dataset", dataset.Name) @@ -332,6 +332,13 @@ func (r *RuntimeReconciler) AddOwnerAndRequeue(ctx cruntime.ReconcileRequestCont Name: dataset.Name, UID: dataset.UID, })) + + labels := objectMeta.GetLabels() + if labels == nil { + labels = make(map[string]string) + } + labels[common.LabelAnnotationOwnerDatasetUID] = string(dataset.UID) + objectMeta.SetLabels(labels) if err := r.Update(ctx, ctx.Runtime); err != nil { ctx.Log.Error(err, "Failed to add ownerreference", "StatusUpdateError", ctx) return utils.RequeueIfError(err) diff --git a/pkg/csi/plugins/nodeserver.go b/pkg/csi/plugins/nodeserver.go index 3c512ed8862..189fef5d3fc 100644 --- a/pkg/csi/plugins/nodeserver.go +++ b/pkg/csi/plugins/nodeserver.go @@ -344,7 +344,7 @@ func (ns *nodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstag // 4. remove label on node // Once the label is removed, fuse pod on corresponding node will be terminated // since node selector in the fuse daemonSet no longer matches. - fuseLabelKey := utils.GetFuseLabelName(namespace, name) + fuseLabelKey := utils.GetFuseLabelName(namespace, name, runtimeInfo.GetNamespacedNameAlias()) var labelsToModify common.LabelsToModify labelsToModify.Delete(fuseLabelKey) @@ -400,7 +400,11 @@ func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol } // 4. Label node to launch FUSE Pod - fuseLabelKey := utils.GetFuseLabelName(namespace, name) + runtimeInfo, err := base.GetRuntimeInfo(ns.client, name, namespace) + if err != nil { + return nil, errors.Wrapf(err, "NodeStageVolume: failed to get runtime info for %s/%s", namespace, name) + } + fuseLabelKey := utils.GetFuseLabelName(namespace, name, runtimeInfo.GetNamespacedNameAlias()) var labelsToModify common.LabelsToModify labelsToModify.Add(fuseLabelKey, "true") diff --git a/pkg/ctrl/fuse.go b/pkg/ctrl/fuse.go index 627d156d675..bfc43ae7fe1 100644 --- a/pkg/ctrl/fuse.go +++ b/pkg/ctrl/fuse.go @@ -109,7 +109,7 @@ func (e *Helper) CheckFuseHealthy(recorder record.EventRecorder, runtime base.Ru func (e *Helper) CleanUpFuse() (count int, err error) { var ( nodeList = &corev1.NodeList{} - fuseLabelKey = utils.GetFuseLabelName(e.runtimeInfo.GetNamespace(), e.runtimeInfo.GetName()) + fuseLabelKey = utils.GetFuseLabelName(e.runtimeInfo.GetNamespace(), e.runtimeInfo.GetName(), e.runtimeInfo.GetNamespacedNameAlias()) ) labelNames := []string{fuseLabelKey} @@ -153,7 +153,7 @@ func (e *Helper) CleanUpFuse() (count int, err error) { func (e *Helper) GetFuseNodes() (nodes []corev1.Node, err error) { var ( nodeList = &corev1.NodeList{} - fuseLabelKey = utils.GetFuseLabelName(e.runtimeInfo.GetNamespace(), e.runtimeInfo.GetName()) + fuseLabelKey = utils.GetFuseLabelName(e.runtimeInfo.GetNamespace(), e.runtimeInfo.GetName(), e.runtimeInfo.GetNamespacedNameAlias()) ) labelNames := []string{fuseLabelKey} diff --git a/pkg/ddc/alluxio/deprecated_label.go b/pkg/ddc/alluxio/deprecated_label.go index 97bfc1f6d91..29e4db5692e 100644 --- a/pkg/ddc/alluxio/deprecated_label.go +++ b/pkg/ddc/alluxio/deprecated_label.go @@ -50,7 +50,7 @@ func (e *AlluxioEngine) HasDeprecatedCommonLabelname() (deprecated bool, err err nodeSelectors := workers.Spec.Template.Spec.NodeSelector e.Log.Info("The current node selectors for worker", "workerName", workerName, "nodeSelector", nodeSelectors) - deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name) + deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) if _, deprecated = nodeSelectors[deprecatedCommonLabelName]; deprecated { // e.Log.Info("the deprecated node selector exists", "nodeselector", deprecatedCommonLabelName) diff --git a/pkg/ddc/alluxio/deprecated_label_test.go b/pkg/ddc/alluxio/deprecated_label_test.go index 3d2595a4c76..9ea840056d0 100644 --- a/pkg/ddc/alluxio/deprecated_label_test.go +++ b/pkg/ddc/alluxio/deprecated_label_test.go @@ -67,7 +67,7 @@ func TestAlluxioEngine_GetDeprecatedCommonLabelname(t *testing.T) { }, } for _, test := range testCases { - out := utils.GetCommonLabelName(true, test.namespace, test.name) + out := utils.GetCommonLabelName(true, test.namespace, test.name, "") if out != test.out { t.Errorf("input parameter is %s-%s,expected %s, got %s", test.namespace, test.name, test.out, out) } diff --git a/pkg/ddc/alluxio/engine.go b/pkg/ddc/alluxio/engine.go index 4a27f5efe62..031bf513047 100644 --- a/pkg/ddc/alluxio/engine.go +++ b/pkg/ddc/alluxio/engine.go @@ -84,6 +84,7 @@ func Build(id string, ctx cruntime.ReconcileRequestContext) (base.Engine, error) if err != nil { return nil, fmt.Errorf("engine %s failed to get runtime info, error %s", ctx.Name, err.Error()) } + engine.runtimeInfo = runtimeInfo // Build the helper engine.Helper = ctrl.BuildHelper(runtimeInfo, ctx.Client, engine.Log) diff --git a/pkg/ddc/alluxio/master_internal_test.go b/pkg/ddc/alluxio/master_internal_test.go index f4a14ca4531..d1d4a9f1de5 100644 --- a/pkg/ddc/alluxio/master_internal_test.go +++ b/pkg/ddc/alluxio/master_internal_test.go @@ -18,6 +18,7 @@ package alluxio import ( "fmt" + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "testing" "github.com/brahma-adshonor/gohook" @@ -85,6 +86,11 @@ func TestSetupMasterInternal(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "alluxio") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := AlluxioEngine{ name: "hbase", namespace: "fluid", @@ -100,8 +106,10 @@ func TestSetupMasterInternal(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } @@ -195,6 +203,11 @@ func TestGenerateAlluxioValueFile(t *testing.T) { client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "alluxio") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := AlluxioEngine{ name: "hbase", namespace: "fluid", @@ -210,9 +223,10 @@ func TestGenerateAlluxioValueFile(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/alluxio/runtime_info.go b/pkg/ddc/alluxio/runtime_info.go index 32dbd55e7ba..67e203ee7b5 100644 --- a/pkg/ddc/alluxio/runtime_info.go +++ b/pkg/ddc/alluxio/runtime_info.go @@ -43,6 +43,26 @@ func (e *AlluxioEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { e.runtimeInfo.SetFuseNodeSelector(runtime.Spec.Fuse.NodeSelector) if !e.UnitTest { + // Setup with Dataset Info + dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) + if err != nil { + if len(runtime.GetOwnerReferences()) > 0 { + e.runtimeInfo.SetNamespacedNameAlias(runtime.GetOwnerReferences()[0].UID) + } + if utils.IgnoreNotFound(err) == nil { + e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) + return e.runtimeInfo, nil + } + + e.Log.Info("Failed to get dataset when getRuntimeInfo") + return e.runtimeInfo, err + } + + e.runtimeInfo.SetupWithDataset(dataset) + e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) + + e.runtimeInfo.SetNamespacedNameAlias(dataset.GetUID()) + // Check if the runtime is using deprecated labels isLabelDeprecated, err := e.HasDeprecatedCommonLabelname() if err != nil { @@ -58,22 +78,6 @@ func (e *AlluxioEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { e.runtimeInfo.SetDeprecatedPVName(isPVNameDeprecated) e.Log.Info("Deprecation check finished", "isLabelDeprecated", e.runtimeInfo.IsDeprecatedNodeLabel(), "isPVNameDeprecated", e.runtimeInfo.IsDeprecatedPVName()) - - // Setup with Dataset Info - dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) - if err != nil { - if utils.IgnoreNotFound(err) == nil { - e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) - return e.runtimeInfo, nil - } - - e.Log.Info("Failed to get dataset when getRuntimeInfo") - return e.runtimeInfo, err - } - - e.runtimeInfo.SetupWithDataset(dataset) - - e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) } } diff --git a/pkg/ddc/alluxio/shutdown.go b/pkg/ddc/alluxio/shutdown.go index 43e04950530..faabecf85dc 100644 --- a/pkg/ddc/alluxio/shutdown.go +++ b/pkg/ddc/alluxio/shutdown.go @@ -292,7 +292,7 @@ func (e *AlluxioEngine) destroyWorkers(expectedWorkers int32) (currentWorkers in labelsToModify.Delete(label) } - exclusiveLabelValue := utils.GetExclusiveValue(e.namespace, e.name) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() if val, exist := toUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue { labelsToModify.Delete(labelExclusiveName) } diff --git a/pkg/ddc/alluxio/shutdown_test.go b/pkg/ddc/alluxio/shutdown_test.go index 2ca33d617d7..1f33a686cf6 100644 --- a/pkg/ddc/alluxio/shutdown_test.go +++ b/pkg/ddc/alluxio/shutdown_test.go @@ -225,6 +225,8 @@ func TestDestroyWorker(t *testing.T) { } if len(newNode.Labels) != len(test.wantedNodeLabels[node.Name]) { + t.Log(newNode.Labels) + t.Log(test.wantedNodeLabels[node.Name]) t.Errorf("fail to decrease the labels") } if len(newNode.Labels) != 0 && !reflect.DeepEqual(newNode.Labels, test.wantedNodeLabels[node.Name]) { diff --git a/pkg/ddc/alluxio/transform.go b/pkg/ddc/alluxio/transform.go index 36b782f821b..68dff9c6759 100644 --- a/pkg/ddc/alluxio/transform.go +++ b/pkg/ddc/alluxio/transform.go @@ -50,7 +50,7 @@ func (e *AlluxioEngine) transform(runtime *datav1alpha1.AlluxioRuntime) (value * } value.FullnameOverride = e.name - value.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", e.namespace, e.name) + value.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) // 1.transform the common part err = e.transformCommonPart(runtime, dataset, value) diff --git a/pkg/ddc/alluxio/transform_fuse.go b/pkg/ddc/alluxio/transform_fuse.go index 71b441aa2e4..f84d143f870 100644 --- a/pkg/ddc/alluxio/transform_fuse.go +++ b/pkg/ddc/alluxio/transform_fuse.go @@ -100,7 +100,7 @@ func (e *AlluxioEngine) transformFuse(runtime *datav1alpha1.AlluxioRuntime, data } else { value.Fuse.NodeSelector = map[string]string{} } - value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name)] = "true" + value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name, e.runtimeInfo.GetNamespacedNameAlias())] = "true" // parse fuse container network mode value.Fuse.HostNetwork = datav1alpha1.IsHostNetwork(runtime.Spec.Fuse.NetworkMode) diff --git a/pkg/ddc/alluxio/transform_fuse_test.go b/pkg/ddc/alluxio/transform_fuse_test.go index 0fbf61516e5..8a0fc795a90 100644 --- a/pkg/ddc/alluxio/transform_fuse_test.go +++ b/pkg/ddc/alluxio/transform_fuse_test.go @@ -17,6 +17,7 @@ limitations under the License. package alluxio import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "reflect" "testing" @@ -62,11 +63,18 @@ func TestTransformFuseWithNoArgs(t *testing.T) { }}, &Alluxio{}, []string{"fuse", "--fuse-opts=kernel_cache,rw,allow_other", "/alluxio/default/test/alluxio-fuse", "/"}, false}, } for _, test := range tests { + runtimeInfo, err := base.BuildRuntimeInfo("test", "default", "alluxio") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } engine := &AlluxioEngine{ - name: "test", - namespace: "default", - Log: fake.NullLogger()} - err := engine.transformFuse(test.runtime, test.dataset, test.alluxioValue) + name: "test", + namespace: "default", + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, + Client: fake.NewFakeClientWithScheme(testScheme), + } + err = engine.transformFuse(test.runtime, test.dataset, test.alluxioValue) if err != nil { t.Errorf("Got err %v", err) } @@ -153,11 +161,18 @@ func TestTransformFuseWithArgs(t *testing.T) { }}, &Alluxio{}, []string{"fuse", "--fuse-opts=kernel_cache,allow_other", "/alluxio/default/test/alluxio-fuse", "/"}, false}, } for _, test := range tests { + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } engine := &AlluxioEngine{ - name: "test", - namespace: "default", - Log: fake.NullLogger()} - err := engine.transformFuse(test.runtime, test.dataset, test.alluxioValue) + name: "test", + namespace: "default", + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, + Client: fake.NewFakeClientWithScheme(testScheme), + } + err = engine.transformFuse(test.runtime, test.dataset, test.alluxioValue) if err != nil { t.Errorf("Got err %v", err) } @@ -230,7 +245,16 @@ func TestTransformFuseWithNetwork(t *testing.T) { }, } - engine := &AlluxioEngine{Log: fake.NullLogger()} + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "alluxio") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + + engine := &AlluxioEngine{ + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, + Client: fake.NewFakeClientWithScheme(testScheme), + } ds := &datav1alpha1.Dataset{} for k, v := range testCases { gotValue := &Alluxio{} diff --git a/pkg/ddc/alluxio/transform_test.go b/pkg/ddc/alluxio/transform_test.go index 1639c59793b..cea65454e5b 100755 --- a/pkg/ddc/alluxio/transform_test.go +++ b/pkg/ddc/alluxio/transform_test.go @@ -75,9 +75,16 @@ func TestTransformFuse(t *testing.T) { }, &Alluxio{}, []string{"fuse", "--fuse-opts=kernel_cache,rw,uid=1000,gid=1000,allow_other"}}, } for _, test := range tests { - engine := &AlluxioEngine{} + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "alluxio") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := &AlluxioEngine{ + runtimeInfo: runtimeInfo, + Client: fake.NewFakeClientWithScheme(testScheme), + } engine.Log = ctrl.Log - err := engine.transformFuse(test.runtime, test.dataset, test.value) + err = engine.transformFuse(test.runtime, test.dataset, test.value) if err != nil { t.Errorf("error %v", err) } @@ -1012,7 +1019,16 @@ func TestTransformWorkerProperties(t *testing.T) { } func TestTransformFuseProperties(t *testing.T) { - engine := &AlluxioEngine{Log: fake.NullLogger()} + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + + engine := &AlluxioEngine{ + Log: fake.NullLogger(), + Client: fake.NewFakeClientWithScheme(testScheme), + runtimeInfo: runtimeInfo, + } var x int64 = 1000 ctrl.SetLogger(zap.New(func(o *zap.Options) { o.Development = true diff --git a/pkg/ddc/base/label.go b/pkg/ddc/base/label.go index 66411313dc9..61a11c47ea1 100644 --- a/pkg/ddc/base/label.go +++ b/pkg/ddc/base/label.go @@ -17,28 +17,30 @@ limitations under the Licensinfo. package base import ( + "fmt" "github.com/fluid-cloudnative/fluid/pkg/common" "github.com/fluid-cloudnative/fluid/pkg/utils" + "k8s.io/apimachinery/pkg/util/validation" ) func (info *RuntimeInfo) GetLabelNameForMemory() string { - return utils.GetLabelNameForMemory(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name) + return utils.GetLabelNameForMemory(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name, info.overLimitNamespacedNameAlias) } func (info *RuntimeInfo) GetLabelNameForDisk() string { - return utils.GetLabelNameForDisk(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name) + return utils.GetLabelNameForDisk(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name, info.overLimitNamespacedNameAlias) } func (info *RuntimeInfo) GetLabelNameForTotal() string { - return utils.GetLabelNameForTotal(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name) + return utils.GetLabelNameForTotal(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name, info.overLimitNamespacedNameAlias) } func (info *RuntimeInfo) GetCommonLabelName() string { - return utils.GetCommonLabelName(info.IsDeprecatedNodeLabel(), info.namespace, info.name) + return utils.GetCommonLabelName(info.IsDeprecatedNodeLabel(), info.namespace, info.name, info.overLimitNamespacedNameAlias) } func (info *RuntimeInfo) GetRuntimeLabelName() string { - return utils.GetRuntimeLabelName(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name) + return utils.GetRuntimeLabelName(info.IsDeprecatedNodeLabel(), info.runtimeType, info.namespace, info.name, info.overLimitNamespacedNameAlias) } // GetDatasetNumLabelname get the label to record how much datasets on a node @@ -48,5 +50,15 @@ func (info *RuntimeInfo) GetDatasetNumLabelName() string { // GetFuseLabelName gets the label indicating a fuse running on some node. func (info *RuntimeInfo) GetFuseLabelName() string { - return utils.TransferFullNamespacedNameWithPrefixToLegalValue(common.LabelAnnotationFusePrefix, info.namespace, info.name) + return utils.GetNamespacedNameValueWithPrefix(common.LabelAnnotationFusePrefix, info.namespace, info.name, info.overLimitNamespacedNameAlias) +} + +func (info *RuntimeInfo) GetExclusiveLabelValue() string { + // ensure forward compatibility + exclusiveLabelValue := fmt.Sprintf("%s_%s", info.namespace, info.name) + if len(exclusiveLabelValue) < validation.DNS1035LabelMaxLength { + return exclusiveLabelValue + } + + return utils.GetNamespacedNameValueWithPrefix("", info.namespace, info.name, info.overLimitNamespacedNameAlias) } diff --git a/pkg/ddc/base/label_test.go b/pkg/ddc/base/label_test.go index 52cd561b15b..21c2dc4e070 100644 --- a/pkg/ddc/base/label_test.go +++ b/pkg/ddc/base/label_test.go @@ -49,7 +49,7 @@ func TestGetStorageLabelName(t *testing.T) { } for _, test := range tests { - result := utils.GetStorageLabelName(common.HumanReadType, common.MemoryStorageType, test.info.IsDeprecatedNodeLabel(), test.info.runtimeType, test.info.namespace, test.info.name) + result := utils.GetStorageLabelName(common.HumanReadType, common.MemoryStorageType, test.info.IsDeprecatedNodeLabel(), test.info.runtimeType, test.info.namespace, test.info.name, "") if test.expectedResult != result { t.Errorf("check failure, expected %s, get %s", test.expectedResult, result) } diff --git a/pkg/ddc/base/runtime.go b/pkg/ddc/base/runtime.go index b675e828dc7..396e9e237c3 100644 --- a/pkg/ddc/base/runtime.go +++ b/pkg/ddc/base/runtime.go @@ -19,6 +19,7 @@ package base import ( "encoding/json" "fmt" + "k8s.io/apimachinery/pkg/types" "strings" "github.com/fluid-cloudnative/fluid/pkg/utils" @@ -53,6 +54,8 @@ type Conventions interface { GetDatasetNumLabelName() string GetWorkerStatefulsetName() string + + GetExclusiveLabelValue() string } // Runtime Information interface defines the interfaces that should be implemented @@ -67,6 +70,8 @@ type RuntimeInfoInterface interface { GetNamespace() string + GetNamespacedNameAlias() string + GetRuntimeType() string IsExclusive() bool @@ -77,6 +82,8 @@ type RuntimeInfoInterface interface { SetupWithDataset(dataset *datav1alpha1.Dataset) + SetNamespacedNameAlias(alias types.UID) + GetFuseNodeSelector() (nodeSelector map[string]string) GetFuseCleanPolicy() datav1alpha1.FuseCleanPolicy @@ -104,9 +111,10 @@ var _ RuntimeInfoInterface = &RuntimeInfo{} // The real Runtime Info should implement type RuntimeInfo struct { - name string - namespace string - runtimeType string + name string + namespace string + overLimitNamespacedNameAlias string + runtimeType string //tieredstore datav1alpha1.TieredStore tieredstoreInfo TieredStoreInfo @@ -272,6 +280,10 @@ func (info *RuntimeInfo) GetNamespace() string { return info.namespace } +func (info *RuntimeInfo) GetNamespacedNameAlias() string { + return info.overLimitNamespacedNameAlias +} + // GetRuntimeType gets runtime type func (info *RuntimeInfo) GetRuntimeType() string { return info.runtimeType @@ -287,6 +299,14 @@ func (info *RuntimeInfo) SetupWithDataset(dataset *datav1alpha1.Dataset) { info.exclusive = dataset.IsExclusiveMode() } +// SetupWithDataset determines if need to setup with the info of dataset +func (info *RuntimeInfo) SetNamespacedNameAlias(alias types.UID) { + if alias == "" { + return + } + info.overLimitNamespacedNameAlias = string(alias) +} + // SetFuseNodeSelector setups the fuse deploy mode func (info *RuntimeInfo) SetFuseNodeSelector(nodeSelector map[string]string) { info.fuse.NodeSelector = nodeSelector @@ -525,6 +545,7 @@ func GetRuntimeInfo(client client.Client, name, namespace string) (runtimeInfo R if runtimeInfo != nil { runtimeInfo.SetClient(client) + runtimeInfo.SetNamespacedNameAlias(dataset.UID) } return runtimeInfo, err } diff --git a/pkg/ddc/efc/master_internal_test.go b/pkg/ddc/efc/master_internal_test.go index bb4f67760b0..d4d8fea8999 100644 --- a/pkg/ddc/efc/master_internal_test.go +++ b/pkg/ddc/efc/master_internal_test.go @@ -18,6 +18,7 @@ package efc import ( "errors" + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "testing" "github.com/fluid-cloudnative/fluid/pkg/ddc/base/portallocator" @@ -100,15 +101,21 @@ func TestSetupMasterInternal(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "efc") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := EFCEngine{ - name: "test", - namespace: "fluid", - Client: client, - Log: fake.NullLogger(), - runtime: efcruntime, + name: "test", + namespace: "fluid", + Client: client, + Log: fake.NullLogger(), + runtime: efcruntime, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } @@ -196,15 +203,21 @@ func TestGenerateEFCValueFile(t *testing.T) { client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "efc") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := EFCEngine{ - name: "test", - namespace: "fluid", - Client: client, - Log: fake.NullLogger(), - runtime: efcruntime, + name: "test", + namespace: "fluid", + Client: client, + Log: fake.NullLogger(), + runtime: efcruntime, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/efc/metadata_test.go b/pkg/ddc/efc/metadata_test.go index 06de36e5fe3..0e6a8797c35 100644 --- a/pkg/ddc/efc/metadata_test.go +++ b/pkg/ddc/efc/metadata_test.go @@ -18,6 +18,7 @@ package efc import ( "errors" + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "testing" "github.com/brahma-adshonor/gohook" @@ -86,14 +87,20 @@ func TestSyncMetadataInternal(t *testing.T) { client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("spark", "fluid", "efc") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := &EFCEngine{ - name: "spark", - namespace: "fluid", - Client: client, - Log: fake.NullLogger(), + name: "spark", + namespace: "fluid", + Client: client, + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, } - err := gohook.HookMethod(engine, "TotalStorageBytes", mockTotalStorageBytesError, nil) + err = gohook.HookMethod(engine, "TotalStorageBytes", mockTotalStorageBytesError, nil) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/efc/runtime_info.go b/pkg/ddc/efc/runtime_info.go index 73f1964e312..33033a49a88 100644 --- a/pkg/ddc/efc/runtime_info.go +++ b/pkg/ddc/efc/runtime_info.go @@ -52,6 +52,9 @@ func (e *EFCEngine) getRuntimeInfo() (info base.RuntimeInfoInterface, err error) // Setup with Dataset Info dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) if err != nil { + if len(runtime.GetOwnerReferences()) > 0 { + e.runtimeInfo.SetNamespacedNameAlias(runtime.GetOwnerReferences()[0].UID) + } if utils.IgnoreNotFound(err) == nil { e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) return e.runtimeInfo, nil @@ -62,6 +65,7 @@ func (e *EFCEngine) getRuntimeInfo() (info base.RuntimeInfoInterface, err error) } e.runtimeInfo.SetupWithDataset(dataset) + e.runtimeInfo.SetNamespacedNameAlias(dataset.UID) e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) } diff --git a/pkg/ddc/efc/shutdown.go b/pkg/ddc/efc/shutdown.go index fe282adec9b..d9f5a00a310 100644 --- a/pkg/ddc/efc/shutdown.go +++ b/pkg/ddc/efc/shutdown.go @@ -272,7 +272,7 @@ func (e *EFCEngine) destroyWorkers(expectedWorkers int32) (currentWorkers int32, labelsToModify.Delete(label) } - exclusiveLabelValue := utils.GetExclusiveValue(e.namespace, e.name) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() if val, exist := toUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue { labelsToModify.Delete(labelExclusiveName) } diff --git a/pkg/ddc/efc/transform.go b/pkg/ddc/efc/transform.go index 1e322fc5656..4e31cc7024f 100644 --- a/pkg/ddc/efc/transform.go +++ b/pkg/ddc/efc/transform.go @@ -47,7 +47,7 @@ func (e *EFCEngine) transform(runtime *datav1alpha1.EFCRuntime) (value *EFC, err } value.FullnameOverride = e.name - value.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", e.namespace, e.name) + value.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) err = e.transformMasters(runtime, dataset, value) if err != nil { @@ -217,7 +217,7 @@ func (e *EFCEngine) transformFuse(runtime *datav1alpha1.EFCRuntime, value.Fuse.NodeSelector = runtime.Spec.Fuse.NodeSelector } // The label will be added by CSI Plugin when any workload pod is scheduled on the node. - value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name)] = "true" + value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name, e.runtimeInfo.GetNamespacedNameAlias())] = "true" // tiered store err = e.transformFuseTieredStore(runtime, value) diff --git a/pkg/ddc/efc/transform_test.go b/pkg/ddc/efc/transform_test.go index d1343e67b33..b17e0bffc66 100644 --- a/pkg/ddc/efc/transform_test.go +++ b/pkg/ddc/efc/transform_test.go @@ -17,6 +17,7 @@ limitations under the License. package efc import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "testing" datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" @@ -66,17 +67,22 @@ func TestEFCEngine_transform(t *testing.T) { testObjs = append(testObjs, test.dataset.DeepCopy()) client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "efc") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } engine := EFCEngine{ - name: "test", - namespace: "fluid", - Client: client, - Log: fake.NullLogger(), - runtime: test.runtime, + name: "test", + namespace: "fluid", + Client: client, + Log: fake.NullLogger(), + runtime: test.runtime, + runtimeInfo: runtimeInfo, } ctrl.SetLogger(zap.New(func(o *zap.Options) { o.Development = true })) - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/goosefs/deprecated_label.go b/pkg/ddc/goosefs/deprecated_label.go index 8d42181a033..ac66126df9f 100644 --- a/pkg/ddc/goosefs/deprecated_label.go +++ b/pkg/ddc/goosefs/deprecated_label.go @@ -50,7 +50,7 @@ func (e *GooseFSEngine) HasDeprecatedCommonLabelname() (deprecated bool, err err nodeSelectors := workers.Spec.Template.Spec.NodeSelector e.Log.Info("The current node selectors for worker", "workerName", workerName, "nodeSelector", nodeSelectors) - deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name) + deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) if _, deprecated = nodeSelectors[deprecatedCommonLabelName]; deprecated { // e.Log.Info("the deprecated node selector exists", "nodeselector", deprecatedCommonLabelName) diff --git a/pkg/ddc/goosefs/deprecated_label_test.go b/pkg/ddc/goosefs/deprecated_label_test.go index e6e50689e6d..7d9145955d1 100644 --- a/pkg/ddc/goosefs/deprecated_label_test.go +++ b/pkg/ddc/goosefs/deprecated_label_test.go @@ -67,7 +67,7 @@ func TestGooseFSEngine_GetDeprecatedCommonLabelname(t *testing.T) { }, } for _, test := range testCases { - out := utils.GetCommonLabelName(true, test.namespace, test.name) + out := utils.GetCommonLabelName(true, test.namespace, test.name, "") if out != test.out { t.Errorf("input parameter is %s-%s,expected %s, got %s", test.namespace, test.name, test.out, out) } diff --git a/pkg/ddc/goosefs/engine.go b/pkg/ddc/goosefs/engine.go index 3ca5da2cf3c..3da5affe9b2 100644 --- a/pkg/ddc/goosefs/engine.go +++ b/pkg/ddc/goosefs/engine.go @@ -87,6 +87,7 @@ func Build(id string, ctx cruntime.ReconcileRequestContext) (base.Engine, error) if err != nil { return nil, fmt.Errorf("engine %s failed to get runtime info", ctx.Name) } + engine.runtimeInfo = runtimeInfo // Build the helper engine.Helper = ctrl.BuildHelper(runtimeInfo, ctx.Client, engine.Log) diff --git a/pkg/ddc/goosefs/master_internal_test.go b/pkg/ddc/goosefs/master_internal_test.go index 7e3193e8d06..cf62e863195 100644 --- a/pkg/ddc/goosefs/master_internal_test.go +++ b/pkg/ddc/goosefs/master_internal_test.go @@ -18,6 +18,7 @@ package goosefs import ( "fmt" + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "github.com/brahma-adshonor/gohook" @@ -131,6 +132,11 @@ func TestSetupMasterInternal(t *testing.T) { client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "goosefs") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := GooseFSEngine{ name: "hbase", @@ -156,9 +162,10 @@ func TestSetupMasterInternal(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } @@ -295,6 +302,11 @@ func TestGenerateGooseFSValueFile(t *testing.T) { client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "goosefs") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := GooseFSEngine{ name: "hbase", @@ -320,9 +332,10 @@ func TestGenerateGooseFSValueFile(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/goosefs/runtime_info.go b/pkg/ddc/goosefs/runtime_info.go index 59ec62943f6..560bd58b291 100644 --- a/pkg/ddc/goosefs/runtime_info.go +++ b/pkg/ddc/goosefs/runtime_info.go @@ -45,6 +45,26 @@ func (e *GooseFSEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { e.runtimeInfo.SetFuseNodeSelector(runtime.Spec.Fuse.NodeSelector) if !e.UnitTest { + // Setup with Dataset Info + dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) + if err != nil { + if len(runtime.GetOwnerReferences()) > 0 { + e.runtimeInfo.SetNamespacedNameAlias(runtime.GetOwnerReferences()[0].UID) + } + if utils.IgnoreNotFound(err) == nil { + e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) + return e.runtimeInfo, nil + } + + e.Log.Info("Failed to get dataset when getruntimeInfo") + return e.runtimeInfo, err + } + + e.runtimeInfo.SetupWithDataset(dataset) + e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) + + e.runtimeInfo.SetNamespacedNameAlias(dataset.UID) + // Check if the runtime is using deprecated labels isLabelDeprecated, err := e.HasDeprecatedCommonLabelname() if err != nil { @@ -61,21 +81,6 @@ func (e *GooseFSEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { e.Log.Info("Deprecation check finished", "isLabelDeprecated", e.runtimeInfo.IsDeprecatedNodeLabel(), "isPVNameDeprecated", e.runtimeInfo.IsDeprecatedPVName()) - // Setup with Dataset Info - dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) - if err != nil { - if utils.IgnoreNotFound(err) == nil { - e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) - return e.runtimeInfo, nil - } - - e.Log.Info("Failed to get dataset when getruntimeInfo") - return e.runtimeInfo, err - } - - e.runtimeInfo.SetupWithDataset(dataset) - - e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) } } diff --git a/pkg/ddc/goosefs/shutdown.go b/pkg/ddc/goosefs/shutdown.go index 577758d115c..1ed891bc791 100644 --- a/pkg/ddc/goosefs/shutdown.go +++ b/pkg/ddc/goosefs/shutdown.go @@ -285,7 +285,7 @@ func (e *GooseFSEngine) destroyWorkers(expectedWorkers int32) (currentWorkers in labelsToModify.Delete(label) } - exclusiveLabelValue := utils.GetExclusiveValue(e.namespace, e.name) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() if val, exist := toUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue { labelsToModify.Delete(labelExclusiveName) } diff --git a/pkg/ddc/goosefs/transform.go b/pkg/ddc/goosefs/transform.go index 62e6d12a28c..3e87baa9d79 100644 --- a/pkg/ddc/goosefs/transform.go +++ b/pkg/ddc/goosefs/transform.go @@ -45,7 +45,7 @@ func (e *GooseFSEngine) transform(runtime *datav1alpha1.GooseFSRuntime) (value * value = &GooseFS{} value.FullnameOverride = e.name - value.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", e.namespace, e.name) + value.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) // 1.transform the common part err = e.transformCommonPart(runtime, dataset, value) diff --git a/pkg/ddc/goosefs/transform_fuse.go b/pkg/ddc/goosefs/transform_fuse.go index 6a4f7b74fca..4ac62fb09c4 100644 --- a/pkg/ddc/goosefs/transform_fuse.go +++ b/pkg/ddc/goosefs/transform_fuse.go @@ -87,7 +87,7 @@ func (e *GooseFSEngine) transformFuse(runtime *datav1alpha1.GooseFSRuntime, data value.Fuse.NodeSelector = map[string]string{} } - value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name)] = "true" + value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name, e.runtimeInfo.GetNamespacedNameAlias())] = "true" value.Fuse.HostNetwork = true value.Fuse.HostPID = common.HostPIDEnabled(runtime.Annotations) value.Fuse.Enabled = true diff --git a/pkg/ddc/goosefs/transform_fuse_test.go b/pkg/ddc/goosefs/transform_fuse_test.go index b0ce174072a..fd39868c041 100644 --- a/pkg/ddc/goosefs/transform_fuse_test.go +++ b/pkg/ddc/goosefs/transform_fuse_test.go @@ -17,6 +17,7 @@ limitations under the License. package goosefs import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "testing" datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" @@ -41,8 +42,16 @@ func TestTransformFuseWithNoArgs(t *testing.T) { }}, &GooseFS{}, "--fuse-opts=rw,direct_io,allow_other"}, } for _, test := range tests { - engine := &GooseFSEngine{Log: fake.NullLogger()} - err := engine.transformFuse(test.runtime, test.dataset, test.goosefsValue) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "goosefs") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := &GooseFSEngine{ + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, + Client: fake.NewFakeClientWithScheme(testScheme), + } + err = engine.transformFuse(test.runtime, test.dataset, test.goosefsValue) if err != nil { t.Errorf("Got err %v", err) } @@ -77,8 +86,16 @@ func TestTransformFuseWithArgs(t *testing.T) { }}, &GooseFS{}, "--fuse-opts=kernel_cache,allow_other"}, } for _, test := range tests { - engine := &GooseFSEngine{Log: fake.NullLogger()} - err := engine.transformFuse(test.runtime, test.dataset, test.goosefsValue) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "goosefs") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := &GooseFSEngine{ + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, + Client: fake.NewFakeClientWithScheme(testScheme), + } + err = engine.transformFuse(test.runtime, test.dataset, test.goosefsValue) if err != nil { t.Errorf("Got err %v", err) } diff --git a/pkg/ddc/goosefs/transform_test.go b/pkg/ddc/goosefs/transform_test.go index bd753cadf57..fc53506ffcc 100644 --- a/pkg/ddc/goosefs/transform_test.go +++ b/pkg/ddc/goosefs/transform_test.go @@ -17,6 +17,8 @@ limitations under the License. package goosefs import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" + fakeutils "github.com/fluid-cloudnative/fluid/pkg/utils/fake" "testing" datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" @@ -55,9 +57,16 @@ func TestTransformFuse(t *testing.T) { }, &GooseFS{}, []string{"fuse", "--fuse-opts=rw,direct_io,uid=1000,gid=1000,allow_other"}}, } for _, test := range tests { - engine := &GooseFSEngine{} + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "goosefs") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := &GooseFSEngine{ + runtimeInfo: runtimeInfo, + Client: fakeutils.NewFakeClientWithScheme(testScheme), + } engine.Log = ctrl.Log - err := engine.transformFuse(test.runtime, test.dataset, test.value) + err = engine.transformFuse(test.runtime, test.dataset, test.value) if err != nil { t.Errorf("error %v", err) } diff --git a/pkg/ddc/jindo/deprecated_label.go b/pkg/ddc/jindo/deprecated_label.go index 73f91410f76..5c697819352 100644 --- a/pkg/ddc/jindo/deprecated_label.go +++ b/pkg/ddc/jindo/deprecated_label.go @@ -49,7 +49,7 @@ func (e *JindoEngine) HasDeprecatedCommonLabelName() (deprecated bool, err error nodeSelectors := workers.Spec.Template.Spec.NodeSelector e.Log.Info("The current node selectors for worker", "workerName", workerName, "nodeSelector", nodeSelectors) - deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name) + deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) if _, deprecated = nodeSelectors[deprecatedCommonLabelName]; deprecated { // e.Log.Info("the deprecated node selector exists", "nodeSelector", deprecatedCommonLabelName) diff --git a/pkg/ddc/jindo/deprecated_label_test.go b/pkg/ddc/jindo/deprecated_label_test.go index 5353b5fffd4..22394e11e2b 100644 --- a/pkg/ddc/jindo/deprecated_label_test.go +++ b/pkg/ddc/jindo/deprecated_label_test.go @@ -68,7 +68,7 @@ func TestJindoEngine_GetDeprecatedCommonLabelName(t *testing.T) { }, } for _, test := range testCases { - out := utils.GetCommonLabelName(true, test.namespace, test.name) + out := utils.GetCommonLabelName(true, test.namespace, test.name, "") if out != test.out { t.Errorf("input parameter is %s-%s,expected %s, got %s", test.namespace, test.name, test.out, out) } diff --git a/pkg/ddc/jindo/master_internal_test.go b/pkg/ddc/jindo/master_internal_test.go index 8154c16e802..ce85d00d773 100644 --- a/pkg/ddc/jindo/master_internal_test.go +++ b/pkg/ddc/jindo/master_internal_test.go @@ -17,6 +17,7 @@ limitations under the License. package jindo import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "testing" "github.com/fluid-cloudnative/fluid/pkg/common" @@ -85,6 +86,11 @@ func TestSetupMasterInternal(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "jindo") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := JindoEngine{ name: "hbase", namespace: "fluid", @@ -97,8 +103,9 @@ func TestSetupMasterInternal(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } @@ -176,6 +183,10 @@ func TestGenerateJindoValueFile(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "jindo") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } result := resource.MustParse("20Gi") engine := JindoEngine{ name: "hbase", @@ -197,9 +208,10 @@ func TestGenerateJindoValueFile(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/jindo/runtime_info.go b/pkg/ddc/jindo/runtime_info.go index b453b01e314..db6fcb32cb0 100644 --- a/pkg/ddc/jindo/runtime_info.go +++ b/pkg/ddc/jindo/runtime_info.go @@ -45,6 +45,26 @@ func (e *JindoEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { // Setup Fuse Deploy Mode e.runtimeInfo.SetFuseNodeSelector(runtime.Spec.Fuse.NodeSelector) + // Setup with Dataset Info + dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) + if err != nil { + if len(runtime.GetOwnerReferences()) > 0 { + e.runtimeInfo.SetNamespacedNameAlias(runtime.GetOwnerReferences()[0].UID) + } + if utils.IgnoreNotFound(err) == nil { + e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) + return e.runtimeInfo, nil + } + + e.Log.Info("Failed to get dataset when getruntimeInfo") + return e.runtimeInfo, err + } + + e.runtimeInfo.SetupWithDataset(dataset) + e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) + + e.runtimeInfo.SetNamespacedNameAlias(dataset.GetUID()) + // Check if the runtime is using deprecated labels isLabelDeprecated, err := e.HasDeprecatedCommonLabelName() if err != nil { @@ -60,22 +80,6 @@ func (e *JindoEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { e.runtimeInfo.SetDeprecatedPVName(isPVNameDeprecated) e.Log.Info("Deprecation check finished", "isLabelDeprecated", e.runtimeInfo.IsDeprecatedNodeLabel(), "isPVNameDeprecated", e.runtimeInfo.IsDeprecatedPVName()) - - // Setup with Dataset Info - dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) - if err != nil { - if utils.IgnoreNotFound(err) == nil { - e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) - return e.runtimeInfo, nil - } - - e.Log.Info("Failed to get dataset when getruntimeInfo") - return e.runtimeInfo, err - } - - e.runtimeInfo.SetupWithDataset(dataset) - - e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) } return e.runtimeInfo, nil diff --git a/pkg/ddc/jindo/shutdown.go b/pkg/ddc/jindo/shutdown.go index 7a422348ee7..2708fe5e246 100644 --- a/pkg/ddc/jindo/shutdown.go +++ b/pkg/ddc/jindo/shutdown.go @@ -230,7 +230,7 @@ func (e *JindoEngine) destroyWorkers(expectedWorkers int32) (currentWorkers int3 labelsToModify.Delete(label) } - exclusiveLabelValue := utils.GetExclusiveValue(e.namespace, e.name) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() if val, exist := toUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue { labelsToModify.Delete(labelExclusiveName) diff --git a/pkg/ddc/jindo/transform.go b/pkg/ddc/jindo/transform.go index f5b7807087f..ef573721012 100644 --- a/pkg/ddc/jindo/transform.go +++ b/pkg/ddc/jindo/transform.go @@ -120,7 +120,7 @@ func (e *JindoEngine) transform(runtime *datav1alpha1.JindoRuntime) (value *Jind }, } - value.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", e.namespace, e.name) + value.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) e.transformNetworkMode(runtime, value) err = e.transformHadoopConfig(runtime, value) @@ -464,7 +464,7 @@ func (e *JindoEngine) transformFuseNodeSelector(runtime *datav1alpha1.JindoRunti } // The label will be added by CSI Plugin when any workload pod is scheduled on the node. - value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name)] = "true" + value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name, e.runtimeInfo.GetNamespacedNameAlias())] = "true" return nil } diff --git a/pkg/ddc/jindo/worker.go b/pkg/ddc/jindo/worker.go index d268340625e..a361c0c50ac 100644 --- a/pkg/ddc/jindo/worker.go +++ b/pkg/ddc/jindo/worker.go @@ -225,7 +225,7 @@ func (e *JindoEngine) buildWorkersAffinity(workers *v1.StatefulSet) (workersToUp Preference: corev1.NodeSelectorTerm{ MatchExpressions: []corev1.NodeSelectorRequirement{ { - Key: utils.GetFuseLabelName(e.namespace, e.name), + Key: utils.GetFuseLabelName(e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()), Operator: corev1.NodeSelectorOpIn, Values: []string{"true"}, }, diff --git a/pkg/ddc/jindo/worker_test.go b/pkg/ddc/jindo/worker_test.go index 60129acbd82..58708296b38 100644 --- a/pkg/ddc/jindo/worker_test.go +++ b/pkg/ddc/jindo/worker_test.go @@ -814,10 +814,15 @@ func TestBuildWorkersAffinity(t *testing.T) { runtimeObjs = append(runtimeObjs, tt.fields.dataset) runtimeObjs = append(runtimeObjs, tt.fields.worker) mockClient := fake.NewFakeClientWithScheme(s, runtimeObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "jindo") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } e := &JindoEngine{ - name: tt.fields.dataset.Name, - namespace: tt.fields.dataset.Namespace, - Client: mockClient, + name: tt.fields.dataset.Name, + namespace: tt.fields.dataset.Namespace, + Client: mockClient, + runtimeInfo: runtimeInfo, } want := tt.fields.want diff --git a/pkg/ddc/jindocache/deprecated_label.go b/pkg/ddc/jindocache/deprecated_label.go index 60beb01bb40..90577c4a6ef 100644 --- a/pkg/ddc/jindocache/deprecated_label.go +++ b/pkg/ddc/jindocache/deprecated_label.go @@ -50,7 +50,7 @@ func (e *JindoCacheEngine) HasDeprecatedCommonLabelname() (deprecated bool, err nodeSelectors := workers.Spec.Template.Spec.NodeSelector e.Log.Info("The current node selectors for worker", "workerName", workerName, "nodeSelector", nodeSelectors) - deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name) + deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) if _, deprecated = nodeSelectors[deprecatedCommonLabelName]; deprecated { // e.Log.Info("the deprecated node selector exists", "nodeselector", deprecatedCommonLabelName) diff --git a/pkg/ddc/jindocache/deprecated_label_test.go b/pkg/ddc/jindocache/deprecated_label_test.go index bb1d7da4e2b..d461a80bbbb 100644 --- a/pkg/ddc/jindocache/deprecated_label_test.go +++ b/pkg/ddc/jindocache/deprecated_label_test.go @@ -68,7 +68,7 @@ func TestJindoCacheEngine_GetDeprecatedCommonLabelname(t *testing.T) { }, } for _, test := range testCases { - out := utils.GetCommonLabelName(true, test.namespace, test.name) + out := utils.GetCommonLabelName(true, test.namespace, test.name, "") if out != test.out { t.Errorf("input parameter is %s-%s,expected %s, got %s", test.namespace, test.name, test.out, out) } diff --git a/pkg/ddc/jindocache/master_internal_test.go b/pkg/ddc/jindocache/master_internal_test.go index 7f4588ed216..c27a2c8372b 100644 --- a/pkg/ddc/jindocache/master_internal_test.go +++ b/pkg/ddc/jindocache/master_internal_test.go @@ -17,6 +17,7 @@ limitations under the License. package jindocache import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "testing" "github.com/fluid-cloudnative/fluid/pkg/common" @@ -85,6 +86,11 @@ func TestSetupMasterInternal(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "jindocache") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := JindoCacheEngine{ name: "hbase", namespace: "fluid", @@ -97,8 +103,9 @@ func TestSetupMasterInternal(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } @@ -178,6 +185,10 @@ func TestGenerateJindoValueFile(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "jindocache") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } result := resource.MustParse("20Gi") engine := JindoCacheEngine{ name: "hbase", @@ -199,9 +210,10 @@ func TestGenerateJindoValueFile(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/jindocache/runtime_info.go b/pkg/ddc/jindocache/runtime_info.go index bcc81b8b719..34456378d9f 100644 --- a/pkg/ddc/jindocache/runtime_info.go +++ b/pkg/ddc/jindocache/runtime_info.go @@ -44,6 +44,26 @@ func (e *JindoCacheEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { // Setup Fuse Deploy Mode e.runtimeInfo.SetFuseNodeSelector(runtime.Spec.Fuse.NodeSelector) + // Setup with Dataset Info + dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) + if err != nil { + if len(runtime.GetOwnerReferences()) > 0 { + e.runtimeInfo.SetNamespacedNameAlias(runtime.GetOwnerReferences()[0].UID) + } + if utils.IgnoreNotFound(err) == nil { + e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) + return e.runtimeInfo, nil + } + + e.Log.Info("Failed to get dataset when getruntimeInfo") + return e.runtimeInfo, err + } + + e.runtimeInfo.SetupWithDataset(dataset) + e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) + + e.runtimeInfo.SetNamespacedNameAlias(dataset.GetUID()) + // Check if the runtime is using deprecated labels isLabelDeprecated, err := e.HasDeprecatedCommonLabelname() if err != nil { @@ -60,21 +80,6 @@ func (e *JindoCacheEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { e.Log.Info("Deprecation check finished", "isLabelDeprecated", e.runtimeInfo.IsDeprecatedNodeLabel(), "isPVNameDeprecated", e.runtimeInfo.IsDeprecatedPVName()) - // Setup with Dataset Info - dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) - if err != nil { - if utils.IgnoreNotFound(err) == nil { - e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) - return e.runtimeInfo, nil - } - - e.Log.Info("Failed to get dataset when getruntimeInfo") - return e.runtimeInfo, err - } - - e.runtimeInfo.SetupWithDataset(dataset) - - e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) } return e.runtimeInfo, nil diff --git a/pkg/ddc/jindocache/shutdown.go b/pkg/ddc/jindocache/shutdown.go index a690abe8e19..8a3827ea17e 100644 --- a/pkg/ddc/jindocache/shutdown.go +++ b/pkg/ddc/jindocache/shutdown.go @@ -230,7 +230,7 @@ func (e *JindoCacheEngine) destroyWorkers(expectedWorkers int32) (currentWorkers labelsToModify.Delete(label) } - exclusiveLabelValue := utils.GetExclusiveValue(e.namespace, e.name) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() if val, exist := toUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue { labelsToModify.Delete(labelExclusiveName) diff --git a/pkg/ddc/jindocache/transform.go b/pkg/ddc/jindocache/transform.go index 5ce14b84926..bbbdad43026 100644 --- a/pkg/ddc/jindocache/transform.go +++ b/pkg/ddc/jindocache/transform.go @@ -154,7 +154,7 @@ func (e *JindoCacheEngine) transform(runtime *datav1alpha1.JindoRuntime) (value }, } - value.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", e.namespace, e.name) + value.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) e.transformNetworkMode(runtime, value) err = e.transformHadoopConfig(runtime, value) @@ -907,7 +907,8 @@ func (e *JindoCacheEngine) transformFuseNodeSelector(runtime *datav1alpha1.Jindo } // The label will be added by CSI Plugin when any workload pod is scheduled on the node. - value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name)] = "true" + e.Log.Info("JIUYUDEBUG", "GetNamespacedNameAlias", e.runtimeInfo.GetNamespacedNameAlias()) + value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name, e.runtimeInfo.GetNamespacedNameAlias())] = "true" } func (e *JindoCacheEngine) transformNodeSelector(runtime *datav1alpha1.JindoRuntime) map[string]string { diff --git a/pkg/ddc/jindocache/transform_test.go b/pkg/ddc/jindocache/transform_test.go index 76cacbd13fd..dc495eb6fc5 100644 --- a/pkg/ddc/jindocache/transform_test.go +++ b/pkg/ddc/jindocache/transform_test.go @@ -17,6 +17,7 @@ limitations under the License. package jindocache import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "os" "reflect" ctrl "sigs.k8s.io/controller-runtime" @@ -636,15 +637,20 @@ func TestJindoCacheEngine_transform(t *testing.T) { s.AddKnownTypes(datav1alpha1.GroupVersion, &datav1alpha1.DatasetList{}) _ = corev1.AddToScheme(s) client := fake.NewFakeClientWithScheme(s, runtimeObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "jindocache") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } e := &JindoCacheEngine{ - runtime: tt.fields.runtime, - name: tt.fields.name, - namespace: tt.fields.namespace, - Client: client, - Log: fake.NullLogger(), + runtime: tt.fields.runtime, + name: tt.fields.name, + namespace: tt.fields.namespace, + Client: client, + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, } tt.args.runtime = tt.fields.runtime - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatalf("failed to set up runtime port allocator due to %v", err) } @@ -1143,15 +1149,21 @@ func TestJindoCacheEngine_transformPolicy(t *testing.T) { s.AddKnownTypes(datav1alpha1.GroupVersion, &datav1alpha1.DatasetList{}) _ = corev1.AddToScheme(s) client := fake.NewFakeClientWithScheme(s, runtimeObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "jinocache") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + e := &JindoCacheEngine{ - runtime: tt.fields.runtime, - name: tt.fields.name, - namespace: tt.fields.namespace, - Client: client, - Log: fake.NullLogger(), + runtime: tt.fields.runtime, + name: tt.fields.name, + namespace: tt.fields.namespace, + Client: client, + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, } tt.args.runtime = tt.fields.runtime - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatalf("failed to set up runtime port allocator due to %v", err) } @@ -1352,15 +1364,21 @@ func TestJindoCacheEngine_transformCacheSet(t *testing.T) { s.AddKnownTypes(datav1alpha1.GroupVersion, &datav1alpha1.DatasetList{}) _ = corev1.AddToScheme(s) client := fake.NewFakeClientWithScheme(s, runtimeObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "jindocache") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + e := &JindoCacheEngine{ - runtime: tt.fields.runtime, - name: tt.fields.name, - namespace: tt.fields.namespace, - Client: client, - Log: fake.NullLogger(), + runtime: tt.fields.runtime, + name: tt.fields.name, + namespace: tt.fields.namespace, + Client: client, + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, } tt.args.runtime = tt.fields.runtime - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatalf("failed to set up runtime port allocator due to %v", err) } diff --git a/pkg/ddc/jindofsx/deprecated_label.go b/pkg/ddc/jindofsx/deprecated_label.go index 0fc4613eca6..49fac5663a3 100644 --- a/pkg/ddc/jindofsx/deprecated_label.go +++ b/pkg/ddc/jindofsx/deprecated_label.go @@ -50,7 +50,7 @@ func (e *JindoFSxEngine) HasDeprecatedCommonLabelname() (deprecated bool, err er nodeSelectors := workers.Spec.Template.Spec.NodeSelector e.Log.Info("The current node selectors for worker", "workerName", workerName, "nodeSelector", nodeSelectors) - deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name) + deprecatedCommonLabelName := utils.GetCommonLabelName(true, e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) if _, deprecated = nodeSelectors[deprecatedCommonLabelName]; deprecated { // e.Log.Info("the deprecated node selector exists", "nodeselector", deprecatedCommonLabelName) diff --git a/pkg/ddc/jindofsx/deprecated_label_test.go b/pkg/ddc/jindofsx/deprecated_label_test.go index ecbfeae4e52..319236ccc50 100644 --- a/pkg/ddc/jindofsx/deprecated_label_test.go +++ b/pkg/ddc/jindofsx/deprecated_label_test.go @@ -68,7 +68,7 @@ func TestJindoFSxEngine_GetDeprecatedCommonLabelName(t *testing.T) { }, } for _, test := range testCases { - out := utils.GetCommonLabelName(true, test.namespace, test.name) + out := utils.GetCommonLabelName(true, test.namespace, test.name, "") if out != test.out { t.Errorf("input parameter is %s-%s,expected %s, got %s", test.namespace, test.name, test.out, out) } diff --git a/pkg/ddc/jindofsx/master_internal_test.go b/pkg/ddc/jindofsx/master_internal_test.go index 088236498ac..6a8ec057e27 100644 --- a/pkg/ddc/jindofsx/master_internal_test.go +++ b/pkg/ddc/jindofsx/master_internal_test.go @@ -17,6 +17,7 @@ limitations under the License. package jindofsx import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "testing" "github.com/fluid-cloudnative/fluid/pkg/common" @@ -85,6 +86,11 @@ func TestSetupMasterInternal(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "jindofsx") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := JindoFSxEngine{ name: "hbase", namespace: "fluid", @@ -97,8 +103,9 @@ func TestSetupMasterInternal(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } @@ -175,6 +182,12 @@ func TestGenerateJindoValueFile(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "jindofsx") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + result := resource.MustParse("20Gi") engine := JindoFSxEngine{ name: "hbase", @@ -196,9 +209,10 @@ func TestGenerateJindoValueFile(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 50}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } diff --git a/pkg/ddc/jindofsx/runtime_info.go b/pkg/ddc/jindofsx/runtime_info.go index 441ae394994..8cb5279880b 100644 --- a/pkg/ddc/jindofsx/runtime_info.go +++ b/pkg/ddc/jindofsx/runtime_info.go @@ -44,6 +44,26 @@ func (e *JindoFSxEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { // Setup Fuse Deploy Mode e.runtimeInfo.SetFuseNodeSelector(runtime.Spec.Fuse.NodeSelector) + // Setup with Dataset Info + dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) + if err != nil { + if len(runtime.GetOwnerReferences()) > 0 { + e.runtimeInfo.SetNamespacedNameAlias(runtime.GetOwnerReferences()[0].UID) + } + if utils.IgnoreNotFound(err) == nil { + e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) + return e.runtimeInfo, nil + } + + e.Log.Info("Failed to get dataset when getruntimeInfo") + return e.runtimeInfo, err + } + + e.runtimeInfo.SetupWithDataset(dataset) + e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) + + e.runtimeInfo.SetNamespacedNameAlias(dataset.GetUID()) + // Check if the runtime is using deprecated labels isLabelDeprecated, err := e.HasDeprecatedCommonLabelname() if err != nil { @@ -59,22 +79,6 @@ func (e *JindoFSxEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { e.runtimeInfo.SetDeprecatedPVName(isPVNameDeprecated) e.Log.Info("Deprecation check finished", "isLabelDeprecated", e.runtimeInfo.IsDeprecatedNodeLabel(), "isPVNameDeprecated", e.runtimeInfo.IsDeprecatedPVName()) - - // Setup with Dataset Info - dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) - if err != nil { - if utils.IgnoreNotFound(err) == nil { - e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) - return e.runtimeInfo, nil - } - - e.Log.Info("Failed to get dataset when getruntimeInfo") - return e.runtimeInfo, err - } - - e.runtimeInfo.SetupWithDataset(dataset) - - e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) } return e.runtimeInfo, nil diff --git a/pkg/ddc/jindofsx/shutdown.go b/pkg/ddc/jindofsx/shutdown.go index 0eaa97529e5..da757946d8f 100644 --- a/pkg/ddc/jindofsx/shutdown.go +++ b/pkg/ddc/jindofsx/shutdown.go @@ -230,7 +230,7 @@ func (e *JindoFSxEngine) destroyWorkers(expectedWorkers int32) (currentWorkers i labelsToModify.Delete(label) } - exclusiveLabelValue := utils.GetExclusiveValue(e.namespace, e.name) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() if val, exist := toUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue { labelsToModify.Delete(labelExclusiveName) diff --git a/pkg/ddc/jindofsx/transform.go b/pkg/ddc/jindofsx/transform.go index bf1431bd3f4..2ae0b5ce96f 100644 --- a/pkg/ddc/jindofsx/transform.go +++ b/pkg/ddc/jindofsx/transform.go @@ -154,7 +154,7 @@ func (e *JindoFSxEngine) transform(runtime *datav1alpha1.JindoRuntime) (value *J }, } - value.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", e.namespace, e.name) + value.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) e.transformNetworkMode(runtime, value) err = e.transformHadoopConfig(runtime, value) @@ -791,7 +791,7 @@ func (e *JindoFSxEngine) transformFuseNodeSelector(runtime *datav1alpha1.JindoRu } // The label will be added by CSI Plugin when any workload pod is scheduled on the node. - value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name)] = "true" + value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name, e.runtimeInfo.GetNamespacedNameAlias())] = "true" } func (e *JindoFSxEngine) transformNodeSelector(runtime *datav1alpha1.JindoRuntime) map[string]string { diff --git a/pkg/ddc/jindofsx/transform_test.go b/pkg/ddc/jindofsx/transform_test.go index 45029f1c018..24ea0a72f8e 100644 --- a/pkg/ddc/jindofsx/transform_test.go +++ b/pkg/ddc/jindofsx/transform_test.go @@ -17,6 +17,7 @@ limitations under the License. package jindofsx import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "os" "reflect" "testing" @@ -634,15 +635,20 @@ func TestJindoFSxEngine_transform(t *testing.T) { s.AddKnownTypes(datav1alpha1.GroupVersion, &datav1alpha1.DatasetList{}) _ = corev1.AddToScheme(s) client := fake.NewFakeClientWithScheme(s, runtimeObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "jindofsx") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } e := &JindoFSxEngine{ - runtime: tt.fields.runtime, - name: tt.fields.name, - namespace: tt.fields.namespace, - Client: client, - Log: fake.NullLogger(), + runtime: tt.fields.runtime, + name: tt.fields.name, + namespace: tt.fields.namespace, + Client: client, + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, } tt.args.runtime = tt.fields.runtime - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatalf("failed to set up runtime port allocator due to %v", err) } diff --git a/pkg/ddc/juicefs/data_migrate.go b/pkg/ddc/juicefs/data_migrate.go index 4c0d78db387..a28b0081f2d 100644 --- a/pkg/ddc/juicefs/data_migrate.go +++ b/pkg/ddc/juicefs/data_migrate.go @@ -174,7 +174,7 @@ func (j *JuiceFSEngine) generateDataMigrateValueFile(r cruntime.ReconcileRequest DataMigrateInfo: dataMigrateInfo, } dataMigrateValue.Owner = transformer.GenerateOwnerReferenceFromObject(dataMigrate) - dataMigrateValue.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", j.namespace, j.name) + dataMigrateValue.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", j.namespace, j.name, j.runtimeInfo.GetNamespacedNameAlias()) // 6. create the value file data, err := yaml.Marshal(dataMigrateValue) diff --git a/pkg/ddc/juicefs/data_migrate_test.go b/pkg/ddc/juicefs/data_migrate_test.go index b744c16f836..d21c8959a0a 100644 --- a/pkg/ddc/juicefs/data_migrate_test.go +++ b/pkg/ddc/juicefs/data_migrate_test.go @@ -20,6 +20,7 @@ import ( "encoding/base64" "fmt" "github.com/fluid-cloudnative/fluid/pkg/dataoperation" + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "github.com/fluid-cloudnative/fluid/pkg/utils" "os" "path/filepath" @@ -162,11 +163,16 @@ func TestJuiceFSEngine_generateDataMigrateValueFile(t *testing.T) { } for _, test := range testCases { + runtimeInfo, err := base.BuildRuntimeInfo("juicefs", "fluid", "juicefs") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } engine := JuiceFSEngine{ - name: "juicefs", - namespace: "fluid", - Client: client, - Log: fake.NullLogger(), + name: "juicefs", + namespace: "fluid", + Client: client, + Log: fake.NullLogger(), + runtimeInfo: runtimeInfo, } fileName, err := engine.generateDataMigrateValueFile(context, &test.dataMigrate) if err != nil { diff --git a/pkg/ddc/juicefs/deprecated_label.go b/pkg/ddc/juicefs/deprecated_label.go index 0f6d1e83dc4..13cdd557f11 100644 --- a/pkg/ddc/juicefs/deprecated_label.go +++ b/pkg/ddc/juicefs/deprecated_label.go @@ -44,7 +44,7 @@ func (j *JuiceFSEngine) HasDeprecatedCommonLabelName() (deprecated bool, err err nodeSelectors := fuses.Spec.Template.Spec.NodeSelector j.Log.Info("The current node selectors for worker", "fuseName", fuseName, "nodeSelector", nodeSelectors) - deprecatedCommonLabelName := utils.GetCommonLabelName(true, j.namespace, j.name) + deprecatedCommonLabelName := utils.GetCommonLabelName(true, j.namespace, j.name, j.runtimeInfo.GetNamespacedNameAlias()) if _, deprecated = nodeSelectors[deprecatedCommonLabelName]; deprecated { // j.Log.Info("the deprecated node selector exists", "nodeselector", deprecatedCommonLabelName) diff --git a/pkg/ddc/juicefs/deprecated_label_test.go b/pkg/ddc/juicefs/deprecated_label_test.go index f533f773edc..9578d963469 100644 --- a/pkg/ddc/juicefs/deprecated_label_test.go +++ b/pkg/ddc/juicefs/deprecated_label_test.go @@ -138,7 +138,7 @@ func TestJuiceFSEngine_getDeprecatedCommonLabelName(t *testing.T) { }, } for _, test := range testCases { - out := utils.GetCommonLabelName(true, test.namespace, test.name) + out := utils.GetCommonLabelName(true, test.namespace, test.name, "") if out != test.out { t.Errorf("input parameter is %s-%s,expected %s, got %s", test.namespace, test.name, test.out, out) } diff --git a/pkg/ddc/juicefs/master_internal_test.go b/pkg/ddc/juicefs/master_internal_test.go index 8083a34344e..0d30c04e772 100644 --- a/pkg/ddc/juicefs/master_internal_test.go +++ b/pkg/ddc/juicefs/master_internal_test.go @@ -19,6 +19,7 @@ package juicefs import ( "errors" "fmt" + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "testing" rbacv1 "k8s.io/api/rbac/v1" @@ -124,6 +125,11 @@ func TestSetupMasterInternal(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("hbase", "fluid", "juicefs") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := JuiceFSEngine{ name: "test", namespace: "fluid", @@ -134,8 +140,9 @@ func TestSetupMasterInternal(t *testing.T) { Fuse: datav1alpha1.JuiceFSFuseSpec{}, }, }, + runtimeInfo: runtimeInfo, } - err := portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) + err = portallocator.SetupRuntimePortAllocator(client, &net.PortRange{Base: 10, Size: 100}, "bitmap", GetReservedPorts) if err != nil { t.Fatal(err.Error()) } @@ -249,6 +256,11 @@ func TestGenerateJuiceFSValueFile(t *testing.T) { client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "juicefs") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := JuiceFSEngine{ name: "test", namespace: "fluid", @@ -259,9 +271,10 @@ func TestGenerateJuiceFSValueFile(t *testing.T) { Fuse: datav1alpha1.JuiceFSFuseSpec{}, }, }, + runtimeInfo: runtimeInfo, } - _, err := engine.generateJuicefsValueFile(juicefsruntime) + _, err = engine.generateJuicefsValueFile(juicefsruntime) if err != nil { t.Errorf("fail to exec the function: %v", err) } diff --git a/pkg/ddc/juicefs/runtime_info.go b/pkg/ddc/juicefs/runtime_info.go index 7b6847d0a0b..8bff1896419 100644 --- a/pkg/ddc/juicefs/runtime_info.go +++ b/pkg/ddc/juicefs/runtime_info.go @@ -45,6 +45,26 @@ func (j *JuiceFSEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { j.runtimeInfo.SetFuseNodeSelector(runtime.Spec.Fuse.NodeSelector) if !j.UnitTest { + // Setup with Dataset Info + dataset, err := utils.GetDataset(j.Client, j.name, j.namespace) + if err != nil { + if len(runtime.GetOwnerReferences()) > 0 { + j.runtimeInfo.SetNamespacedNameAlias(runtime.GetOwnerReferences()[0].UID) + } + if utils.IgnoreNotFound(err) == nil { + j.Log.Info("Dataset is notfound", "name", j.name, "namespace", j.namespace) + return j.runtimeInfo, nil + } + + j.Log.Info("Failed to get dataset when getruntimeInfo") + return j.runtimeInfo, err + } + + j.runtimeInfo.SetupWithDataset(dataset) + j.Log.Info("Setup with dataset done", "exclusive", j.runtimeInfo.IsExclusive()) + + j.runtimeInfo.SetNamespacedNameAlias(dataset.GetUID()) + // Check if the runtime is using deprecated labels isLabelDeprecated, err := j.HasDeprecatedCommonLabelName() if err != nil { @@ -60,22 +80,6 @@ func (j *JuiceFSEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { j.runtimeInfo.SetDeprecatedPVName(isPVNameDeprecated) j.Log.Info("Deprecation check finished", "isLabelDeprecated", j.runtimeInfo.IsDeprecatedNodeLabel(), "isPVNameDeprecated", j.runtimeInfo.IsDeprecatedPVName()) - - // Setup with Dataset Info - dataset, err := utils.GetDataset(j.Client, j.name, j.namespace) - if err != nil { - if utils.IgnoreNotFound(err) == nil { - j.Log.Info("Dataset is notfound", "name", j.name, "namespace", j.namespace) - return j.runtimeInfo, nil - } - - j.Log.Info("Failed to get dataset when getruntimeInfo") - return j.runtimeInfo, err - } - - j.runtimeInfo.SetupWithDataset(dataset) - - j.Log.Info("Setup with dataset done", "exclusive", j.runtimeInfo.IsExclusive()) } } diff --git a/pkg/ddc/juicefs/shutdown.go b/pkg/ddc/juicefs/shutdown.go index 1c91587d94b..cf00fb51f51 100644 --- a/pkg/ddc/juicefs/shutdown.go +++ b/pkg/ddc/juicefs/shutdown.go @@ -315,7 +315,7 @@ func (j *JuiceFSEngine) destroyWorkers(expectedWorkers int32) (currentWorkers in labelsToModify.Delete(label) } - exclusiveLabelValue := utils.GetExclusiveValue(j.namespace, j.name) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() if val, exist := toUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue { labelsToModify.Delete(labelExclusiveName) } diff --git a/pkg/ddc/juicefs/transform.go b/pkg/ddc/juicefs/transform.go index 47faef03603..36bf84b7ac1 100644 --- a/pkg/ddc/juicefs/transform.go +++ b/pkg/ddc/juicefs/transform.go @@ -52,7 +52,7 @@ func (j *JuiceFSEngine) transform(runtime *datav1alpha1.JuiceFSRuntime) (value * } value.FullnameOverride = j.name - value.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", j.namespace, j.name) + value.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", j.namespace, j.name, j.runtimeInfo.GetNamespacedNameAlias()) value.Owner = transformer.GenerateOwnerReferenceFromObject(runtime) // transform toleration diff --git a/pkg/ddc/juicefs/transform_fuse.go b/pkg/ddc/juicefs/transform_fuse.go index 6020a8582de..911fbb7f2f9 100644 --- a/pkg/ddc/juicefs/transform_fuse.go +++ b/pkg/ddc/juicefs/transform_fuse.go @@ -128,7 +128,7 @@ func (j *JuiceFSEngine) transformFuseNodeSelector(runtime *datav1alpha1.JuiceFSR } // The label will be added by CSI Plugin when any workload pod is scheduled on the node. - value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name)] = "true" + value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name, j.runtimeInfo.GetNamespacedNameAlias())] = "true" } // genValue: generate the value of juicefs diff --git a/pkg/ddc/juicefs/transform_fuse_test.go b/pkg/ddc/juicefs/transform_fuse_test.go index 6bb588a5b9e..017c78f687f 100644 --- a/pkg/ddc/juicefs/transform_fuse_test.go +++ b/pkg/ddc/juicefs/transform_fuse_test.go @@ -18,6 +18,7 @@ package juicefs import ( "encoding/base64" + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "reflect" "testing" @@ -73,6 +74,10 @@ func TestTransformFuse(t *testing.T) { juicefsSecret3.DeepCopy()) client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "juicefs") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } engine := JuiceFSEngine{ name: "test", namespace: "fluid", @@ -83,6 +88,7 @@ func TestTransformFuse(t *testing.T) { Fuse: datav1alpha1.JuiceFSFuseSpec{}, }, }, + runtimeInfo: runtimeInfo, } var tests = []struct { diff --git a/pkg/ddc/juicefs/transform_test.go b/pkg/ddc/juicefs/transform_test.go index 691be064404..a954bfab666 100644 --- a/pkg/ddc/juicefs/transform_test.go +++ b/pkg/ddc/juicefs/transform_test.go @@ -17,6 +17,7 @@ limitations under the License. package juicefs import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "reflect" "testing" @@ -54,6 +55,10 @@ func TestJuiceFSEngine_transform(t *testing.T) { testObjs = append(testObjs, (*juicefsSecret).DeepCopy()) client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "juicefs") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } engine := JuiceFSEngine{ name: "test", namespace: "fluid", @@ -64,6 +69,7 @@ func TestJuiceFSEngine_transform(t *testing.T) { Fuse: datav1alpha1.JuiceFSFuseSpec{}, }, }, + runtimeInfo: runtimeInfo, } ctrl.SetLogger(zap.New(func(o *zap.Options) { o.Development = true diff --git a/pkg/ddc/thin/referencedataset/runtime.go b/pkg/ddc/thin/referencedataset/runtime.go index 40ba677e813..b4fc791978b 100644 --- a/pkg/ddc/thin/referencedataset/runtime.go +++ b/pkg/ddc/thin/referencedataset/runtime.go @@ -102,6 +102,7 @@ func (e *ReferenceDatasetEngine) getRuntimeInfo() (base.RuntimeInfoInterface, er // set exclusive mode // TODO: how to handle the exclusive mode ? e.runtimeInfo.SetupWithDataset(dataset) + e.runtimeInfo.SetNamespacedNameAlias(string(dataset.UID)) e.Log.Info("Setup with dataset done", "exclusive", e.runtimeInfo.IsExclusive()) diff --git a/pkg/ddc/thin/referencedataset/volume.go b/pkg/ddc/thin/referencedataset/volume.go index f5fd55b48c5..f9580671f91 100644 --- a/pkg/ddc/thin/referencedataset/volume.go +++ b/pkg/ddc/thin/referencedataset/volume.go @@ -171,7 +171,7 @@ func createFusePersistentVolumeClaim(client client.Client, virtualRuntime base.R Namespace: virtualNamespace, Labels: map[string]string{ // see 'pkg/util/webhook/scheduler/mutating/schedule_pod_handler.go' 'CheckIfPVCIsDataset' function usage - utils.TransferFullNamespacedNameWithPrefixToLegalValue(common.LabelAnnotationStorageCapacityPrefix, virtualNamespace, virtualName): "true", + utils.GetNamespacedNameValueWithPrefix(common.LabelAnnotationStorageCapacityPrefix, virtualNamespace, virtualName, virtualRuntime.GetNamespacedNameAlias()): "true", common.LabelAnnotationDatasetReferringName: runtimePVC.Name, common.LabelAnnotationDatasetReferringNameSpace: runtimePVC.Namespace, }, diff --git a/pkg/ddc/thin/runtime_info.go b/pkg/ddc/thin/runtime_info.go index a553f5d1993..72d73ffa4bc 100644 --- a/pkg/ddc/thin/runtime_info.go +++ b/pkg/ddc/thin/runtime_info.go @@ -62,6 +62,9 @@ func (t *ThinEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { // Setup with Dataset Info dataset, err := utils.GetDataset(t.Client, t.name, t.namespace) if err != nil { + if len(runtime.GetOwnerReferences()) > 0 { + t.runtimeInfo.SetNamespacedNameAlias(runtime.GetOwnerReferences()[0].UID) + } if utils.IgnoreNotFound(err) == nil { t.Log.Info("Dataset is notfound", "name", t.name, "namespace", t.namespace) return t.runtimeInfo, nil @@ -72,6 +75,7 @@ func (t *ThinEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { } t.runtimeInfo.SetupWithDataset(dataset) + t.runtimeInfo.SetNamespacedNameAlias(dataset.GetUID()) t.Log.Info("Setup with dataset done", "exclusive", t.runtimeInfo.IsExclusive()) } diff --git a/pkg/ddc/thin/shutdown.go b/pkg/ddc/thin/shutdown.go index 45872ea3337..7aface30f5f 100644 --- a/pkg/ddc/thin/shutdown.go +++ b/pkg/ddc/thin/shutdown.go @@ -170,7 +170,7 @@ func (t *ThinEngine) destroyWorkers(expectedWorkers int32) (currentWorkers int32 labelsToModify.Delete(label) } - exclusiveLabelValue := utils.GetExclusiveValue(t.namespace, t.name) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() if val, exist := toUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue { labelsToModify.Delete(labelExclusiveName) } diff --git a/pkg/ddc/thin/transform.go b/pkg/ddc/thin/transform.go index 671b07db304..f0d7b1df6bd 100644 --- a/pkg/ddc/thin/transform.go +++ b/pkg/ddc/thin/transform.go @@ -51,7 +51,7 @@ func (t *ThinEngine) transform(runtime *datav1alpha1.ThinRuntime, profile *datav } value.FullnameOverride = t.name - value.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", t.namespace, t.name) + value.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", t.namespace, t.name, t.runtimeInfo.GetNamespacedNameAlias()) value.Owner = transformer.GenerateOwnerReferenceFromObject(runtime) toRuntimeSetConfig, err := t.toRuntimeSetConfig(nil, nil) if err != nil { diff --git a/pkg/ddc/thin/transform_fuse.go b/pkg/ddc/thin/transform_fuse.go index 3616e042dbf..7dd70171e22 100644 --- a/pkg/ddc/thin/transform_fuse.go +++ b/pkg/ddc/thin/transform_fuse.go @@ -56,7 +56,7 @@ func (t *ThinEngine) transformFuse(runtime *datav1alpha1.ThinRuntime, profile *d if len(runtime.Spec.Fuse.NodeSelector) > 0 { value.Fuse.NodeSelector = runtime.Spec.Fuse.NodeSelector } - value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name)] = "true" + value.Fuse.NodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name, t.runtimeInfo.GetNamespacedNameAlias())] = "true" // 5. ports if len(runtime.Spec.Fuse.Ports) != 0 { diff --git a/pkg/ddc/thin/transform_fuse_test.go b/pkg/ddc/thin/transform_fuse_test.go index 6ee73e08cc3..4c41c3fb097 100644 --- a/pkg/ddc/thin/transform_fuse_test.go +++ b/pkg/ddc/thin/transform_fuse_test.go @@ -17,6 +17,7 @@ package thin import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "reflect" "strings" "testing" @@ -492,8 +493,19 @@ func TestThinEngine_transformFuse(t1 *testing.T) { }, } value := &ThinValue{} + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "thin") + if err != nil { + t1.Errorf("fail to create the runtimeInfo with error %v", err) + } t1.Run("test", func(t1 *testing.T) { - t := &ThinEngine{Log: fake.NullLogger(), namespace: "fluid", name: "test", runtime: runtime} + t := &ThinEngine{ + Log: fake.NullLogger(), + namespace: "fluid", + name: "test", + runtime: runtime, + runtimeInfo: runtimeInfo, + Client: fake.NewFakeClientWithScheme(testScheme), + } if err := t.transformFuse(runtime, profile, dataset, value); err != nil { t1.Errorf("transformFuse() error = %v", err) } @@ -712,7 +724,19 @@ func TestThinEngine_transformFuseWithDuplicateOptionKey(t1 *testing.T) { } value := &ThinValue{} t1.Run("test", func(t1 *testing.T) { - t := &ThinEngine{Log: fake.NullLogger(), namespace: "fluid", name: "test", runtime: runtime} + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "thin") + if err != nil { + t1.Errorf("fail to create the runtimeInfo with error %v", err) + } + + t := &ThinEngine{ + Log: fake.NullLogger(), + namespace: "fluid", + name: "test", + runtime: runtime, + runtimeInfo: runtimeInfo, + Client: fake.NewFakeClientWithScheme(testScheme), + } if err := t.transformFuse(runtime, profile, dataset, value); err != nil { t1.Errorf("transformFuse() error = %v", err) } diff --git a/pkg/ddc/vineyard/master_internal_test.go b/pkg/ddc/vineyard/master_internal_test.go index 4b4b7b1dfe3..3d257e1c023 100644 --- a/pkg/ddc/vineyard/master_internal_test.go +++ b/pkg/ddc/vineyard/master_internal_test.go @@ -14,6 +14,7 @@ limitations under the License. package vineyard import ( + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "testing" "github.com/brahma-adshonor/gohook" @@ -101,6 +102,11 @@ func TestSetupMasterInternal(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "vineyard", base.WithTieredStore(vineyardruntime.Spec.TieredStore)) + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := VineyardEngine{ name: "hbase", namespace: "fluid", @@ -115,6 +121,7 @@ func TestSetupMasterInternal(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } err = engine.setupMasterInternal() if err == nil { @@ -128,7 +135,7 @@ func TestSetupMasterInternal(t *testing.T) { } err = engine.setupMasterInternal() if err != nil { - t.Errorf("fail to exec check helm release") + t.Errorf("fail to exec check helm release, %v", err) } wrappedUnhookCheckRelease() @@ -167,7 +174,7 @@ func TestSetupMasterInternal(t *testing.T) { } err = engine.setupMasterInternal() if err != nil { - t.Errorf("fail to install release") + t.Errorf("fail to install release, %v", err) } wrappedUnhookInstallRelease() wrappedUnhookCheckRelease() @@ -208,6 +215,10 @@ func TestGenerateVineyardValueFile(t *testing.T) { } client := fake.NewFakeClientWithScheme(testScheme, testObjs...) + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "vineyard", base.WithTieredStore(vineyardruntime.Spec.TieredStore)) + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } engine := VineyardEngine{ name: "hbase", namespace: "fluid", @@ -222,9 +233,10 @@ func TestGenerateVineyardValueFile(t *testing.T) { }, }, }, + runtimeInfo: runtimeInfo, } - _, err := engine.generateVineyardValueFile(vineyardruntime) + _, err = engine.generateVineyardValueFile(vineyardruntime) if err != nil { t.Errorf("fail to generateVineyardValueFile %v", err) } diff --git a/pkg/ddc/vineyard/runtime_info.go b/pkg/ddc/vineyard/runtime_info.go index d145f32ea12..e4f72b56e57 100644 --- a/pkg/ddc/vineyard/runtime_info.go +++ b/pkg/ddc/vineyard/runtime_info.go @@ -14,8 +14,8 @@ limitations under the License. package vineyard import ( - "github.com/fluid-cloudnative/fluid/pkg/common" "github.com/fluid-cloudnative/fluid/pkg/ddc/base" + "github.com/fluid-cloudnative/fluid/pkg/utils" ) func (e *VineyardEngine) CheckRuntimeReady() (ready bool) { @@ -45,8 +45,20 @@ func (e *VineyardEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) { return e.runtimeInfo, err } - // Setup Fuse Deploy Mode - e.runtimeInfo.SetFuseNodeSelector(common.VineyardFuseNodeSelector) + dataset, err := utils.GetDataset(e.Client, e.name, e.namespace) + if err != nil { + if len(runtime.GetOwnerReferences()) > 0 { + e.runtimeInfo.SetNamespacedNameAlias(runtime.GetOwnerReferences()[0].UID) + } + if utils.IgnoreNotFound(err) == nil { + e.Log.Info("Dataset is notfound", "name", e.name, "namespace", e.namespace) + return e.runtimeInfo, nil + } + + e.Log.Info("Failed to get dataset when getruntimeInfo") + return e.runtimeInfo, err + } + e.runtimeInfo.SetNamespacedNameAlias(dataset.GetUID()) } return e.runtimeInfo, nil diff --git a/pkg/ddc/vineyard/shut_down.go b/pkg/ddc/vineyard/shut_down.go index 25e4797efd5..890eaca4f00 100644 --- a/pkg/ddc/vineyard/shut_down.go +++ b/pkg/ddc/vineyard/shut_down.go @@ -174,7 +174,7 @@ func (e *VineyardEngine) destroyWorkers(expectedWorkers int32) (currentWorkers i labelsToModify.Delete(label) } - exclusiveLabelValue := utils.GetExclusiveValue(e.namespace, e.name) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() if val, exist := toUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue { labelsToModify.Delete(labelExclusiveName) } diff --git a/pkg/ddc/vineyard/transform.go b/pkg/ddc/vineyard/transform.go index 3ed3db926d1..1e6b77cc1cf 100644 --- a/pkg/ddc/vineyard/transform.go +++ b/pkg/ddc/vineyard/transform.go @@ -41,7 +41,7 @@ func (e *VineyardEngine) transform(runtime *datav1alpha1.VineyardRuntime) (value Owner: transformer.GenerateOwnerReferenceFromObject(runtime), } value.FullnameOverride = e.name - value.FullNamespacedNameOverride = utils.TransferFullNamespacedNameWithPrefixToLegalValue("", e.namespace, e.name) + value.FullNamespacedNameOverride = utils.GetNamespacedNameValueWithPrefix("", e.namespace, e.name, e.runtimeInfo.GetNamespacedNameAlias()) value.TieredStore, err = e.transformTieredStore(runtime) if err != nil { return @@ -271,7 +271,7 @@ func (e *VineyardEngine) transformWorkerPorts(runtime *datav1alpha1.VineyardRunt func (e *VineyardEngine) transformFuseNodeSelector(runtime *datav1alpha1.VineyardRuntime) map[string]string { nodeSelector := map[string]string{} - nodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name)] = "true" + nodeSelector[utils.GetFuseLabelName(runtime.Namespace, runtime.Name, e.runtimeInfo.GetNamespacedNameAlias())] = "true" return nodeSelector } diff --git a/pkg/ddc/vineyard/transform_test.go b/pkg/ddc/vineyard/transform_test.go index d9ec7460109..9aba5c3bfbd 100755 --- a/pkg/ddc/vineyard/transform_test.go +++ b/pkg/ddc/vineyard/transform_test.go @@ -84,7 +84,13 @@ func TestTransformFuse(t *testing.T) { if strings.Contains(k, "env") { os.Setenv("VINEYARD_FUSE_IMAGE_ENV", "image-from-env:image-tag-from-env") } - engine := &VineyardEngine{} + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "vineyard") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } + engine := &VineyardEngine{ + runtimeInfo: runtimeInfo, + } engine.Log = ctrl.Log engine.transformFuse(test.runtime, test.value) @@ -623,9 +629,14 @@ func TestTransformFuseNodeSelector(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "vineyard") + if err != nil { + t.Errorf("fail to create the runtimeInfo with error %v", err) + } engine := &VineyardEngine{ - name: "vineyard", - namespace: "fluid", + name: "vineyard", + namespace: "fluid", + runtimeInfo: runtimeInfo, } actual := engine.transformFuseNodeSelector(tt.runtime) if !reflect.DeepEqual(actual, tt.expected) { diff --git a/pkg/utils/dataset/lifecycle/node.go b/pkg/utils/dataset/lifecycle/node.go index bf8a0d9ebaf..dae5c772b05 100644 --- a/pkg/utils/dataset/lifecycle/node.go +++ b/pkg/utils/dataset/lifecycle/node.go @@ -241,7 +241,7 @@ func labelCacheNode(nodeToLabel corev1.Node, runtimeInfo base.RuntimeInfoInterfa labelsToModify.Add(commonLabel, "true") if exclusiveness { - exclusiveLabelValue := utils.GetExclusiveValue(runtimeInfo.GetNamespace(), runtimeInfo.GetName()) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() labelsToModify.Add(exclusiveLabel, exclusiveLabelValue) } @@ -359,7 +359,7 @@ func unlabelCacheNode(node corev1.Node, runtimeInfo base.RuntimeInfoInterface, c labelsToModify.Delete(label) } - exclusiveLabelValue := utils.GetExclusiveValue(runtimeInfo.GetNamespace(), runtimeInfo.GetName()) + exclusiveLabelValue := runtimeInfo.GetExclusiveLabelValue() if val, exist := nodeToUpdate.Labels[labelExclusiveName]; exist && val == exclusiveLabelValue { labelsToModify.Delete(labelExclusiveName) } diff --git a/pkg/utils/dataset/volume/get.go b/pkg/utils/dataset/volume/get.go index a3cf8249f76..b0282f7bb67 100644 --- a/pkg/utils/dataset/volume/get.go +++ b/pkg/utils/dataset/volume/get.go @@ -35,12 +35,12 @@ func GetNamespacedNameByVolumeId(client client.Reader, volumeId string) (namespa namespace = pv.Spec.ClaimRef.Namespace name = pv.Spec.ClaimRef.Name - ok, err := kubeclient.IsDatasetPVC(client, name, namespace) + pvc, err := kubeclient.GetPersistentVolumeClaim(client, name, namespace) if err != nil { return "", "", err } - if !ok { + if !kubeclient.CheckIfPVCIsDataset(pvc) { return "", "", errors.Errorf("pv %s is not bounded with a fluid pvc", volumeId) } diff --git a/pkg/utils/excluisve.go b/pkg/utils/excluisve.go deleted file mode 100644 index 7dfbb06ca6c..00000000000 --- a/pkg/utils/excluisve.go +++ /dev/null @@ -1,40 +0,0 @@ -/* -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 utils - -import ( - "fmt" - - "github.com/fluid-cloudnative/fluid/pkg/common" -) - -// GetExclusiveKey gets exclusive key -func GetExclusiveKey() string { - return common.FluidExclusiveKey -} - -// GetExclusiveValue gets exclusive value -func GetExclusiveValue(namespace, name string) string { - exclusiveValue := fmt.Sprintf("%s_%s", namespace, name) - - // ensure forward compatibility - if len(exclusiveValue) <= 63 { - return exclusiveValue - } - - return TransferFullNamespacedNameWithPrefixToLegalValue("", namespace, name) -} diff --git a/pkg/utils/excluisve_test.go b/pkg/utils/excluisve_test.go deleted file mode 100644 index db0f1ac8269..00000000000 --- a/pkg/utils/excluisve_test.go +++ /dev/null @@ -1,78 +0,0 @@ -/* -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 utils - -import ( - "testing" - - "github.com/fluid-cloudnative/fluid/pkg/common" -) - -func TestGetExclusiveKey(t *testing.T) { - tests := []struct { - name string - want string - }{ - { - name: "test for GetExclusiveKey", - want: common.FluidExclusiveKey, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := GetExclusiveKey(); got != tt.want { - t.Errorf("GetExclusiveKey() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetExclusiveValue(t *testing.T) { - type args struct { - namespace string - name string - } - tests := []struct { - name string - args args - want string - }{ - { - name: "default test-dataset-1", - args: args{ - name: "test-dataset-1", - namespace: "default", - }, - want: "default_test-dataset-1", - }, - { - name: "otherns test-dataset-2", - args: args{ - name: "test-dataset-2", - namespace: "otherns", - }, - want: "otherns_test-dataset-2", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := GetExclusiveValue(tt.args.namespace, tt.args.name); got != tt.want { - t.Errorf("GetExclusiveValue() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/pkg/utils/hash.go b/pkg/utils/hash.go deleted file mode 100644 index 16536837675..00000000000 --- a/pkg/utils/hash.go +++ /dev/null @@ -1,80 +0,0 @@ -/* -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 utils - -import ( - "bytes" - "encoding/json" - "fmt" - "github.com/pkg/errors" - "hash/fnv" - "io" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/rand" - "strings" -) - -func ComputeHash(object interface{}) (string, error) { - objString, err := json.Marshal(object) - if err != nil { - return "", errors.Wrap(err, "failed to compute hash.") - } - - // hasher := sha1.New() - hasher := fnv.New32() - if _, err := io.Copy(hasher, bytes.NewReader(objString)); err != nil { - return "", errors.Wrapf(err, "failed to compute hash for sha256. [%s]", objString) - } - - sha := hasher.Sum32() - return rand.SafeEncodeString(fmt.Sprint(sha)), nil -} - -func ComputeFullNamespacedNameHashValue(namespace, name string) (string, error) { - return ComputeHash(v1.ObjectMeta{ - Namespace: namespace, - Name: name, - }) -} - -// TransferFullNamespacedNameWithPrefixToLegalValue Transfer a fully namespaced name with a prefix to a legal value which under max length limit. -// If the full namespaced name exceeds 63 characters, it calculates the hash value of the name and truncates the name and namespace, -// then appends the hash value to ensure the name's uniqueness and length constraint. -func TransferFullNamespacedNameWithPrefixToLegalValue(prefix, namespace, name string) (fullNamespacedName string) { - fullNamespacedName = fmt.Sprintf("%s%s-%s", prefix, namespace, name) - - // ensure forward compatibility - if len(fullNamespacedName) < 63 { - return - } - - namespacedNameHashValue, err := ComputeFullNamespacedNameHashValue(namespace, name) - if err != nil { - log.Error(err, "fail to compute hash value for namespacedName, and fall back to the original value which will cause the failure of resource creation.") - return - } - trimMetadata := func(s string) string { - s = strings.ReplaceAll(s, "-", "") - if len(s) <= 8 { - return s - } - return s[:8] - } - fullNamespacedName = fmt.Sprintf("%s%s-%s-%s", prefix, trimMetadata(namespace), trimMetadata(name), namespacedNameHashValue) - - return -} diff --git a/pkg/utils/hash_test.go b/pkg/utils/hash_test.go deleted file mode 100644 index 6a85303e544..00000000000 --- a/pkg/utils/hash_test.go +++ /dev/null @@ -1,66 +0,0 @@ -/* -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 utils - -import "testing" - -func TestComputeHash(t *testing.T) { - type args struct { - object interface{} - } - tests := []struct { - name string - args args - want string - wantErr bool - }{{ - "test1", - args{ - map[string]string{ - "abdc": "bcde", - }, - }, - "58c7f7c8b5", - false, - }, { - "empty_test", - args{ - map[string]string{}, - }, - "5894b84845", - false, - }, { - "nil_test", - args{ - nil, - }, - "cd856cb98", - false, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := ComputeHash(tt.args.object) - if (err != nil) != tt.wantErr { - t.Errorf("ComputeHash() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("ComputeHash() got = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/pkg/utils/kubeclient/volume.go b/pkg/utils/kubeclient/volume.go index cece944670b..31a5e603682 100644 --- a/pkg/utils/kubeclient/volume.go +++ b/pkg/utils/kubeclient/volume.go @@ -19,6 +19,7 @@ package kubeclient import ( "context" "fmt" + "strings" "time" "github.com/fluid-cloudnative/fluid/pkg/common" @@ -345,31 +346,18 @@ func ShouldRemoveProtectionFinalizer(client client.Client, name, namespace strin return } -// IsDatasetPVC check whether the PVC is a dataset PVC -func IsDatasetPVC(client client.Reader, name string, namespace string) (find bool, err error) { - pvc := &corev1.PersistentVolumeClaim{} - err = client.Get(context.TODO(), types.NamespacedName{ - Namespace: namespace, - Name: name, - }, pvc) - if err != nil { - return - } - _, find = pvc.Labels[utils.TransferFullNamespacedNameWithPrefixToLegalValue(common.LabelAnnotationStorageCapacityPrefix, namespace, name)] - return -} - // CheckIfPVCIsDataset checks if the pvc is dataset func CheckIfPVCIsDataset(pvc *corev1.PersistentVolumeClaim) (isDataset bool) { - if pvc == nil { + if pvc == nil || pvc.Labels == nil { return } - name := pvc.GetName() - namespace := pvc.GetNamespace() - if len(namespace) == 0 { - namespace = corev1.NamespaceDefault + + for labelKey := range pvc.Labels { + if strings.HasPrefix(labelKey, common.LabelAnnotationStorageCapacityPrefix) { + isDataset = true + break + } } - _, isDataset = pvc.Labels[utils.TransferFullNamespacedNameWithPrefixToLegalValue(common.LabelAnnotationStorageCapacityPrefix, namespace, name)] if _, exists := common.GetManagerDatasetFromLabels(pvc.Labels); exists { isDataset = true diff --git a/pkg/utils/kubeclient/volume_claim.go b/pkg/utils/kubeclient/volume_claim.go index bede75ddb72..ab7b9c77bc1 100644 --- a/pkg/utils/kubeclient/volume_claim.go +++ b/pkg/utils/kubeclient/volume_claim.go @@ -27,7 +27,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -func GetPersistentVolumeClaim(client client.Client, name, namespace string) (pvc *v1.PersistentVolumeClaim, err error) { +func GetPersistentVolumeClaim(client client.Reader, name, namespace string) (pvc *v1.PersistentVolumeClaim, err error) { pvc = &v1.PersistentVolumeClaim{} err = client.Get(context.TODO(), types.NamespacedName{ diff --git a/pkg/utils/kubeclient/volume_test.go b/pkg/utils/kubeclient/volume_test.go index 61a6ad66bad..6eaaca38721 100644 --- a/pkg/utils/kubeclient/volume_test.go +++ b/pkg/utils/kubeclient/volume_test.go @@ -574,80 +574,6 @@ func TestShouldRemoveProtectionFinalizer(t *testing.T) { } } -func TestIsDatasetPVC(t *testing.T) { - - namespace := "default" - datasetName := "createdByFluid" - expectFluidAnnotations := common.ExpectedFluidAnnotations - expectFluidAnnotations[common.LabelAnnotationStorageCapacityPrefix+namespace+"-"+datasetName] = "true" - testPVCInputs := []*v1.PersistentVolumeClaim{{ - ObjectMeta: metav1.ObjectMeta{Name: "notCreatedByFluid", - Namespace: namespace}, - Spec: v1.PersistentVolumeClaimSpec{}, - }, { - ObjectMeta: metav1.ObjectMeta{Name: "createdByFluid", - Labels: expectFluidAnnotations, - Namespace: namespace}, - Spec: v1.PersistentVolumeClaimSpec{}, - }} - - testPVCs := []runtime.Object{} - - for _, pvc := range testPVCInputs { - testPVCs = append(testPVCs, pvc.DeepCopy()) - } - - client := fake.NewFakeClientWithScheme(testScheme, testPVCs...) - - type args struct { - name string - namespace string - } - tests := []struct { - name string - args args - found bool - }{ - { - name: "volume for dataset doesn't exist", - args: args{ - name: "notExist", - namespace: namespace, - }, - found: false, - }, - { - name: "volume notCreatedByFluid is not created by fluid", - args: args{ - name: "notCreatedByFluid", - namespace: namespace, - }, - found: false, - }, - { - name: "volume is created by fluid", - args: args{ - name: datasetName, - namespace: namespace, - }, - found: true, - }, { - name: "volume is not created by fluid 2", - args: args{ - name: "notCreatedByFluid2", - }, - found: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if found, _ := IsDatasetPVC(client, tt.args.name, tt.args.namespace); found != tt.found { - t.Errorf("testcase %v IsDatasetPVC() = %v, want %v", tt.name, found, tt.found) - } - }) - } -} - func TestGetReferringDatasetPVCInfo(t *testing.T) { type args struct { pvc *v1.PersistentVolumeClaim diff --git a/pkg/utils/label.go b/pkg/utils/label.go index b7919f5ae45..f87ceb848eb 100644 --- a/pkg/utils/label.go +++ b/pkg/utils/label.go @@ -20,12 +20,13 @@ import ( "context" "encoding/json" "fmt" - "github.com/fluid-cloudnative/fluid/pkg/common" "github.com/fluid-cloudnative/fluid/pkg/common/deprecated" "github.com/pkg/errors" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/validation" + "runtime" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -114,7 +115,7 @@ func ChangeNodeLabelWithPatchMode(cli client.Client, node *v1.Node, labelsToModi return PatchLabels(cli, node, labelsToModify) } -func GetStorageLabelName(read common.ReadType, storage common.StorageType, isDeprecated bool, runtimeType string, namespace string, name string) string { +func GetStorageLabelName(read common.ReadType, storage common.StorageType, isDeprecated bool, runtimeType string, namespace, name, overLimitNamespacedNameAlias string) string { prefix := common.LabelAnnotationStorageCapacityPrefix if isDeprecated { prefix = deprecated.LabelAnnotationStorageCapacityPrefix @@ -122,49 +123,49 @@ func GetStorageLabelName(read common.ReadType, storage common.StorageType, isDep prefix = prefix + string(read) + runtimeType + "-" + string(storage) - return TransferFullNamespacedNameWithPrefixToLegalValue(prefix, namespace, name) + return GetNamespacedNameValueWithPrefix(prefix, namespace, name, overLimitNamespacedNameAlias) } -func GetLabelNameForMemory(isDeprecated bool, runtimeType string, namespace string, name string) string { +func GetLabelNameForMemory(isDeprecated bool, runtimeType string, namespace, name, overLimitNamespacedNameAlias string) string { read := common.HumanReadType storage := common.MemoryStorageType if isDeprecated { read = deprecated.HumanReadType storage = deprecated.MemoryStorageType } - return GetStorageLabelName(read, storage, isDeprecated, runtimeType, namespace, name) + return GetStorageLabelName(read, storage, isDeprecated, runtimeType, namespace, name, overLimitNamespacedNameAlias) } -func GetLabelNameForDisk(isDeprecated bool, runtimeType string, namespace string, name string) string { +func GetLabelNameForDisk(isDeprecated bool, runtimeType string, namespace, name, overLimitNamespacedNameAlias string) string { read := common.HumanReadType storage := common.DiskStorageType if isDeprecated { read = deprecated.HumanReadType storage = deprecated.DiskStorageType } - return GetStorageLabelName(read, storage, isDeprecated, runtimeType, namespace, name) + return GetStorageLabelName(read, storage, isDeprecated, runtimeType, namespace, name, overLimitNamespacedNameAlias) } -func GetLabelNameForTotal(isDeprecated bool, runtimeType string, namespace string, name string) string { +func GetLabelNameForTotal(isDeprecated bool, runtimeType string, namespace, name, overLimitNamespacedNameAlias string) string { read := common.HumanReadType storage := common.TotalStorageType if isDeprecated { read = deprecated.HumanReadType storage = deprecated.TotalStorageType } - return GetStorageLabelName(read, storage, isDeprecated, runtimeType, namespace, name) + return GetStorageLabelName(read, storage, isDeprecated, runtimeType, namespace, name, overLimitNamespacedNameAlias) } -func GetCommonLabelName(isDeprecated bool, namespace string, name string) string { +func GetCommonLabelName(isDeprecated bool, namespace, name, overLimitNamespacedNameAlias string) string { prefix := common.LabelAnnotationStorageCapacityPrefix if isDeprecated { prefix = deprecated.LabelAnnotationStorageCapacityPrefix } - return TransferFullNamespacedNameWithPrefixToLegalValue(prefix, namespace, name) + return GetNamespacedNameValueWithPrefix(prefix, namespace, name, overLimitNamespacedNameAlias) } -func GetRuntimeLabelName(isDeprecated bool, runtimeType string, namespace string, name string) string { +func GetRuntimeLabelName(isDeprecated bool, runtimeType string, namespace, name, overLimitNamespacedNameAlias string) string { prefix := common.LabelAnnotationStorageCapacityPrefix if isDeprecated { prefix = deprecated.LabelAnnotationStorageCapacityPrefix @@ -172,9 +173,37 @@ func GetRuntimeLabelName(isDeprecated bool, runtimeType string, namespace string prefix = prefix + runtimeType + "-" - return TransferFullNamespacedNameWithPrefixToLegalValue(prefix, namespace, name) + return GetNamespacedNameValueWithPrefix(prefix, namespace, name, overLimitNamespacedNameAlias) +} + +func GetFuseLabelName(namespace, name, overLimitNamespacedNameAlias string) string { + return GetNamespacedNameValueWithPrefix(common.LabelAnnotationFusePrefix, namespace, name, overLimitNamespacedNameAlias) +} + +func GetExclusiveKey() string { + return common.FluidExclusiveKey } -func GetFuseLabelName(namespace, name string) string { - return TransferFullNamespacedNameWithPrefixToLegalValue(common.LabelAnnotationFusePrefix, namespace, name) +// GetNamespacedNameValueWithPrefix Transfer a fully namespaced name with a prefix to a legal value which under max length limit. +// If the full namespaced name exceeds 63 characters, it calculates the hash value of the name and truncates the name and namespace, +// then appends the hash value to ensure the name's uniqueness and length constraint. +func GetNamespacedNameValueWithPrefix(prefix, namespace, name, overLimitNamespacedNameAlias string) (fullNamespacedNameWithPrefix string) { + namespacedName := fmt.Sprintf("%s-%s", namespace, name) + fullNamespacedNameWithPrefix = fmt.Sprintf("%s%s", prefix, namespacedName) + // ensure forward compatibility + if len(fullNamespacedNameWithPrefix) < validation.DNS1035LabelMaxLength { + return + } + + if overLimitNamespacedNameAlias == "" { + buf := make([]byte, 1024) + n := runtime.Stack(buf, false) + fmt.Printf("=== Stack Trace ===\n%s\n", buf[:n]) + log.Info("The overLimitNamespacedNameAlias is absent, fall back to original value which causes the resource creation failed by scheme validation", "key", fmt.Sprintf("%s-%s", namespace, name)) + return fullNamespacedNameWithPrefix + } + + fullNamespacedNameWithPrefix = fmt.Sprintf("%s%s", prefix, overLimitNamespacedNameAlias) + + return } diff --git a/pkg/utils/label_test.go b/pkg/utils/label_test.go index f104b597337..69abf139cae 100644 --- a/pkg/utils/label_test.go +++ b/pkg/utils/label_test.go @@ -184,18 +184,18 @@ func TestChangeNodeLabelWithPatchModel(t *testing.T) { func TestGetFullNamespacedNameWithPrefixValue(t *testing.T) { tests := []struct { - prefix, namespace, name string - expected string + prefix, namespace, name, overLimitNamespacedNameAlias string + expected string }{ - {"normal-", "default", "test-dataset", "normal-default-test-dataset"}, - {"overlimit-", "namespace-ajsdjikebnfacdsvwcaxqcackjascnbaksjcnakjscnackjasn", "dataset-demo", "overlimit-namespac-datasetd-58df5bd9cc"}, - {"overlimit-", "namespace-demo", "dataset-ajsdjikebnfacdsvwcaxqcackjascnbaksjcnakjscnackjasn", "overlimit-namespac-dataseta-6dfd85695"}, + {"normal-", "default", "test-dataset", "", "normal-default-test-dataset"}, + {"overlimit-", "namespace-ajsdjikebnfacdsvwcaxqcackjascnbaksjcnakjscnackjasn", "dataset-demo", "58df5bd9cc", "overlimit-58df5bd9cc"}, + {"overlimit-", "namespace-demo", "dataset-ajsdjikebnfacdsvwcaxqcackjascnbaksjcnakjscnackjasn", "6dfd85695", "overlimit-6dfd85695"}, } for _, test := range tests { - result := TransferFullNamespacedNameWithPrefixToLegalValue(test.prefix, test.namespace, test.name) + result := GetNamespacedNameValueWithPrefix(test.prefix, test.namespace, test.name, test.overLimitNamespacedNameAlias) if result != test.expected { - t.Errorf("TransferFullNamespacedNameWithPrefixToLegalValue(%v) = %v, want %v", test, result, test.expected) + t.Errorf("GetNamespacedNameValueWithPrefix(%v) = %v, want %v", test, result, test.expected) } } }