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