github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/common/client.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  	"strconv"
     9  
    10  	"github.com/spf13/cobra"
    11  	"golang.org/x/oauth2"
    12  
    13  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
    14  	"github.com/kubeshop/testkube/pkg/api/v1/client"
    15  	"github.com/kubeshop/testkube/pkg/cloudlogin"
    16  )
    17  
    18  // GetClient returns api client
    19  func GetClient(cmd *cobra.Command) (client.Client, string, error) {
    20  	clientType := cmd.Flag("client").Value.String()
    21  	namespace := cmd.Flag("namespace").Value.String()
    22  	apiURI := cmd.Flag("api-uri").Value.String()
    23  	oauthEnabled, err := strconv.ParseBool(cmd.Flag("oauth-enabled").Value.String())
    24  	if err != nil {
    25  		return nil, "", fmt.Errorf("parsing flag value %w", err)
    26  	}
    27  
    28  	insecure, err := strconv.ParseBool(cmd.Flag("insecure").Value.String())
    29  	if err != nil {
    30  		return nil, "", fmt.Errorf("parsing flag value %w", err)
    31  	}
    32  
    33  	options := client.Options{
    34  		Namespace: namespace,
    35  		ApiUri:    apiURI,
    36  		Insecure:  insecure,
    37  	}
    38  
    39  	cfg, err := config.Load()
    40  	if err != nil {
    41  		return nil, "", fmt.Errorf("loading config file %w", err)
    42  	}
    43  
    44  	// set kubeconfig as default config type
    45  	if cfg.ContextType == "" {
    46  		cfg.ContextType = config.ContextTypeKubeconfig
    47  	}
    48  
    49  	if cfg.APIServerName == "" {
    50  		cfg.APIServerName = config.APIServerName
    51  	}
    52  
    53  	if cfg.APIServerPort == 0 {
    54  		cfg.APIServerPort = config.APIServerPort
    55  	}
    56  
    57  	options.APIServerName = cfg.APIServerName
    58  	options.APIServerPort = cfg.APIServerPort
    59  
    60  	switch cfg.ContextType {
    61  	case config.ContextTypeKubeconfig:
    62  		if oauthEnabled {
    63  			options.Provider = cfg.OAuth2Data.Provider
    64  			options.ClientID = cfg.OAuth2Data.ClientID
    65  			options.ClientSecret = cfg.OAuth2Data.ClientSecret
    66  			options.Scopes = cfg.OAuth2Data.Scopes
    67  			options.Token = cfg.OAuth2Data.Token
    68  
    69  			if os.Getenv("TESTKUBE_OAUTH_ACCESS_TOKEN") != "" {
    70  				options.Token = &oauth2.Token{
    71  					AccessToken: os.Getenv("TESTKUBE_OAUTH_ACCESS_TOKEN"),
    72  				}
    73  			}
    74  
    75  			if options.Token == nil {
    76  				return nil, "", errors.New("oauth token is empty, please configure your oauth settings first")
    77  			}
    78  		}
    79  	case config.ContextTypeCloud:
    80  
    81  		token := cfg.CloudContext.ApiKey
    82  
    83  		if cfg.CloudContext.ApiKey != "" && cfg.CloudContext.RefreshToken != "" && cfg.OAuth2Data.Enabled {
    84  			var refreshToken string
    85  			authURI := fmt.Sprintf("%s/idp", cfg.CloudContext.ApiUri)
    86  			token, refreshToken, err = cloudlogin.CheckAndRefreshToken(context.Background(), authURI, cfg.CloudContext.ApiKey, cfg.CloudContext.RefreshToken)
    87  			if err != nil {
    88  				// Error: failed refreshing, go thru login flow
    89  				token, refreshToken, err = LoginUser(authURI)
    90  				if err != nil {
    91  					return nil, "", fmt.Errorf("error logging in: %w", err)
    92  				}
    93  			}
    94  			if err := UpdateTokens(cfg, token, refreshToken); err != nil {
    95  				return nil, "", fmt.Errorf("error storing new token: %w", err)
    96  			}
    97  		}
    98  		clientType = string(client.ClientCloud)
    99  		options.CloudApiPathPrefix = fmt.Sprintf("/organizations/%s/environments/%s/agent", cfg.CloudContext.OrganizationId, cfg.CloudContext.EnvironmentId)
   100  		options.CloudApiKey = token
   101  		options.CloudEnvironment = cfg.CloudContext.EnvironmentId
   102  		options.CloudOrganization = cfg.CloudContext.OrganizationId
   103  		options.ApiUri = cfg.CloudContext.ApiUri
   104  	}
   105  
   106  	c, err := client.GetClient(client.ClientType(clientType), options)
   107  	if err != nil {
   108  		return nil, "", fmt.Errorf("setting up client type %w", err)
   109  	}
   110  
   111  	return c, namespace, nil
   112  }