Add options to multi client (#2257)
This commit is contained in:
parent
55951c2bdb
commit
8f62e35f6b
|
|
@ -34,10 +34,10 @@ func NewMultiClient(opts ...MultiClientOption) actions.MultiClient {
|
|||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ import (
|
|||
)
|
||||
|
||||
type MultiClient interface {
|
||||
GetClientFor(ctx context.Context, githubConfigURL string, creds ActionsAuth, namespace string) (ActionsService, error)
|
||||
GetClientFromSecret(ctx context.Context, githubConfigURL, namespace string, secretData KubernetesSecretData) (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, options ...ClientOption) (ActionsService, error)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
if creds.Token == "" && creds.AppCreds == nil {
|
||||
|
|
@ -66,8 +66,10 @@ func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string,
|
|||
client, err := NewClient(
|
||||
githubConfigURL,
|
||||
&creds,
|
||||
append([]ClientOption{
|
||||
WithUserAgent(m.userAgent),
|
||||
WithLogger(m.logger),
|
||||
}, options...)...,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -98,7 +100,7 @@ func (m *multiClient) GetClientFor(ctx context.Context, githubConfigURL string,
|
|||
|
||||
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 {
|
||||
return nil, fmt.Errorf("must provide secret data with either PAT or GitHub App Auth")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,32 @@ func TestMultiClientCaching(t *testing.T) {
|
|||
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) {
|
||||
key := `-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICWgIBAAKBgHXfRT9cv9UY9fAAD4+1RshpfSSZe277urfEmPfX3/Og9zJYRk//
|
||||
|
|
|
|||
Loading…
Reference in New Issue