Skip to content

Commit

Permalink
add key conditions to K6 status and update logic to improve idempotency
Browse files Browse the repository at this point in the history
  • Loading branch information
yorugac committed Apr 24, 2023
1 parent a1d8333 commit 3b623e2
Show file tree
Hide file tree
Showing 9 changed files with 636 additions and 191 deletions.
181 changes: 181 additions & 0 deletions api/v1alpha1/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package v1alpha1

import (
"fmt"
"strings"
"time"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
// TestRunRunning indicates if the test run is currently running.
// - if empty / Unknown, it's any stage before k6 resume (starter)
// - if False, it's after all runners have finished successfully or with error
// - if True, it's after successful starter but before all runners have finished
TestRunRunning = "TestRunRunning"

// CloudTestRun indicates if this test run is supposed to be a cloud test run.
// - if empty / Unknown, the type of test is unknown yet
// - if False, it is not a cloud test run
// - if True, it is a cloud test run
CloudTestRun = "CloudTestRun"

// CloudTestRunCreated indicates if k6 Cloud test run ID has been created for this test.
// - if empty / Unknown, it's either a non-cloud test run or it is a cloud test run
// that wasn't created yet
// - if False, it is a cloud test run and it is yet to be created
// - if True, it is a cloud test run and it has been created already
CloudTestRunCreated = "CloudTestRunCreated"

// CloudTestRunFinalized indicates if k6 Cloud test run has been finalized.
// - if empty / Unknown, it's either a non-cloud test run or it is a cloud test run
// that wasn't finalized yet
// - if False, it's a cloud test run and it is yet to be finalized
// - if True, it's a cloud test run that has been finalized already
CloudTestRunFinalized = "CloudTestRunFinalized"
)

var reasons = map[string]string{
"TestRunRunningUnknown": "TestRunPreparation",
"TestRunRunningTrue": "TestRunRunningTrue",
"TestRunRunningFalse": "TestRunRunningFalse",

"CloudTestRunUnknown": "TestRunTypeUnknown",
"CloudTestRunTrue": "CloudTestRunTrue",
"CloudTestRunFalse": "CloudTestRunFalse",

"CloudTestRunCreatedUnknown": "CloudTestRunCreatedUnknown",
"CloudTestRunCreatedTrue": "CloudTestRunCreatedTrue",
"CloudTestRunCreatedFalse": "CloudTestRunCreatedFalse",

"CloudTestRunFinalizedUnknown": "CloudTestRunFinalizedUnknown",
"CloudTestRunFinalizedTrue": "CloudTestRunFinalizedTrue",
"CloudTestRunFinalizedFalse": "CloudTestRunFinalizedFalse",
}

// InitializeConditions defines only conditions common to all test runs.
func (k6 *K6) InitializeConditions() {
t := metav1.Now()
k6.Status.Conditions = []metav1.Condition{
metav1.Condition{
Type: CloudTestRun,
Status: metav1.ConditionUnknown,
LastTransitionTime: t,
Reason: "TestRunTypeUnknown",
Message: "",
},
metav1.Condition{
Type: TestRunRunning,
Status: metav1.ConditionUnknown,
LastTransitionTime: t,
Reason: "TestRunPreparation",
Message: "",
},
}
}

func (k6 *K6) UpdateCondition(conditionType string, conditionStatus metav1.ConditionStatus) {
reason, ok := reasons[conditionType+string(conditionStatus)]
if !ok {
panic(fmt.Sprintf("Invalid condition type and status! `%s` - this should never happen!", conditionType+string(conditionStatus)))
}
meta.SetStatusCondition(&k6.Status.Conditions, metav1.Condition{
Type: conditionType,
Status: conditionStatus,
LastTransitionTime: metav1.Now(),
Reason: reason,
Message: "",
})
}

func (k6 K6) IsTrue(conditionType string) bool {
return meta.IsStatusConditionTrue(k6.Status.Conditions, conditionType)
}

func (k6 K6) IsFalse(conditionType string) bool {
return meta.IsStatusConditionFalse(k6.Status.Conditions, conditionType)
}

func (k6 K6) IsUnknown(conditionType string) bool {
return !k6.IsFalse(conditionType) && !k6.IsTrue(conditionType)
}

func (k6 K6) LastUpdate(conditionType string) (time.Time, bool) {
cond := meta.FindStatusCondition(k6.Status.Conditions, conditionType)
if cond != nil {
return cond.LastTransitionTime.Time, true
}
return time.Now(), false
}

// SetIfNewer changes k6status only if changes in proposedStatus are consistent
// with the expected progression of a test run. If there were any acceptable
// changes proposed, it returns true.
func (k6status *K6Status) SetIfNewer(proposedStatus K6Status) (isNewer bool) {
existingConditions := map[string]metav1.Condition{}
for i := range k6status.Conditions {
existingConditions[k6status.Conditions[i].Type] = k6status.Conditions[i]
}

for _, proposedCondition := range proposedStatus.Conditions {
// If a new condition is being proposed, just add it to the list.
if existingCondition, ok := existingConditions[proposedCondition.Type]; !ok {
k6status.Conditions = append(k6status.Conditions, proposedCondition)
isNewer = true
} else {
// If a change in existing condition is being proposed, check if
// its timestamp is later than the one in existing condition.
//
// Additionally: condition should never return to Unknown status
// unless it's newly created.

if proposedCondition.Status != metav1.ConditionUnknown {
if existingCondition.LastTransitionTime.UnixNano() < proposedCondition.LastTransitionTime.UnixNano() {
meta.SetStatusCondition(&k6status.Conditions, proposedCondition)
isNewer = true
}
}
}

// Accept change of test run ID only if it's not set yet and together with
// corresponding condition.
if proposedCondition.Type == CloudTestRunCreated &&
len(k6status.TestRunID) == 0 &&
len(proposedStatus.TestRunID) > 0 {
k6status.TestRunID = proposedStatus.TestRunID
isNewer = true
}
// log if proposedStatus.TestRunID is empty here?
}

// If a change in stage is proposed, confirm that it is consistent with
// expected flow of any test run.
if k6status.Stage != proposedStatus.Stage && len(proposedStatus.Stage) > 0 {
switch k6status.Stage {
case "", "initialization":
k6status.Stage = proposedStatus.Stage
isNewer = true

case "initialized":
if !strings.HasPrefix(string(proposedStatus.Stage), "init") {
k6status.Stage = proposedStatus.Stage
isNewer = true
}
case "created":
if proposedStatus.Stage == "started" || proposedStatus.Stage == "finished" || proposedStatus.Stage == "error" {
k6status.Stage = proposedStatus.Stage
isNewer = true
}
case "started":
if proposedStatus.Stage == "finished" || proposedStatus.Stage == "error" {
k6status.Stage = proposedStatus.Stage
isNewer = true
}
// in case of finished or error stage, skip
}
}

return
}
2 changes: 2 additions & 0 deletions api/v1alpha1/k6_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ type Stage string
type K6Status struct {
Stage Stage `json:"stage,omitempty"`
TestRunID string `json:"testRunId,omitempty"`

Conditions []metav1.Condition `json:"conditions,omitempty"`
}

// K6 is the Schema for the k6s API
Expand Down
10 changes: 9 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

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

70 changes: 70 additions & 0 deletions config/crd/bases/k6.io_k6s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4064,6 +4064,76 @@ spec:
status:
description: K6Status defines the observed state of K6
properties:
conditions:
items:
description: "Condition contains details for one aspect of the current
state of this API Resource. --- This struct is intended for direct
use as an array at the field path .status.conditions. For example,
\n \ttype FooStatus struct{ \t // Represents the observations
of a foo's current state. \t // Known .status.conditions.type
are: \"Available\", \"Progressing\", and \"Degraded\" \t //
+patchMergeKey=type \t // +patchStrategy=merge \t // +listType=map
\t // +listMapKey=type \t Conditions []metav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n \t // other fields
\t}"
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition
transitioned from one status to another. This should be when
the underlying condition changed. If that is not known, then
using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance, if .metadata.generation
is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the current
state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values and
meanings for this field, and whether the values are considered
a guaranteed API. The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
--- Many .condition.type values are consistent across resources
like Available, but because arbitrary conditions can be useful
(see .node.status.conditions), the ability to deconflict is
important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
stage:
description: Stage describes which stage of the test execution lifecycle
our runners are in
Expand Down

0 comments on commit 3b623e2

Please sign in to comment.