Repository: grafana/k6-operator
Pull request #129 (2022-07-15 02:39:51)

feat: ability to set the environment variables with envFrom in k6 runner/starter.
Closes https://github.com/grafana/k6-operator/issues/122.



Patch:
diff --git a/api/v1alpha1/k6_types.go b/api/v1alpha1/k6_types.go
index fdc31a0..e53838c 100644
--- a/api/v1alpha1/k6_types.go
+++ b/api/v1alpha1/k6_types.go
@@ -34,6 +34,7 @@ type Pod struct {
 	Resources                    corev1.ResourceRequirements `json:"resources,omitempty"`
 	ServiceAccountName           string                      `json:"serviceAccountName,omitempty"`
 	SecurityContext              corev1.PodSecurityContext   `json:"securityContext,omitempty"`
+	EnvFrom                      []corev1.EnvFromSource      `json:"envFrom,omitempty"`
 }
 
 type K6Scuttle struct {
diff --git a/config/crd/bases/k6.io_k6s.yaml b/config/crd/bases/k6.io_k6s.yaml
index de4b2b8..43ddfe5 100644
--- a/config/crd/bases/k6.io_k6s.yaml
+++ b/config/crd/bases/k6.io_k6s.yaml
@@ -988,13 +988,36 @@ spec:
                     type: object
                   serviceAccountName:
                     type: string
+                  # refs: https://github.com/kubernetes/kubernetes/blob/7e2c02d4c86d73051b6d4f47298d35a7ffb42b6a/api/openapi-spec/v3/apis__apps__v1_openapi.json#L1681
+                  envFrom:
+                    type: array
+                    items:
+                      description: K6EnvFrom represents the source of a set of ConfigMaps/Secrets
+                      default: {}
+                      properties:
+                        configMapRef:
+                          type: object
+                          description: The ConfigMap to select from
+                          properties:
+                            name:
+                              type: string
+                            optional:
+                              type: bool
+                        secretRef:
+                          type: object
+                          description: The Secret to select from
+                          properties:
+                            name:
+                              type: string
+                            optional:
+                              type: bool
                 type: object
               script:
                 description: K6Script describes where the script to execute the tests
                   is found
                 properties:
                   configMap:
-                    description: K6Configmap describes the config map script location
+                    description: K6ConfigMap describes the config map script location
                     properties:
                       file:
                         type: string
diff --git a/pkg/resources/jobs/runner.go b/pkg/resources/jobs/runner.go
index 62faabc..acf5451 100644
--- a/pkg/resources/jobs/runner.go
+++ b/pkg/resources/jobs/runner.go
@@ -153,6 +153,7 @@ func NewRunnerJob(k6 *v1alpha1.K6, index int, testRunId, token string) (*batchv1
 						Resources:    k6.Spec.Runner.Resources,
 						VolumeMounts: script.VolumeMount(),
 						Ports:        ports,
+						EnvFrom:      k6.Spec.Runner.EnvFrom,
 					}},
 					TerminationGracePeriodSeconds: &zero,
 					Volumes:                       script.Volume(),


Test patch:
diff --git a/pkg/resources/jobs/runner_test.go b/pkg/resources/jobs/runner_test.go
index 721410d..2ba36de 100644
--- a/pkg/resources/jobs/runner_test.go
+++ b/pkg/resources/jobs/runner_test.go
@@ -294,6 +294,15 @@ func TestNewRunnerJob(t *testing.T) {
 						Resources:    corev1.ResourceRequirements{},
 						VolumeMounts: script.VolumeMount(),
 						Ports:        []corev1.ContainerPort{{ContainerPort: 6565}},
+						EnvFrom: []corev1.EnvFromSource{
+							{
+								ConfigMapRef: &corev1.ConfigMapEnvSource{
+									LocalObjectReference: corev1.LocalObjectReference{
+										Name: "env",
+									},
+								},
+							},
+						},
 					}},
 					TerminationGracePeriodSeconds: &zero,
 					Volumes:                       script.Volume(),
@@ -322,6 +331,15 @@ func TestNewRunnerJob(t *testing.T) {
 						"awesomeAnnotation": "dope",
 					},
 				},
+				EnvFrom: []corev1.EnvFromSource{
+					{
+						ConfigMapRef: &corev1.ConfigMapEnvSource{
+							LocalObjectReference: corev1.LocalObjectReference{
+								Name: "env",
+							},
+						},
+					},
+				},
 			},
 		},
 	}