mirror of
https://github.com/actions/actions-runner-controller.git
synced 2025-12-17 15:44:55 +00:00
Add Identifier to actions.Client (#2237)
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
@@ -19,7 +18,7 @@ type MultiClient interface {
|
||||
type multiClient struct {
|
||||
// To lock adding and removing of individual clients.
|
||||
mu sync.Mutex
|
||||
clients map[ActionsClientKey]*actionsClientWrapper
|
||||
clients map[ActionsClientKey]*Client
|
||||
|
||||
logger logr.Logger
|
||||
userAgent string
|
||||
@@ -40,22 +39,14 @@ type ActionsAuth struct {
|
||||
}
|
||||
|
||||
type ActionsClientKey struct {
|
||||
ActionsURL string
|
||||
Auth ActionsAuth
|
||||
Identifier string
|
||||
Namespace string
|
||||
}
|
||||
|
||||
type actionsClientWrapper struct {
|
||||
// To lock client usage when tokens are being refreshed.
|
||||
mu sync.Mutex
|
||||
|
||||
client ActionsService
|
||||
}
|
||||
|
||||
func NewMultiClient(userAgent string, logger logr.Logger) MultiClient {
|
||||
return &multiClient{
|
||||
mu: sync.Mutex{},
|
||||
clients: make(map[ActionsClientKey]*actionsClientWrapper),
|
||||
clients: make(map[ActionsClientKey]*Client),
|
||||
logger: logger,
|
||||
userAgent: userAgent,
|
||||
}
|
||||
@@ -64,11 +55,6 @@ func NewMultiClient(userAgent string, logger logr.Logger) MultiClient {
|
||||
func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string, creds ActionsAuth, namespace string) (ActionsService, error) {
|
||||
m.logger.Info("retrieve actions client", "githubConfigURL", githubConfigURL, "namespace", namespace)
|
||||
|
||||
parsedGitHubURL, err := url.Parse(githubConfigURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if creds.Token == "" && creds.AppCreds == nil {
|
||||
return nil, fmt.Errorf("no credentials provided. either a PAT or GitHub App credentials should be provided")
|
||||
}
|
||||
@@ -77,34 +63,6 @@ func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string,
|
||||
return nil, fmt.Errorf("both PAT and GitHub App credentials provided. should only provide one")
|
||||
}
|
||||
|
||||
key := ActionsClientKey{
|
||||
ActionsURL: parsedGitHubURL.String(),
|
||||
Namespace: namespace,
|
||||
}
|
||||
|
||||
if creds.AppCreds != nil {
|
||||
key.Auth = ActionsAuth{
|
||||
AppCreds: creds.AppCreds,
|
||||
}
|
||||
}
|
||||
|
||||
if creds.Token != "" {
|
||||
key.Auth = ActionsAuth{
|
||||
Token: creds.Token,
|
||||
}
|
||||
}
|
||||
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
clientWrapper, has := m.clients[key]
|
||||
if has {
|
||||
m.logger.Info("using cache client", "githubConfigURL", githubConfigURL, "namespace", namespace)
|
||||
return clientWrapper.client, nil
|
||||
}
|
||||
|
||||
m.logger.Info("creating new client", "githubConfigURL", githubConfigURL, "namespace", namespace)
|
||||
|
||||
client, err := NewClient(
|
||||
githubConfigURL,
|
||||
&creds,
|
||||
@@ -115,11 +73,24 @@ func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m.clients[key] = &actionsClientWrapper{
|
||||
mu: sync.Mutex{},
|
||||
client: client,
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
key := ActionsClientKey{
|
||||
Identifier: client.Identifier(),
|
||||
Namespace: namespace,
|
||||
}
|
||||
|
||||
cachedClient, has := m.clients[key]
|
||||
if has {
|
||||
m.logger.Info("using cache client", "githubConfigURL", githubConfigURL, "namespace", namespace)
|
||||
return cachedClient, nil
|
||||
}
|
||||
|
||||
m.logger.Info("creating new client", "githubConfigURL", githubConfigURL, "namespace", namespace)
|
||||
|
||||
m.clients[key] = client
|
||||
|
||||
m.logger.Info("successfully created new client", "githubConfigURL", githubConfigURL, "namespace", namespace)
|
||||
|
||||
return client, nil
|
||||
|
||||
Reference in New Issue
Block a user