Skip to content

Commit

Permalink
feat: [TKC-2063] Delete testworkflow output when workflow is deleted (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
povilasv authored May 29, 2024
1 parent 5bd9944 commit 10f8ed7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func main() {
}
storageClient = minioClient
// Pro edition only (tcl protected code)
testWorkflowOutputRepository = testworkflow.NewMinioOutputRepository(storageClient, cfg.LogsBucket)
testWorkflowOutputRepository = testworkflow.NewMinioOutputRepository(storageClient, db.Collection(testworkflow.CollectionName), cfg.LogsBucket)
artifactStorage = minio.NewMinIOArtifactClient(storageClient)
// init storage
isMinioStorage := cfg.LogsStorage == "minio"
Expand Down
4 changes: 4 additions & 0 deletions pkg/tcl/apitcl/v1/testworkflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (s *apiTCL) DeleteTestWorkflowHandler() fiber.Handler {
}
skipExecutions := c.Query("skipDeleteExecutions", "")
if skipExecutions != "true" {
err = s.TestWorkflowOutput.DeleteOutputByTestWorkflow(context.Background(), name)
if err != nil {
return s.ClientError(c, "deleting executions output", err)
}
err = s.TestWorkflowResults.DeleteByTestWorkflow(context.Background(), name)
if err != nil {
return s.ClientError(c, "deleting executions", err)
Expand Down
7 changes: 7 additions & 0 deletions pkg/tcl/cloudtcl/data/testworkflow/execution_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ type ExecutionDeleteByWorkflowRequest struct {
type ExecutionDeleteByWorkflowResponse struct {
}

type ExecutionDeleteOutputByWorkflowRequest struct {
WorkflowName string `json:"workflowName"`
}

type ExecutionDeleteOutputByWorkflowResponse struct {
}

type ExecutionDeleteAllRequest struct {
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/tcl/cloudtcl/data/testworkflow/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,9 @@ func (r *CloudOutputRepository) HasLog(ctx context.Context, id, workflowName str
}
return pass(r.executor, ctx, req, process)
}

// DeleteByTestWorkflow deletes execution results by workflow
func (r *CloudOutputRepository) DeleteOutputByTestWorkflow(ctx context.Context, workflowName string) (err error) {
req := ExecutionDeleteOutputByWorkflowRequest{WorkflowName: workflowName}
return passNoContent(r.executor, ctx, req)
}
3 changes: 3 additions & 0 deletions pkg/tcl/repositorytcl/testworkflow/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,7 @@ type OutputRepository interface {
ReadLog(ctx context.Context, id, workflowName string) (io.Reader, error)
// HasLog checks if there is an output in Minio
HasLog(ctx context.Context, id, workflowName string) (bool, error)

// DeleteOutputByTestWorkflow deletes execution output by test workflow
DeleteOutputByTestWorkflow(ctx context.Context, testWorkflowName string) error
}
43 changes: 38 additions & 5 deletions pkg/tcl/repositorytcl/testworkflow/minio_output_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,30 @@ import (
"io"
"time"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/log"
"github.com/kubeshop/testkube/pkg/storage"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

var _ OutputRepository = (*MinioRepository)(nil)

const bucketFolder = "testworkflows"

type MinioRepository struct {
storage storage.Client
bucket string
storage storage.Client
executionCollection *mongo.Collection
bucket string
}

func NewMinioOutputRepository(storageClient storage.Client, bucket string) *MinioRepository {
func NewMinioOutputRepository(storageClient storage.Client, executionCollection *mongo.Collection, bucket string) *MinioRepository {
log.DefaultLogger.Debugw("creating minio workflow output repository", "bucket", bucket)
return &MinioRepository{
storage: storageClient,
bucket: bucket,
storage: storageClient,
executionCollection: executionCollection,
bucket: bucket,
}
}

Expand Down Expand Up @@ -66,3 +72,30 @@ func (m *MinioRepository) HasLog(ctx context.Context, id, workflowName string) (
}
return true, nil
}

func (m *MinioRepository) DeleteOutputByTestWorkflow(ctx context.Context, testWorkflowName string) error {
log.DefaultLogger.Debugw("deleting output by testWorkflowName", "testWorkflowName", testWorkflowName)
var executions []testkube.TestWorkflowExecution
//TODO
cursor, err := m.executionCollection.Find(ctx, bson.M{"testworkflow.name": testWorkflowName})
if err != nil {
return err
}
err = cursor.All(ctx, &executions)
if err != nil {
return err
}
for _, execution := range executions {
log.DefaultLogger.Debugw("deleting output for execution", "execution", execution)
err = m.DeleteOutput(ctx, execution.Id)
if err != nil {
return err
}
}
return nil
}

func (m *MinioRepository) DeleteOutput(ctx context.Context, id string) error {
log.DefaultLogger.Debugw("deleting test workflow output", "id", id)
return m.storage.DeleteFileFromBucket(ctx, m.bucket, bucketFolder, id)
}
14 changes: 14 additions & 0 deletions pkg/tcl/repositorytcl/testworkflow/mock_output_repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 10f8ed7

Please sign in to comment.