Skip to content

Commit

Permalink
remove K6 CRD
Browse files Browse the repository at this point in the history
  • Loading branch information
yorugac committed Nov 14, 2024
1 parent cbbc656 commit 263b2fb
Show file tree
Hide file tree
Showing 22 changed files with 185 additions and 12,304 deletions.
172 changes: 0 additions & 172 deletions api/v1alpha1/k6_types.go

This file was deleted.

12 changes: 6 additions & 6 deletions api/v1alpha1/k6conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const (
)

// Initialize defines only conditions common to all test runs.
func Initialize(k6 TestRunI) {
func Initialize(k6 *TestRun) {
t := metav1.Now()
k6.GetStatus().Conditions = []metav1.Condition{
metav1.Condition{
Expand Down Expand Up @@ -100,23 +100,23 @@ func Initialize(k6 TestRunI) {
}
}

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

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

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

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

func LastUpdate(k6 TestRunI, conditionType string) (time.Time, bool) {
func LastUpdate(k6 *TestRun, conditionType string) (time.Time, bool) {
cond := meta.FindStatusCondition(k6.GetStatus().Conditions, conditionType)
if cond != nil {
return cond.LastTransitionTime.Time, true
Expand Down
139 changes: 135 additions & 4 deletions api/v1alpha1/testrun_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
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
Expand All @@ -18,13 +16,125 @@ 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"
"k8s.io/apimachinery/pkg/labels"
k8stypes "k8s.io/apimachinery/pkg/types"
"path/filepath"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type PodMetadata struct {
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}

type Pod struct {
Affinity *corev1.Affinity `json:"affinity,omitempty"`
AutomountServiceAccountToken string `json:"automountServiceAccountToken,omitempty"`
Env []corev1.EnvVar `json:"env,omitempty"`
Image string `json:"image,omitempty"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Metadata PodMetadata `json:"metadata,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
ServiceAccountName string `json:"serviceAccountName,omitempty"`
SecurityContext corev1.PodSecurityContext `json:"securityContext,omitempty"`
ContainerSecurityContext corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
ReadinessProbe *corev1.Probe `json:"readinessProbe,omitempty"`
LivenessProbe *corev1.Probe `json:"livenessProbe,omitempty"`
InitContainers []InitContainer `json:"initContainers,omitempty"`
Volumes []corev1.Volume `json:"volumes,omitempty"`
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
}

type InitContainer struct {
Name string `json:"name,omitempty"`
Image string `json:"image,omitempty"`
Env []corev1.EnvVar `json:"env,omitempty"`
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
Command []string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
}

type K6Scuttle struct {
Enabled string `json:"enabled,omitempty"`
EnvoyAdminApi string `json:"envoyAdminApi,omitempty"`
NeverKillIstio bool `json:"neverKillIstio,omitempty"`
NeverKillIstioOnFailure bool `json:"neverKillIstioOnFailure,omitempty"`
DisableLogging bool `json:"disableLogging,omitempty"`
StartWithoutEnvoy bool `json:"startWithoutEnvoy,omitempty"`
WaitForEnvoyTimeout string `json:"waitForEnvoyTimeout,omitempty"`
IstioQuitApi string `json:"istioQuitApi,omitempty"`
GenericQuitEndpoint string `json:"genericQuitEndpoint,omitempty"`
QuitWithoutEnvoyTimeout string `json:"quitWithoutEnvoyTimeout,omitempty"`
}

// TestRunSpec defines the desired state of TestRun
type TestRunSpec struct {
Script K6Script `json:"script"`
Parallelism int32 `json:"parallelism"`
Separate bool `json:"separate,omitempty"`
Arguments string `json:"arguments,omitempty"`
Ports []corev1.ContainerPort `json:"ports,omitempty"`
Initializer *Pod `json:"initializer,omitempty"`
Starter Pod `json:"starter,omitempty"`
Runner Pod `json:"runner,omitempty"`
Quiet string `json:"quiet,omitempty"`
Paused string `json:"paused,omitempty"`
Scuttle K6Scuttle `json:"scuttle,omitempty"`
Cleanup Cleanup `json:"cleanup,omitempty"`

TestRunID string `json:"testRunId,omitempty"` // PLZ reserved field
Token string `json:"token,omitempty"` // PLZ reserved field (for now)
}

// K6Script describes where the script to execute the tests is found
type K6Script struct {
VolumeClaim K6VolumeClaim `json:"volumeClaim,omitempty"`
ConfigMap K6Configmap `json:"configMap,omitempty"`
LocalFile string `json:"localFile,omitempty"`
}

// K6VolumeClaim describes the volume claim script location
type K6VolumeClaim struct {
Name string `json:"name"`
File string `json:"file,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
}

// K6Configmap describes the config map script location
type K6Configmap struct {
Name string `json:"name"`
File string `json:"file,omitempty"`
}

//TODO: cleanup pre-execution?

// Cleanup allows for automatic cleanup of resources post execution
// +kubebuilder:validation:Enum=post
type Cleanup string

// Stage describes which stage of the test execution lifecycle our runners are in
// +kubebuilder:validation:Enum=initialization;initialized;created;started;stopped;finished;error
type Stage string

// 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"`

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

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Stage",type="string",JSONPath=".status.stage",description="Stage"
Expand Down Expand Up @@ -105,3 +215,24 @@ func (k6 *TestRun) GetSpec() *TestRunSpec {
func (k6 *TestRun) NamespacedName() k8stypes.NamespacedName {
return k8stypes.NamespacedName{Namespace: k6.Namespace, Name: k6.Name}
}

// TestRunID is a tiny helper to get k6 Cloud test run ID.
// PLZ test run will have test run ID as part of spec,
// while cloud output test run as part of status.
func (k6 *TestRun) TestRunID() string {
specId := k6.GetSpec().TestRunID
if len(specId) > 0 {
return specId
}
return k6.GetStatus().TestRunID
}

func (k6 *TestRun) ListOptions() *client.ListOptions {
selector := labels.SelectorFromSet(map[string]string{
"app": "k6",
"k6_cr": k6.NamespacedName().Name,
"runner": "true",
})

return &client.ListOptions{LabelSelector: selector, Namespace: k6.NamespacedName().Namespace}
}
Loading

0 comments on commit 263b2fb

Please sign in to comment.