Skip to content

Commit

Permalink
feat: trigger concurrency policy
Browse files Browse the repository at this point in the history
  • Loading branch information
vsukhin committed Sep 14, 2023
1 parent e93d866 commit 5298a2b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
13 changes: 13 additions & 0 deletions apis/testtriggers/v1/testtrigger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type TestTriggerSpec struct {
Execution TestTriggerExecution `json:"execution"`
// TestSelector identifies on which Testkube Kubernetes Objects an Action should be taken
TestSelector TestTriggerSelector `json:"testSelector"`
// ConcurrencyPolicy defines concurrency policy for selected Execution
ConcurrencyPolicy TestTriggerConcurrencyPolicy `json:"concurrencyPolicy"`
// Delay is a duration string which specifies how long should the test be delayed after a trigger is matched
// +kubebuilder:validation:Type:=string
// +kubebuilder:validation:Format:=duration
Expand Down Expand Up @@ -113,6 +115,17 @@ const (
TestTriggerExecutionTestsuite TestTriggerExecution = "testsuite"
)

// TestTriggerConcurrencyPolicy defines concurrency policy for test triggers
// +kubebuilder:validation:Enum=allow;forbid;replace
type TestTriggerConcurrencyPolicy string

// List of TestTriggerConcurrencyPolicy
const (
TestTriggerConcurrencyPolicyAllow TestTriggerConcurrencyPolicy = "allow"
TestTriggerConcurrencyPolicyForbid TestTriggerConcurrencyPolicy = "forbid"
TestTriggerConcurrencyPolicyReplace TestTriggerConcurrencyPolicy = "replace"
)

// TestTriggerSelector is used for selecting Kubernetes Objects
type TestTriggerSelector struct {
// Name selector is used to identify a Kubernetes Object based on the metadata name
Expand Down
9 changes: 9 additions & 0 deletions config/crd/bases/tests.testkube.io_testtriggers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ spec:
enum:
- run
type: string
concurrencyPolicy:
description: ConcurrencyPolicy defines concurrency policy for selected
Execution
enum:
- allow
- forbid
- replace
type: string
conditionSpec:
description: What resource conditions should be matched
properties:
Expand Down Expand Up @@ -293,6 +301,7 @@ spec:
type: object
required:
- action
- concurrencyPolicy
- event
- execution
- resource
Expand Down
16 changes: 16 additions & 0 deletions controllers/testtriggers/testtrigger_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (v *Validator) ValidateCreate(ctx context.Context, t *testtriggerv1.TestTri
allErrs = append(allErrs, err)
}

if err := v.validateConcurrencyPolicy(t.Spec.ConcurrencyPolicy); err != nil {
allErrs = append(allErrs, err)
}

if err := v.validateResourceSelector(t.Spec.ResourceSelector); err != nil {
allErrs = append(allErrs, err...)
}
Expand Down Expand Up @@ -106,6 +110,10 @@ func (v *Validator) ValidateUpdate(ctx context.Context, old runtime.Object, new
allErrs = append(allErrs, err)
}

if err := v.validateConcurrencyPolicy(new.Spec.ConcurrencyPolicy); err != nil {
allErrs = append(allErrs, err)
}

if errs := v.validateResourceSelector(new.Spec.ResourceSelector); errs != nil {
allErrs = append(allErrs, errs...)
}
Expand Down Expand Up @@ -208,6 +216,14 @@ func (v *Validator) validateExecution(execution testtriggerv1.TestTriggerExecuti
return nil
}

func (v *Validator) validateConcurrencyPolicy(concurrencyPlociy testtriggerv1.TestTriggerConcurrencyPolicy) *field.Error {
if !utils.In(string(concurrencyPlociy), testtrigger.GetSupportedConcurrencyPolicies()) {
fld := field.NewPath("spec").Child("concurrencyPolicy")
return field.NotSupported(fld, concurrencyPlociy, testtrigger.GetSupportedConcurrencyPolicies())
}
return nil
}

func (v *Validator) validateConditions(conditionSpec *testtriggerv1.TestTriggerConditionSpec) field.ErrorList {
var allErrs field.ErrorList
if conditionSpec == nil {
Expand Down
20 changes: 20 additions & 0 deletions controllers/testtriggers/testtrigger_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ func TestValidator_validateExecution(t *testing.T) {
})
}

func TestValidator_validateConcurrencyPlocy(t *testing.T) {
t.Parallel()

v := NewValidator(buildFakeK8sClient(t))

t.Run("no error for valid concurrency policy", func(t *testing.T) {
t.Parallel()

err := v.validateConcurrencyPolicy("allow")
assert.Nil(t, err)
})

t.Run("error for invalid concurrency policy", func(t *testing.T) {
t.Parallel()

err := v.validateConcurrencyPolicy("skip")
assert.ErrorContains(t, err, "spec.concurrencyPolicy: Unsupported value: \"skip\"")
})
}

func TestValidator_validateResource(t *testing.T) {
t.Parallel()

Expand Down
3 changes: 3 additions & 0 deletions pkg/validation/tests/v1/testtrigger/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const (
ExecutionTest = "test"
ExecutionTestsuite = "testsuite"
ActionRun = "run"
ConcurrencyPolicyAllow = "allow"
ConcurrencyPolicyForbid = "forbid"
ConcurrencyPolicyReplace = "replace"
ResourcePod = "pod"
ResourceDeployment = "deployment"
ResourceStatefulSet = "statefulset"
Expand Down
4 changes: 4 additions & 0 deletions pkg/validation/tests/v1/testtrigger/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func GetSupportedExecutions() []string {
return []string{ExecutionTest, ExecutionTestsuite}
}

func GetSupportedConcurrencyPolicies() []string {
return []string{ConcurrencyPolicyAllow, ConcurrencyPolicyForbid, ConcurrencyPolicyReplace}
}

func GetSupportedConditionStatuses() []string {
return []string{
string(testtriggerv1.TRUE_TestTriggerConditionStatuses),
Expand Down

0 comments on commit 5298a2b

Please sign in to comment.