github.com/kubeshop/testkube@v1.17.23/cmd/tcl/testworkflow-toolkit/env/client.go (about) 1 // Copyright 2024 Testkube. 2 // 3 // Licensed as a Testkube Pro file under the Testkube Community 4 // License (the "License"); you may not use this file except in compliance with 5 // the License. You may obtain a copy of the License at 6 // 7 // https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt 8 9 package env 10 11 import ( 12 "context" 13 "fmt" 14 15 "k8s.io/client-go/kubernetes" 16 "k8s.io/client-go/rest" 17 18 "github.com/kubeshop/testkube/cmd/kubectl-testkube/config" 19 "github.com/kubeshop/testkube/pkg/agent" 20 "github.com/kubeshop/testkube/pkg/api/v1/client" 21 "github.com/kubeshop/testkube/pkg/cloud" 22 cloudexecutor "github.com/kubeshop/testkube/pkg/cloud/data/executor" 23 phttp "github.com/kubeshop/testkube/pkg/http" 24 "github.com/kubeshop/testkube/pkg/k8sclient" 25 "github.com/kubeshop/testkube/pkg/log" 26 "github.com/kubeshop/testkube/pkg/storage/minio" 27 "github.com/kubeshop/testkube/pkg/ui" 28 ) 29 30 func KubernetesConfig() *rest.Config { 31 c, err := rest.InClusterConfig() 32 if err != nil { 33 var fsErr error 34 c, fsErr = k8sclient.GetK8sClientConfig() 35 if fsErr != nil { 36 ui.Fail(fmt.Errorf("couldn't find Kubernetes config: %w and %w", err, fsErr)) 37 } 38 } 39 return c 40 } 41 42 func Kubernetes() *kubernetes.Clientset { 43 c, err := kubernetes.NewForConfig(KubernetesConfig()) 44 if err != nil { 45 ui.Fail(fmt.Errorf("couldn't instantiate Kubernetes client: %w", err)) 46 } 47 return c 48 } 49 50 func Testkube() client.Client { 51 if UseProxy() { 52 return client.NewProxyAPIClient(Kubernetes(), client.NewAPIConfig(Namespace(), config.APIServerName, config.APIServerPort)) 53 } 54 httpClient := phttp.NewClient(true) 55 sseClient := phttp.NewSSEClient(true) 56 return client.NewDirectAPIClient(httpClient, sseClient, fmt.Sprintf("http://%s:%d", config.APIServerName, config.APIServerPort), "") 57 } 58 59 func ObjectStorageClient() (*minio.Client, error) { 60 cfg := Config().ObjectStorage 61 opts := minio.GetTLSOptions(cfg.Ssl, cfg.SkipVerify, cfg.CertFile, cfg.KeyFile, cfg.CAFile) 62 c := minio.NewClient(cfg.Endpoint, cfg.AccessKeyID, cfg.SecretAccessKey, cfg.Region, cfg.Token, cfg.Bucket, opts...) 63 return c, c.Connect() 64 } 65 66 func Cloud(ctx context.Context) cloudexecutor.Executor { 67 cfg := Config().Cloud 68 grpcConn, err := agent.NewGRPCConnection(ctx, cfg.TlsInsecure, cfg.SkipVerify, cfg.Url, "", "", "", log.DefaultLogger) 69 if err != nil { 70 ui.Fail(fmt.Errorf("failed to connect with Cloud: %w", err)) 71 } 72 grpcClient := cloud.NewTestKubeCloudAPIClient(grpcConn) 73 return cloudexecutor.NewCloudGRPCExecutor(grpcClient, grpcConn, cfg.ApiKey) 74 }