Repository: grafana/k6-operator
Pull request #54 (2021-07-22 17:23:18)

Feat/paused quiet bool
Build off of #49  and #53 this adds the option to start tests immediately, and allows for full logs to be outputted. Defaults stay the same. Passing tests on top of it.

dgzlopes: Same as #53 :P

Patch:
diff --git a/api/v1alpha1/k6_types.go b/api/v1alpha1/k6_types.go
index 4363a33..c2e1888 100644
--- a/api/v1alpha1/k6_types.go
+++ b/api/v1alpha1/k6_types.go
@@ -37,14 +37,15 @@ type Pod struct {
 
 // K6Spec defines the desired state of K6
 type K6Spec 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"`
-	ServiceAccountName string                 `json:"serviceaccount,omitempty"`
-	Starter            Pod                    `json:"starter,omitempty"`
-	Runner             Pod                    `json:"runner,omitempty"`
+	Script      K6Script               `json:"script"`
+	Parallelism int32                  `json:"parallelism"`
+	Separate    bool                   `json:"separate,omitempty"`
+	Arguments   string                 `json:"arguments,omitempty"`
+	Ports       []corev1.ContainerPort `json:"ports,omitempty"`
+	Starter     Pod                    `json:"starter,omitempty"`
+	Runner      Pod                    `json:"runner,omitempty"`
+	Quiet       string                 `json:"quiet,omitempty"`
+	Paused      string                 `json:"paused,omitempty"`
 	//	Cleanup     Cleanup `json:"cleanup,omitempty"` // TODO
 }
 
diff --git a/config/crd/bases/k6.io_k6s.yaml b/config/crd/bases/k6.io_k6s.yaml
index 8423ea7..e31122a 100644
--- a/config/crd/bases/k6.io_k6s.yaml
+++ b/config/crd/bases/k6.io_k6s.yaml
@@ -41,6 +41,8 @@ spec:
             parallelism:
               format: int32
               type: integer
+            paused:
+              type: string
             ports:
               items:
                 description: ContainerPort represents a network port in a single container.
@@ -73,6 +75,8 @@ spec:
                 - containerPort
                 type: object
               type: array
+            quiet:
+              type: string
             runner:
               properties:
                 affinity:
@@ -855,8 +859,6 @@ spec:
               type: object
             separate:
               type: boolean
-            serviceaccount:
-              type: string
             starter:
               properties:
                 affinity:
diff --git a/pkg/resources/jobs/runner.go b/pkg/resources/jobs/runner.go
index e49e9f9..55e6186 100644
--- a/pkg/resources/jobs/runner.go
+++ b/pkg/resources/jobs/runner.go
@@ -24,7 +24,16 @@ type Script struct {
 // NewRunnerJob creates a new k6 job from a CRD
 func NewRunnerJob(k6 *v1alpha1.K6, index int) (*batchv1.Job, error) {
 	name := fmt.Sprintf("%s-%d", k6.Name, index)
-	command := []string{"scuttle", "k6", "run", "--quiet"}
+	command := []string{"scuttle", "k6", "run"}
+
+	quiet := true
+	if k6.Spec.Quiet != "" {
+		quiet, _ = strconv.ParseBool(k6.Spec.Quiet)
+	}
+
+	if quiet {
+		command = append(command, "--quiet")
+	}
 
 	if k6.Spec.Parallelism > 1 {
 		var args []string
@@ -51,8 +60,16 @@ func NewRunnerJob(k6 *v1alpha1.K6, index int) (*batchv1.Job, error) {
 	command = append(
 		command,
 		fmt.Sprintf("/test/%s", script.File),
-		"--address=0.0.0.0:6565",
-		"--paused")
+		"--address=0.0.0.0:6565")
+
+	paused := true
+	if k6.Spec.Paused != "" {
+		paused, _ = strconv.ParseBool(k6.Spec.Paused)
+	}
+
+	if paused {
+		command = append(command, "--paused")
+	}
 
 	var zero int64 = 0
 


Test patch:
diff --git a/pkg/resources/jobs/runner_test.go b/pkg/resources/jobs/runner_test.go
index 9a24a29..2a748c6 100644
--- a/pkg/resources/jobs/runner_test.go
+++ b/pkg/resources/jobs/runner_test.go
@@ -327,3 +327,431 @@ func TestNewRunnerJob(t *testing.T) {
 		t.Errorf("NewRunnerJob returned unexpected data, diff: %s", diff)
 	}
 }
+
+
+func TestNewRunnerJobNoisy(t *testing.T) {
+	script := &Script{
+		Name: "test",
+		File: "thing.js",
+		Type: "ConfigMap",
+	}
+
+	var zero int64 = 0
+	automountServiceAccountToken := true
+
+	expectedOutcome := &batchv1.Job{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "test-1",
+			Namespace: "test",
+			Labels: map[string]string{
+				"app":    "k6",
+				"k6_cr":  "test",
+				"label1": "awesome",
+			},
+			Annotations: map[string]string{
+				"awesomeAnnotation": "dope",
+			},
+		},
+		Spec: batchv1.JobSpec{
+			Template: corev1.PodTemplateSpec{
+				ObjectMeta: metav1.ObjectMeta{
+					Labels: map[string]string{
+						"app":    "k6",
+						"k6_cr":  "test",
+						"label1": "awesome",
+					},
+					Annotations: map[string]string{
+						"awesomeAnnotation": "dope",
+					},
+				},
+				Spec: corev1.PodSpec{
+					Hostname:                     "test-1",
+					RestartPolicy:                corev1.RestartPolicyNever,
+					Affinity:                     nil,
+					NodeSelector:                 nil,
+					ServiceAccountName:           "default",
+					AutomountServiceAccountToken: &automountServiceAccountToken,
+					Containers: []corev1.Container{{
+						Image:   "loadimpact/k6:latest",
+						Name:    "k6",
+						Command: []string{"scuttle", "k6", "run", "/test/test.js", "--address=0.0.0.0:6565", "--paused"},
+						Env: []corev1.EnvVar{{
+							Name:  "ISTIO_QUIT_API",
+							Value: "http://127.0.0.1:15020",
+						},
+							{
+								Name:  "ENVOY_ADMIN_API",
+								Value: "http://127.0.0.1:15000",
+							}, {
+								Name:  "WAIT_FOR_ENVOY_TIMEOUT",
+								Value: "15",
+							},
+						},
+						Resources: corev1.ResourceRequirements{},
+						VolumeMounts: []corev1.VolumeMount{{
+							Name:      "k6-test-volume",
+							MountPath: "/test",
+						}},
+						Ports: []corev1.ContainerPort{{ContainerPort: 6565}},
+					}},
+					TerminationGracePeriodSeconds: &zero,
+					Volumes:                       newVolumeSpec(script),
+				},
+			},
+		},
+	}
+	k6 := &v1alpha1.K6{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "test",
+			Namespace: "test",
+		},
+		Spec: v1alpha1.K6Spec{
+			Quiet: "false",
+			Script: v1alpha1.K6Script{
+				ConfigMap: v1alpha1.K6Configmap{
+					Name: "test",
+					File: "test.js",
+				},
+			},
+			Runner: v1alpha1.Pod{
+				Metadata: v1alpha1.PodMetadata{
+					Labels: map[string]string{
+						"label1": "awesome",
+					},
+					Annotations: map[string]string{
+						"awesomeAnnotation": "dope",
+					},
+				},
+			},
+		},
+	}
+
+	job, err := NewRunnerJob(k6, 1)
+	if err != nil {
+		t.Errorf("NewRunnerJob errored, got: %v", err)
+	}
+	if diff := deep.Equal(job, expectedOutcome); diff != nil {
+		t.Errorf("NewRunnerJob returned unexpected data, diff: %s", diff)
+	}
+}
+
+func TestNewRunnerJobUnpaused(t *testing.T) {
+	script := &Script{
+		Name: "test",
+		File: "thing.js",
+		Type: "ConfigMap",
+	}
+
+	var zero int64 = 0
+	automountServiceAccountToken := true
+
+	expectedOutcome := &batchv1.Job{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "test-1",
+			Namespace: "test",
+			Labels: map[string]string{
+				"app":    "k6",
+				"k6_cr":  "test",
+				"label1": "awesome",
+			},
+			Annotations: map[string]string{
+				"awesomeAnnotation": "dope",
+			},
+		},
+		Spec: batchv1.JobSpec{
+			Template: corev1.PodTemplateSpec{
+				ObjectMeta: metav1.ObjectMeta{
+					Labels: map[string]string{
+						"app":    "k6",
+						"k6_cr":  "test",
+						"label1": "awesome",
+					},
+					Annotations: map[string]string{
+						"awesomeAnnotation": "dope",
+					},
+				},
+				Spec: corev1.PodSpec{
+					Hostname:                     "test-1",
+					RestartPolicy:                corev1.RestartPolicyNever,
+					Affinity:                     nil,
+					NodeSelector:                 nil,
+					ServiceAccountName:           "default",
+					AutomountServiceAccountToken: &automountServiceAccountToken,
+					Containers: []corev1.Container{{
+						Image:   "loadimpact/k6:latest",
+						Name:    "k6",
+						Command: []string{"scuttle", "k6", "run", "--quiet", "/test/test.js", "--address=0.0.0.0:6565"},
+						Env: []corev1.EnvVar{{
+							Name:  "ISTIO_QUIT_API",
+							Value: "http://127.0.0.1:15020",
+						},
+							{
+								Name:  "ENVOY_ADMIN_API",
+								Value: "http://127.0.0.1:15000",
+							}, {
+								Name:  "WAIT_FOR_ENVOY_TIMEOUT",
+								Value: "15",
+							},
+						},
+						Resources: corev1.ResourceRequirements{},
+						VolumeMounts: []corev1.VolumeMount{{
+							Name:      "k6-test-volume",
+							MountPath: "/test",
+						}},
+						Ports: []corev1.ContainerPort{{ContainerPort: 6565}},
+					}},
+					TerminationGracePeriodSeconds: &zero,
+					Volumes:                       newVolumeSpec(script),
+				},
+			},
+		},
+	}
+	k6 := &v1alpha1.K6{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "test",
+			Namespace: "test",
+		},
+		Spec: v1alpha1.K6Spec{
+			Paused: "false",
+			Script: v1alpha1.K6Script{
+				ConfigMap: v1alpha1.K6Configmap{
+					Name: "test",
+					File: "test.js",
+				},
+			},
+			Runner: v1alpha1.Pod{
+				Metadata: v1alpha1.PodMetadata{
+					Labels: map[string]string{
+						"label1": "awesome",
+					},
+					Annotations: map[string]string{
+						"awesomeAnnotation": "dope",
+					},
+				},
+			},
+		},
+	}
+
+	job, err := NewRunnerJob(k6, 1)
+	if err != nil {
+		t.Errorf("NewRunnerJob errored, got: %v", err)
+	}
+	if diff := deep.Equal(job, expectedOutcome); diff != nil {
+		t.Errorf("NewRunnerJob returned unexpected data, diff: %s", diff)
+	}
+}
+
+func TestNewRunnerJobArguments(t *testing.T) {
+	script := &Script{
+		Name: "test",
+		File: "thing.js",
+		Type: "ConfigMap",
+	}
+
+	var zero int64 = 0
+	automountServiceAccountToken := true
+
+	expectedOutcome := &batchv1.Job{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "test-1",
+			Namespace: "test",
+			Labels: map[string]string{
+				"app":    "k6",
+				"k6_cr":  "test",
+				"label1": "awesome",
+			},
+			Annotations: map[string]string{
+				"awesomeAnnotation": "dope",
+			},
+		},
+		Spec: batchv1.JobSpec{
+			Template: corev1.PodTemplateSpec{
+				ObjectMeta: metav1.ObjectMeta{
+					Labels: map[string]string{
+						"app":    "k6",
+						"k6_cr":  "test",
+						"label1": "awesome",
+					},
+					Annotations: map[string]string{
+						"awesomeAnnotation": "dope",
+					},
+				},
+				Spec: corev1.PodSpec{
+					Hostname:                     "test-1",
+					RestartPolicy:                corev1.RestartPolicyNever,
+					Affinity:                     nil,
+					NodeSelector:                 nil,
+					ServiceAccountName:           "default",
+					AutomountServiceAccountToken: &automountServiceAccountToken,
+					Containers: []corev1.Container{{
+						Image:   "loadimpact/k6:latest",
+						Name:    "k6",
+						Command: []string{"scuttle", "k6", "run", "--quiet", "--cool-thing", "/test/test.js", "--address=0.0.0.0:6565", "--paused"},
+						Env: []corev1.EnvVar{{
+							Name:  "ISTIO_QUIT_API",
+							Value: "http://127.0.0.1:15020",
+						},
+							{
+								Name:  "ENVOY_ADMIN_API",
+								Value: "http://127.0.0.1:15000",
+							}, {
+								Name:  "WAIT_FOR_ENVOY_TIMEOUT",
+								Value: "15",
+							},
+						},
+						Resources: corev1.ResourceRequirements{},
+						VolumeMounts: []corev1.VolumeMount{{
+							Name:      "k6-test-volume",
+							MountPath: "/test",
+						}},
+						Ports: []corev1.ContainerPort{{ContainerPort: 6565}},
+					}},
+					TerminationGracePeriodSeconds: &zero,
+					Volumes:                       newVolumeSpec(script),
+				},
+			},
+		},
+	}
+
+	k6 := &v1alpha1.K6{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "test",
+			Namespace: "test",
+		},
+		Spec: v1alpha1.K6Spec{
+			Arguments: "--cool-thing",
+			Script: v1alpha1.K6Script{
+				ConfigMap: v1alpha1.K6Configmap{
+					Name: "test",
+					File: "test.js",
+				},
+			},
+			Runner: v1alpha1.Pod{
+				Metadata: v1alpha1.PodMetadata{
+					Labels: map[string]string{
+						"label1": "awesome",
+					},
+					Annotations: map[string]string{
+						"awesomeAnnotation": "dope",
+					},
+				},
+			},
+		},
+	}
+
+	job, err := NewRunnerJob(k6, 1)
+	if err != nil {
+		t.Errorf("NewRunnerJob errored, got: %v", err)
+	}
+	if diff := deep.Equal(job, expectedOutcome); diff != nil {
+		t.Errorf("NewRunnerJob returned unexpected data, diff: %s", diff)
+	}
+}
+
+func TestNewRunnerJobServiceAccount(t *testing.T) {
+	script := &Script{
+		Name: "test",
+		File: "thing.js",
+		Type: "ConfigMap",
+	}
+
+	var zero int64 = 0
+	automountServiceAccountToken := true
+
+	expectedOutcome := &batchv1.Job{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "test-1",
+			Namespace: "test",
+			Labels: map[string]string{
+				"app":    "k6",
+				"k6_cr":  "test",
+				"label1": "awesome",
+			},
+			Annotations: map[string]string{
+				"awesomeAnnotation": "dope",
+			},
+		},
+		Spec: batchv1.JobSpec{
+			Template: corev1.PodTemplateSpec{
+				ObjectMeta: metav1.ObjectMeta{
+					Labels: map[string]string{
+						"app":    "k6",
+						"k6_cr":  "test",
+						"label1": "awesome",
+					},
+					Annotations: map[string]string{
+						"awesomeAnnotation": "dope",
+					},
+				},
+				Spec: corev1.PodSpec{
+					Hostname:                     "test-1",
+					RestartPolicy:                corev1.RestartPolicyNever,
+					Affinity:                     nil,
+					NodeSelector:                 nil,
+					ServiceAccountName:           "test",
+					AutomountServiceAccountToken: &automountServiceAccountToken,
+					Containers: []corev1.Container{{
+						Image:   "loadimpact/k6:latest",
+						Name:    "k6",
+						Command: []string{"scuttle", "k6", "run", "--quiet", "/test/test.js", "--address=0.0.0.0:6565", "--paused"},
+						Env: []corev1.EnvVar{{
+							Name:  "ISTIO_QUIT_API",
+							Value: "http://127.0.0.1:15020",
+						},
+							{
+								Name:  "ENVOY_ADMIN_API",
+								Value: "http://127.0.0.1:15000",
+							}, {
+								Name:  "WAIT_FOR_ENVOY_TIMEOUT",
+								Value: "15",
+							},
+						},
+						Resources: corev1.ResourceRequirements{},
+						VolumeMounts: []corev1.VolumeMount{{
+							Name:      "k6-test-volume",
+							MountPath: "/test",
+						}},
+						Ports: []corev1.ContainerPort{{ContainerPort: 6565}},
+					}},
+					TerminationGracePeriodSeconds: &zero,
+					Volumes:                       newVolumeSpec(script),
+				},
+			},
+		},
+	}
+
+	k6 := &v1alpha1.K6{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "test",
+			Namespace: "test",
+		},
+		Spec: v1alpha1.K6Spec{
+
+			Script: v1alpha1.K6Script{
+				ConfigMap: v1alpha1.K6Configmap{
+					Name: "test",
+					File: "test.js",
+				},
+			},
+			Runner: v1alpha1.Pod{
+				ServiceAccountName: "test",
+				Metadata: v1alpha1.PodMetadata{
+					Labels: map[string]string{
+						"label1": "awesome",
+					},
+					Annotations: map[string]string{
+						"awesomeAnnotation": "dope",
+					},
+				},
+			},
+		},
+	}
+
+	job, err := NewRunnerJob(k6, 1)
+	if err != nil {
+		t.Errorf("NewRunnerJob errored, got: %v", err)
+	}
+	if diff := deep.Equal(job, expectedOutcome); diff != nil {
+		t.Errorf("NewRunnerJob returned unexpected data, diff: %s", diff)
+	}
+}
diff --git a/pkg/resources/jobs/starter_test.go b/pkg/resources/jobs/starter_test.go
index ffeb4bf..2e0e99b 100644
--- a/pkg/resources/jobs/starter_test.go
+++ b/pkg/resources/jobs/starter_test.go
@@ -12,6 +12,7 @@ import (
 )
 
 func TestNewStarterJob(t *testing.T) {
+
 	automountServiceAccountToken := true
 
 	expectedOutcome := &batchv1.Job{