@@ -17,19 +17,23 @@ limitations under the License.
1717package goosefs
1818
1919import (
20+ "fmt"
21+
22+ datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
2023 "github.com/fluid-cloudnative/fluid/pkg/ddc/base"
2124 "github.com/fluid-cloudnative/fluid/pkg/utils"
2225 "github.com/fluid-cloudnative/fluid/pkg/utils/dataset/volume"
26+ "k8s.io/apimachinery/pkg/util/validation/field"
2327)
2428
2529// getRuntimeInfo gets runtime info
2630func (e * GooseFSEngine ) getRuntimeInfo () (base.RuntimeInfoInterface , error ) {
27- if e .runtimeInfo == nil {
28- runtime , err := e .getRuntime ()
29- if err != nil {
30- return e .runtimeInfo , err
31- }
31+ runtime , err := e .getRuntime ()
32+ if err != nil {
33+ return e .runtimeInfo , err
34+ }
3235
36+ if e .runtimeInfo == nil {
3337 opts := []base.RuntimeInfoOption {
3438 base .WithTieredStore (runtime .Spec .TieredStore ),
3539 base .WithMetadataList (base .GetMetadataListFromAnnotation (runtime )),
@@ -45,26 +49,6 @@ func (e *GooseFSEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) {
4549 e .runtimeInfo .SetFuseNodeSelector (runtime .Spec .Fuse .NodeSelector )
4650
4751 if ! e .UnitTest {
48- // Setup with Dataset Info
49- dataset , err := utils .GetDataset (e .Client , e .name , e .namespace )
50- if err != nil {
51- if len (runtime .GetOwnerReferences ()) > 0 {
52- e .runtimeInfo .SetOwnerDatasetUID (runtime .GetOwnerReferences ()[0 ].UID )
53- }
54- if utils .IgnoreNotFound (err ) == nil {
55- e .Log .Info ("Dataset is notfound" , "name" , e .name , "namespace" , e .namespace )
56- return e .runtimeInfo , nil
57- }
58-
59- e .Log .Info ("Failed to get dataset when getruntimeInfo" )
60- return e .runtimeInfo , err
61- }
62-
63- e .runtimeInfo .SetupWithDataset (dataset )
64- e .Log .Info ("Setup with dataset done" , "exclusive" , e .runtimeInfo .IsExclusive ())
65-
66- e .runtimeInfo .SetOwnerDatasetUID (dataset .UID )
67-
6852 // Check if the runtime is using deprecated labels
6953 isLabelDeprecated , err := e .HasDeprecatedCommonLabelname ()
7054 if err != nil {
@@ -84,5 +68,36 @@ func (e *GooseFSEngine) getRuntimeInfo() (base.RuntimeInfoInterface, error) {
8468 }
8569 }
8670
71+ // Handling information of bound dataset. XXXEngine.getRuntimeInfo() might be called before the runtime is bound to a dataset,
72+ // so here we must lazily set dataset-related information once we found there's one bound dataset.
73+ if len (e .runtimeInfo .GetOwnerDatasetUID ()) == 0 {
74+ owners := runtime .GetOwnerReferences ()
75+ if len (owners ) > 0 {
76+ firstOwner := owners [0 ]
77+ firstOwnerPath := field .NewPath ("metadata" ).Child ("ownerReferences" ).Index (0 )
78+ if firstOwner .Kind != datav1alpha1 .Datasetkind {
79+ return nil , fmt .Errorf ("first owner of the runtime (%s) has invalid Kind \" %s\" , expected to be %s " , firstOwnerPath .String (), firstOwner .Kind , datav1alpha1 .Datasetkind )
80+ }
81+
82+ if firstOwner .Name != runtime .GetName () {
83+ return nil , fmt .Errorf ("first owner of the runtime (%s) has different name with runtime, expected to be same" , firstOwnerPath .String ())
84+ }
85+
86+ e .runtimeInfo .SetOwnerDatasetUID (firstOwner .UID )
87+ }
88+ }
89+
90+ exclusiveModePtr := e .runtimeInfo .IsExclusive ()
91+ if exclusiveModePtr == nil {
92+ dataset , err := utils .GetDataset (e .Client , e .name , e .namespace )
93+ if utils .IgnoreNotFound (err ) != nil {
94+ return nil , err
95+ }
96+
97+ if dataset != nil {
98+ e .runtimeInfo .SetupWithDataset (dataset )
99+ }
100+ }
101+
87102 return e .runtimeInfo , nil
88103}
0 commit comments