@@ -18,6 +18,7 @@ package shareadapters
18
18
19
19
import (
20
20
"fmt"
21
+ "strings"
21
22
"time"
22
23
23
24
"github.com/gophercloud/gophercloud/v2"
@@ -31,82 +32,81 @@ type Cephfs struct{}
31
32
32
33
var _ ShareAdapter = & Cephfs {}
33
34
34
- func (Cephfs ) GetOrGrantAccess (args * GrantAccessArgs ) (accessRight * shares.AccessRight , err error ) {
35
+ func (Cephfs ) GetOrGrantAccesses (args * GrantAccessArgs ) ([] shares.AccessRight , error ) {
35
36
// First, check if the access right exists or needs to be created
36
37
37
- var rights []shares.AccessRight
38
-
39
- accessTo := args .Options .CephfsClientID
40
- if accessTo == "" {
41
- accessTo = args .Share .Name
42
- }
43
-
44
- rights , err = args .ManilaClient .GetAccessRights (args .Share .ID )
38
+ rights , err := args .ManilaClient .GetAccessRights (args .Share .ID )
45
39
if err != nil {
46
40
if _ , ok := err .(gophercloud.ErrResourceNotFound ); ! ok {
47
41
return nil , fmt .Errorf ("failed to list access rights: %v" , err )
48
42
}
49
- } else {
50
- // Try to find the access right
43
+ }
44
+
45
+ accessToList := []string {args .Share .Name }
46
+ if args .Options .CephfsClientID != "" {
47
+ accessToList = strings .Split (args .Options .CephfsClientID , "," )
48
+ }
51
49
50
+ created := false
51
+ for _ , at := range accessToList {
52
+ // Try to find the access right
53
+ found := false
52
54
for _ , r := range rights {
53
- if r .AccessTo == accessTo && r .AccessType == "cephx" && r .AccessLevel == "rw" {
55
+ if r .AccessTo == at && r .AccessType == "cephx" && r .AccessLevel == "rw" {
54
56
klog .V (4 ).Infof ("cephx access right for share %s already exists" , args .Share .Name )
55
-
56
- accessRight = & r
57
+ found = true
57
58
break
58
59
}
59
60
}
60
- }
61
61
62
- if accessRight == nil {
63
62
// Not found, create it
64
-
65
- accessRight , err = args .ManilaClient .GrantAccess (args .Share .ID , shares.GrantAccessOpts {
66
- AccessType : "cephx" ,
67
- AccessLevel : "rw" ,
68
- AccessTo : accessTo ,
69
- })
70
-
71
- if err != nil {
72
- return
63
+ if ! found {
64
+ result , err := args .ManilaClient .GrantAccess (args .Share .ID , shares.GrantAccessOpts {
65
+ AccessType : "cephx" ,
66
+ AccessLevel : "rw" ,
67
+ AccessTo : at ,
68
+ })
69
+ if err != nil {
70
+ return nil , fmt .Errorf ("failed to grant access right: %v" , err )
71
+ }
72
+ if result .AccessKey == "" {
73
+ // Wait till a ceph key is assigned to the access right
74
+ backoff := wait.Backoff {
75
+ Duration : time .Second * 5 ,
76
+ Factor : 1.2 ,
77
+ Steps : 10 ,
78
+ }
79
+ wait .ExponentialBackoff (backoff , func () (bool , error ) {
80
+ rights , err := args .ManilaClient .GetAccessRights (args .Share .ID )
81
+ if err != nil {
82
+ return false , fmt .Errorf ("error get access rights for share %s: %v" , args .Share .ID , err )
83
+ }
84
+ if len (rights ) == 0 {
85
+ return false , fmt .Errorf ("cannot find the access right we've just created" )
86
+ }
87
+ for _ , r := range rights {
88
+ if r .AccessTo == at && r .AccessKey != "" {
89
+ return true , nil
90
+ }
91
+ }
92
+ klog .V (4 ).Infof ("Access key for %s is not set yet, retrying..." , at )
93
+ return false , nil
94
+ })
95
+ }
96
+ created = true
73
97
}
74
98
}
75
99
76
- if accessRight .AccessKey != "" {
77
- // The access right is ready
78
- return
79
- }
80
-
81
- // Wait till a ceph key is assigned to the access right
82
-
83
- backoff := wait.Backoff {
84
- Duration : time .Second * 5 ,
85
- Factor : 1.2 ,
86
- Steps : 10 ,
87
- }
88
-
89
- return accessRight , wait .ExponentialBackoff (backoff , func () (bool , error ) {
90
- rights , err := args .ManilaClient .GetAccessRights (args .Share .ID )
100
+ // Search again because access rights have changed
101
+ if created {
102
+ rights , err = args .ManilaClient .GetAccessRights (args .Share .ID )
91
103
if err != nil {
92
- return false , err
93
- }
94
-
95
- var accessRight * shares.AccessRight
96
-
97
- for i := range rights {
98
- if rights [i ].AccessTo == accessTo {
99
- accessRight = & rights [i ]
100
- break
104
+ if _ , ok := err .(gophercloud.ErrResourceNotFound ); ! ok {
105
+ return nil , fmt .Errorf ("failed to list access rights: %v" , err )
101
106
}
102
107
}
103
-
104
- if accessRight == nil {
105
- return false , fmt .Errorf ("cannot find the access right we've just created" )
106
- }
107
-
108
- return accessRight .AccessKey != "" , nil
109
- })
108
+ }
109
+ return rights , nil
110
110
}
111
111
112
112
func (Cephfs ) BuildVolumeContext (args * VolumeContextArgs ) (volumeContext map [string ]string , err error ) {
0 commit comments