Skip to content

Commit

Permalink
add TestRun CRD with shared logic with K6 CRD
Browse files Browse the repository at this point in the history
  • Loading branch information
yorugac committed Sep 19, 2023
1 parent ce9935e commit bfe014e
Show file tree
Hide file tree
Showing 37 changed files with 5,903 additions and 608 deletions.
9 changes: 9 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ resources:
kind: PrivateLoadZone
path: github.com/grafana/k6-operator/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1beta1
namespaced: true
controller: true
domain: io
group: k6
kind: TestRun
path: github.com/grafana/k6-operator/api/v1alpha1
version: v1alpha1
version: "3"
16 changes: 9 additions & 7 deletions api/v1alpha1/k6_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ type K6Scuttle struct {
QuitWithoutEnvoyTimeout string `json:"quitWithoutEnvoyTimeout,omitempty"`
}

// K6Spec defines the desired state of K6
type K6Spec struct {
// TestRunSpec defines the desired state of TestRun
type TestRunSpec struct {
Script K6Script `json:"script"`
Parallelism int32 `json:"parallelism"`
Separate bool `json:"separate,omitempty"`
Expand Down Expand Up @@ -121,8 +121,8 @@ type Cleanup string
// +kubebuilder:validation:Enum=initialization;initialized;created;started;stopped;finished;error
type Stage string

// K6Status defines the observed state of K6
type K6Status struct {
// TestRunStatus defines the observed state of TestRun
type TestRunStatus struct {
Stage Stage `json:"stage,omitempty"`
TestRunID string `json:"testRunId,omitempty"`
AggregationVars string `json:"aggregationVars,omitempty"`
Expand All @@ -136,12 +136,14 @@ type K6Status struct {
// +kubebuilder:printcolumn:name="Stage",type="string",JSONPath=".status.stage",description="Stage"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:printcolumn:name="TestRunID",type="string",JSONPath=".status.testRunId"
// +kubebuilder:deprecated:warning="This CRD is deprecated in favor of testruns.k6.io/v1alpha1"

type K6 struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec K6Spec `json:"spec,omitempty"`
Status K6Status `json:"status,omitempty"`
Spec TestRunSpec `json:"spec,omitempty"`
Status TestRunStatus `json:"status,omitempty"`
}

// K6List contains a list of K6
Expand All @@ -157,7 +159,7 @@ func init() {
}

// Parse extracts Script data bits from K6 spec and performs basic validation
func (k6 K6Spec) ParseScript() (*types.Script, error) {
func (k6 TestRunSpec) ParseScript() (*types.Script, error) {
spec := k6.Script
s := &types.Script{
Filename: "test.js",
Expand Down
44 changes: 22 additions & 22 deletions api/v1alpha1/k6conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ const (
)

// Initialize defines only conditions common to all test runs.
func (k6 *K6) Initialize() {
func Initialize(k6 TestRunI) {
t := metav1.Now()
k6.Status.Conditions = []metav1.Condition{
k6.GetStatus().Conditions = []metav1.Condition{
metav1.Condition{
Type: CloudTestRun,
Status: metav1.ConditionUnknown,
Expand All @@ -74,39 +74,39 @@ func (k6 *K6) Initialize() {
}

// PLZ test run case
if len(k6.Spec.TestRunID) > 0 {
k6.UpdateCondition(CloudTestRun, metav1.ConditionTrue)
k6.UpdateCondition(CloudPLZTestRun, metav1.ConditionTrue)
k6.UpdateCondition(CloudTestRunCreated, metav1.ConditionTrue)
k6.UpdateCondition(CloudTestRunFinalized, metav1.ConditionFalse)
k6.UpdateCondition(CloudTestRunAborted, metav1.ConditionFalse)

k6.Status.TestRunID = k6.Spec.TestRunID
if len(k6.GetSpec().TestRunID) > 0 {
UpdateCondition(k6, CloudTestRun, metav1.ConditionTrue)
UpdateCondition(k6, CloudPLZTestRun, metav1.ConditionTrue)
UpdateCondition(k6, CloudTestRunCreated, metav1.ConditionTrue)
UpdateCondition(k6, CloudTestRunFinalized, metav1.ConditionFalse)
UpdateCondition(k6, CloudTestRunAborted, metav1.ConditionFalse)

k6.GetStatus().TestRunID = k6.GetSpec().TestRunID
} else {
k6.UpdateCondition(CloudPLZTestRun, metav1.ConditionFalse)
UpdateCondition(k6, CloudPLZTestRun, metav1.ConditionFalse)
// PLZ test run can be defined only via spec.testRunId;
// otherwise it's not a PLZ test run.
}
}

func (k6 *K6) UpdateCondition(conditionType string, conditionStatus metav1.ConditionStatus) {
types.UpdateCondition(&k6.Status.Conditions, conditionType, conditionStatus)
func UpdateCondition(k6 TestRunI, conditionType string, conditionStatus metav1.ConditionStatus) {
types.UpdateCondition(&k6.GetStatus().Conditions, conditionType, conditionStatus)
}

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

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

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

func (k6 K6) LastUpdate(conditionType string) (time.Time, bool) {
cond := meta.FindStatusCondition(k6.Status.Conditions, conditionType)
func LastUpdate(k6 TestRunI, conditionType string) (time.Time, bool) {
cond := meta.FindStatusCondition(k6.GetStatus().Conditions, conditionType)
if cond != nil {
return cond.LastTransitionTime.Time, true
}
Expand All @@ -116,7 +116,7 @@ func (k6 K6) LastUpdate(conditionType string) (time.Time, bool) {
// 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) {
func (k6status *TestRunStatus) SetIfNewer(proposedStatus TestRunStatus) (isNewer bool) {
isNewer = types.SetIfNewer(&k6status.Conditions, proposedStatus.Conditions,
func(proposedCondition metav1.Condition) (isNewer bool) {
// Accept change of test run ID only if it's not set yet and together with
Expand Down
76 changes: 76 additions & 0 deletions api/v1alpha1/testrun_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Stage",type="string",JSONPath=".status.stage",description="Stage"
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
//+kubebuilder:printcolumn:name="TestRunID",type="string",JSONPath=".status.testRunId"

// TestRun is the Schema for the testruns API
type TestRun struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec TestRunSpec `json:"spec,omitempty"`
Status TestRunStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// TestRunList contains a list of TestRun
type TestRunList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []TestRun `json:"items"`
}

func init() {
SchemeBuilder.Register(&TestRun{}, &TestRunList{})
}

// TestRunI implementation for TestRun
func (k6 *TestRun) GetStatus() *TestRunStatus {
return &k6.Status
}

func (k6 *TestRun) GetSpec() *TestRunSpec {
return &k6.Spec
}

func (k6 *TestRun) NamespacedName() types.NamespacedName {
return types.NamespacedName{Namespace: k6.Namespace, Name: k6.Name}
}

// TestRunI implementation for TestRun
func (k6 *K6) GetStatus() *TestRunStatus {
return &k6.Status
}

func (k6 *K6) GetSpec() *TestRunSpec {
return &k6.Spec
}

func (k6 *K6) NamespacedName() types.NamespacedName {
return types.NamespacedName{Namespace: k6.Namespace, Name: k6.Name}
}
22 changes: 22 additions & 0 deletions api/v1alpha1/testruni.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// TestRunI is meant as abstraction over both TestRun and K6 while
// both types are supported. Consider removing it, when K6 is deprecated.
// +kubebuilder:object:generate=false

type TestRunI interface {
runtime.Object
metav1.Object
client.Object

GetStatus() *TestRunStatus
GetSpec() *TestRunSpec
NamespacedName() types.NamespacedName
}
Loading

0 comments on commit bfe014e

Please sign in to comment.