Skip to content

Commit

Permalink
feat: yaml content type (#4070)
Browse files Browse the repository at this point in the history
* feat: create yaml support

* feat: test source yaml update

* feat: update test suite yaml

* feat: test trigger yaml update

* docs: openapi yaml input

* Revert "docs: openapi yaml input"

This reverts commit 1710b0e.

* Revert "Revert "docs: openapi yaml input""

This reverts commit 90f7697.

* fix: update flow token

* fix: lint version

* fix: check linting

* fix: lint version check

* fix: change lint version

* fix: test linter

* fix: openapi linter

* fix: var shadow
  • Loading branch information
vsukhin authored Jun 21, 2023
1 parent 028acc8 commit 9d07344
Show file tree
Hide file tree
Showing 16 changed files with 897 additions and 199 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docusaurus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
github_token: ${{ secrets.CI_BOT_TOKEN }}
# Build output to publish to the `gh-pages` branch:
publish_dir: ./docs/build
# The following lines assign commit authorship to the official
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ jobs:
uses: actions/checkout@v3

- name: OpenAPI Lint Checks
uses: nwestfall/openapi-action@v1.0.1
uses: char0n/swagger-editor-validate@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
file: api/v1/testkube.yaml
definition-file: api/v1/testkube.yaml

lint-go:
name: Lint Go
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint_pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- name: Lint PR
uses: amannn/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.CI_BOT_TOKEN }}
47 changes: 34 additions & 13 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TestTriggerUpsertRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
Expand Down Expand Up @@ -161,9 +164,6 @@ paths:
type: array
items:
$ref: "#/components/schemas/TestTrigger"
text/yaml:
schema:
type: string
400:
description: "problem with test trigger definition - probably some bad input occurs (invalid JSON body or similar)"
content:
Expand Down Expand Up @@ -274,16 +274,16 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TestTriggerUpsertRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
content:
application/json:
schema:
$ref: "#/components/schemas/TestTrigger"
text/yaml:
schema:
type: string
400:
description: "problem with test trigger definition - probably some bad input occurs (invalid JSON body or similar)"
content:
Expand Down Expand Up @@ -354,6 +354,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TestSuiteUpsertRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
Expand Down Expand Up @@ -512,6 +515,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TestSuiteUpdateRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
Expand Down Expand Up @@ -592,9 +598,6 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/ExecutionsMetrics"
text/yaml:
schema:
type: string
500:
description: "problem with read information from storage"
content:
Expand Down Expand Up @@ -649,7 +652,7 @@ paths:
- test-suites
- api
parameters:
- $ref: "#/components/parameters/Name"
- $ref: "#/components/parameters/ID"
summary: "Abort all executions of a test suite"
description: "Abort all test executions of a test suite"
operationId: abortTestSuiteExecutions
Expand Down Expand Up @@ -1460,6 +1463,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TestUpsertRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
Expand Down Expand Up @@ -1543,6 +1549,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TestUpdateRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
Expand Down Expand Up @@ -1712,9 +1721,6 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/ExecutionsMetrics"
text/yaml:
schema:
type: string
500:
description: "problem with getting metrics"
content:
Expand Down Expand Up @@ -2051,6 +2057,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/ExecutorUpsertRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
Expand Down Expand Up @@ -2183,6 +2192,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/ExecutorUpdateRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
Expand Down Expand Up @@ -2301,6 +2313,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/WebhookCreateRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
Expand Down Expand Up @@ -2590,6 +2605,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TestSourceUpsertRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
Expand Down Expand Up @@ -2694,6 +2712,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TestSourceUpdateRequest"
text/yaml:
schema:
type: string
responses:
200:
description: "successful operation"
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/articles/scheduling-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Testkube's schedule data format is the same that is used to define Kubernetes Cr

Testkube uses the scheduling engine from Kubernetes Cron jobs.
In fact, for each scheduled Test or Test Suite, a special cron job is created from this template:
<https://github.com/kubeshop/helm-charts/tree/main/charts/testkube-api/cronjob-template.yml>.
<https://github.com/kubeshop/helm-charts/tree/main/charts/testkube-operator/cronjob-template.yml>.
Technically, it is a callback to the Testkube API server method, launching either Test or Test Suite execution.
This works similarly to scheduled Test and Test Suite executions done by external scheduling platforms.

Expand Down
54 changes: 38 additions & 16 deletions internal/app/api/v1/executor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package v1

import (
"bytes"
"fmt"
"net/http"

"github.com/gofiber/fiber/v2"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/yaml"

executorv1 "github.com/kubeshop/testkube-operator/apis/executor/v1"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/crd"
executorsmapper "github.com/kubeshop/testkube/pkg/mapper/executors"
Expand All @@ -15,20 +18,29 @@ import (
func (s TestkubeAPI) CreateExecutorHandler() fiber.Handler {
return func(c *fiber.Ctx) error {
errPrefix := "failed to create executor"
var request testkube.ExecutorUpsertRequest
err := c.BodyParser(&request)
if err != nil {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("%s: could not parse request: %w", errPrefix, err))
}
var executor executorv1.Executor
if string(c.Request().Header.ContentType()) == mediaTypeYAML {
executorSpec := string(c.Body())
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(executorSpec), len(executorSpec))
if err := decoder.Decode(&executor); err != nil {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("%s: could not parse yaml request: %w", errPrefix, err))
}
} else {
var request testkube.ExecutorUpsertRequest
err := c.BodyParser(&request)
if err != nil {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("%s: could not parse json request: %w", errPrefix, err))
}

if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {
request.QuoteExecutorTextFields()
data, err := crd.GenerateYAML(crd.TemplateExecutor, []testkube.ExecutorUpsertRequest{request})
return s.getCRDs(c, data, err)
}
if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {
request.QuoteExecutorTextFields()
data, err := crd.GenerateYAML(crd.TemplateExecutor, []testkube.ExecutorUpsertRequest{request})
return s.getCRDs(c, data, err)
}

executor := executorsmapper.MapAPIToCRD(request)
executor.Namespace = s.Namespace
executor = executorsmapper.MapAPIToCRD(request)
executor.Namespace = s.Namespace
}

created, err := s.ExecutorsClient.Create(&executor)
if err != nil {
Expand All @@ -50,17 +62,27 @@ func (s TestkubeAPI) UpdateExecutorHandler() fiber.Handler {
return func(c *fiber.Ctx) error {
errPrefix := "failed to update executor"
var request testkube.ExecutorUpdateRequest
err := c.BodyParser(&request)
if err != nil {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("%s: could not parse request: %w", errPrefix, err))
if string(c.Request().Header.ContentType()) == mediaTypeYAML {
var executor executorv1.Executor
executorSpec := string(c.Body())
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(executorSpec), len(executorSpec))
if err := decoder.Decode(&executor); err != nil {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("%s: could not parse yaml request: %w", errPrefix, err))
}

request = executorsmapper.MapSpecToUpdate(&executor)
} else {
err := c.BodyParser(&request)
if err != nil {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("%s: could not parse json request: %w", errPrefix, err))
}
}

var name string
if request.Name != nil {
name = *request.Name
}
errPrefix = errPrefix + " " + name

// we need to get resource first and load its metadata.ResourceVersion
executor, err := s.ExecutorsClient.Get(name)
if err != nil {
Expand Down
Loading

0 comments on commit 9d07344

Please sign in to comment.