Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a TestRun CRD as a duplicate of K6 CRD #286

Merged
merged 6 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add TestRun CRD with shared logic with K6 CRD
  • Loading branch information
yorugac committed Sep 25, 2023
commit ab54a745d17f15334fddb3eb5ad3ba70da7af819
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"
67 changes: 20 additions & 47 deletions api/v1alpha1/k6_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ limitations under the License.
package v1alpha1

import (
"errors"
"path/filepath"

"github.com/grafana/k6-operator/pkg/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
)

type PodMetadata struct {
Expand Down Expand Up @@ -73,8 +70,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 +118,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 +133,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 @@ -156,41 +155,15 @@ func init() {
SchemeBuilder.Register(&K6{}, &K6List{})
}

// Parse extracts Script data bits from K6 spec and performs basic validation
func (k6 K6Spec) ParseScript() (*types.Script, error) {
spec := k6.Script
s := &types.Script{
Filename: "test.js",
Path: "/test/",
}

if spec.VolumeClaim.Name != "" {
s.Name = spec.VolumeClaim.Name
if spec.VolumeClaim.File != "" {
s.Filename = spec.VolumeClaim.File
}

s.Type = "VolumeClaim"
return s, nil
}

if spec.ConfigMap.Name != "" {
s.Name = spec.ConfigMap.Name

if spec.ConfigMap.File != "" {
s.Filename = spec.ConfigMap.File
}

s.Type = "ConfigMap"
return s, nil
}

if spec.LocalFile != "" {
s.Name = "LocalFile"
s.Type = "LocalFile"
s.Path, s.Filename = filepath.Split(spec.LocalFile)
return s, nil
}

return nil, errors.New("Script definition should contain one of: ConfigMap, VolumeClaim, LocalFile")
// TestRunI implementation for K6
func (k6 *K6) GetStatus() *TestRunStatus {
return &k6.Status
}

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

func (k6 *K6) NamespacedName() k8stypes.NamespacedName {
return k8stypes.NamespacedName{Namespace: k6.Namespace, Name: k6.Name}
}
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
106 changes: 106 additions & 0 deletions api/v1alpha1/testrun_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*


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 (
"errors"
"path/filepath"

"github.com/grafana/k6-operator/pkg/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "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{})
}

// Parse extracts Script data bits from K6 spec and performs basic validation
func (k6 TestRunSpec) ParseScript() (*types.Script, error) {
spec := k6.Script
s := &types.Script{
Filename: "test.js",
Path: "/test/",
}

if spec.VolumeClaim.Name != "" {
s.Name = spec.VolumeClaim.Name
if spec.VolumeClaim.File != "" {
s.Filename = spec.VolumeClaim.File
}

s.Type = "VolumeClaim"
return s, nil
}

if spec.ConfigMap.Name != "" {
s.Name = spec.ConfigMap.Name

if spec.ConfigMap.File != "" {
s.Filename = spec.ConfigMap.File
}

s.Type = "ConfigMap"
return s, nil
}

if spec.LocalFile != "" {
s.Name = "LocalFile"
s.Type = "LocalFile"
s.Path, s.Filename = filepath.Split(spec.LocalFile)
return s, nil
}

return nil, errors.New("Script definition should contain one of: ConfigMap, VolumeClaim, LocalFile")
}

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

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

func (k6 *TestRun) NamespacedName() k8stypes.NamespacedName {
return k8stypes.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