-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Delay CSI client initialization #74652
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"io" | ||
"net" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
csipbv1 "github.com/container-storage-interface/spec/lib/go/csi" | ||
|
@@ -807,3 +808,36 @@ func versionRequiresV0Client(version *utilversion.Version) bool { | |
|
||
return false | ||
} | ||
|
||
// CSI client getter with cache. | ||
// This provides a method to initialize CSI client with driver name and caches | ||
// it for later use. When CSI clients have not been discovered yet (e.g. | ||
// on kubelet restart), client initialization will fail. Users of CSI client (e.g. | ||
// mounter manager and block mapper) can use this to delay CSI client | ||
// initialization until needed. | ||
type csiClientGetter struct { | ||
sync.RWMutex | ||
csiClient csiClient | ||
driverName csiDriverName | ||
} | ||
|
||
func (c *csiClientGetter) Get() (csiClient, error) { | ||
c.RLock() | ||
if c.csiClient != nil { | ||
c.RUnlock() | ||
return c.csiClient, nil | ||
} | ||
c.RUnlock() | ||
c.Lock() | ||
defer c.Unlock() | ||
// Double-checking locking criterion. | ||
if c.csiClient != nil { | ||
return c.csiClient, nil | ||
} | ||
csi, err := newCsiDriverClient(c.driverName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can c.csiClient be assigned directly (we have the write lock) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is one difference, with the second style |
||
if err != nil { | ||
return nil, err | ||
} | ||
c.csiClient = csi | ||
return c.csiClient, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to add some detailed comments on this struct, why it is needed and how it is used. The fix uses a quite subtle approach so I think adding comments will help others understand and might use it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That will be better. I've added some comments.