Skip to content

Commit

Permalink
feat(testworkflows): move auto-creating secrets parameter to query st…
Browse files Browse the repository at this point in the history
…ring instead of body
  • Loading branch information
rangoo94 committed Jun 21, 2024
1 parent d8c14a1 commit d416421
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 113 deletions.
40 changes: 18 additions & 22 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3579,6 +3579,8 @@ paths:
- test-workflows
- api
- pro
parameters:
- $ref: "#/components/parameters/AutoCreateSecrets"
summary: Create test workflow
description: Create test workflow in the kubernetes cluster
operationId: createTestWorkflow
Expand All @@ -3588,7 +3590,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/TestWorkflowInput"
$ref: "#/components/schemas/TestWorkflow"
text/yaml:
schema:
type: string
Expand Down Expand Up @@ -4369,6 +4371,7 @@ paths:
- pro
parameters:
- $ref: "#/components/parameters/ID"
- $ref: "#/components/parameters/AutoCreateSecrets"
summary: Update test workflow details
description: Update test workflow details in the kubernetes cluster
operationId: updateTestWorkflow
Expand All @@ -4378,7 +4381,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/TestWorkflowInput"
$ref: "#/components/schemas/TestWorkflow"
text/yaml:
schema:
type: string
Expand Down Expand Up @@ -4544,6 +4547,8 @@ paths:
- test-workflows
- api
- pro
parameters:
- $ref: "#/components/parameters/AutoCreateSecrets"
summary: Create test workflow template
description: Create test workflow template in the kubernetes cluster
operationId: createTestWorkflowTemplate
Expand All @@ -4553,7 +4558,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/TestWorkflowTemplateInput"
$ref: "#/components/schemas/TestWorkflowTemplate"
text/yaml:
schema:
type: string
Expand Down Expand Up @@ -4645,6 +4650,7 @@ paths:
- pro
parameters:
- $ref: "#/components/parameters/ID"
- $ref: "#/components/parameters/AutoCreateSecrets"
summary: Update test workflow template details
description: Update test workflow template details in the kubernetes cluster
operationId: updateTestWorkflowTemplate
Expand All @@ -4654,7 +4660,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/TestWorkflowTemplateInput"
$ref: "#/components/schemas/TestWorkflowTemplate"
text/yaml:
schema:
type: string
Expand Down Expand Up @@ -7842,18 +7848,6 @@ components:
status:
$ref: "#/components/schemas/TestWorkflowStatusSummary"

TestWorkflowParams:
type: object
properties:
autoCreateSecrets:
type: boolean

TestWorkflowInput:
type: object
anyOf:
- $ref: "#/components/schemas/TestWorkflow"
- $ref: "#/components/schemas/TestWorkflowParams"

TestWorkflowExecutionRequest:
type: object
properties:
Expand Down Expand Up @@ -8305,12 +8299,6 @@ components:
spec:
$ref: "#/components/schemas/TestWorkflowTemplateSpec"

TestWorkflowTemplateInput:
type: object
anyOf:
- $ref: "#/components/schemas/TestWorkflowTemplate"
- $ref: "#/components/schemas/TestWorkflowParams"

TestWorkflowStepParallel:
type: object
allOf:
Expand Down Expand Up @@ -10474,6 +10462,14 @@ components:
default: false
description: should inline templates in the resolved workflow
required: false
AutoCreateSecrets:
in: query
name: inline
schema:
type: boolean
default: false
description: should automatically create secrets from plain-text credentials
required: false
All:
in: query
name: all
Expand Down
41 changes: 18 additions & 23 deletions internal/app/api/v1/testworkflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"

testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
"github.com/kubeshop/testkube/internal/common"
Expand Down Expand Up @@ -138,27 +137,25 @@ func (s *TestkubeAPI) DeleteTestWorkflowsHandler() fiber.Handler {
func (s *TestkubeAPI) CreateTestWorkflowHandler() fiber.Handler {
errPrefix := "failed to create test workflow"
return func(c *fiber.Ctx) (err error) {
autoCreateSecrets, err := strconv.ParseBool(c.Query("autoCreateSecrets", "false"))
if err != nil {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("%s: could not parse autoCreateSecrets parameter: %s", errPrefix, err))
}

// Deserialize resource
obj := new(testworkflowsv1.TestWorkflow)
params := testkube.TestWorkflowParams{}
if HasYAML(c) {
body := c.Body()
err = common.DeserializeCRD(obj, body)
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
err = yaml.Unmarshal(body, &params)
err = common.DeserializeCRD(obj, c.Body())
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
} else {
var v *testkube.TestWorkflowInput
var v *testkube.TestWorkflow
err = c.BodyParser(&v)
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
obj = testworkflows.MapAPIToKube(&v.TestWorkflow)
params = v.TestWorkflowParams
obj = testworkflows.MapAPIToKube(v)
}

// Validate resource
Expand All @@ -176,7 +173,7 @@ func (s *TestkubeAPI) CreateTestWorkflowHandler() fiber.Handler {

// Handle secrets auto-creation
secrets := s.SecretManager.Batch(execNamespace, "creds-", obj.Name)
if params.AutoCreateSecrets {
if autoCreateSecrets {
_, _ = testworkflowresolver.ReplacePlainTextCredentialsInWorkflow(obj, secrets.Prepare)
if err != nil {
return s.BadRequest(c, errPrefix, "auto-creating secrets", err)
Expand Down Expand Up @@ -218,27 +215,25 @@ func (s *TestkubeAPI) UpdateTestWorkflowHandler() fiber.Handler {
return func(c *fiber.Ctx) (err error) {
name := c.Params("id")

autoCreateSecrets, err := strconv.ParseBool(c.Query("autoCreateSecrets", "false"))
if err != nil {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("%s: could not parse autoCreateSecrets parameter: %s", errPrefix, err))
}

// Deserialize resource
obj := new(testworkflowsv1.TestWorkflow)
params := testkube.TestWorkflowParams{}
if HasYAML(c) {
body := c.Body()
err = common.DeserializeCRD(obj, body)
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
err = yaml.Unmarshal(body, &params)
err = common.DeserializeCRD(obj, c.Body())
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
} else {
var v *testkube.TestWorkflowInput
var v *testkube.TestWorkflow
err = c.BodyParser(&v)
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
obj = testworkflows.MapAPIToKube(&v.TestWorkflow)
params = v.TestWorkflowParams
obj = testworkflows.MapAPIToKube(v)
}

// Read existing resource
Expand All @@ -265,7 +260,7 @@ func (s *TestkubeAPI) UpdateTestWorkflowHandler() fiber.Handler {

// Handle secrets auto-creation
secrets := s.SecretManager.Batch(execNamespace, "creds-", obj.Name)
if params.AutoCreateSecrets {
if autoCreateSecrets {
_, _ = testworkflowresolver.ReplacePlainTextCredentialsInWorkflow(obj, secrets.Prepare)
if err != nil {
return s.BadRequest(c, errPrefix, "auto-creating secrets", err)
Expand Down
44 changes: 20 additions & 24 deletions internal/app/api/v1/testworkflowtemplates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package v1
import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
Expand Down Expand Up @@ -76,27 +76,25 @@ func (s *TestkubeAPI) DeleteTestWorkflowTemplatesHandler() fiber.Handler {
func (s *TestkubeAPI) CreateTestWorkflowTemplateHandler() fiber.Handler {
errPrefix := "failed to create test workflow template"
return func(c *fiber.Ctx) (err error) {
autoCreateSecrets, err := strconv.ParseBool(c.Query("autoCreateSecrets", "false"))
if err != nil {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("%s: could not parse autoCreateSecrets parameter: %s", errPrefix, err))
}

// Deserialize resource
obj := new(testworkflowsv1.TestWorkflowTemplate)
params := testkube.TestWorkflowParams{}
if HasYAML(c) {
body := c.Body()
err = common.DeserializeCRD(obj, body)
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
err = yaml.Unmarshal(body, &params)
err = common.DeserializeCRD(obj, c.Body())
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
} else {
var v *testkube.TestWorkflowTemplateInput
err = c.BodyParser(&v)
var v *testkube.TestWorkflowTemplate
err = c.BodyParser(&obj)
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
obj = testworkflows.MapTemplateAPIToKube(&v.TestWorkflowTemplate)
params = v.TestWorkflowParams
obj = testworkflows.MapTemplateAPIToKube(v)
}

// Validate resource
Expand All @@ -114,7 +112,7 @@ func (s *TestkubeAPI) CreateTestWorkflowTemplateHandler() fiber.Handler {

// Handle secrets auto-creation
secrets := s.SecretManager.Batch(execNamespace, "creds-", obj.Name)
if params.AutoCreateSecrets {
if autoCreateSecrets {
_, _ = testworkflowresolver.ReplacePlainTextCredentialsInTemplate(obj, secrets.Prepare)
if err != nil {
return s.BadRequest(c, errPrefix, "auto-creating secrets", err)
Expand Down Expand Up @@ -155,27 +153,25 @@ func (s *TestkubeAPI) UpdateTestWorkflowTemplateHandler() fiber.Handler {
return func(c *fiber.Ctx) (err error) {
name := c.Params("id")

autoCreateSecrets, err := strconv.ParseBool(c.Query("autoCreateSecrets", "false"))
if err != nil {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("%s: could not parse autoCreateSecrets parameter: %s", errPrefix, err))
}

// Deserialize resource
obj := new(testworkflowsv1.TestWorkflowTemplate)
params := testkube.TestWorkflowParams{}
if HasYAML(c) {
body := c.Body()
err = common.DeserializeCRD(obj, body)
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
err = yaml.Unmarshal(body, &params)
err = common.DeserializeCRD(obj, c.Body())
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
} else {
var v *testkube.TestWorkflowTemplateInput
var v *testkube.TestWorkflowTemplate
err = c.BodyParser(&v)
if err != nil {
return s.BadRequest(c, errPrefix, "invalid body", err)
}
obj = testworkflows.MapTemplateAPIToKube(&v.TestWorkflowTemplate)
params = v.TestWorkflowParams
obj = testworkflows.MapTemplateAPIToKube(v)
}

// Read existing resource
Expand All @@ -202,7 +198,7 @@ func (s *TestkubeAPI) UpdateTestWorkflowTemplateHandler() fiber.Handler {

// Handle secrets auto-creation
secrets := s.SecretManager.Batch(execNamespace, "creds-", obj.Name)
if params.AutoCreateSecrets {
if autoCreateSecrets {
_, _ = testworkflowresolver.ReplacePlainTextCredentialsInTemplate(obj, secrets.Prepare)
if err != nil {
return s.BadRequest(c, errPrefix, "auto-creating secrets", err)
Expand Down
15 changes: 0 additions & 15 deletions pkg/api/v1/testkube/model_test_workflow_input.go

This file was deleted.

14 changes: 0 additions & 14 deletions pkg/api/v1/testkube/model_test_workflow_params.go

This file was deleted.

15 changes: 0 additions & 15 deletions pkg/api/v1/testkube/model_test_workflow_template_input.go

This file was deleted.

0 comments on commit d416421

Please sign in to comment.