github.com/kubeshop/testkube@v1.17.23/pkg/api/v1/client/factory.go (about) 1 package client 2 3 import ( 4 "fmt" 5 6 "golang.org/x/oauth2" 7 8 phttp "github.com/kubeshop/testkube/pkg/http" 9 "github.com/kubeshop/testkube/pkg/oauth" 10 ) 11 12 type ClientType string 13 14 const ( 15 ClientDirect ClientType = "direct" 16 ClientCloud ClientType = "cloud" 17 ClientProxy ClientType = "proxy" 18 ClientCluster ClientType = "cluster" 19 ) 20 21 // Options contains client options 22 type Options struct { 23 Namespace string 24 ApiUri string 25 ApiPath string 26 Token *oauth2.Token 27 Provider oauth.ProviderType 28 ClientID string 29 ClientSecret string 30 Scopes []string 31 APIServerName string 32 APIServerPort int 33 Insecure bool 34 35 // Testkube Cloud 36 CloudApiPathPrefix string 37 CloudApiKey string 38 CloudOrganization string 39 CloudEnvironment string 40 } 41 42 // GetClient returns configured Testkube API client, can be one of direct and proxy - direct need additional proxy to be run (`make api-proxy`) 43 func GetClient(clientType ClientType, options Options) (client Client, err error) { 44 httpClient := phttp.NewClient(options.Insecure) 45 sseClient := phttp.NewSSEClient(options.Insecure) 46 47 switch clientType { 48 49 case ClientCloud: 50 ConfigureClient(httpClient, nil, options.CloudApiKey) 51 ConfigureClient(sseClient, nil, options.CloudApiKey) 52 client = NewCloudAPIClient(httpClient, sseClient, options.ApiUri, options.CloudApiPathPrefix) 53 54 case ClientDirect: 55 var token *oauth2.Token 56 if options.Token != nil { 57 provider := oauth.NewProvider(options.ClientID, options.ClientSecret, options.Scopes) 58 if token, err = provider.ValidateToken(options.Provider, options.Token); err != nil { 59 return client, err 60 } 61 } 62 63 ConfigureClient(httpClient, token, "") 64 ConfigureClient(sseClient, token, "") 65 client = NewDirectAPIClient(httpClient, sseClient, options.ApiUri, "") 66 67 case ClientProxy, ClientCluster: 68 clientset, err := GetClientSet("", clientType) 69 if err != nil { 70 return client, err 71 } 72 73 client = NewProxyAPIClient(clientset, NewAPIConfig(options.Namespace, options.APIServerName, options.APIServerPort)) 74 default: 75 return client, fmt.Errorf("unsupported client type %s", clientType) 76 } 77 78 return client, err 79 }