From f31f59092a4537b0702b85fefebda39f3ccec5b0 Mon Sep 17 00:00:00 2001
From: Povilas Versockas
Date: Tue, 6 Aug 2024 15:53:42 +0300
Subject: [PATCH] feat: [TKC-2364] Add cloud TestWorkflow client
---
cmd/api-server/main.go | 6 +-
internal/app/api/v1/server.go | 2 +-
pkg/cloud/data/testworkflow/commands.go | 3 +
pkg/cloud/data/testworkflow/execution.go | 18 ++++
.../data/testworkflow/execution_models.go | 16 ++++
pkg/cloud/data/testworkflow/workflows.go | 84 +++++++++++++++++++
6 files changed, 127 insertions(+), 2 deletions(-)
create mode 100644 pkg/cloud/data/testworkflow/workflows.go
diff --git a/cmd/api-server/main.go b/cmd/api-server/main.go
index b350e2e156d..1fb0c7fb8f5 100644
--- a/cmd/api-server/main.go
+++ b/cmd/api-server/main.go
@@ -235,7 +235,8 @@ func main() {
testsourcesClient := testsourcesclientv1.NewClient(kubeClient, cfg.TestkubeNamespace)
testExecutionsClient := testexecutionsclientv1.NewClient(kubeClient, cfg.TestkubeNamespace)
testsuiteExecutionsClient := testsuiteexecutionsclientv1.NewClient(kubeClient, cfg.TestkubeNamespace)
- testWorkflowsClient := testworkflowsclientv1.NewClient(kubeClient, cfg.TestkubeNamespace)
+ var testWorkflowsClient testworkflowsclientv1.Interface
+ testWorkflowsClient = testworkflowsclientv1.NewClient(kubeClient, cfg.TestkubeNamespace)
testWorkflowTemplatesClient := testworkflowsclientv1.NewTestWorkflowTemplatesClient(kubeClient, cfg.TestkubeNamespace)
testWorkflowExecutionsClient := testworkflowsclientv1.NewTestWorkflowExecutionsClient(kubeClient, cfg.TestkubeNamespace)
templatesClient := templatesclientv1.NewClient(kubeClient, cfg.TestkubeNamespace)
@@ -274,6 +275,9 @@ func main() {
resultsRepository = cloudresult.NewCloudResultRepository(grpcClient, grpcConn, cfg.TestkubeProAPIKey)
testResultsRepository = cloudtestresult.NewCloudRepository(grpcClient, grpcConn, cfg.TestkubeProAPIKey)
configRepository = cloudconfig.NewCloudResultRepository(grpcClient, grpcConn, cfg.TestkubeProAPIKey)
+
+ //TODO add some flag
+ testWorkflowsClient = cloudtestworkflow.NewCloudTestWorkflowRepository(grpcClient, grpcConn, cfg.TestkubeProAPIKey)
// Pro edition only (tcl protected code)
testWorkflowResultsRepository = cloudtestworkflow.NewCloudRepository(grpcClient, grpcConn, cfg.TestkubeProAPIKey)
var opts []cloudtestworkflow.Option
diff --git a/internal/app/api/v1/server.go b/internal/app/api/v1/server.go
index 8700711b262..082f21adb4a 100644
--- a/internal/app/api/v1/server.go
+++ b/internal/app/api/v1/server.go
@@ -82,7 +82,7 @@ func NewTestkubeAPI(
clientset kubernetes.Interface,
testkubeClientset testkubeclientset.Interface,
testsourcesClient *testsourcesclientv1.TestSourcesClient,
- testWorkflowsClient *testworkflowsv1.TestWorkflowsClient,
+ testWorkflowsClient testworkflowsv1.Interface,
testWorkflowTemplatesClient *testworkflowsv1.TestWorkflowTemplatesClient,
configMap repoConfig.Repository,
clusterId string,
diff --git a/pkg/cloud/data/testworkflow/commands.go b/pkg/cloud/data/testworkflow/commands.go
index fa2291483bc..0da5302cec4 100644
--- a/pkg/cloud/data/testworkflow/commands.go
+++ b/pkg/cloud/data/testworkflow/commands.go
@@ -28,6 +28,9 @@ const (
CmdTestWorkflowOutputHasLog executor.Command = "workflow_output_has_log"
CmdTestWorkflowOutputDeleteByTestWorkflow executor.Command = "workflow_output_delete_by_test_workflow"
CmdTestworkflowOutputDeleteForTestWorkflows executor.Command = "workflow_output_delete_for_test_workflows"
+
+ CmdTestWorkflowList executor.Command = "workflow_list"
+ CmdTestWorkflowGet executor.Command = "workflow_get"
)
func command(v interface{}) executor.Command {
diff --git a/pkg/cloud/data/testworkflow/execution.go b/pkg/cloud/data/testworkflow/execution.go
index a0dd8b69d0b..949c2262e56 100644
--- a/pkg/cloud/data/testworkflow/execution.go
+++ b/pkg/cloud/data/testworkflow/execution.go
@@ -7,6 +7,7 @@ import (
"google.golang.org/grpc"
+ testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
testworkflow2 "github.com/kubeshop/testkube/pkg/repository/testworkflow"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
@@ -169,3 +170,20 @@ func (r *CloudRepository) GetNextExecutionNumber(ctx context.Context, testWorkfl
}
return commandResponse.TestWorkflowNumber, nil
}
+
+func (r *CloudRepository) List(selector string) (*testworkflowsv1.TestWorkflowList, error) {
+ req := TestWorkflowListRequest{Selector: selector}
+ response, err := r.executor.Execute(context.Background(), CmdTestWorkflowList, req)
+ if err != nil {
+ return nil, err
+ }
+ var commandResponse TestWorkflowListResponse
+ if err := json.Unmarshal(response, &commandResponse); err != nil {
+ return nil, err
+ }
+ var list testworkflowsv1.TestWorkflowList
+ for _, tw := range commandResponse.TestWorkflows {
+ _ = tw
+ }
+ return &list, nil
+}
diff --git a/pkg/cloud/data/testworkflow/execution_models.go b/pkg/cloud/data/testworkflow/execution_models.go
index 25834ad6de8..f5d90127304 100644
--- a/pkg/cloud/data/testworkflow/execution_models.go
+++ b/pkg/cloud/data/testworkflow/execution_models.go
@@ -178,3 +178,19 @@ type ExecutionGetNextExecutionNumberRequest struct {
type ExecutionGetNextExecutionNumberResponse struct {
TestWorkflowNumber int32 `json:"testWorkflowNumber"`
}
+
+type TestWorkflowListRequest struct {
+ Selector string `json:"selector"`
+}
+
+type TestWorkflowListResponse struct {
+ TestWorkflows []testkube.TestWorkflow `json:"testWorkflows"`
+}
+
+type TestWorkflowGetRequest struct {
+ Name string `json:"name"`
+}
+
+type TestWorkflowGetResponse struct {
+ TestWorkflow testkube.TestWorkflow `json:"testWorkflow"`
+}
diff --git a/pkg/cloud/data/testworkflow/workflows.go b/pkg/cloud/data/testworkflow/workflows.go
new file mode 100644
index 00000000000..3e132b7e3bc
--- /dev/null
+++ b/pkg/cloud/data/testworkflow/workflows.go
@@ -0,0 +1,84 @@
+package testworkflow
+
+import (
+ "context"
+ "encoding/json"
+
+ testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
+ testworkflowsclientv1 "github.com/kubeshop/testkube-operator/pkg/client/testworkflows/v1"
+ "github.com/kubeshop/testkube/pkg/cloud"
+ "github.com/kubeshop/testkube/pkg/cloud/data/executor"
+ testworkflowmappers "github.com/kubeshop/testkube/pkg/mapper/testworkflows"
+
+ "google.golang.org/grpc"
+)
+
+var _ testworkflowsclientv1.Interface = (*CloudTestWorkflowRepository)(nil)
+
+type CloudTestWorkflowRepository struct {
+ executor executor.Executor
+}
+
+func NewCloudTestWorkflowRepository(client cloud.TestKubeCloudAPIClient, grpcConn *grpc.ClientConn, apiKey string) *CloudTestWorkflowRepository {
+ return &CloudTestWorkflowRepository{executor: executor.NewCloudGRPCExecutor(client, grpcConn, apiKey)}
+}
+
+func (r *CloudTestWorkflowRepository) List(selector string) (*testworkflowsv1.TestWorkflowList, error) {
+ req := TestWorkflowListRequest{Selector: selector}
+ response, err := r.executor.Execute(context.Background(), CmdTestWorkflowList, req)
+ if err != nil {
+ return nil, err
+ }
+ var commandResponse TestWorkflowListResponse
+ if err := json.Unmarshal(response, &commandResponse); err != nil {
+ return nil, err
+ }
+ list := testworkflowmappers.MapListAPIToKube(commandResponse.TestWorkflows)
+ return &list, nil
+}
+
+func (r *CloudTestWorkflowRepository) ListLabels() (map[string][]string, error) {
+ return make(map[string][]string), nil
+}
+
+func (r *CloudTestWorkflowRepository) Get(name string) (*testworkflowsv1.TestWorkflow, error) {
+ req := TestWorkflowGetRequest{Name: name}
+ response, err := r.executor.Execute(context.Background(), CmdTestWorkflowGet, req)
+ if err != nil {
+ return nil, err
+ }
+ var commandResponse TestWorkflowGetResponse
+ if err := json.Unmarshal(response, &commandResponse); err != nil {
+ return nil, err
+ }
+ return testworkflowmappers.MapAPIToKube(&commandResponse.TestWorkflow), nil
+}
+
+// Create creates new TestWorkflow
+func (r *CloudTestWorkflowRepository) Create(workflow *testworkflowsv1.TestWorkflow) (*testworkflowsv1.TestWorkflow, error) {
+ return nil, nil
+}
+
+func (r *CloudTestWorkflowRepository) Update(workflow *testworkflowsv1.TestWorkflow) (*testworkflowsv1.TestWorkflow, error) {
+ return nil, nil
+}
+
+func (r *CloudTestWorkflowRepository) Apply(workflow *testworkflowsv1.TestWorkflow) error {
+ return nil
+}
+
+func (r *CloudTestWorkflowRepository) Delete(name string) error {
+ return nil
+}
+
+func (r *CloudTestWorkflowRepository) DeleteAll() error {
+ return nil
+}
+
+func (r *CloudTestWorkflowRepository) DeleteByLabels(selector string) error {
+ return nil
+}
+
+func (r *CloudTestWorkflowRepository) UpdateStatus(workflow *testworkflowsv1.TestWorkflow) error {
+ return nil
+}