Add options to multi client (#2257)

This commit is contained in:
Francesco Renzi 2023-02-07 08:47:59 +01:00 committed by GitHub
parent 55951c2bdb
commit 8f62e35f6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 8 deletions

View File

@ -34,10 +34,10 @@ func NewMultiClient(opts ...MultiClientOption) actions.MultiClient {
return f return f
} }
func (f *fakeMultiClient) GetClientFor(ctx context.Context, githubConfigURL string, creds actions.ActionsAuth, namespace string) (actions.ActionsService, error) { func (f *fakeMultiClient) GetClientFor(ctx context.Context, githubConfigURL string, creds actions.ActionsAuth, namespace string, options ...actions.ClientOption) (actions.ActionsService, error) {
return f.defaultClient, f.defaultErr return f.defaultClient, f.defaultErr
} }
func (f *fakeMultiClient) GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData actions.KubernetesSecretData) (actions.ActionsService, error) { func (f *fakeMultiClient) GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData actions.KubernetesSecretData, options ...actions.ClientOption) (actions.ActionsService, error) {
return f.defaultClient, f.defaultErr return f.defaultClient, f.defaultErr
} }

View File

@ -11,8 +11,8 @@ import (
) )
type MultiClient interface { type MultiClient interface {
GetClientFor(ctx context.Context, githubConfigURL string, creds ActionsAuth, namespace string) (ActionsService, error) GetClientFor(ctx context.Context, githubConfigURL string, creds ActionsAuth, namespace string, options ...ClientOption) (ActionsService, error)
GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData KubernetesSecretData) (ActionsService, error) GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData KubernetesSecretData, options ...ClientOption) (ActionsService, error)
} }
type multiClient struct { type multiClient struct {
@ -52,7 +52,7 @@ func NewMultiClient(userAgent string, logger logr.Logger) MultiClient {
} }
} }
func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string, creds ActionsAuth, namespace string) (ActionsService, error) { func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string, creds ActionsAuth, namespace string, options ...ClientOption) (ActionsService, error) {
m.logger.Info("retrieve actions client", "githubConfigURL", githubConfigURL, "namespace", namespace) m.logger.Info("retrieve actions client", "githubConfigURL", githubConfigURL, "namespace", namespace)
if creds.Token == "" && creds.AppCreds == nil { if creds.Token == "" && creds.AppCreds == nil {
@ -66,8 +66,10 @@ func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string,
client, err := NewClient( client, err := NewClient(
githubConfigURL, githubConfigURL,
&creds, &creds,
WithUserAgent(m.userAgent), append([]ClientOption{
WithLogger(m.logger), WithUserAgent(m.userAgent),
WithLogger(m.logger),
}, options...)...,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -98,7 +100,7 @@ func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string,
type KubernetesSecretData map[string][]byte type KubernetesSecretData map[string][]byte
func (m *multiClient) GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData KubernetesSecretData) (ActionsService, error) { func (m *multiClient) GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData KubernetesSecretData, options ...ClientOption) (ActionsService, error) {
if len(secretData) == 0 { if len(secretData) == 0 {
return nil, fmt.Errorf("must provide secret data with either PAT or GitHub App Auth") return nil, fmt.Errorf("must provide secret data with either PAT or GitHub App Auth")
} }

View File

@ -49,6 +49,32 @@ func TestMultiClientCaching(t *testing.T) {
assert.Len(t, multiClient.clients, 2) assert.Len(t, multiClient.clients, 2)
} }
func TestMultiClientOptions(t *testing.T) {
logger := logr.Discard()
ctx := context.Background()
defaultNamespace := "default"
defaultConfigURL := "https://github.com/org/repo"
defaultCreds := &ActionsAuth{
Token: "token",
}
multiClient := NewMultiClient("test-user-agent", logger)
service, err := multiClient.GetClientFor(
ctx,
defaultConfigURL,
*defaultCreds,
defaultNamespace,
WithUserAgent("test-option"),
)
require.NoError(t, err)
client := service.(*Client)
req, err := client.NewGitHubAPIRequest(ctx, "GET", "/test", nil)
require.NoError(t, err)
assert.Equal(t, "test-option", req.Header.Get("User-Agent"))
}
func TestCreateJWT(t *testing.T) { func TestCreateJWT(t *testing.T) {
key := `-----BEGIN RSA PRIVATE KEY----- key := `-----BEGIN RSA PRIVATE KEY-----
MIICWgIBAAKBgHXfRT9cv9UY9fAAD4+1RshpfSSZe277urfEmPfX3/Og9zJYRk// MIICWgIBAAKBgHXfRT9cv9UY9fAAD4+1RshpfSSZe277urfEmPfX3/Og9zJYRk//