fix(kubernetes): avoid a deadlock EE-6901 (#11447)

pull/11464/head
andres-portainer 2024-03-25 14:19:28 -03:00 committed by GitHub
parent 08643ed872
commit 2443a0f568
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 11 deletions

View File

@ -81,21 +81,30 @@ func (factory *ClientFactory) RemoveKubeClient(endpointID portainer.EndpointID)
// If no client is registered, it will create a new client, register it, and returns it. // If no client is registered, it will create a new client, register it, and returns it.
func (factory *ClientFactory) GetKubeClient(endpoint *portainer.Endpoint) (*KubeClient, error) { func (factory *ClientFactory) GetKubeClient(endpoint *portainer.Endpoint) (*KubeClient, error) {
factory.mu.Lock() factory.mu.Lock()
defer factory.mu.Unlock()
key := strconv.Itoa(int(endpoint.ID)) key := strconv.Itoa(int(endpoint.ID))
client, ok := factory.endpointClients[key] if client, ok := factory.endpointClients[key]; ok {
if !ok { factory.mu.Unlock()
var err error return client, nil
}
factory.mu.Unlock()
client, err = factory.createCachedAdminKubeClient(endpoint) // EE-6901: Do not lock
client, err := factory.createCachedAdminKubeClient(endpoint)
if err != nil { if err != nil {
return nil, err return nil, err
} }
factory.endpointClients[key] = client factory.mu.Lock()
defer factory.mu.Unlock()
// The lock was released before the client was created,
// so we need to check again
if c, ok := factory.endpointClients[key]; ok {
return c, nil
} }
factory.endpointClients[key] = client
return client, nil return client, nil
} }