1
1
/*
2
-
3
-
4
2
Licensed under the Apache License, Version 2.0 (the "License");
5
3
you may not use this file except in compliance with the License.
6
4
You may obtain a copy of the License at
@@ -18,13 +16,125 @@ package v1alpha1
18
16
19
17
import (
20
18
"errors"
21
- "path/filepath"
22
-
23
19
"github.com/grafana/k6-operator/pkg/types"
20
+ corev1 "k8s.io/api/core/v1"
24
21
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22
+ "k8s.io/apimachinery/pkg/labels"
25
23
k8stypes "k8s.io/apimachinery/pkg/types"
24
+ "path/filepath"
25
+ "sigs.k8s.io/controller-runtime/pkg/client"
26
26
)
27
27
28
+ type PodMetadata struct {
29
+ Annotations map [string ]string `json:"annotations,omitempty"`
30
+ Labels map [string ]string `json:"labels,omitempty"`
31
+ }
32
+
33
+ type Pod struct {
34
+ Affinity * corev1.Affinity `json:"affinity,omitempty"`
35
+ AutomountServiceAccountToken string `json:"automountServiceAccountToken,omitempty"`
36
+ Env []corev1.EnvVar `json:"env,omitempty"`
37
+ Image string `json:"image,omitempty"`
38
+ ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
39
+ ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
40
+ Metadata PodMetadata `json:"metadata,omitempty"`
41
+ NodeSelector map [string ]string `json:"nodeSelector,omitempty"`
42
+ Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
43
+ TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
44
+ Resources corev1.ResourceRequirements `json:"resources,omitempty"`
45
+ ServiceAccountName string `json:"serviceAccountName,omitempty"`
46
+ SecurityContext corev1.PodSecurityContext `json:"securityContext,omitempty"`
47
+ ContainerSecurityContext corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
48
+ EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
49
+ ReadinessProbe * corev1.Probe `json:"readinessProbe,omitempty"`
50
+ LivenessProbe * corev1.Probe `json:"livenessProbe,omitempty"`
51
+ InitContainers []InitContainer `json:"initContainers,omitempty"`
52
+ Volumes []corev1.Volume `json:"volumes,omitempty"`
53
+ VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
54
+ }
55
+
56
+ type InitContainer struct {
57
+ Name string `json:"name,omitempty"`
58
+ Image string `json:"image,omitempty"`
59
+ Env []corev1.EnvVar `json:"env,omitempty"`
60
+ EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
61
+ Command []string `json:"command,omitempty"`
62
+ Args []string `json:"args,omitempty"`
63
+ WorkingDir string `json:"workingDir,omitempty"`
64
+ VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
65
+ }
66
+
67
+ type K6Scuttle struct {
68
+ Enabled string `json:"enabled,omitempty"`
69
+ EnvoyAdminApi string `json:"envoyAdminApi,omitempty"`
70
+ NeverKillIstio bool `json:"neverKillIstio,omitempty"`
71
+ NeverKillIstioOnFailure bool `json:"neverKillIstioOnFailure,omitempty"`
72
+ DisableLogging bool `json:"disableLogging,omitempty"`
73
+ StartWithoutEnvoy bool `json:"startWithoutEnvoy,omitempty"`
74
+ WaitForEnvoyTimeout string `json:"waitForEnvoyTimeout,omitempty"`
75
+ IstioQuitApi string `json:"istioQuitApi,omitempty"`
76
+ GenericQuitEndpoint string `json:"genericQuitEndpoint,omitempty"`
77
+ QuitWithoutEnvoyTimeout string `json:"quitWithoutEnvoyTimeout,omitempty"`
78
+ }
79
+
80
+ // TestRunSpec defines the desired state of TestRun
81
+ type TestRunSpec struct {
82
+ Script K6Script `json:"script"`
83
+ Parallelism int32 `json:"parallelism"`
84
+ Separate bool `json:"separate,omitempty"`
85
+ Arguments string `json:"arguments,omitempty"`
86
+ Ports []corev1.ContainerPort `json:"ports,omitempty"`
87
+ Initializer * Pod `json:"initializer,omitempty"`
88
+ Starter Pod `json:"starter,omitempty"`
89
+ Runner Pod `json:"runner,omitempty"`
90
+ Quiet string `json:"quiet,omitempty"`
91
+ Paused string `json:"paused,omitempty"`
92
+ Scuttle K6Scuttle `json:"scuttle,omitempty"`
93
+ Cleanup Cleanup `json:"cleanup,omitempty"`
94
+
95
+ TestRunID string `json:"testRunId,omitempty"` // PLZ reserved field
96
+ Token string `json:"token,omitempty"` // PLZ reserved field (for now)
97
+ }
98
+
99
+ // K6Script describes where the script to execute the tests is found
100
+ type K6Script struct {
101
+ VolumeClaim K6VolumeClaim `json:"volumeClaim,omitempty"`
102
+ ConfigMap K6Configmap `json:"configMap,omitempty"`
103
+ LocalFile string `json:"localFile,omitempty"`
104
+ }
105
+
106
+ // K6VolumeClaim describes the volume claim script location
107
+ type K6VolumeClaim struct {
108
+ Name string `json:"name"`
109
+ File string `json:"file,omitempty"`
110
+ ReadOnly bool `json:"readOnly,omitempty"`
111
+ }
112
+
113
+ // K6Configmap describes the config map script location
114
+ type K6Configmap struct {
115
+ Name string `json:"name"`
116
+ File string `json:"file,omitempty"`
117
+ }
118
+
119
+ //TODO: cleanup pre-execution?
120
+
121
+ // Cleanup allows for automatic cleanup of resources post execution
122
+ // +kubebuilder:validation:Enum=post
123
+ type Cleanup string
124
+
125
+ // Stage describes which stage of the test execution lifecycle our runners are in
126
+ // +kubebuilder:validation:Enum=initialization;initialized;created;started;stopped;finished;error
127
+ type Stage string
128
+
129
+ // TestRunStatus defines the observed state of TestRun
130
+ type TestRunStatus struct {
131
+ Stage Stage `json:"stage,omitempty"`
132
+ TestRunID string `json:"testRunId,omitempty"`
133
+ AggregationVars string `json:"aggregationVars,omitempty"`
134
+
135
+ Conditions []metav1.Condition `json:"conditions,omitempty"`
136
+ }
137
+
28
138
//+kubebuilder:object:root=true
29
139
//+kubebuilder:subresource:status
30
140
//+kubebuilder:printcolumn:name="Stage",type="string",JSONPath=".status.stage",description="Stage"
@@ -105,3 +215,24 @@ func (k6 *TestRun) GetSpec() *TestRunSpec {
105
215
func (k6 * TestRun ) NamespacedName () k8stypes.NamespacedName {
106
216
return k8stypes.NamespacedName {Namespace : k6 .Namespace , Name : k6 .Name }
107
217
}
218
+
219
+ // TestRunID is a tiny helper to get k6 Cloud test run ID.
220
+ // PLZ test run will have test run ID as part of spec,
221
+ // while cloud output test run as part of status.
222
+ func (k6 * TestRun ) TestRunID () string {
223
+ specId := k6 .GetSpec ().TestRunID
224
+ if len (specId ) > 0 {
225
+ return specId
226
+ }
227
+ return k6 .GetStatus ().TestRunID
228
+ }
229
+
230
+ func (k6 * TestRun ) ListOptions () * client.ListOptions {
231
+ selector := labels .SelectorFromSet (map [string ]string {
232
+ "app" : "k6" ,
233
+ "k6_cr" : k6 .NamespacedName ().Name ,
234
+ "runner" : "true" ,
235
+ })
236
+
237
+ return & client.ListOptions {LabelSelector : selector , Namespace : k6 .NamespacedName ().Namespace }
238
+ }
0 commit comments