Skip to content

Commit

Permalink
feat: [TKC-2364] Add cloud TestWorkflow client
Browse files Browse the repository at this point in the history
  • Loading branch information
povilasv committed Aug 13, 2024
1 parent 7120500 commit 3740aab
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 5 deletions.
10 changes: 8 additions & 2 deletions cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ 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)
testWorkflowTemplatesClient := testworkflowsclientv1.NewTestWorkflowTemplatesClient(kubeClient, cfg.TestkubeNamespace)
var testWorkflowsClient testworkflowsclientv1.Interface
testWorkflowsClient = testworkflowsclientv1.NewClient(kubeClient, cfg.TestkubeNamespace)
var testWorkflowTemplatesClient testworkflowsclientv1.TestWorkflowTemplatesInterface
testWorkflowTemplatesClient = testworkflowsclientv1.NewTestWorkflowTemplatesClient(kubeClient, cfg.TestkubeNamespace)
testWorkflowExecutionsClient := testworkflowsclientv1.NewTestWorkflowExecutionsClient(kubeClient, cfg.TestkubeNamespace)
templatesClient := templatesclientv1.NewClient(kubeClient, cfg.TestkubeNamespace)

Expand Down Expand Up @@ -274,6 +276,10 @@ 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)
testWorkflowTemplatesClient = cloudtestworkflow.NewCloudTestWorkflowTemplateRepository(grpcClient, grpcConn, cfg.TestkubeProAPIKey)
// Pro edition only (tcl protected code)
testWorkflowResultsRepository = cloudtestworkflow.NewCloudRepository(grpcClient, grpcConn, cfg.TestkubeProAPIKey)
var opts []cloudtestworkflow.Option
Expand Down
4 changes: 2 additions & 2 deletions internal/app/api/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func NewTestkubeAPI(
clientset kubernetes.Interface,
testkubeClientset testkubeclientset.Interface,
testsourcesClient *testsourcesclientv1.TestSourcesClient,
testWorkflowsClient *testworkflowsv1.TestWorkflowsClient,
testWorkflowTemplatesClient *testworkflowsv1.TestWorkflowTemplatesClient,
testWorkflowsClient testworkflowsv1.Interface,
testWorkflowTemplatesClient testworkflowsv1.TestWorkflowTemplatesInterface,
configMap repoConfig.Repository,
clusterId string,
eventsEmitter *event.Emitter,
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Config struct {
TestkubeProWorkerCount int `envconfig:"TESTKUBE_PRO_WORKER_COUNT" default:"50"`
TestkubeProLogStreamWorkerCount int `envconfig:"TESTKUBE_PRO_LOG_STREAM_WORKER_COUNT" default:"25"`
TestkubeProWorkflowNotificationsWorkerCount int `envconfig:"TESTKUBE_PRO_WORKFLOW_NOTIFICATIONS_STREAM_WORKER_COUNT" default:"25"`
TestkubeProSkipVerify bool `envconfig:"TESTKUBE_PRO_SKIP_VERIFY" default:"false"`
TestkubeProSkipVerify bool `envconfig:"TESTKUBE_PRO_SKIP_VERIFY" default:"true"`
TestkubeProEnvID string `envconfig:"TESTKUBE_PRO_ENV_ID" default:""`
TestkubeProOrgID string `envconfig:"TESTKUBE_PRO_ORG_ID" default:""`
TestkubeProMigrate string `envconfig:"TESTKUBE_PRO_MIGRATE" default:"false"`
Expand Down
12 changes: 12 additions & 0 deletions pkg/cloud/data/testworkflow/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ 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"
CmdTestWorkflowTemplateGet executor.Command = "workflow_template_get"
)

func command(v interface{}) executor.Command {
Expand Down Expand Up @@ -79,6 +83,14 @@ func command(v interface{}) executor.Command {
return CmdTestWorkflowOutputDeleteByTestWorkflow
case ExecutionDeleteOutputForTestWorkflowsRequest:
return CmdTestworkflowOutputDeleteForTestWorkflows

case TestWorkflowGetRequest:
return CmdTestWorkflowGet
case TestWorkflowListRequest:
return CmdTestWorkflowList
case TestWorkflowTemplateGetRequest:
return CmdTestWorkflowTemplateGet

}
panic("unknown test workflows Cloud request")
}
18 changes: 18 additions & 0 deletions pkg/cloud/data/testworkflow/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
24 changes: 24 additions & 0 deletions pkg/cloud/data/testworkflow/execution_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,27 @@ 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"`
}

type TestWorkflowTemplateGetRequest struct {
Name string `json:"name"`
}

type TestWorkflowTemplateGetResponse struct {
TestWorkflowTemplate testkube.TestWorkflowTemplate `json:"testWorkflowTemplate"`
}
77 changes: 77 additions & 0 deletions pkg/cloud/data/testworkflow/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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"

"github.com/pkg/errors"
"google.golang.org/grpc"
)

var _ testworkflowsclientv1.TestWorkflowTemplatesInterface = (*CloudTestWorkflowTemplateRepository)(nil)

type CloudTestWorkflowTemplateRepository struct {
executor executor.Executor
}

func NewCloudTestWorkflowTemplateRepository(client cloud.TestKubeCloudAPIClient, grpcConn *grpc.ClientConn, apiKey string) *CloudTestWorkflowTemplateRepository {
return &CloudTestWorkflowTemplateRepository{executor: executor.NewCloudGRPCExecutor(client, grpcConn, apiKey)}
}

func (r *CloudTestWorkflowTemplateRepository) List(selector string) (*testworkflowsv1.TestWorkflowTemplateList, error) {
return nil, errors.New("unimplemented")
}

func (r *CloudTestWorkflowTemplateRepository) ListLabels() (map[string][]string, error) {
return make(map[string][]string), nil
}

func (r *CloudTestWorkflowTemplateRepository) Get(name string) (*testworkflowsv1.TestWorkflowTemplate, error) {
req := TestWorkflowTemplateGetRequest{Name: name}
response, err := r.executor.Execute(context.Background(), CmdTestWorkflowTemplateGet, req)
if err != nil {
return nil, err
}
var commandResponse TestWorkflowTemplateGetResponse
if err := json.Unmarshal(response, &commandResponse); err != nil {
return nil, err
}
return testworkflowmappers.MapTemplateAPIToKube(&commandResponse.TestWorkflowTemplate), nil
}

// Create creates new TestWorkflow
func (r *CloudTestWorkflowTemplateRepository) Create(workflow *testworkflowsv1.TestWorkflowTemplate) (*testworkflowsv1.TestWorkflowTemplate, error) {
return nil, errors.New("unimplemented")
}

func (r *CloudTestWorkflowTemplateRepository) Update(workflow *testworkflowsv1.TestWorkflowTemplate) (*testworkflowsv1.TestWorkflowTemplate, error) {
return nil, errors.New("unimplemented")
}

func (r *CloudTestWorkflowTemplateRepository) Apply(workflow *testworkflowsv1.TestWorkflowTemplate) error {
return errors.New("unimplemented")
}

func (r *CloudTestWorkflowTemplateRepository) Delete(name string) error {
return errors.New("unimplemented")
}

func (r *CloudTestWorkflowTemplateRepository) DeleteAll() error {
return errors.New("unimplemented")
}

func (r *CloudTestWorkflowTemplateRepository) DeleteByLabels(selector string) error {
return errors.New("unimplemented")
}

func (r *CloudTestWorkflowTemplateRepository) UpdateStatus(workflow *testworkflowsv1.TestWorkflowTemplate) error {
// This is the actual implementation, as update status
// should update k8s crd's status field, but we don't have it when stored in mongo
return nil
}
87 changes: 87 additions & 0 deletions pkg/cloud/data/testworkflow/workflows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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"

"github.com/pkg/errors"
"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), errors.New("unimplemented")
}

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, errors.New("unimplemented")
}

func (r *CloudTestWorkflowRepository) Update(workflow *testworkflowsv1.TestWorkflow) (*testworkflowsv1.TestWorkflow, error) {
return nil, errors.New("unimplemented")
}

func (r *CloudTestWorkflowRepository) Apply(workflow *testworkflowsv1.TestWorkflow) error {
return errors.New("unimplemented")
}

func (r *CloudTestWorkflowRepository) Delete(name string) error {
return errors.New("unimplemented")
}

func (r *CloudTestWorkflowRepository) DeleteAll() error {
return errors.New("unimplemented")
}

func (r *CloudTestWorkflowRepository) DeleteByLabels(selector string) error {
return errors.New("unimplemented")
}

func (r *CloudTestWorkflowRepository) UpdateStatus(workflow *testworkflowsv1.TestWorkflow) error {
// This is the actual implementation, as update status
// should update k8s crd's status field, but we don't have it when stored in mongo
return nil
}

0 comments on commit 3740aab

Please sign in to comment.